-
Notifications
You must be signed in to change notification settings - Fork 2
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
base: master
Are you sure you want to change the base?
Changes from 15 commits
035f86f
25ccbca
ec13599
20b646c
795763f
ef7c084
2618320
05a3b31
605b733
5c0ddf2
394fe5c
f07218e
bd67867
5a26566
d59ca87
d42f069
2659f76
4b0f160
b5e9100
0a0376e
38aa66c
afe79e4
c8bd0a9
34a8f3f
b6cdc7a
7276efa
4bda7f5
a174743
dc3444d
a018240
e1c51ed
15724e2
6711bea
1f9fa96
8a8d0a2
951da76
1023350
8250df9
475c388
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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')), | ||
], | ||
), | ||
] |
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', | ||
), | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
from seshdash.models import Sesh_Site,Site_Weather_Data,BoM_Data_Point, Alert_Rule, Sesh_Alert,Daily_Data_Point, Sesh_User | ||
from seshdash.utils.send_mail import send_mail | ||
from seshdash.utils.model_tools import get_measurement_unit | ||
from seshdash.data.db.kapacitor import Kapacitor | ||
|
||
from django.forms.models import model_to_dict | ||
from django.utils import timezone | ||
from django.db.models import Avg, Sum | ||
from django.apps import apps | ||
from django.core.exceptions import FieldError | ||
from django.conf import settings | ||
from django.template.loader import get_template | ||
|
||
from guardian.shortcuts import get_users_with_perms | ||
from datetime import datetime | ||
|
@@ -213,3 +216,27 @@ def get_table_report_dict(report_table_name, operations): | |
report_table_attributes.append(report_attribute_dict) | ||
|
||
return report_table_attributes | ||
|
||
|
||
""" | ||
Kapacitor report utils | ||
""" | ||
def add_report_kap_task(): | ||
""" | ||
This function adds a report task to kapacitor | ||
""" | ||
data = { | ||
'database': settings.INFLUX_DB, | ||
'operator': 'mean', | ||
'field': 'battery_voltage', | ||
'output_field': 'mean_battery_voltage', | ||
'duration': '5m', | ||
'site_id': '1' | ||
} | ||
|
||
kap = Kapacitor() | ||
t = get_template('seshdash/kapacitor_tasks/aggregate_report.tick') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is using the Django template for the tick script. The user should also be able to give his/her own custom tick script. using the tick script model object. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So we do if the function is given a tick script instance use that as default we have the template for reports |
||
print "About to send this template: %s" % (t.render(data)) | ||
response = kap.create_task('testing', t.render(data)) | ||
|
||
return response |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Example report tick script | ||
// A user wants to get average(mean) for a field battery_voltage in a db called sesh | ||
// The result will be outputed in a field called mean_battery_voltage in the sesh db | ||
|
||
|
||
var database = '{{ database }}' | ||
|
||
var duration = '{{ duration }}' | ||
|
||
var output_field = '{{ output_field }}' | ||
|
||
var points = batch | ||
|query (''' | ||
SELECT {{operator}}(value) | ||
FROM "{{database}}"."autogen"."{{ field }}" | ||
WHERE site_id={{site_id}} | ||
''') | ||
.period(duration) | ||
.every(duration) | ||
|
||
points | ||
|influxDBOut() | ||
.database(database) | ||
.measurement(output_field) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is data hardcoded?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was for testing ,Now the function accepts a report instance and pulls the data from the report instance.