Skip to content

Commit

Permalink
remove measurement names from localsettings_template.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ellachao committed May 28, 2017
1 parent fd2733c commit 48c78bc
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 32 deletions.
9 changes: 4 additions & 5 deletions chain/core/migrations/0014_create_continuous_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
from south.v2 import DataMigration
from django.db import models
from chain.core.resources import influx_client
from chain.localsettings import INFLUX_DATABASE, INFLUX_MEASUREMENT, \
INFLUX_MEASUREMENT_1H, INFLUX_MEASUREMENT_1D, INFLUX_MEASUREMENT_1W
from chain.localsettings import INFLUX_DATABASE, INFLUX_MEASUREMENT

class Migration(DataMigration):

Expand All @@ -21,23 +20,23 @@ def forwards(self, orm):
SELECT max("value"), min("value"), mean("value"), count("value"), sum("value")
INTO "{1}" FROM "{2}" GROUP BY "sensor_id", time(1h), *
END
'''.format(INFLUX_DATABASE, INFLUX_MEASUREMENT_1H, INFLUX_MEASUREMENT), True)
'''.format(INFLUX_DATABASE, INFLUX_MEASUREMENT + '_1h', INFLUX_MEASUREMENT), True)
influx_client.post('query', '''
CREATE CONTINUOUS QUERY "cq_1d" ON "{0}"
RESAMPLE FOR 2d
BEGIN
SELECT max("max"), min("min"), sum("sum")/sum("count") as "mean", sum("count") as "count", sum("sum")
INTO "{1}" FROM "{2}" GROUP BY "sensor_id", time(1d), *
END
'''.format(INFLUX_DATABASE, INFLUX_MEASUREMENT_1D, INFLUX_MEASUREMENT_1H), True)
'''.format(INFLUX_DATABASE, INFLUX_MEASUREMENT + '_1d', INFLUX_MEASUREMENT + '_1h'), True)
influx_client.post('query', '''
CREATE CONTINUOUS QUERY "cq_1w" ON "{0}"
RESAMPLE FOR 2w
BEGIN
SELECT max("max"), min("min"), sum("sum")/sum("count") as "mean", sum("count") as "count", sum("sum")
INTO "{1}" FROM "{2}" GROUP BY "sensor_id", time(1w), *
END
'''.format(INFLUX_DATABASE, INFLUX_MEASUREMENT_1W, INFLUX_MEASUREMENT_1D), True)
'''.format(INFLUX_DATABASE, INFLUX_MEASUREMENT + '_1w', INFLUX_MEASUREMENT + '_1d'), True)

def backwards(self, orm):
"Write your backwards methods here."
Expand Down
7 changes: 2 additions & 5 deletions chain/core/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@
from django.utils import timezone
from datetime import timedelta, datetime
import calendar
from chain.localsettings import INFLUX_HOST, INFLUX_PORT, INFLUX_DATABASE, INFLUX_MEASUREMENT, \
INFLUX_MEASUREMENT_1H, INFLUX_MEASUREMENT_1D, INFLUX_MEASUREMENT_1W
from chain.localsettings import INFLUX_HOST, INFLUX_PORT, INFLUX_DATABASE, INFLUX_MEASUREMENT
from chain.influx_client import InfluxClient
from django.views.decorators.csrf import csrf_exempt
from django.utils.dateparse import parse_datetime


influx_client = InfluxClient(INFLUX_HOST, INFLUX_PORT, INFLUX_DATABASE,
INFLUX_MEASUREMENT, INFLUX_MEASUREMENT_1H,
INFLUX_MEASUREMENT_1D, INFLUX_MEASUREMENT_1W)
influx_client = InfluxClient(INFLUX_HOST, INFLUX_PORT, INFLUX_DATABASE, INFLUX_MEASUREMENT)

class SensorDataResource(Resource):

Expand Down
18 changes: 8 additions & 10 deletions chain/core/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,11 @@ def clear(self):
from chain.core.api import HTTP_STATUS_SUCCESS, HTTP_STATUS_CREATED
from chain.core.hal import HALDoc
from chain.core import resources
from chain.localsettings import INFLUX_HOST, INFLUX_PORT, INFLUX_MEASUREMENT, \
INFLUX_MEASUREMENT_1H, INFLUX_MEASUREMENT_1D, INFLUX_MEASUREMENT_1W
from chain.localsettings import INFLUX_HOST, INFLUX_PORT, INFLUX_MEASUREMENT
from chain.influx_client import InfluxClient

resources.influx_client = InfluxClient(INFLUX_HOST, INFLUX_PORT, 'test',
INFLUX_MEASUREMENT, INFLUX_MEASUREMENT_1H,
INFLUX_MEASUREMENT_1D, INFLUX_MEASUREMENT_1W)
INFLUX_MEASUREMENT)

HTTP_STATUS_NOT_ACCEPTABLE = 406
HTTP_STATUS_NOT_FOUND = 404
Expand Down Expand Up @@ -1121,22 +1119,22 @@ def setUp(self):
resources.influx_client.post('query', '''
SELECT max("value"), min("value"), mean("value"), count("value"), sum("value")
INTO "{0}" FROM "{1}" WHERE "time" < '{2}' AND "time" >= '{3}'
GROUP BY "sensor_id", time(1h), *'''.format(INFLUX_MEASUREMENT_1H,
GROUP BY "sensor_id", time(1h), *'''.format(INFLUX_MEASUREMENT + '_1h',
INFLUX_MEASUREMENT,
time_end,
time_begin), True)
resources.influx_client.post('query', '''
SELECT max("max"), min("min"), sum("sum")/sum("count") as "mean", sum("count") as "count", sum("sum")
INTO "{0}" FROM "{1}" WHERE "time" < '{2}' AND "time" >= '{3}'
GROUP BY "sensor_id", time(1d), *'''.format(INFLUX_MEASUREMENT_1D,
INFLUX_MEASUREMENT_1H,
GROUP BY "sensor_id", time(1d), *'''.format(INFLUX_MEASUREMENT + '_1d',
INFLUX_MEASUREMENT + '_1h',
time_end,
time_begin), True)
resources.influx_client.post('query', '''
SELECT max("max"), min("min"), sum("sum")/sum("count") as "mean", sum("count") as "count", sum("sum")
INTO "{0}" FROM "{1}" WHERE "time" < '{2}' AND "time" >= '{3}'
GROUP BY "sensor_id", time(1w), *'''.format(INFLUX_MEASUREMENT_1W,
INFLUX_MEASUREMENT_1D,
GROUP BY "sensor_id", time(1w), *'''.format(INFLUX_MEASUREMENT + '_1w',
INFLUX_MEASUREMENT + '_1d',
time_end,
time_begin), True)

Expand Down Expand Up @@ -1184,7 +1182,7 @@ def test_aggregate_sensor_data_invalid_arguements(self):
href.replace('{&aggtime}', '&aggtime=1s'),
expect_status_code=HTTP_STATUS_BAD_REQUEST,
check_mime_type=False)

except Exception:
self.assertTrue(False)

Expand Down
12 changes: 4 additions & 8 deletions chain/influx_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@

class InfluxClient(object):

def __init__(self, host, port, database, measurement,
measurement_1h, measurement_1d, measurement_1w):
def __init__(self, host, port, database, measurement):
self._host = host
self._port = port
self._database = database
self._measurement = measurement
self._measurement_1h = measurement_1h
self._measurement_1d = measurement_1d
self._measurement_1w = measurement_1w
# Persist TCP connection
self._session = requests
self._url = 'http://' + self._host + ':' + self._port
Expand Down Expand Up @@ -82,11 +78,11 @@ def get_sensor_data(self, filters):
measurement = self._measurement
# arguements are unicode strings
elif filters['aggtime'] == u'1h':
measurement = self._measurement_1h
measurement = self._measurement + '_1h'
elif filters['aggtime'] == u'1d':
measurement = self._measurement_1d
measurement = self._measurement + '_1d'
elif filters['aggtime'] == u'1w':
measurement = self._measurement_1w
measurement = self._measurement + '_1w'
else:
raise BadRequestException('Invalid arguement for aggtime')

Expand Down
4 changes: 0 additions & 4 deletions chain/localsettings_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,3 @@
INFLUX_DATABASE = 'chain'
# the measurement (like a table) where the scalar sensor data will be stored
INFLUX_MEASUREMENT = 'sensordata'
# measurements for aggregated data
INFLUX_MEASUREMENT_1H = 'sensordata_1h'
INFLUX_MEASUREMENT_1D = 'sensordata_1d'
INFLUX_MEASUREMENT_1W = 'sensordata_1w'

0 comments on commit 48c78bc

Please sign in to comment.