Skip to content

Commit e406808

Browse files
authored
Feature/3 elastic search setup (#15)
* Fix/10 static prefix (#11) * config static_url correctly for both local & AWS * changelog * fix bumpversion cfg * Bump version: 0.1.1 → 0.1.2 * Feature/9 about resolver (#12) * upgrade pip version (as per dependabot advisory) * add new resolvers + tests * changelog * Bump version: 0.1.2 → 0.1.3 * basic working with django-elasticsearch-dsl * working a little better now - with local docker ES v6.8.0 * WIP - where we hit the wall * remove monkeypatch nshm/management * this is now running in --stage DEV and in use by weka-apigw * better search indexing;
1 parent 22c67c8 commit e406808

18 files changed

+782
-52
lines changed

.bumpversion.cfg

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 0.1.1
2+
current_version = 0.1.3
33
commit = True
44
tag = False
55

@@ -9,8 +9,8 @@ replace = version = "{new_version}"
99

1010
[bumpversion:file:package.json]
1111
search = "version": "{current_version}",
12-
replace = "version": "{new_version}"
12+
replace = "version": "{new_version}",
1313

1414
[bumpversion:file:nzshm_model_graphql_api/__init__.py]
15-
search = __version__ = '{current_version}'
16-
replace = __version__ = '{new_version}'
15+
search = __version__ = "{current_version}"
16+
replace = __version__ = "{new_version}"

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changelog
22

3+
## [0.1.3] - 2023-09-04
4+
## Added
5+
- new about and version resolvers
6+
7+
## [0.1.2] - 2023-09-04
8+
## Changed
9+
- configure static_url correctly for both local & AWS
10+
311
## [0.1.1] - 2023-09-01
412
### Added
513
- pytest, tox, etc

README.md

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
ref: https://docs.graphene-python.org/projects/django/en/latest/tutorial-relay/
2-
2+
1
33

44
## the Sqlite3 on lambda issue
55

@@ -50,4 +50,19 @@ poetry run python manage.py runserver_plus
5050
```
5151
poetry shell
5252
npx sls wsgi serve
53-
```
53+
```
54+
55+
### AWS OpenSearch integration
56+
57+
Alert we use [email protected]:daily-science/django-elasticsearch-dsl.git fork for now. This should be PR'd onto the main project.
58+
59+
This project now has ES settings and uses the above to maintain indexes as the django SQL db is maintained.
60+
61+
You can also maintain the indexes via new manage.py commaneds e.g:
62+
63+
```
64+
DEBUG=1 poetry run python manage.py search_index --create
65+
DEBUG=1 poetry run python manage.py search_index --populate
66+
DEBUG=1 poetry run python manage.py search_index --rebuild
67+
DEBug=1 poetry run python manage.py search_index --help
68+
```

db.sqlite3

240 KB
Binary file not shown.

nshm/documents.py

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# documents.py
2+
3+
from django_elasticsearch_dsl import Document, Index, fields
4+
from django_elasticsearch_dsl.registries import registry
5+
6+
from nshm import models
7+
8+
COMMON_INDEX = 'toshi_nshm_models_index'
9+
models_index = Index(COMMON_INDEX)
10+
models_index.settings(
11+
number_of_shards=1,
12+
number_of_replicas=0
13+
)
14+
15+
class DocumentWithNodeId(Document):
16+
"""
17+
Override the object ID used in the search engine to be similar to that use in Toshi_ID
18+
"""
19+
@classmethod
20+
def generate_id(cls, object_instance):
21+
"""
22+
The default behavior is to use the Django object's pk (id) as the
23+
elasticseach index id (_id). If needed, this method can be overloaded
24+
to change this default behavior.
25+
"""
26+
#return object_instance.pk
27+
return f"{cls.Django.model.__name__}_{object_instance.pk}"
28+
29+
@registry.register_document
30+
class SeismicHazardModelDocument(DocumentWithNodeId):
31+
32+
class Index:
33+
name = COMMON_INDEX
34+
35+
class Django:
36+
model = models.SeismicHazardModel # The model associated with this Document
37+
fields = [
38+
"id",
39+
"version",
40+
"notes",
41+
]
42+
43+
source_logic_tree = fields.ObjectField(properties={
44+
'version': fields.TextField(),
45+
})
46+
gmcm_logic_tree = fields.ObjectField(properties={
47+
'version': fields.TextField(),
48+
})
49+
50+
@registry.register_document
51+
class SourceLogicTreeDocument(DocumentWithNodeId):
52+
class Index:
53+
name = COMMON_INDEX
54+
55+
class Django:
56+
model = models.SourceLogicTree
57+
fields = [
58+
'version',
59+
'notes'
60+
]
61+
62+
seismic_hazard_models = fields.NestedField(properties={
63+
'version': fields.TextField(),
64+
'id': fields.TextField()
65+
})
66+
67+
slt_weighted_components = fields.NestedField(properties={
68+
'weight': fields.TextField(),
69+
'id': fields.TextField()
70+
})
71+
72+
@registry.register_document
73+
class SourceLogicTreeComponentDocument(DocumentWithNodeId):
74+
class Index:
75+
name = COMMON_INDEX
76+
77+
class Django:
78+
model = models.SourceLogicTreeComponent
79+
fields = [
80+
'tag',
81+
'notes',
82+
'inversion_toshi_id',
83+
'background_toshi_id',
84+
'tectonic_region',
85+
'group'
86+
]
87+
88+
slt_weighted_components = fields.NestedField(properties={
89+
'weight': fields.TextField(),
90+
'id': fields.TextField()
91+
})
92+
93+
94+
@registry.register_document
95+
class SourceLogicTreeWeightedComponentDocument(DocumentWithNodeId):
96+
97+
class Index:
98+
name = COMMON_INDEX
99+
100+
class Django:
101+
model = models.SourceLogicTreeWeightedComponent
102+
fields = [
103+
'weight',
104+
]
105+
106+
source_logic_tree = fields.ObjectField(properties={
107+
'version': fields.TextField(),
108+
'id': fields.TextField()
109+
})
110+
111+
source_logic_tree_component = fields.ObjectField(properties={
112+
'tag': fields.TextField(),
113+
'id': fields.TextField()
114+
})
115+
116+
117+
@registry.register_document
118+
class HazardSolutionDocument(DocumentWithNodeId):
119+
class Index:
120+
name = COMMON_INDEX
121+
122+
class Django:
123+
model = models.HazardSolution # The model associated with this Document
124+
fields = [
125+
"id",
126+
"solution_id",
127+
"created",
128+
"vs30",
129+
"notes",
130+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 4.2.4 on 2023-09-04 04:54
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("nshm", "0013_delete_openquakehazardtask"),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name="hazardsolution",
15+
name="typename",
16+
field=models.CharField(default="HazardSolution", max_length=50),
17+
),
18+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 4.2.4 on 2023-09-04 04:59
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("nshm", "0014_hazardsolution_typename"),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name="seismichazardmodel",
15+
name="typename",
16+
field=models.CharField(default="SeismicHazardModel", max_length=50),
17+
),
18+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Generated by Django 4.2.4 on 2023-09-04 06:11
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("nshm", "0015_seismichazardmodel_typename"),
10+
]
11+
12+
operations = [
13+
migrations.RemoveField(
14+
model_name="seismichazardmodel",
15+
name="typename",
16+
),
17+
]

nshm/models.py

+1-12
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ class SeismicHazardModel(models.Model):
8383
def __str__(self):
8484
return self.version
8585

86-
8786
class LocationList(models.Model):
8887
list_id = models.CharField(max_length=10)
8988
notes = models.TextField(null=True, blank=True)
@@ -94,6 +93,7 @@ def __str__(self):
9493

9594

9695
class HazardSolution(models.Model):
96+
typename = models.CharField(max_length=50, null=False, default="HazardSolution")
9797
solution_id = models.CharField(max_length=50, null=False)
9898
created = models.DateTimeField(auto_now=False, auto_now_add=False)
9999
vs30 = models.SmallIntegerField()
@@ -105,14 +105,3 @@ class HazardSolution(models.Model):
105105
SourceLogicTreeWeightedComponent, related_name="hazard_solutions"
106106
)
107107

108-
109-
# class OpenquakeHazardTask(models.Model):
110-
# general_task_id = models.CharField(max_length=50, null=False)
111-
# date = models.DateField(auto_now=False, auto_now_add=False)
112-
# config_info = models.TextField(null=True, blank=True)
113-
# notes = models.TextField(null=True, blank=True)
114-
# part_of = models.ForeignKey(
115-
# SeismicHazardModel, related_name='hazard_tasks',null=True, blank=True, on_delete=models.SET_NULL)
116-
117-
# def __str__(self):
118-
# return self.general_task_id

nzshm_model_graphql_api/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Top-level package for kororaa-graphql-api."""
22

33
__author__ = """GNS Science New Zealand"""
4-
__email__ = '[email protected]'
5-
__version__ = '0.1.1'
4+
__email__ = "[email protected]"
5+
__version__ = "0.1.3"

nzshm_model_graphql_api/schema.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
import graphene
2+
from graphene import relay
23

34
import nshm.schema
45
import pipeline.schema
6+
from nzshm_model_graphql_api import __version__
57

8+
from nshm.schema import SeismicHazardModel #, HazardSolution
9+
from nzshm_model_graphql_api import settings
610

711
class Query(nshm.schema.Query, pipeline.schema.Query, graphene.ObjectType):
812
# This class will inherit from multiple Queries
913
# as we begin to add more apps to our project
10-
pass
1114

15+
node = relay.Node.Field()
16+
about = graphene.String(description="About this API ")
17+
version = graphene.String(description="API version string")
18+
19+
def resolve_version(root, info, **args):
20+
return __version__
21+
22+
def resolve_about(root, info, **args):
23+
return f"Hello World, I am nshm_model_graphql_api version {__version__}"
1224

1325
schema = graphene.Schema(query=Query)

0 commit comments

Comments
 (0)