Skip to content

Commit 39f21e5

Browse files
authored
Merge pull request #61 from di2ag/yakaboskic-opentelemetry
Upgrading TRAPI and BioLink and adding in OpenTelemetry
2 parents 196438b + c106ac5 commit 39f21e5

File tree

76 files changed

+3082
-216
lines changed

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

+3082
-216
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "gennifer"]
2+
path = gennifer
3+
url = git@github.com:di2ag/gennifer.git

Dockerfile renamed to chp_api/Dockerfile

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
###########
44

55
# first stage of build to pull repos
6-
FROM python:3.8 as intermediate
6+
FROM python:3.9 as intermediate
77

88
# set work directory
99
WORKDIR /usr/src/chp_api
1010

11-
RUN git clone --single-branch --branch gene_spec_pydantic-ghyde https://github.com/di2ag/gene-specificity.git
11+
RUN git clone --single-branch --branch master https://github.com/di2ag/gene-specificity.git
12+
13+
# Upgrade pip
14+
RUN pip3 install --upgrade pip
1215

1316
# Upgrade pip
1417
RUN pip3 install --upgrade pip
@@ -25,7 +28,7 @@ RUN cd gene-specificity && python3 setup.py bdist_wheel && cd dist && cp gene_sp
2528
#########
2629

2730
#pull official base image
28-
FROM python:3.8
31+
FROM python:3.9
2932

3033
# add app user
3134
RUN groupadd chp_api && useradd -ms /bin/bash -g chp_api chp_api
@@ -53,10 +56,13 @@ RUN pip3 install pydantic==1.10.12
5356
RUN pip3 install --no-cache /wheels/*
5457

5558
# copy project
56-
COPY ./chp_api/chp_api $APP_HOME/chp_api
57-
COPY ./chp_api/manage.py $APP_HOME
58-
COPY ./chp_api/dispatcher $APP_HOME/dispatcher
59-
COPY ./gunicorn.config.py $APP_HOME
59+
COPY . .
60+
#COPY ./chp_api $APP_HOME/chp_api
61+
#COPY ./manage.py $APP_HOME
62+
#COPY ./dispatcher $APP_HOME/dispatcher
63+
#COPY ./gennifer $APP_HOME/gennifer
64+
#COPY ./chp_db_fixture.json.gz $APP_HOME
65+
#COPY ./gunicorn.config.py $APP_HOME
6066

6167
# chown all the files to the app user
6268
RUN chown -R chp_api:chp_api $APP_HOME \

chp_api/chp_api/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .celery import app as celery_app
2+
3+
__all__ = ("celery_app",)

chp_api/chp_api/celery.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import os
2+
from celery import Celery
3+
4+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "chp_api.settings")
5+
app = Celery(
6+
"chp_api",
7+
include=['gennifer.tasks'],
8+
)
9+
app.config_from_object("django.conf:settings", namespace="CELERY")
10+
app.autodiscover_tasks()
11+
app.conf.update({
12+
"task_routes": {
13+
"create_gennifer_task": {"queue": 'chp_api'}
14+
}
15+
})

chp_api/chp_api/serializers.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
2+
3+
class ChpTokenObtainPairSerializer(TokenObtainPairSerializer):
4+
@classmethod
5+
def get_token(cls, user):
6+
token = super().get_token(user)
7+
8+
# Add custom claims
9+
token['email'] = user.email
10+
token['username'] = user.username
11+
12+
return token

chp_api/chp_api/settings.py

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
https://docs.djangoproject.com/en/3.0/ref/settings/
1111
"""
1212
import os
13-
from importlib import import_module
1413
import environ as environ # type: ignore
1514

15+
from importlib import import_module
16+
17+
1618
# Initialise environment variables
1719
env = environ.Env()
1820
environ.Env.read_env()
@@ -27,9 +29,33 @@
2729
REST_FRAMEWORK = {
2830
'DEFAULT_PARSER_CLASSES': [
2931
'rest_framework.parsers.JSONParser',
30-
]
32+
],
33+
'DEFAULT_AUTHENTICATION_CLASSES': (
34+
#'rest_framework_simplejwt.authentication.JWTAuthentication',
35+
'oauth2_provider.contrib.rest_framework.OAuth2Authentication',
36+
),
37+
'DEFAULT_PERMISSION_CLASSES': (
38+
'rest_framework.permissions.IsAuthenticatedOrReadOnly',
39+
)
3140
}
3241

42+
AUTHENTICATION_BACKENDS = [
43+
'oauth2_provider.backends.OAuth2Backend',
44+
# Uncomment following if you want to access the admin
45+
'django.contrib.auth.backends.ModelBackend',
46+
]
47+
48+
OAUTH2_PROVIDER = {
49+
# this is the list of available scopes
50+
'SCOPES': {'read': 'Read scope', 'write': 'Write scope', 'groups': 'Access to your groups'}
51+
}
52+
53+
# Cors stuff (must go before installed apps)
54+
CORS_ALLOWED_ORIGINS = [
55+
'http://localhost',
56+
'http://localhost:3000',
57+
]
58+
3359
# Application definition
3460
INSTALLED_BASE_APPS = [
3561
'django.contrib.admin',
@@ -38,13 +64,20 @@
3864
'django.contrib.sessions',
3965
'django.contrib.messages',
4066
'django.contrib.staticfiles',
67+
'corsheaders',
4168
'rest_framework',
69+
'rest_framework_simplejwt',
70+
'django_filters',
4271
'dispatcher.apps.DispatcherConfig',
4372
'django_extensions',
73+
'users',
74+
'oauth2_provider',
75+
#'gennifer', # Need to make into CHP app
4476
]
4577

4678
INSTALLED_CHP_APPS = [
4779
'gene_specificity',
80+
'gennifer',
4881
]
4982

5083
# CHP Versions
@@ -54,13 +87,16 @@
5487
INSTALLED_APPS = INSTALLED_BASE_APPS + INSTALLED_CHP_APPS
5588

5689
MIDDLEWARE = [
90+
'corsheaders.middleware.CorsMiddleware',
5791
'django.middleware.security.SecurityMiddleware',
5892
'django.contrib.sessions.middleware.SessionMiddleware',
5993
'django.middleware.common.CommonMiddleware',
6094
'django.middleware.csrf.CsrfViewMiddleware',
6195
'django.contrib.auth.middleware.AuthenticationMiddleware',
6296
'django.contrib.messages.middleware.MessageMiddleware',
6397
'django.middleware.clickjacking.XFrameOptionsMiddleware',
98+
'django.contrib.auth.middleware.AuthenticationMiddleware',
99+
'oauth2_provider.middleware.OAuth2TokenMiddleware',
64100
]
65101

66102
ROOT_URLCONF = 'chp_api.urls'
@@ -116,6 +152,10 @@
116152
},
117153
]
118154

155+
# Authorization
156+
AUTH_USER_MODEL='users.User'
157+
LOGIN_URL='/admin/login/'
158+
119159
# Internationalization
120160
# https://docs.djangoproject.com/en/3.0/topics/i18n/
121161
LANGUAGE_CODE = 'en-us'
@@ -193,3 +233,21 @@
193233
if not DJANGO_SUPERUSER_PASSWORD:
194234
with open(env("DJANGO_SUPERUSER_PASSWORD_FILE"), 'r') as dsp_file:
195235
os.environ["DJANGO_SUPERUSER_PASSWORD"] = dsp_file.readline().strip()
236+
237+
# Simple JWT Settings
238+
SIMPLE_JWT = {
239+
"TOKEN_OBTAIN_SERIALIZER": "chp_api.serializers.ChpTokenObtainPairSerializer",
240+
}
241+
242+
# Celery Settings
243+
CELERY_BROKER_URL = os.environ.get("CELERY_BROKER_URL", "redis://localhost:6379")
244+
CELERY_RESULT_BACKEND = os.environ.get("CELERY_RESULT_BACKEND", "redis://localhost:6379")
245+
246+
# Gennifer settings
247+
GENNIFER_ALGORITHM_URLS = [
248+
"http://pidc:5000",
249+
"http://grisli:5000",
250+
"http://genie3:5000",
251+
"http://grnboost2:5000",
252+
"http://bkb-grn:5000",
253+
]

chp_api/chp_api/urls.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,20 @@
1717
from django.urls import path, include
1818
from rest_framework.urlpatterns import format_suffix_patterns
1919

20+
from rest_framework_simplejwt.views import (
21+
TokenObtainPairView,
22+
TokenRefreshView,
23+
)
24+
25+
2026
urlpatterns = [
2127
path('admin/', admin.site.urls),
2228
path('', include('dispatcher.urls')),
29+
path('gennifer/api/', include('gennifer.urls')),
30+
path('api/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
31+
path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
32+
path('o/', include('oauth2_provider.urls', namespace='oauth2_provider')),
33+
path('users/', include('users.urls')),
2334
]
2435

2536

chp_api/dispatcher/admin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from django.contrib import admin
22

3-
from .models import App, ZenodoFile, DispatcherSettings
3+
from .models import App, ZenodoFile, DispatcherSetting
44

55
admin.site.register(App)
66
admin.site.register(ZenodoFile)
7-
admin.site.register(DispatcherSettings)
7+
admin.site.register(DispatcherSetting)

chp_api/dispatcher/base.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
from importlib import import_module
1313
from collections import defaultdict
1414

15-
from .models import Transaction, App, DispatcherSettings, Template, TemplateMatch
16-
from reasoner_pydantic import MetaKnowledgeGraph, Message, MetaEdge
15+
from .models import Transaction, App, DispatcherSetting, Template, TemplateMatch
16+
from reasoner_pydantic import MetaKnowledgeGraph, Message, MetaEdge, MetaNode
1717
from reasoner_pydantic.qgraph import QNode, QEdge
1818

1919
# Setup logging
@@ -42,9 +42,27 @@ def __init__(self, request, trapi_version, biolink_version):
4242
#self.validator = TRAPISchemaValidator(self.trapi_version)
4343
self.logger = Logger()
4444

45+
def merge_meta_kg(self, metakg1, metakg2):
46+
new_metakg = MetaKnowledgeGraph.parse_obj({"nodes": [], "edges": []})
47+
# Merge nodes
48+
new_metakg.nodes = metakg1.nodes
49+
for n, v in metakg2.nodes.items():
50+
if n in new_metakg.nodes:
51+
id_prefixes = list(set.union(*[set(list(metakg1.nodes[n].id_prefixes)), set(list(metakg2.nodes[n].id_prefixes))]))
52+
new_node = MetaNode.parse_obj({"id_prefixes": id_prefixes})
53+
new_metakg.nodes[n] = new_node
54+
else:
55+
new_metakg.nodes[n] = MetaNode.parse_obj(v)
56+
# Merge edges
57+
for e in metakg1.edges:
58+
new_metakg.edges.append(e)
59+
for e in metakg2.edges:
60+
new_metakg.edges.append(e)
61+
return new_metakg
62+
4563
def get_meta_knowledge_graph(self):
4664
# Get current trapi and biolink versions
47-
dispatcher_settings = DispatcherSettings.load()
65+
dispatcher_settings = DispatcherSetting.load()
4866
merged_meta_kg = None
4967
for app, app_name in zip(APPS, settings.INSTALLED_CHP_APPS):
5068
app_db_obj = App.objects.get(name=app_name)
@@ -59,7 +77,7 @@ def get_meta_knowledge_graph(self):
5977
if merged_meta_kg is None:
6078
merged_meta_kg = meta_kg
6179
else:
62-
merged_meta_kg.update(meta_kg)
80+
merged_meta_kg = self.merge_meta_kg(merged_meta_kg, meta_kg)
6381
return merged_meta_kg
6482

6583
def process_invalid_trapi(self, request):
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 4.2.1 on 2023-05-29 22:26
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('dispatcher', '0007_template_templatematch'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='dispatchersettings',
15+
name='sri_node_normalizer_baseurl',
16+
field=models.URLField(default='https://nodenormalization-sri.renci.org', max_length=128),
17+
),
18+
]

0 commit comments

Comments
 (0)