-
-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy pathmodels.py
More file actions
1033 lines (933 loc) · 36.2 KB
/
models.py
File metadata and controls
1033 lines (933 loc) · 36.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import logging
from collections import OrderedDict
from copy import deepcopy
from datetime import date, datetime, timedelta
import django
from cache_memoize import cache_memoize
from dateutil.parser import parse as parse_date
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.core.validators import MaxValueValidator
from django.db import IntegrityError, models
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from openwisp_notifications.signals import notify
from pytz import timezone as tz
from pytz import utc
from swapper import get_model_name
from openwisp_monitoring.monitoring.utils import clean_timeseries_data_key
from openwisp_utils.base import TimeStampedEditableModel
from ...db import default_chart_query, timeseries_db
from ...settings import CACHE_TIMEOUT, DEFAULT_CHART_TIME
from .. import settings as app_settings
from ..configuration import (
CHART_CONFIGURATION_CHOICES,
DEFAULT_COLORS,
METRIC_CONFIGURATION_CHOICES,
get_chart_configuration,
get_chart_configuration_choices,
get_metric_configuration,
get_metric_configuration_choices,
)
from ..exceptions import InvalidChartConfigException, InvalidMetricConfigException
from ..signals import pre_metric_write, threshold_crossed
from ..tasks import _timeseries_batch_write, _timeseries_write, delete_timeseries
User = get_user_model()
logger = logging.getLogger(__name__)
def get_metric_cache_key(*args, **kwargs):
cache_key = [
"get_or_create_metric",
]
cache_key.append(
"key={}".format(
kwargs.get("key", kwargs.get("name", kwargs.get("configuration")))
)
)
for key in ["content_type_id", "object_id", "configuration"]:
cache_key.append("{}={}".format(key, kwargs.get(key)))
cache_key.append("main_tags={}".format(kwargs.get("main_tags", OrderedDict())))
cache_key = ",".join(cache_key)
return cache_key
class AbstractMetric(TimeStampedEditableModel):
name = models.CharField(max_length=64)
key = models.SlugField(
max_length=64, blank=True, help_text=_("leave blank to determine automatically")
)
field_name = models.CharField(
max_length=16,
default="value",
blank=True,
help_text=_("leave blank to determine automatically"),
)
configuration = models.CharField(
max_length=16,
null=True,
choices=(
METRIC_CONFIGURATION_CHOICES
if django.VERSION < (5, 0)
# TODO: Remove when dropping support for Django 4.2
# In Django 5.0+, choices are normalized at model definition,
# creating a static list of tuples that doesn't update when metrics
# are dynamically registered or unregistered. Using a callable
# ensures we always get the current choices from the registry.
else get_metric_configuration_choices
),
)
content_type = models.ForeignKey(
ContentType, on_delete=models.CASCADE, null=True, blank=True
)
object_id = models.CharField(max_length=36, db_index=True, blank=True, null=True)
content_object = GenericForeignKey("content_type", "object_id")
main_tags = models.JSONField(
_("main tags"),
default=dict,
blank=True,
db_index=True,
)
extra_tags = models.JSONField(
_("extra tags"),
default=dict,
blank=True,
)
# NULL means the health has yet to be assessed
is_healthy = models.BooleanField(default=None, null=True, blank=True, db_index=True)
# Like "is_healthy", but respects tolerance of alert settings
is_healthy_tolerant = models.BooleanField(default=None, null=True, blank=True)
class Meta:
abstract = True
unique_together = (
"key",
"field_name",
"content_type",
"object_id",
"main_tags",
)
def __str__(self):
obj = self.content_object
if not obj:
return self.name
model_name = obj.__class__.__name__
return "{0} ({1}: {2})".format(self.name, model_name, obj)
def __setattr__(self, attrname, value):
if attrname in ["main_tags", "extra_tags"]:
value = self._sort_dict(value)
return super().__setattr__(attrname, value)
def clean(self):
if self.key:
return
elif self.config_dict["key"] != "{key}":
self.key = self.config_dict["key"]
else:
self.key = self.codename
def validate_alert_fields(self):
# When field_name is not provided while creating a metric
# then use config_dict['field_name] as metric field_name
if self.config_dict["field_name"] != "{field_name}":
if self.field_name in ["", "value"]:
self.field_name = self.config_dict["field_name"]
return
# field_name must be one of the metric fields
alert_fields = [self.config_dict["field_name"]] + self.related_fields
if self.field_name not in alert_fields:
raise ValidationError(
f'"{self.field_name}" must be one of the following metric fields ie. {alert_fields}'
)
def full_clean(self, *args, **kwargs):
# The name of the metric will be the same as the
# configuration chosen by the user only when the
# name field is empty (useful for AlertSettingsInline)
if not self.name:
self.name = self.get_configuration_display()
# clean up key before field validation
self.key = self._makekey(self.key)
# validate metric field_name for alerts
self.validate_alert_fields()
return super().full_clean(*args, **kwargs)
@classmethod
def post_delete_receiver(cls, instance, *args, **kwargs):
delete_timeseries.delay(instance.key, instance.tags)
@classmethod
def _get_or_create(
cls,
**kwargs,
):
"""Gets or creates a metric.
Like ``get_or_create`` method of django model managers but with
validation before creation.
"""
if "key" in kwargs:
kwargs["key"] = cls._makekey(kwargs["key"])
try:
lookup_kwargs = deepcopy(kwargs)
if lookup_kwargs.get("name"):
del lookup_kwargs["name"]
extra_tags = lookup_kwargs.pop("extra_tags", {})
metric = cls._get_metric(**lookup_kwargs)
created = False
if extra_tags != metric.extra_tags:
metric.extra_tags.update(kwargs["extra_tags"])
metric.extra_tags = cls._sort_dict(metric.extra_tags)
metric.save()
except cls.DoesNotExist:
try:
metric = cls(**kwargs)
metric.full_clean()
metric.save()
created = True
except IntegrityError:
# Potential race conditions may arise when multiple
# celery workers concurrently write data to InfluxDB.
# These simultaneous writes can result in the database
# processing transactions from another "metric.save()"
# call, potentially leading to IntegrityError exceptions.
return cls._get_or_create(**kwargs)
return metric, created
@classmethod
@cache_memoize(CACHE_TIMEOUT, key_generator_callable=get_metric_cache_key)
def _get_metric(cls, *args, **kwargs):
return cls.objects.get(**kwargs)
@classmethod
def invalidate_cache(cls, instance, *args, **kwargs):
if kwargs.get("created", False):
return
cls._get_metric.invalidate(
**{
"name": instance.name,
"key": instance.key,
"content_type_id": instance.content_type_id,
"object_id": instance.object_id,
"configuration": instance.configuration,
"main_tags": instance.main_tags,
}
)
@property
def codename(self):
"""Identifier stored in timeseries db."""
return self._makekey(self.name)
@property
def config_dict(self):
try:
return get_metric_configuration()[self.configuration]
except KeyError:
raise InvalidMetricConfigException(
f'Invalid metric configuration: "{self.configuration}"'
)
@property
def related_fields(self):
return self.config_dict.get("related_fields", [])
# TODO: This method needs to be refactored when adding the other db
@staticmethod
def _makekey(value):
"""Produces a valid InfluxDB key from ``value``.
Takes ``value`` as input argument and returs a valid InfluxDB key.
"""
return clean_timeseries_data_key(value)
@property
def tags(self):
tags = {}
if self.content_type_id and self.object_id:
tags.update(
{
"content_type": self.content_type_key,
"object_id": str(self.object_id),
}
)
if self.main_tags:
tags.update(self.main_tags)
if self.extra_tags:
tags.update(self.extra_tags)
return tags
@staticmethod
def _sort_dict(dict_):
"""Ensures the order of the keys in the dict is predictable."""
if not isinstance(dict_, OrderedDict):
return OrderedDict(sorted(dict_.items()))
return dict_
@property
def content_type_key(self):
try:
content_type = ContentType.objects.get_for_id(self.content_type_id)
return ".".join(content_type.natural_key())
except AttributeError:
return None
@property
def alert_field(self):
if self.field_name != self.config_dict["field_name"]:
return self.field_name
return self.config_dict.get("alert_field", self.field_name)
@property
def alert_on_related_field(self):
return self.alert_field in self.related_fields
def _get_time(self, time):
"""If time is a string, it converts it to a datetime."""
if isinstance(time, str):
return parse_date(time)
return time
def _set_is_healthy(self, alert_settings, value):
"""Sets the value of "is_healthy" field when necessary.
Executes only if "value" crosses the threshold defined in
"alert_settings". Returns "True" if "is_healthy" field is changed.
Otherwise, returns "None".
This method does not take into account the alert settings
tolerance, which is done by "_set_is_healthy_tolerant" method.
"""
crossed = alert_settings._value_crossed(value)
if (not crossed and self.is_healthy) or (crossed and self.is_healthy is False):
return
# problem: not within threshold limit
elif crossed and self.is_healthy in [True, None]:
self.is_healthy = False
# ok: returned within threshold limit
elif not crossed and self.is_healthy in [False, None]:
self.is_healthy = True
return True
def _set_is_healthy_tolerant(
self, alert_settings, value, time, retention_policy, send_alert
):
"""Sets the value of "is_tolerance_healthy" if necessary.
Executes only if "value" crosses the threshold for more than the
amount of seconds defined in the alert_settings "tolerance" field.
It also sends the notification if required. Returns "None" if
value of "is_healthy_tolerant" is unchanged. Returns "True" if it
is the first metric write within threshold. Returns "False" in
other cases.
This method is similar to "_set_is_healthy" but it takes into
account the alert settings tolerance so it's slightly different
and more complex.
"""
time = self._get_time(time)
crossed = alert_settings._is_crossed_by(value, time, retention_policy)
first_time = False
# situation has not changed
if (not crossed and self.is_healthy_tolerant) or (
crossed and self.is_healthy_tolerant is False
):
return
# problem: not within threshold limit
elif crossed and self.is_healthy_tolerant in [True, None]:
if self.is_healthy_tolerant is None:
first_time = True
self.is_healthy_tolerant = False
notification_type = f"{self.configuration}_problem"
# ok: returned within threshold limit
elif not crossed and self.is_healthy_tolerant is False:
self.is_healthy_tolerant = True
notification_type = f"{self.configuration}_recovery"
# First metric write within threshold
elif not crossed and self.is_healthy_tolerant is None:
self.is_healthy_tolerant = True
first_time = True
# If we got to this point, it means we have to send an alert,
# because the metric has been crossed for more than the
# tolerated amount of time. There's one exception though:
# if the device is new, its status will be unknown and the metric
# will become healthy for the first time, in this case we do not need
# to send an alert.
if (
not (first_time and self.is_healthy_tolerant)
and alert_settings.is_active
and send_alert
):
self._notify_users(notification_type, alert_settings)
return first_time
def _is_historical_data(self, time):
"""
Data older than 5 minutes is considered historical data.
"""
recent_time = timezone.now() - timedelta(minutes=5)
return time < recent_time
def check_threshold(self, value, time=None, retention_policy=None, send_alert=True):
"""Checks if the threshold is crossed and notifies users accordingly"""
try:
alert_settings = self.alertsettings
except ObjectDoesNotExist:
return
is_healthy_changed = self._set_is_healthy(alert_settings, value)
tolerance_healthy_changed_first_time = self._set_is_healthy_tolerant(
alert_settings, value, time, retention_policy, send_alert
)
is_healthy_tolerant_changed = tolerance_healthy_changed_first_time is not None
# Do nothing if none of the fields changed.
if not is_healthy_changed and not is_healthy_tolerant_changed:
return
update_fields = []
if is_healthy_changed:
update_fields.append("is_healthy")
if is_healthy_tolerant_changed:
update_fields.append("is_healthy_tolerant")
self.save(update_fields=update_fields)
threshold_crossed.send(
sender=self.__class__,
alert_settings=alert_settings,
metric=self,
target=self.content_object,
first_time=tolerance_healthy_changed_first_time,
tolerance_crossed=is_healthy_tolerant_changed,
)
def write(
self,
value,
current=False,
time=None,
database=None,
check=True,
extra_values=None,
retention_policy=None,
send_alert=True,
write=True,
):
"""write timeseries data"""
values = {self.field_name: value}
if extra_values and isinstance(extra_values, dict):
for key in extra_values.keys():
if not self.related_fields or key not in self.related_fields:
raise ValueError(f'"{key}" not defined in metric configuration')
values.update(extra_values)
signal_kwargs = dict(
sender=self.__class__,
metric=self,
values=values,
time=time,
current=current,
)
pre_metric_write.send(**signal_kwargs)
timestamp = time or timezone.now()
if isinstance(timestamp, str):
timestamp = parse_date(timestamp)
options = dict(
tags=self.tags,
timestamp=timestamp.isoformat(),
database=database,
retention_policy=retention_policy,
current=current,
metric=self,
)
# check can be disabled,
# mostly for automated testing and debugging purposes
if check and not self._is_historical_data(timestamp):
options["check_threshold_kwargs"] = {
"value": value,
"time": time,
"retention_policy": retention_policy,
"send_alert": send_alert,
}
# if alert_on_related_field then check threshold
# on the related_field instead of field_name
if self.alert_on_related_field:
if not extra_values:
raise ValueError(
'write() missing keyword argument: "extra_values" required for alert on related field'
)
if self.alert_field not in extra_values.keys():
raise ValueError(
f'"{key}" is not defined for alert_field in metric configuration'
)
options["check_threshold_kwargs"].update(
{"value": extra_values[self.alert_field]}
)
if write:
_timeseries_write(name=self.key, values=values, **options)
return {"name": self.key, "values": values, **options}
@classmethod
def batch_write(cls, raw_data):
error_dict = {}
write_data = []
for metric, kwargs in raw_data:
try:
write_data.append(metric.write(**kwargs, write=False))
except ValueError as error:
error_dict[metric.key] = str(error)
_timeseries_batch_write(write_data)
if error_dict:
raise ValueError(error_dict)
def read(self, **kwargs):
"""reads timeseries data"""
options = dict(key=self.key, fields=self.field_name, tags=self.tags)
options.update(kwargs)
return timeseries_db.read(**options)
def _notify_users(self, notification_type, alert_settings):
"""creates notifications for users"""
opts = dict(sender=self, type=notification_type, action_object=alert_settings)
if self.content_object is not None:
opts["target"] = self.content_object
notify.send(**opts)
class AbstractChart(TimeStampedEditableModel):
metric = models.ForeignKey(
get_model_name("monitoring", "Metric"), on_delete=models.CASCADE
)
configuration = models.CharField(
max_length=16,
null=True,
choices=(
CHART_CONFIGURATION_CHOICES
if django.VERSION < (5, 0)
# TODO: Remove when dropping support for Django 4.2
# In Django 5.0+, choices are normalized at model definition,
# creating a static list of tuples that doesn't update when charts
# are dynamically registered or unregistered. Using a callable
# ensures we always get the current choices from the registry.
else get_chart_configuration_choices
),
)
GROUP_MAP = {"1d": "10m", "3d": "20m", "7d": "1h", "30d": "24h", "365d": "7d"}
DEFAULT_TIME = DEFAULT_CHART_TIME
class Meta:
abstract = True
def __str__(self):
return str(self.label) or self.metric.name
def clean(self):
self._clean_query()
def _clean_query(self):
try:
timeseries_db.validate_query(self.query)
timeseries_db.query(self.get_query())
except timeseries_db.client_error as e:
raise ValidationError({"configuration": e}) from e
except InvalidChartConfigException as e:
raise ValidationError({"configuration": str(e)}) from e
@property
def config_dict(self):
try:
return get_chart_configuration()[self.configuration]
except KeyError as e:
raise InvalidChartConfigException(
f'Invalid chart configuration: "{self.configuration}"'
) from e
@property
def type(self):
return self.config_dict["type"]
@property
def fill(self):
return self.config_dict.get("fill")
@property
def xaxis(self):
return self.config_dict.get("xaxis", {})
@property
def yaxis(self):
return self.config_dict.get("yaxis", {})
@property
def label(self):
return self.config_dict.get("label") or self.title
@property
def trace_type(self):
return self.config_dict.get("trace_type", {})
@property
def trace_order(self):
return self.config_dict.get("trace_order", [])
@property
def trace_labels(self):
return self.config_dict.get("trace_labels", {})
@property
def calculate_total(self):
return self.config_dict.get("calculate_total", False)
@property
def connect_points(self):
return self.config_dict.get("connect_points", False)
@property
def description(self):
return self.config_dict["description"].format(
metric=self.metric, **self.metric.tags
)
@property
def title(self):
return self.config_dict["title"]
@property
def summary_labels(self):
return self.config_dict.get("summary_labels")
@property
def order(self):
return self.config_dict["order"]
@property
def colors(self):
colors = self.config_dict.get("colors")
if not colors and self.summary_labels:
summary_length = len(self.summary_labels)
return DEFAULT_COLORS[0:summary_length]
return colors
@property
def colorscale(self):
return self.config_dict.get("colorscale")
@property
def unit(self):
return self.config_dict.get("unit")
@property
def query(self):
query = self.config_dict["query"]
if query:
return query[timeseries_db.backend_name]
return self._default_query
@property
def summary_query(self):
query = self.config_dict.get("summary_query", None)
if query:
return query[timeseries_db.backend_name]
@property
def top_fields(self):
return self.config_dict.get("top_fields", None)
@property
def _default_query(self):
q = default_chart_query[0]
if self.metric.object_id:
q += default_chart_query[1]
return q
@classmethod
def _get_group_map(cls, time=None):
"""Returns group map.
Returns the chart group map for the specified days, otherwise the
default Chart.GROUP_MAP is returned.
"""
if (
not time
or time in cls.GROUP_MAP.keys()
or not isinstance(time, str)
or time[-1] != "d"
):
return cls.GROUP_MAP
group = "10m"
days = int(time.split("d")[0])
# Use copy of class variable to avoid unpredictable results
custom_group_map = cls.GROUP_MAP.copy()
# custom grouping between 1 to 2 days
if days > 0 and days < 3:
group = "10m"
# custom grouping between 3 to 6 days (base 5)
elif days >= 3 and days < 7:
group = str(5 * round(((days / 3) * 20) / 5)) + "m"
# custom grouping between 8 to 27 days
elif days > 7 and days < 28:
group = str(round(days / 7)) + "h"
# custom grouping between 1 month to 7 month
elif days >= 30 and days <= 210:
group = "1d"
# custom grouping between 7 month to 1 year
elif days > 210 and days <= 365:
group = "7d"
custom_group_map.update({time: group})
return custom_group_map
def get_query(
self,
time=DEFAULT_TIME,
summary=False,
fields=None,
query=None,
timezone=settings.TIME_ZONE,
start_date=None,
end_date=None,
additional_params=None,
):
query = query or self.query
if summary and self.summary_query:
query = self.summary_query
additional_params = additional_params or {}
params = self._get_query_params(time, start_date, end_date)
params.update(additional_params)
params.update({"start_date": start_date, "end_date": end_date})
if not params.get("organization_id") and self.config_dict.get("__all__", False):
params["organization_id"] = ["__all__"]
return timeseries_db.get_query(
self.type,
params,
time,
self._get_group_map(time),
summary,
fields,
query,
timezone,
)
def get_top_fields(self, number):
"""Returns the top fields.
Returns list of top ``number`` of fields (highest sum) of a
measurement in the specified time range (descending order).
"""
q = self._default_query.replace("{field_name}", "{fields}")
params = self._get_query_params(self.DEFAULT_TIME)
return timeseries_db._get_top_fields(
query=q,
chart_type=self.type,
group_map=self._get_group_map(params["days"]),
number=number,
params=params,
time=self.DEFAULT_TIME,
)
def _get_query_params(self, time, start_date=None, end_date=None):
m = self.metric
params = dict(
field_name=m.field_name,
key=m.key,
time=self._get_time(time, start_date, end_date),
days=time,
)
if m.object_id:
params.update(
{
"content_type": m.content_type_key,
"object_id": m.object_id,
**m.tags,
}
)
for key, value in self.config_dict.get("query_default_param", {}).items():
params.setdefault(key, value)
return params
@classmethod
def _get_time(cls, time, start_date=None, end_date=None):
if start_date and end_date:
return start_date
if not isinstance(time, str):
return str(time)
if time in cls._get_group_map(time).keys():
days = int(time.strip("d"))
now = timezone.now()
if days > 3:
now = date(now.year, now.month, now.day)
if days == 7:
# subtract one day because we want to include
# the current day in the time range
days -= 1
time = str(now - timedelta(days=days))[0:19]
return time
def read(
self,
decimal_places=2,
time=DEFAULT_TIME,
x_axys=True,
timezone=settings.TIME_ZONE,
start_date=None,
end_date=None,
additional_query_kwargs=None,
):
additional_query_kwargs = additional_query_kwargs or {}
traces = {}
if x_axys:
x = []
try:
query_kwargs = dict(
time=time, timezone=timezone, start_date=start_date, end_date=end_date
)
query_kwargs.update(additional_query_kwargs)
if self.top_fields:
fields = self.get_top_fields(self.top_fields)
data_query = self.get_query(fields=fields, **query_kwargs)
summary_query = self.get_query(
fields=fields, summary=True, **query_kwargs
)
else:
data_query = self.get_query(**query_kwargs)
summary_query = self.get_query(summary=True, **query_kwargs)
points = timeseries_db.get_list_query(data_query)
summary = timeseries_db.get_list_query(summary_query)
except timeseries_db.client_error as e:
logging.error(e, exc_info=True)
raise e
for point in points:
for key, value in point.items():
if key == "time":
continue
traces.setdefault(key, [])
if decimal_places and isinstance(value, (int, float)):
value = self._round(value, decimal_places)
traces[key].append(value)
time = datetime.fromtimestamp(point["time"], tz=tz(timezone)).strftime(
"%Y-%m-%d %H:%M"
)
if x_axys:
x.append(time)
# prepare result to be returned
# (transform chart data so its order is not random)
result = {"traces": sorted(traces.items())}
if x_axys:
result["x"] = x
# add summary
if len(summary) > 0:
result["summary"] = {}
for key, value in summary[0].items():
if key == "time":
continue
if not timeseries_db.validate_query(self.query):
value = None
elif value:
value = self._round(value, decimal_places)
result["summary"][key] = value
return result
def json(self, time=DEFAULT_TIME, **kwargs):
try:
# unit needs to be passed for chart_inline
data = self.read(time=time)
data.update(
{
"unit": self.unit,
"trace_type": self.trace_type,
"trace_order": self.trace_order,
"calculate_total": self.calculate_total,
"connect_points": self.connect_points,
"colors": self.colors,
}
)
return json.dumps(data, **kwargs, default=str)
except KeyError as e:
logger.warning(f"Got KeyError in Chart.json method: {e}")
@staticmethod
def _round(value, decimal_places):
"""Rounds value when necessary."""
control = 1.0 / 10**decimal_places
if value < control:
decimal_places += 2
return round(value, decimal_places)
class AbstractAlertSettings(TimeStampedEditableModel):
_MINUTES_MAX = 60 * 24 * 7 # 7 days
_MINUTES_HELP = (
"for how many minutes should the threshold value be crossed before "
"an alert is sent? A value of zero means the alert is sent immediately"
)
_ALERTSETTINGS_OPERATORS = (("<", _("less than")), (">", _("greater than")))
is_active = models.BooleanField(
_("Alerts enabled"),
default=True,
help_text=_(
"whether alerts are enabled for this metric, uncheck to "
"disable this alert for this object and all users"
),
)
metric = models.OneToOneField(
get_model_name("monitoring", "Metric"), on_delete=models.CASCADE
)
custom_operator = models.CharField(
_("operator"),
max_length=1,
choices=_ALERTSETTINGS_OPERATORS,
null=True,
blank=True,
)
custom_threshold = models.FloatField(
_("threshold value"), help_text=_("threshold value"), blank=True, null=True
)
custom_tolerance = models.PositiveIntegerField(
_("threshold tolerance"),
validators=[MaxValueValidator(_MINUTES_MAX)],
help_text=_(_MINUTES_HELP),
blank=True,
null=True,
)
class Meta:
abstract = True
verbose_name = _("Alert settings")
verbose_name_plural = verbose_name
permissions = (
("add_alertsettings_inline", "Can add Alert settings inline"),
("change_alertsettings_inline", "Can change Alert settings inline"),
("delete_alertsettings_inline", "Can delete Alert settings inline"),
("view_alertsettings_inline", "Can view Alert settings inline"),
)
@classmethod
def invalidate_cache(cls, instance, *args, **kwargs):
Metric = instance.metric._meta.model
Metric.invalidate_cache(instance.metric)
def full_clean(self, *args, **kwargs):
if self.custom_threshold == self.config_dict["threshold"]:
self.custom_threshold = None
if self.custom_tolerance == self.config_dict["tolerance"]:
self.custom_tolerance = None
if self.custom_operator == self.config_dict["operator"]:
self.custom_operator = None
return super().full_clean(*args, **kwargs)
@property
def config_dict(self):
return self.metric.config_dict.get(
"alert_settings", {"operator": "<", "threshold": 1, "tolerance": 0}
)
@property
def threshold(self):
if self.custom_threshold is None:
return self.config_dict["threshold"]
return self.custom_threshold
@property
def tolerance(self):
if self.custom_tolerance is None:
return self.config_dict["tolerance"]
return self.custom_tolerance
@property
def operator(self):
if self.custom_operator is None:
return self.config_dict["operator"]
return self.custom_operator
def _value_crossed(self, current_value):
threshold_value = self.threshold
method = "__gt__" if self.operator == ">" else "__lt__"
if isinstance(current_value, int):
current_value = float(current_value)
return getattr(current_value, method)(threshold_value)
def _time_crossed(self, time):
threshold_time = timezone.now() - timedelta(minutes=self.tolerance)
return time < threshold_time
@property
def _tolerance_search_range(self):
tolerance_seconds = self.tolerance * 60
if (
app_settings.TOLERANCE_INTERVAL
<= tolerance_seconds
<= app_settings.TOLERANCE_INTERVAL * 2
):
return tolerance_seconds + int(tolerance_seconds * 0.25)
return tolerance_seconds
def _is_crossed_by(self, current_value, time=None, retention_policy=None):
"""Answers the following question:
do current_value and time cross the threshold and trespass the
tolerance?
"""
value_crossed = self._value_crossed(current_value)
if value_crossed is NotImplemented:
raise ValueError("Supplied value type not supported")
tolerance = self.tolerance * 60
# no tolerance specified, return immediately
if tolerance == 0 or tolerance < app_settings.TOLERANCE_INTERVAL:
return value_crossed
now = time or timezone.now()
if value_crossed:
operator = self.operator
flapping_operator = ">=" if operator == "<" else "<="
else:
# If the value is not crossed, we need to check the opposite operator
# to ensure the recovery also respects the tolerance.
if self.operator == ">":
operator = "<="
flapping_operator = ">"
else:
operator = ">="
flapping_operator = "<"
# There should be atleast two points that have trespassed the threshold
trespassed_points = self.metric.read(
limit=2,
retention_policy=retention_policy,