Skip to content

Commit 1b644bb

Browse files
authored
Merge pull request #95 from aaxelb/10665-support-osfmetrics-migration
[ENG-10665] support osfmetrics migration
2 parents 28a048c + 4d4b67a commit 1b644bb

17 files changed

Lines changed: 526 additions & 489 deletions

README.md

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ Django app for storing time-series metrics in Elasticsearch.
66

77
python importables:
88
- `elasticsearch_metrics`
9-
- `elasticsearch_metrics.imps.elastic8`
10-
- `elasticsearch_metrics.imps.elastic6`
9+
- `elasticsearch_metrics.imps.elastic8` (an implementation with elasticsearch 8)
10+
- `elasticsearch_metrics.imps.elastic6` (an implementation with elasticsearch 6; deprecated)
1111
- ...
1212

1313
## Pre-requisites
@@ -58,7 +58,7 @@ class UsageRecord(EventRecord):
5858
item_id: int
5959

6060
class Index:
61-
using = "my-es8-backend" # optional if only one backend
61+
using = "my-es8-backend" # backend name -- required if multiple backends use the same imp
6262
```
6363

6464
Either enable autosetup...
@@ -98,19 +98,26 @@ UsageRecord.search_timeseries_range(datetime.date(2030, 1, 1), datetime.date(203
9898

9999
## Timeseries indexes
100100

101-
By default, behind the scenes, a new elasticsearch index is created for each record type for each month
102-
in which a record is saved (using UTC timezone). You can set a default change the per-index timespan by
103-
setting `Meta.timedepth` on the record type.
101+
By default, behind the scenes, a new elasticsearch index is created for each record type for each day
102+
in which a record is saved (using UTC timezone). You can change this for a record type by setting
103+
`Meta.timedepth`, or change the default timedepth with the setting `DJELME_DEFAULT_TIMEDEPTH` (see below).
104104

105-
- index per day, '...YYYY_MM_DD...': `timedepth = 3`
106-
- index per month, '...YYYY_MM...': `timedepth = 2`
107-
- index per year, '...YYYY...': `timedepth = 1`
105+
```python
106+
class MyEventWithMonthlyIndexes(EventRecord):
107+
class Meta:
108+
timedepth = 2 # year and month
109+
```
110+
111+
- index per year: `timedepth = 1`
112+
- index per month: `timedepth = 2`
113+
- index per day: `timedepth = 3` (default)
114+
- index per hour: `timedepth = 4`
108115

109116

110117
## Index settings
111118

112-
You can configure the index template settings by setting
113-
`Index.settings` on a record type.
119+
You can configure the index settings that will be set on the index template
120+
and used for each new timeseries index with `Index.settings` on a record type.
114121

115122
```python
116123
class UsageRecord(EventRecord):
@@ -166,27 +173,6 @@ class UsageRecord(MyBaseMetric):
166173
app_label = "myapp"
167174
```
168175

169-
## Optional factory_boy integration
170-
171-
```python
172-
import factory
173-
from elasticsearch_metrics.factory import MetricFactory
174-
175-
from ..myapp.metrics import MyMetric
176-
177-
178-
class MyMetricFactory(MetricFactory):
179-
my_int = factory.Faker("pyint")
180-
181-
class Meta:
182-
model = MyMetric
183-
184-
185-
def test_something():
186-
metric = MyMetricFactory() # index metric in ES
187-
assert isinstance(metric.my_int, int)
188-
```
189-
190176
## Configuration
191177

192178
* `DJELME_BACKENDS`: Named backends for storing or searching records from your django app
@@ -213,9 +199,9 @@ def test_something():
213199
* `DJELME_DEFAULT_TIMEDEPTH`: Set the granularity of timeseries indexes by the number of "time parts" in index names
214200
```
215201
DJELME_DEFAULT_TIMEDEPTH = 1 # yearly indexes; YYYY
216-
DJELME_DEFAULT_TIMEDEPTH = 2 # monthly indexes; YYYY_MM
217-
DJELME_DEFAULT_TIMEDEPTH = 3 # daily indexes; YYYY_MM_DD (this is the default)
218-
DJELME_DEFAULT_TIMEDEPTH = 4 # hourly indexes; YYYY_MM_DD_HH
202+
DJELME_DEFAULT_TIMEDEPTH = 2 # monthly indexes; YYYY.MM
203+
DJELME_DEFAULT_TIMEDEPTH = 3 # daily indexes; YYYY.MM.DD (this is the default)
204+
DJELME_DEFAULT_TIMEDEPTH = 4 # hourly indexes; YYYY.MM.DD.HH
219205
```
220206
you can also set `Meta.timedepth` on a specific record type; this will take precedence
221207

elasticsearch_metrics/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,10 @@
88
"management",
99
"util",
1010
)
11+
12+
###
13+
# timedepth constants, for when you'd rather think in dates than timedepths
14+
YEARLY = 1
15+
MONTHLY = 2
16+
DAILY = 3
17+
HOURLY = 4

elasticsearch_metrics/apps.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ def ready(self) -> None:
3333
)
3434
# autosetup? (default no)
3535
if getattr(settings, AUTOSETUP_SETTING, False) is True:
36-
for (
37-
_backend_name,
38-
_recordtypes,
39-
) in djelme_registry.each_recordtype_by_backend():
36+
_types_by_backend = djelme_registry.recordtypes_by_backend()
37+
for _backend_name, _recordtypes in _types_by_backend.items():
4038
djelme_registry.get_backend(_backend_name).djelme_setup(_recordtypes)

elasticsearch_metrics/imps/elastic6.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ def __new__(mcls, name, bases, attrs): # noqa: B902
9999
# compute it as <app label>_<lowercased class name>
100100
template_name = f"{app_label}_{metric_name}"
101101

102-
new_cls._template_name = template_name
103-
new_cls._template_pattern = f"{template_name}_*" # template pattern
102+
new_cls.__template_name = template_name
103+
new_cls.__template_pattern = f"{template_name}_*" # template pattern
104104
# Abstract base metrics can't be instantiated and don't appear in
105105
# the list of metrics for an app.
106106
if not abstract:
@@ -135,6 +135,16 @@ def construct_index(cls, opts, bases):
135135
i.analyzer(a)
136136
return i
137137

138+
@property
139+
def _template_name(self):
140+
_prefix = self.get_timeseries_name_prefix()
141+
return f"{_prefix}{self.__template_name}"
142+
143+
@property
144+
def _template_pattern(self):
145+
_prefix = self.get_timeseries_name_prefix()
146+
return f"{_prefix}{self.__template_pattern}"
147+
138148

139149
# We need this intermediate BaseMetric class so that
140150
# we can run MetricMeta ahead of IndexMeta
@@ -262,10 +272,7 @@ def get_index_name(cls, date: datetime.date | None = None) -> str:
262272
settings, "ELASTICSEARCH_METRICS_DATE_FORMAT", DEFAULT_DATE_FORMAT
263273
)
264274
date_formatted = date.strftime(dateformat)
265-
_name_parts = (cls._template_name, date_formatted)
266-
if _prefix := cls.get_timeseries_name_prefix():
267-
_name_parts = (_prefix, *_name_parts)
268-
return "_".join(_name_parts)
275+
return f"{cls._template_name}_{date_formatted}"
269276

270277

271278
class Metric(Document, BaseMetric):
@@ -342,7 +349,6 @@ class DjelmeElastic6Backend:
342349

343350
backend_name: str
344351
imp_kwargs: dict[str, str]
345-
namespace_prefix: str = ""
346352

347353
@property
348354
def elastic6_client(self):

0 commit comments

Comments
 (0)