44from elasticsearch .exceptions import NotFoundError
55from elasticsearch_dsl import Date , Document , InnerDoc , Nested , Q , Search
66
7- # TODO: Remove this. Current Structure
8- # Index Templates <-- Indexes <-- Documents <-- Points
9-
107
118class Point (InnerDoc ):
129 time = Date (required = True , default_timezone = settings .TIME_ZONE )
1310 fields = Nested (dynamic = True , required = True , multi = True )
1411
1512
16- class MetricIndex (Document ):
13+ class MetricDocument (Document ):
1714 tags = Nested (dynamic = True , required = False , multi = True )
1815 points = Nested (Point )
1916
@@ -47,18 +44,20 @@ def find_metric(client, index, tags, retention_policy=None, add=False):
4744 return result ['id' ], result ['index' ]
4845 except (NotFoundError , AttributeError , IndexError ):
4946 if add :
50- obj = add_doc (client , index , tags , retention_policy = retention_policy )
51- return obj ['_id' ], obj ['_index' ]
47+ document = create_document (
48+ client , index , tags , retention_policy = retention_policy
49+ )
50+ return document ['_id' ], document ['_index' ]
5251 return None
5352
5453
55- def add_doc (client , key , tags , _id = None , retention_policy = None ):
54+ def create_document (client , key , tags , _id = None , retention_policy = None ):
5655 """
57- Add index to elasticsearch using ``keys``, ``tags`` and ``id`` provided.
56+ Adds document to relevant index using ``keys``, ``tags`` and ``id`` provided.
5857 If no ``id`` is provided a random ``uuid`` would be used.
5958 """
6059 _id = str (_id or uuid .uuid1 ())
61- # If index exists, add the doc and return
60+ # If index exists, create the document and return
6261 try :
6362 index_aliases = client .indices .get_alias (index = key )
6463 for k , v in index_aliases .items ():
@@ -70,24 +69,24 @@ def add_doc(client, key, tags, _id=None, retention_policy=None):
7069 pass
7170 # Create a new index if it doesn't exist
7271 name = f'{ key } -000001'
73- obj = MetricIndex (meta = {'id' : _id })
74- obj ._index = obj ._index .clone (name )
72+ document = MetricDocument (meta = {'id' : _id })
73+ document ._index = document ._index .clone (name )
7574 # Create a new index template if it doesn't exist
7675 if not client .indices .exists_template (name = key ):
77- obj ._index .settings (** {'lifecycle.rollover_alias' : key })
76+ document ._index .settings (** {'lifecycle.rollover_alias' : key })
7877 if retention_policy :
79- obj ._index .settings (** {'lifecycle.name' : retention_policy })
80- # index pattern is added for Index Lifecycle Management
81- obj ._index .as_template (key , f'{ key } -*' ).save (using = client )
82- obj .init (using = client , index = name )
83- obj .meta .index = name
84- obj .tags = tags
85- obj .save (using = client , index = name )
78+ document ._index .settings (** {'lifecycle.name' : retention_policy })
79+ # add index pattern is added for Index Lifecycle Management
80+ document ._index .as_template (key , f'{ key } -*' ).save (using = client )
81+ document .init (using = client , index = name )
82+ document .meta .index = name
83+ document .tags = tags
84+ document .save (using = client , index = name )
8685 client .indices .put_alias (index = name , name = key , body = {'is_write_index' : True })
8786 if retention_policy :
8887 client .indices .put_settings (
8988 body = {'lifecycle.name' : retention_policy }, index = name
9089 )
9190 client .indices .put_settings (body = {'lifecycle.rollover_alias' : key }, index = name )
9291 client .indices .refresh (index = key )
93- return obj .to_dict (include_meta = True )
92+ return document .to_dict (include_meta = True )
0 commit comments