Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Inital automated status check work
- Loading branch information
1 parent
51bc87b
commit ed4021a
Showing
14 changed files
with
406 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from django.core.management import call_command, get_commands | ||
from django.core.management.base import BaseCommand | ||
|
||
from ...decorators import handle_lock | ||
|
||
class Command(BaseCommand): | ||
help = 'Runs data sanity checks to generate any alerts for potential data issues.' | ||
|
||
@handle_lock | ||
def handle(self, *args, **options): | ||
command_names = get_commands().keys() | ||
|
||
for command_name in command_names: | ||
if command_name.startswith('pdk_status_check_'): | ||
call_command(command_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# pylint: disable=line-too-long, no-member | ||
|
||
from django.core.management.base import BaseCommand | ||
from django.utils import timezone | ||
|
||
from ...decorators import handle_lock | ||
|
||
from ...models import DataPoint, DataSource, DataSourceAlert | ||
|
||
GENERATOR = 'pdk-device-battery' | ||
CRITICAL_LEVEL = 20 | ||
WARNING_LEVEL = 33 | ||
|
||
class Command(BaseCommand): | ||
help = 'Runs the battery level status check to alert when battery level is low.' | ||
|
||
@handle_lock | ||
def handle(self, *args, **options): # pylint: disable=too-many-branches, too-many-statements | ||
for source in DataSource.objects.all(): | ||
last_battery = DataPoint.objects.filter(source=source.identifier, generator_identifier=GENERATOR).order_by('-created').first() | ||
last_alert = DataSourceAlert.objects.filter(data_source=source, generator_identifier=GENERATOR, active=True).order_by('-created').first() | ||
|
||
alert_name = None | ||
alert_details = {} | ||
alert_level = 'info' | ||
|
||
if last_battery is not None: | ||
properties = last_battery.fetch_properties() | ||
|
||
if properties['level'] < CRITICAL_LEVEL: | ||
alert_name = 'Battery Level Critically Low' | ||
alert_details['message'] = 'Latest battery level is ' + str(properties['level']) + '%.' | ||
alert_level = 'critical' | ||
|
||
elif properties['level'] < WARNING_LEVEL: | ||
alert_name = 'Battery Level Low' | ||
alert_details['message'] = 'Latest battery level is ' + str(properties['level']) + '%.' | ||
alert_level = 'warning' | ||
|
||
if alert_name is not None: | ||
if last_alert is None or last_alert.alert_name != alert_name or last_alert.alert_level != alert_level: | ||
if last_alert is not None: | ||
last_alert.active = False | ||
last_alert.updated = timezone.now() | ||
last_alert.save() | ||
|
||
new_alert = DataSourceAlert(alert_name=alert_name, data_source=source, generator_identifier=GENERATOR) | ||
new_alert.alert_level = alert_level | ||
new_alert.update_alert_details(alert_details) | ||
new_alert.created = timezone.now() | ||
new_alert.updated = timezone.now() | ||
new_alert.active = True | ||
|
||
new_alert.save() | ||
else: | ||
last_alert.updated = timezone.now() | ||
last_alert.update_alert_details(alert_details) | ||
|
||
last_alert.save() | ||
elif last_alert is not None: | ||
last_alert.updated = timezone.now() | ||
last_alert.active = False | ||
|
||
last_alert.save() | ||
else: | ||
alert_name = 'No Battery Levels Logged' | ||
alert_details['message'] = 'No battery levels have been logged for this device yet.' | ||
|
||
if last_alert is None or last_alert.alert_name != alert_name or last_alert.alert_level != alert_level: | ||
if last_alert is not None: | ||
last_alert.active = False | ||
last_alert.updated = timezone.now() | ||
last_alert.save() | ||
|
||
new_alert = DataSourceAlert(alert_name=alert_name, data_source=source, generator_identifier=GENERATOR) | ||
new_alert.alert_level = alert_level | ||
new_alert.update_alert_details(alert_details) | ||
new_alert.created = timezone.now() | ||
new_alert.updated = timezone.now() | ||
new_alert.active = True | ||
|
||
new_alert.save() | ||
else: | ||
last_alert.updated = timezone.now() | ||
last_alert.update_alert_details(alert_details) | ||
|
||
last_alert.save() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.11.1 on 2017-05-30 14:21 | ||
# pylint: skip-file | ||
|
||
from __future__ import unicode_literals | ||
|
||
import django.contrib.postgres.fields.jsonb | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('passive_data_kit', '0016_datapoint_secondary_identifier'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='DataSourceAlert', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('alert_name', models.CharField(max_length=1024)), | ||
('alert_details', django.contrib.postgres.fields.jsonb.JSONField()), | ||
('generator_identifier', models.CharField(blank=True, max_length=1024, null=True)), | ||
('created', models.DateTimeField()), | ||
('active', models.BooleanField(default=True)), | ||
('data_source', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='alerts', to='passive_data_kit.DataSource')), | ||
], | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.11.1 on 2017-05-30 14:38 | ||
# pylint: skip-file | ||
|
||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('passive_data_kit', '0017_datasourcealert'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='datasourcealert', | ||
name='updated', | ||
field=models.DateTimeField(blank=True, null=True), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.11.1 on 2017-05-30 14:56 | ||
# pylint: skip-file | ||
|
||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('passive_data_kit', '0018_datasourcealert_updated'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='datasourcealert', | ||
name='alert_level', | ||
field=models.CharField(choices=[('info', 'Informative'), ('warning', 'Warning'), ('critical', 'Critical')], default='info', max_length=64), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
<span style="display: none;">{{ date.isoformat }}</span> | ||
<span data-toggle="tooltip" data-placement="top" title="{{ date }}">{{ ago }}</span> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{% load passive_data_kit %} | ||
<table class="alerts_table table-striped" data-toggle="table" data-pagination="true" data-search="true" style="z-index: 10;"> | ||
<thead> | ||
<tr> | ||
<th data-sortable="true"><span class="glyphicon glyphicon-alert" aria-hidden="true"></span></th> | ||
<th data-sortable="true">Generator</th> | ||
<th data-sortable="true" data-sort-order="desc">Created</th> | ||
<th data-sortable="true" data-sort-order="desc">Updated</th> | ||
<th>Message</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{% for alert in alerts %} | ||
{% if alert.alert_level == 'warning' %} | ||
<tr class="warning"> | ||
<td> | ||
<span style="display: none;">3</span> | ||
<span class="glyphicon glyphicon-alert" aria-hidden="true"> | ||
</td> | ||
{% elif alert.alert_level == 'critical' %} | ||
<tr class="danger"> | ||
<td> | ||
<span style="display: none;">2</span> | ||
<span class="glyphicon glyphicon-alert" aria-hidden="true"> | ||
</td> | ||
{% else %} | ||
<tr> | ||
<td> | ||
<span style="display: none;">1</span> | ||
<span class="glyphicon glyphicon-info-sign" aria-hidden="true"> | ||
</td> | ||
{% endif %} | ||
<td> | ||
<span style="display: none;">{{ alert.data_source.name }}</span> | ||
<a href="{% url 'pdk_source' alert.data_source.identifier %}">{{ alert.data_source.name }}</a> | ||
</td> | ||
<td> | ||
{% date_ago alert.created %} | ||
</td> | ||
<td> | ||
{% date_ago alert.updated %} | ||
</td> | ||
<td>{{ alert.fetch_alert_details.message }}</td> | ||
</tr> | ||
{% endfor %} | ||
</tbody> | ||
</table> |
Oops, something went wrong.