Skip to content

Commit ff0be37

Browse files
committed
ensure properties and ids contain ISO8601-compliant datetime
1 parent 9216827 commit ff0be37

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

msc_pygeoapi/connector/elasticsearch_.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -180,17 +180,27 @@ def delete(self, indexes):
180180

181181
return True
182182

183-
def create_template(self, name, settings):
183+
def create_template(self, name, settings, overwrite=False):
184184
"""
185185
create an Elasticsearch index template
186186
187187
:param name: `str` index template name
188188
:param settings: `dict` settings dictionnary for index template
189+
:param overwrite: `bool` indicating whether to overwrite existing
190+
template
189191
190192
:returns: `bool` of index template creation status
191193
"""
192194

193-
if not self.Elasticsearch.indices.exists_template(name=name):
195+
template_exists = self.Elasticsearch.indices.exists_template(name=name)
196+
197+
if template_exists and overwrite:
198+
self.Elasticsearch.indices.delete_template(name=name)
199+
self.Elasticsearch.indices.put_template(name=name, body=settings)
200+
elif template_exists:
201+
LOGGER.warning(f'Template {name} already exists')
202+
return False
203+
else:
194204
self.Elasticsearch.indices.put_template(name=name, body=settings)
195205

196206
return True

msc_pygeoapi/loader/hydrometric_realtime.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from msc_pygeoapi.util import (
4242
check_es_indexes_to_delete,
4343
configure_es_connection,
44+
DATETIME_RFC3339_FMT
4445
)
4546

4647

@@ -60,7 +61,7 @@
6061
SETTINGS = {
6162
'order': 0,
6263
'version': 1,
63-
'index_patterns': [INDEX_BASENAME],
64+
'index_patterns': [f'{INDEX_BASENAME}*'],
6465
'settings': {
6566
'number_of_shards': 1,
6667
'number_of_replicas': 0
@@ -98,7 +99,7 @@
9899
},
99100
'DATETIME': {
100101
'type': 'date',
101-
'format': 'strict_date_hour_minute_second||strict_date_optional_time' # noqa
102+
'format': 'date_time_no_millis||strict_date_optional_time' # noqa
102103
},
103104
'DATETIME_LST': {
104105
'type': 'date',
@@ -174,7 +175,7 @@ def __init__(self, conn_config={}):
174175
BaseLoader.__init__(self)
175176

176177
self.conn = ElasticsearchConnector(conn_config)
177-
self.conn.create_template(INDEX_BASENAME, SETTINGS)
178+
self.conn.create_template(INDEX_BASENAME, SETTINGS, overwrite=True)
178179

179180
self.stations = {}
180181
self.read_stations_list()
@@ -284,11 +285,10 @@ def generate_observations(self, filepath):
284285
try:
285286
# Convert timestamp to UTC time.
286287
utc_datetime = delocalize_date(date_)
287-
utc_datestamp = utc_datetime.strftime('%Y-%m-%d.%H:%M:%S')
288+
utc_datestamp = utc_datetime.strftime(DATETIME_RFC3339_FMT)
288289
# Generate an ID now that all fields are known.
289290
observation_id = f'{station}.{utc_datestamp}'
290291

291-
utc_datestamp = utc_datestamp.replace('.', 'T')
292292
except Exception as err:
293293
LOGGER.error(f'Cannot interpret datetime value {date_} in {filepath}' # noqa
294294
f' due to: {err} (skipping)')

0 commit comments

Comments
 (0)