# Path: chain/core/resources.py
# class MetadataResource(Resource):
# class SensorDataResource(Resource):
# class ScalarSensorDataResource(SensorDataResource):
# class AggregateScalarSensorDataResource(SensorDataResource):
# class ScalarSensorResource(Resource):
# class PresenceDataResource(SensorDataResource):
# class PresenceSensorResource(Resource):
# class PersonResource(Resource):
# class MixedSensorResource(Resource):
# class DeviceResource(Resource):
# class SiteResource(Resource):
# class ApiRootResource(Resource):
#     def get_queryset(self):
#     def get_total_count(self):
#     def serialize_list(self, embed, cache):
#     def serialize_single(self, embed=True, cache=None, rels=True, *args, **kwargs):
#     def edit_view(cls, request, id):
#     def __init__(self, *args, **kwargs):
#     def format_time(self, timestamp):
#     def add_page_links(self, data, href, page_start, page_end):
#     def default_timespan(self):
#     def __init__(self, *args, **kwargs):
#     def serialize_single(self, embed=True, cache=None, rels=True):
#     def sanitize_field_value(cls, field_name, value):
#     def save(self):
#     def serialize_list(self, embed, cache):
#     def get_cache_key(self):
#     def serialize_stream(self):
#     def get_single_href(self):
#     def get_tags(self):
#     def get_field_schema_type(cls, field_name):
#     def model_has_field(cls, field_name):
#     def __init__(self, *args, **kwargs):
#     def get_list_href(self, embed=False):
#     def serialize_list(self, embed, cache):
#     def urls(cls):
#     def serialize_single(self, embed, cache, *args, **kwargs):
#     def get_tags(self):
#     def __init__(self, *args, **kwargs):
#     def serialize_single(self, embed, cache):
#     def get_additional_links(self):
#     def serialize_list(self, embed, cache):
#     def get_person_url(self, obj):
#     def get_sensor_url(self, obj):
#     def serialize_stream(self):
#     def get_tags(self):
#     def get_filled_schema(self):
#     def serialize_single(self, embed, cache, *args, **kwargs):
#     def get_additional_links(self):
#     def get_additional_embedded(self):
#     def get_person_url(self, obj):
#     def get_presense_data_url(self, obj):
#     def get_sensor_url(self, obj):
#     def get_tags(self):
#     def serialize_single(self, embed, cache, *args, **kwargs):
#     def get_presence_data(self):
#     def get_additional_links(self):
#     def get_additional_embedded(self):
#     def get_presense_data_url(self, obj):
#     def get_tags(self):
# def json_merge(obj1, obj2):
#     def get_schema(cls, filters=None):
#     def create_list(cls, data, req):
#     def create_single(cls, data, req):
#     def serialize_single(self, embed, cache, *args, **kwargs):
#     def serialize_list(self, embed, cache, *args, **kwargs):
#     def get_links(self):
#     def map_model_to_resource(self):
#     def query_models(self):
#     def get_tags(self):
#     def get_tags(self):
#     def serialize_single(self, embed, cache):
#     def get_filled_schema(self):
#     def deserialize(self):
#     def update(self, data):
#     def get_tags(self):
#     def get_schema(cls, filters=None):
#     def site_summary_view(cls, request, id):
#     def urls(cls):
#     def __init__(self, request):
#     def serialize(self):
#     def single_view(cls, request):


from django.db import models, migrations
from chain.core.resources import influx_client
from chain.localsettings import INFLUX_DATABASE, INFLUX_MEASUREMENT

def add_cqs(apps, schema_editor):
    influx_client.post('query', '''
     CREATE CONTINUOUS QUERY "cq_1h" ON "{0}"
         RESAMPLE EVERY 1h
         BEGIN
             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)
    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)
    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)

def remove_cqs(apps, schema_editor):
    influx_client.post('query',
                       'DROP CONTINUOUS QUERY "cq_1h" on "{}"'.format(INFLUX_DATABASE), True)

# Path: chain/core/resources.py
# class MetadataResource(Resource):
# class SensorDataResource(Resource):
# class ScalarSensorDataResource(SensorDataResource):
# class AggregateScalarSensorDataResource(SensorDataResource):
# class ScalarSensorResource(Resource):
# class PresenceDataResource(SensorDataResource):
# class PresenceSensorResource(Resource):
# class PersonResource(Resource):
# class MixedSensorResource(Resource):
# class DeviceResource(Resource):
# class SiteResource(Resource):
# class ApiRootResource(Resource):
#     def get_queryset(self):
#     def get_total_count(self):
#     def serialize_list(self, embed, cache):
#     def serialize_single(self, embed=True, cache=None, rels=True, *args, **kwargs):
#     def edit_view(cls, request, id):
#     def __init__(self, *args, **kwargs):
#     def format_time(self, timestamp):
#     def add_page_links(self, data, href, page_start, page_end):
#     def default_timespan(self):
#     def __init__(self, *args, **kwargs):
#     def serialize_single(self, embed=True, cache=None, rels=True):
#     def sanitize_field_value(cls, field_name, value):
#     def save(self):
#     def serialize_list(self, embed, cache):
#     def get_cache_key(self):
#     def serialize_stream(self):
#     def get_single_href(self):
#     def get_tags(self):
#     def get_field_schema_type(cls, field_name):
#     def model_has_field(cls, field_name):
#     def __init__(self, *args, **kwargs):
#     def get_list_href(self, embed=False):
#     def serialize_list(self, embed, cache):
#     def urls(cls):
#     def serialize_single(self, embed, cache, *args, **kwargs):
#     def get_tags(self):
#     def __init__(self, *args, **kwargs):
#     def serialize_single(self, embed, cache):
#     def get_additional_links(self):
#     def serialize_list(self, embed, cache):
#     def get_person_url(self, obj):
#     def get_sensor_url(self, obj):
#     def serialize_stream(self):
#     def get_tags(self):
#     def get_filled_schema(self):
#     def serialize_single(self, embed, cache, *args, **kwargs):
#     def get_additional_links(self):
#     def get_additional_embedded(self):
#     def get_person_url(self, obj):
#     def get_presense_data_url(self, obj):
#     def get_sensor_url(self, obj):
#     def get_tags(self):
#     def serialize_single(self, embed, cache, *args, **kwargs):
#     def get_presence_data(self):
#     def get_additional_links(self):
#     def get_additional_embedded(self):
#     def get_presense_data_url(self, obj):
#     def get_tags(self):
# def json_merge(obj1, obj2):
#     def get_schema(cls, filters=None):
#     def create_list(cls, data, req):
#     def create_single(cls, data, req):
#     def serialize_single(self, embed, cache, *args, **kwargs):
#     def serialize_list(self, embed, cache, *args, **kwargs):
#     def get_links(self):
#     def map_model_to_resource(self):
#     def query_models(self):
#     def get_tags(self):
#     def get_tags(self):
#     def serialize_single(self, embed, cache):
#     def get_filled_schema(self):
#     def deserialize(self):
#     def update(self, data):
#     def get_tags(self):
#     def get_schema(cls, filters=None):
#     def site_summary_view(cls, request, id):
#     def urls(cls):
#     def __init__(self, request):
#     def serialize(self):
#     def single_view(cls, request):


# Path: chain/core/migrations/0004_influx_setup.py
from django.db import models, migrations
from chain.core.resources import influx_client
from chain.localsettings import INFLUX_DATABASE, INFLUX_MEASUREMENT


def add_cqs(apps, schema_editor):
    influx_client.post('query', '''
     CREATE CONTINUOUS QUERY "cq_1h" ON "{0}"
         RESAMPLE EVERY 1h
         BEGIN
             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)
    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)
    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)

def remove_cqs(apps, schema_editor):
    influx_client.post('query',
                       'DROP CONTINUOUS QUERY "cq_1h" on "{}"'.format(INFLUX_DATABASE), True)

    influx_client.post('query',