Skip to content

Commit 2c72aba

Browse files
committed
Pilot HDC 1.0
1 parent 12ee68a commit 2c72aba

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+6632
-1
lines changed

.dockerignore

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Folders
2+
.git
3+
.venv
4+
venv
5+
kubernetes
6+
7+
# Files
8+
.DS_Store
9+
.gitignore
10+
.gitlab-ci*
11+
gitlab-ci*
12+
.github
13+
Dockerfile*
14+
Jenkinsfile

.env.schema

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
KAFKA_SERVICE=
2+
KAFKA_TOPICS=[]
3+
ELASTICSEARCH_SERVICE=
4+
APP_NAME=
5+
VERSION=
6+
HOST=
7+
PORT=
8+
WORKERS=
9+
METADATA_SERVICE=
10+
PROJECT_SERVICE=
11+
METADATA_SERVICE_PAGE_SIZE=
12+
PROJECT_SERVICE_PAGE_SIZE=

.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
.pybuilder/
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pyenv
86+
# For a library or package, you might want to ignore these files since the code is
87+
# intended to run in multiple environments; otherwise, check them in:
88+
# .python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
96+
97+
# poetry
98+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99+
# This is especially recommended for binary packages to ensure reproducibility, and is more
100+
# commonly ignored for libraries.
101+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102+
#poetry.lock
103+
104+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
105+
__pypackages__/
106+
107+
# Celery stuff
108+
celerybeat-schedule
109+
celerybeat.pid
110+
111+
# SageMath parsed files
112+
*.sage.py
113+
114+
# Environments
115+
.env
116+
.venv
117+
env/
118+
venv/
119+
ENV/
120+
env.bak/
121+
venv.bak/
122+
123+
# Spyder project settings
124+
.spyderproject
125+
.spyproject
126+
127+
# Rope project settings
128+
.ropeproject
129+
130+
# mkdocs documentation
131+
/site
132+
133+
# mypy
134+
.mypy_cache/
135+
.dmypy.json
136+
dmypy.json
137+
138+
# Pyre type checker
139+
.pyre/
140+
141+
# pytype static type analyzer
142+
.pytype/
143+
144+
# Cython debug symbols
145+
cython_debug/
146+
147+
# PyCharm
148+
# JetBrains specific template is maintainted in a separate JetBrains.gitignore that can
149+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
150+
# and can be added to the global gitignore or merged into this file. For a more nuclear
151+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
152+
153+
.idea/
154+
155+
.DS_Store
156+
logs/

.pre-commit-config.yaml

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
repos:
2+
3+
- repo: https://github.com/pre-commit/pre-commit-hooks
4+
rev: v4.3.0
5+
hooks:
6+
- id: check-added-large-files
7+
- id: check-docstring-first
8+
- id: check-merge-conflict
9+
- id: check-toml
10+
- id: check-yaml
11+
- id: double-quote-string-fixer
12+
- id: end-of-file-fixer
13+
- id: trailing-whitespace
14+
15+
- repo: https://github.com/psf/black
16+
rev: 22.8.0
17+
hooks:
18+
- id: black
19+
args: [
20+
'--line-length=120',
21+
'--skip-string-normalization',
22+
]
23+
24+
- repo: https://github.com/PyCQA/isort
25+
rev: 5.12.0
26+
hooks:
27+
- id: isort
28+
args: [
29+
'--line-length=120',
30+
'--profile=black',
31+
'--filter-files',
32+
'--force-single-line-imports',
33+
'--reverse-relative',
34+
]
35+
36+
- repo: https://github.com/PyCQA/flake8
37+
rev: 5.0.4
38+
hooks:
39+
- id: flake8
40+
additional_dependencies: [
41+
'pycodestyle==2.9.1', # E,W
42+
'pyflakes==2.5.0', # F
43+
'mccabe==0.7.0', # C
44+
'flake8-bugbear==22.9.11', # B
45+
'flake8-builtins==1.5.3', # A
46+
'flake8-comprehensions==3.10.0', # C4
47+
'flake8-debugger==4.1.2', # T1
48+
'flake8-logging-format==0.7.5', # G
49+
'flake8-print==5.0.0', # T2
50+
]
51+
args: [
52+
'--select=E,W,F,C,B,A,C4,T1,G,T2',
53+
'--ignore=E203,W503,B008,B305,A003,G004',
54+
'--max-complexity=10',
55+
'--max-line-length=120',
56+
]
57+
58+
- repo: https://github.com/myint/docformatter
59+
rev: v1.5.0
60+
hooks:
61+
- id: docformatter
62+
args: [
63+
'--wrap-summaries=120',
64+
'--wrap-descriptions=120',
65+
'--in-place',
66+
]
67+
68+
- repo: https://github.com/Lucas-C/pre-commit-hooks
69+
rev: v1.4.2
70+
hooks:
71+
- id: insert-license
72+
files: \.py$
73+
args: [
74+
'--license-filepath=COPYRIGHT',
75+
'--comment-style=#',
76+
'--use-current-year',
77+
]

COPYRIGHT

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Copyright (C) 2022-2023 Indoc Systems
2+
3+
Licensed under the GNU AFFERO GENERAL PUBLIC LICENSE, Version 3.0 (the "License") available at https://www.gnu.org/licenses/agpl-3.0.en.html.
4+
You may not use this file except in compliance with the License.

LICENSE

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
Copyright (C) 2022-2023 Indoc Systems
2+
13
GNU AFFERO GENERAL PUBLIC LICENSE
24
Version 3, 19 November 2007
35

@@ -658,4 +660,4 @@ specific requirements.
658660
You should also get your employer (if you work as a programmer) or school,
659661
if any, to sign a "copyright disclaimer" for the program, if necessary.
660662
For more information on this, and how to apply and follow the GNU AGPL, see
661-
<https://www.gnu.org/licenses/>.
663+
<https://www.gnu.org/licenses/>.

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Metadata event handler
2+
3+
[![Python](https://img.shields.io/badge/python-3.10-brightgreen.svg)](https://www.python.org/)
4+
5+
## About
6+
7+
Consumes Metadata events produced by the Postgres connector and writes data into ElasticSearch.
8+
9+
### Built With
10+
11+
- Python
12+
- [elasticsearch](https://pypi.org/project/elasticsearch/)
13+
- [kafka-python3](https://pypi.org/project/kafka-python3/)
14+
15+
## Acknowledgements
16+
The development of the HealthDataCloud open source software was supported by the EBRAINS research infrastructure, funded from the European Union's Horizon 2020 Framework Programme for Research and Innovation under the Specific Grant Agreement No. 945539 (Human Brain Project SGA3) and H2020 Research and Innovation Action Grant Interactive Computing E-Infrastructure for the Human Brain Project ICEI 800858.

commons/__init__.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Copyright (C) 2022-2023 Indoc Systems
2+
#
3+
# Licensed under the GNU AFFERO GENERAL PUBLIC LICENSE, Version 3.0 (the "License") available at https://www.gnu.org/licenses/agpl-3.0.en.html.
4+
# You may not use this file except in compliance with the License.
5+

commons/base_consumer.py

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Copyright (C) 2022-2023 Indoc Systems
2+
#
3+
# Licensed under the GNU AFFERO GENERAL PUBLIC LICENSE, Version 3.0 (the "License") available at https://www.gnu.org/licenses/agpl-3.0.en.html.
4+
# You may not use this file except in compliance with the License.
5+
6+
import base64
7+
import io
8+
import logging
9+
import math
10+
from datetime import datetime
11+
from typing import Any
12+
13+
from aiokafka import ConsumerRecord
14+
from fastavro import schema
15+
from fastavro import schemaless_reader
16+
from fastavro import validate
17+
18+
logger = logging.getLogger(__name__)
19+
20+
21+
class BaseConsumer:
22+
def __init__(self) -> None:
23+
pass
24+
25+
def decode_label_from_ltree(self, encoded_string: str) -> str:
26+
missing_padding = math.ceil(len(encoded_string) / 8) * 8 - len(encoded_string)
27+
if missing_padding:
28+
encoded_string += '=' * missing_padding
29+
utf8_string = base64.b32decode(encoded_string.encode('utf-8')).decode('utf-8')
30+
return utf8_string
31+
32+
def convert_timestamp_millis_to_second(self, timestamp: int) -> int:
33+
return timestamp // 1000
34+
35+
def convert_datetime_to_timestamp(self, date: datetime) -> int:
36+
return int(date.timestamp())
37+
38+
def decode_message(self, message: bytes, topic: str) -> dict[str, Any]:
39+
logger.info(f'Starting to decode message from topic "{topic}".')
40+
try:
41+
imported_schema = schema.load_schema(self.KAFKA_SCHEMAS_PATH / f'{topic}.avsc')
42+
message_reader = io.BytesIO(message)
43+
message_decoded = schemaless_reader(message_reader, imported_schema)
44+
is_valid = validate(message_decoded, imported_schema, raise_errors=False)
45+
logger.info(f'Decoded a message from a topic "{topic}": {message_decoded}')
46+
if not is_valid:
47+
logger.warning(f'Unable validate decoded message from topic "{topic}".')
48+
return {}
49+
50+
except Exception:
51+
logger.exception(f'Unable to decode message from topic "{topic}".')
52+
return {}
53+
54+
logger.info(f'Decoded a message from a topic "{topic}": {message_decoded}')
55+
return message_decoded
56+
57+
async def process_event(self, event: ConsumerRecord) -> None:
58+
topic = event.topic
59+
message = self.decode_message(message=event.value, topic=topic)
60+
if not message:
61+
await self.producer.send_and_wait('metadata.dlq', event.value)
62+
else:
63+
await self.process_topic_message(topic, message)
64+
65+
async def process_topic_message(self, topic: str, message: dict[str, Any]) -> None:
66+
pass
67+
68+
async def run(self) -> None:
69+
raise Exception('The class is missing the entry funtion `run()`!')

commons/encoders.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright (C) 2022-2023 Indoc Systems
2+
#
3+
# Licensed under the GNU AFFERO GENERAL PUBLIC LICENSE, Version 3.0 (the "License") available at https://www.gnu.org/licenses/agpl-3.0.en.html.
4+
# You may not use this file except in compliance with the License.
5+
6+
from datetime import datetime
7+
8+
9+
def convert_datetime_to_timestamp_millisecond(date: datetime) -> int:
10+
"""Translate datetime to timestamp in milliseconds.
11+
12+
:param date: Date
13+
:return: Timestamp in milliseconds
14+
"""
15+
return int(date.timestamp() * 1000)
16+
17+
18+
datetime_as_timestamp_milli_encoder = {
19+
datetime: convert_datetime_to_timestamp_millisecond,
20+
}

0 commit comments

Comments
 (0)