Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP]Tick aggregate functions #386

Open
wants to merge 39 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
035f86f
added functionality to edit status card
holayeah Dec 19, 2016
25ccbca
fixing issues
holayeah Dec 19, 2016
ec13599
fixing issues
holayeah Dec 19, 2016
20b646c
enhanced the ui
holayeah Dec 19, 2016
795763f
switched to useing modal
holayeah Dec 19, 2016
ef7c084
aligned the data analysis graph update button
holayeah Dec 19, 2016
2618320
removed test that import csv from victron api
holayeah Dec 19, 2016
05a3b31
added permission to edit status card
holayeah Dec 20, 2016
605b733
added Tick script models
holayeah Dec 21, 2016
5c0ddf2
added tick script to the admin
holayeah Dec 21, 2016
394fe5c
Merge branch 'master' into tick_aggregate_functions
holayeah Dec 21, 2016
f07218e
quick rename for field type to script type
holayeah Dec 22, 2016
bd67867
Merge branch 'tick_aggregate_functions' of https://github.com/GreatLa…
holayeah Dec 22, 2016
5a26566
added a report tick template
holayeah Jan 3, 2017
d59ca87
enhanced the report aggregate template
holayeah Jan 3, 2017
d42f069
added a function to add report tasks to kapacitor given a report inst…
holayeah Jan 5, 2017
2659f76
fixing issues
holayeah Jan 5, 2017
4b0f160
restsarting tests
holayeah Jan 9, 2017
b5e9100
some debugging messages
holayeah Jan 9, 2017
0a0376e
fixing
holayeah Jan 9, 2017
38aa66c
testing edit reports
holayeah Jan 9, 2017
afe79e4
DEBUGGING: changed travis to make tests fast for this bug
holayeah Jan 10, 2017
c8bd0a9
debugging
holayeah Jan 10, 2017
34a8f3f
fixing
holayeah Jan 10, 2017
b6cdc7a
kapacitor tasks created on add report
holayeah Jan 10, 2017
7276efa
fixing
holayeah Jan 10, 2017
4bda7f5
Merge branch 'master' into tick_aggregate_functions
holayeah Jan 12, 2017
a174743
functionality to delete the tasks
holayeah Jan 23, 2017
dc3444d
merging master
holayeah Jan 26, 2017
a018240
fixed: when adding tasks corresponding kapacitor jobs are added
holayeah Jan 26, 2017
e1c51ed
removed admin backup files
holayeah Jan 26, 2017
15724e2
added pattern to list tasks, and changed reports jobs to not match th…
holayeah Jan 26, 2017
6711bea
added functionality to delete tasks when deleting report Job
holayeah Jan 26, 2017
1f9fa96
Merge branch 'tick_aggregate_functions' of https://github.com/GreatLa…
holayeah Jan 26, 2017
8a8d0a2
fixing
holayeah Jan 27, 2017
951da76
added tests for deleting reports jobs
holayeah Jan 27, 2017
1023350
added functions to enable and disable tasks
holayeah Jan 27, 2017
8250df9
added test for set status kapacitor attribute
holayeah Jan 27, 2017
475c388
added functionality to edit report task
holayeah Jan 27, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ install:
before_script:
- python manage.py migrate --noinput
script:
- python -W ignore manage.py test -v 2
- python -W ignore manage.py test -v 2
5 changes: 2 additions & 3 deletions sesh/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,5 @@


# Sesh Settings
SESH_REPORT_TABLES = [
'Daily_Data_Point',
]
# The report tables are used to allow report generation for sesh models
SESH_REPORT_TABLES = []
4 changes: 4 additions & 0 deletions seshdash/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ class SensorMapping(GuardedModelAdmin):
class ReportDisplay(GuardedModelAdmin):
pass

@admin.register(Tick_Script)
class TickScript(GuardedModelAdmin):
pass

@admin.register(Report_Sent)
class ReportSentDisplay(GuardedModelAdmin):
list_display = ('report_job', 'date', 'title', 'status')
Expand Down
12 changes: 12 additions & 0 deletions seshdash/data/db/influx.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,18 @@ def get_measurement_range_bucket(self, measurement_name, start, end, group_by, s
results = list(self._influx_client.query(query, database=db).get_points())
return results

def drop_measurement(self, measurement_name, database=None):
"""
Deletes a measurement from the influx db
"""
db = self.db
if database:
db = database

query = "DROP MEASUREMENT %s" % (measurement_name)

results = list(self._influx_client.query(query, database=db).get_points())
return results


# Helper classes to the interface
Expand Down
60 changes: 57 additions & 3 deletions seshdash/data/db/kapacitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,18 @@ def get_task(self, task_id ):
return re[1]


def list_tasks(self):
def list_tasks(self, pattern=''):
"""
List the current tasks in kapacitor
@param pattern - This specifies a pattern to query kapacitor with, Note: it uses shell global matching syntax
"""
data_dict = {}
data_dict = {
'pattern': pattern,
}

re = self._make_request('tasks',
type_of_request = 'GET',
data = data_dict,
)

logger.debug("got response %s %s" % re)
Expand Down Expand Up @@ -231,9 +235,34 @@ def update_task(self, task_id ):
return re[1]


def set_task_status(self, task_id, status):
"""
Sets the status for a given task

@param task_id - The task to set the status to
@param status - value to set, 'enabled' or 'disabled'
"""
path_params = {
'task_id': task_id,
}

data = {
'status': status,
}

re = self._make_request('get_tasks',
type_of_request = 'PATCH',
path_params = path_params,
data = data
)

logger.debug("got response %s %s"%re)
return re[1]



def delete_task(self, task_id ):

def delete_task(self, task_id):
"""
Delete a created task

Expand All @@ -254,6 +283,31 @@ def delete_task(self, task_id ):
return re[1]


def delete_tasks(self, pattern):
"""
Deletes tasks matching a specific pattern,

@param pattern - specifies a shell glob mapping pattern
"""
tasks = self.list_tasks(pattern=pattern)

for task in tasks["tasks"]:
self.delete_task(task['id'])

return True

def delete_all_tasks(self):
"""
Delete all the tasks in kapacitor
"""
tasks = self.list_tasks()

for task in tasks["tasks"]:
self.delete_task(task['id'])

return True


def create_recording(self, task_id, stop, method='stream', id=None):
"""
Creates a kapacitor recording
Expand Down
29 changes: 29 additions & 0 deletions seshdash/migrations/0004_tick_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.1 on 2016-12-21 15:25
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('seshdash', '0003_auto_20161212_1209'),
]

operations = [
migrations.CreateModel(
name='Tick_Script',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('input_field_name', models.CharField(max_length=40)),
('output_field_name', models.CharField(max_length=40)),
('script', models.TextField()),
('function', models.CharField(max_length=20)),
('interval', models.CharField(max_length=10)),
('type', models.CharField(choices=[(b'stream', b'Stream'), (b'batch', b'Batch')], max_length=10)),
('site', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='seshdash.Sesh_Site')),
],
),
]
20 changes: 20 additions & 0 deletions seshdash/migrations/0005_auto_20161222_1026.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.1 on 2016-12-22 08:26
from __future__ import unicode_literals

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('seshdash', '0004_tick_script'),
]

operations = [
migrations.RenameField(
model_name='tick_script',
old_name='type',
new_name='script_type',
),
]
16 changes: 16 additions & 0 deletions seshdash/migrations/0009_merge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.1 on 2017-01-26 09:19
from __future__ import unicode_literals

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('seshdash', '0005_auto_20161222_1026'),
('seshdash', '0008_auto_20170125_1352'),
]

operations = [
]
19 changes: 19 additions & 0 deletions seshdash/migrations/0010_auto_20170126_1401.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.1 on 2017-01-26 12:01
from __future__ import unicode_literals

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('seshdash', '0009_merge'),
]

operations = [
migrations.AlterUniqueTogether(
name='report_job',
unique_together=set([('site', 'duration')]),
),
]
62 changes: 62 additions & 0 deletions seshdash/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

from django_mysql.models import JSONField

from seshdash.data.db.kapacitor import Kapacitor

# Create your models here.
class VRM_Account(models.Model):
"""
Expand Down Expand Up @@ -695,6 +697,45 @@ def get_duration_choices(self):

return duration_list

def get_kapacitor_tasks(self):
kap = Kapacitor()
pattern = self.site.site_name + '_' + self.duration + "*"
return kap.list_tasks(pattern=pattern)

def save(self, *args, **kwargs):
"""
On save add kapacitor task referencing the report
"""
self.add_kapacitor_tasks()
super(Report_Job, self).save(*args, **kwargs)

def add_kapacitor_tasks(self):
"""
Adds the kapacitor tasks for the report
"""
from seshdash.utils.reporting import add_report_kap_tasks
add_report_kap_tasks(self)

def delete(self, *args, **kwargs):
"""
On delete delet kapacitor tasks referencing the report Job
"""
self.delete_kapacitor_tasks()
super(Report_Job, self).delete(*args, **kwargs)


def delete_kapacitor_tasks(self):
"""
Deletes the kapacitor tasks attached to the report
"""
kap = Kapacitor()
pattern = self.site.site_name + '_' + self.duration + "*" # tasks begining with site_name_duration
return kap.delete_tasks(pattern)


class Meta:
unique_together = (('site','duration'),)


class Report_Sent(models.Model):
"""
Expand Down Expand Up @@ -744,3 +785,24 @@ class Data_Process_Rule(models.Model):
duration = models.CharField(max_length=40, default="24h", choices=DURATION)
interval = models.CharField(max_length=10, default="5m", choices=TIME_BUCKETS)
output_field = models.ForeignKey(Trend_Data_Point)


class Tick_Script(models.Model):
"""
This is a model to hold data about the
tick scripts
"""
SCRIPT_TYPE_CHOICES = (
('stream', 'Stream'),
('batch', 'Batch'),
)

site = models.ForeignKey(Sesh_Site)
input_field_name = models.CharField(max_length=40) # The influx measurement to operate on
output_field_name = models.CharField(max_length=40) # The influx measurement to output results of the operation
script = models.TextField()
function = models.CharField(max_length=20) # Influx function operation to use
interval = models.CharField(max_length=10) # The interval the task script runs on e.g 1min, 1h, 1d, 1w
script_type = models.CharField(max_length=10, choices=SCRIPT_TYPE_CHOICES)


23 changes: 23 additions & 0 deletions seshdash/tests/test_kapacitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def setUp(self):
#assign a user to the sites
assign_perm("view_Sesh_Site",self.test_user,self.site)


def tearDown(self):
self.i.delete_database(self._influx_db_name)
self.kap.delete_template(self.template_id)
Expand Down Expand Up @@ -251,6 +252,28 @@ def test_task_dj_template(self):
result = self.kap.create_task(alert_id, dbrps= self.dbrps, script=rendered_alert)
self.assertEquals(result['status'], 'enabled')

def test_set_task_status(self):
"""
Testing the function that updates status
of tasks
"""
script = """
stream
|from()
.measurement('cpu')
|alert()
.crit(lambda: "value" < 70)
.log('/tmp/alerts.log')
"""

task = self.kap.create_task('test_task', script, status='enabled')
self.kap.set_task_status('test_task', 'disabled')

# asserting
task = self.kap.get_task('test_task')
self.assertEqual(task['status'], 'disabled')





Expand Down
Loading