Commit e3e11734 authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Minor refactoring. Added coe search mapping.

parent 9af49a37
......@@ -48,7 +48,7 @@ class Calc(Base, datamodel.Calc): # type: ignore
backref='children')
@classmethod
def create_from(cls, obj):
def load_from(cls, obj):
repo_db = infrastructure.repository_db
return repo_db.query(Calc).filter_by(calc_id=int(obj.pid)).first()
......
......@@ -99,7 +99,7 @@ class Upload(Base, datamodel.Upload): # type: ignore
calcs = relationship('Calc')
@classmethod
def create_from(cls, obj):
def load_from(cls, obj):
return Upload.from_upload_hash(obj.upload_hash)
@staticmethod
......
......@@ -34,7 +34,7 @@ FSConfig = namedtuple('FSConfig', ['tmp', 'objects'])
RepositoryDBConfig = namedtuple('RepositoryDBConfig', ['host', 'port', 'dbname', 'user', 'password'])
""" Used to configure access to NOMAD-coe repository db. """
ElasticConfig = namedtuple('ElasticConfig', ['host', 'port', 'index_name'])
ElasticConfig = namedtuple('ElasticConfig', ['host', 'port', 'index_name', 'coe_repo_calcs_index_name'])
""" Used to configure elastic search. """
MongoConfig = namedtuple('MongoConfig', ['host', 'port', 'db_name'])
......@@ -84,7 +84,8 @@ fs = FSConfig(
elastic = ElasticConfig(
host=os.environ.get('NOMAD_ELASTIC_HOST', 'localhost'),
port=int(os.environ.get('NOMAD_ELASTIC_PORT', 9200)),
index_name=os.environ.get('NOMAD_ELASTIC_INDEX_NAME', 'calcs')
index_name=os.environ.get('NOMAD_ELASTIC_INDEX_NAME', 'calcs'),
coe_repo_calcs_index_name='coe_repo_calcs'
)
repository_db = RepositoryDBConfig(
host=os.environ.get('NOMAD_COE_REPO_DB_HOST', 'localhost'),
......
......@@ -29,18 +29,18 @@ T = TypeVar('T')
class Entity():
@classmethod
def create_from(cls: Type[T], obj) -> T:
def load_from(cls: Type[T], obj) -> T:
raise NotImplementedError
def to(self, entity_cls: Type[T]) -> T:
"""
Either provides a type cast if it already has the right type, or adapt
the type using the :func:`create_from` of the target class :param:`entity_cls`.
the type using the :func:`load_from` of the target class :param:`entity_cls`.
"""
if (isinstance(self, entity_cls)):
return cast(T, self)
else:
return cast(T, cast(Type[Entity], entity_cls).create_from(self))
return cast(T, cast(Type[Entity], entity_cls).load_from(self))
class Calc(Entity):
......
......@@ -86,6 +86,8 @@ def setup_elastic():
try:
from nomad.repo import RepoCalc
RepoCalc.init()
from nomad.search import Entry
Entry.init()
except RequestError as e:
if e.status_code == 400 and 'resource_already_exists_exception' in e.error:
pass # happens if two services try this at the same time
......
......@@ -50,7 +50,7 @@ class RepoUpload(datamodel.Entity):
self.upload_hash = upload_hash
@classmethod
def create_from(cls, obj):
def load_from(cls, obj):
return RepoUpload(obj.upload_id, obj.upload_hash)
@property
......
# Copyright 2018 Markus Scheidgen
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an"AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This module represents calculations in elastic search.
"""
from elasticsearch_dsl import Document, InnerDoc, Keyword, Text, Long, Integer, Date, \
Nested
from nomad import config, datamodel, coe_repo
class UserData(InnerDoc):
repository_open_date = Date(format='epoch_millis')
repository_access_now = Keyword()
repository_comment = Keyword()
section_citation = Nested(properties=dict(
citation_repo_id=Integer(),
citation_value=Keyword()
))
section_author_info = Nested(properties=dict(
author_repo_id=Integer(index=True),
author_first_name=Keyword(),
author_last_name=Keyword(),
author_name=Text()
))
section_shared_with = Nested(properties=dict(
shared_with_repo_id=Keyword(),
shared_with_first_name=Keyword(),
shared_with_last_name=Keyword(),
shared_with_username=Keyword(),
shared_with_name=Text()
))
section_repository_dataset = Nested(properties=dict(
dataset_checksum=Keyword(),
dataset_pid=Keyword(),
dataset_name=Keyword(),
dataset_parent_pid=Keyword(),
dataset_calc_id=Long(),
dataset_parent_calc_id=Long(),
section_dataset_doi=Nested(properties=dict(
dataset_doi_name=Keyword(),
dataset_doi_id=Long()))
))
class CalcData(InnerDoc):
repository_checksum = Keyword()
repository_chemical_formula = Keyword()
repository_parser_id = Keyword()
repository_atomic_elements = Keyword(store=True)
repository_atomic_elements_count = Integer(store=True)
repository_basis_set_type = Keyword(store=True)
repository_code_version = Keyword(store=True)
repository_crystal_system = Keyword(store=True)
repository_program_name = Keyword(store=True)
repository_spacegroup_nr = Keyword(store=True)
repository_system_type = Keyword(store=True)
repository_xc_treatment = Keyword(store=True)
class Calc(InnerDoc, datamodel.Entity):
main_file_uri = Keyword()
secondary_file_uris = Keyword()
repository_filepaths = Keyword(index=False)
repository_archive_gid = Keyword()
repository_calc_id = Long(store=True)
repository_calc_pid = Keyword(store=True)
upload_id = Long()
upload_date = Date(format='epoch_millis')
repository_grouping_checksum = Keyword()
section_repository_userdata = Nested(UserData)
section_repository_parserdata = Nested(CalcData)
section_uploader_info = Nested(properties=dict(
uploader_repo_id=Keyword(),
uploader_first_name=Keyword(),
uploader_last_name=Keyword(),
uploader_username=Keyword(),
uploader_name=Text()
))
@staticmethod
def create_from(source: datamodel.Calc):
coe_calc = source.to(coe_repo.Calc)
calc = Calc()
calc.main_file_uri = coe_calc.mainfile
class Entry(Document):
class Index:
name = config.elastic.coe_repo_calcs_index_name
section_repository_info = Nested(Calc)
# Copyright 2018 Markus Scheidgen
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an"AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import nomad.search
def test_helloworld(elastic):
pass
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment