# Path: chain/core/api.py
# class BadRequestException(Exception):
# 
#     def __init__(self, message):
#         self.message = message
# 
#     def __str__(self):
#         return "[Bad Request: " + repr(self.message) + "]"


import requests
import itertools
            import pdb
from pytz import UTC
from datetime import datetime
from django.db import IntegrityError
from time import sleep
from chain.core.api import BadRequestException
from itertools import izip
from time import sleep

    def get_last_sensor_data(self, sensor_id):
        query = "SELECT LAST(value) FROM {0} WHERE sensor_id = \'{1}\'".format(self._measurement,
                                                                           sensor_id)
        result = self.get_values(self.get(query, True))
        return result

    def get_last_data_from_all_sensors(self, site_id):
        query = "SELECT LAST(*) FROM {0} WHERE site_id = \'{1}\' GROUP BY sensor_id".format(self._measurement,
                                                                                           site_id)
        result = self.get_values(self.get(query, True))
        return result

    def get_databases(self):
        response = self.get('SHOW DATABASES', False)
        series = response.json()['results'][0]['series'][0]
        if 'values' not in series:
            # there's only a values list if there's at least one value
            return []

        return [sub[0] for sub in series['values']]

    # returns a list of dictionaries, with one dictionary for each series in the
    # query result. Each dictionary maps the column name to a list of data
    def get_values(self,response):
        json = response.json()
        try:
            if len(json['results'])==0:
                return []
        except:

# Path: chain/core/api.py
# class BadRequestException(Exception):
# 
#     def __init__(self, message):
#         self.message = message
# 
#     def __str__(self):
#         return "[Bad Request: " + repr(self.message) + "]"


# Path: chain/influx_client.py
import requests
import itertools
            import pdb
from pytz import UTC
from datetime import datetime
from django.db import IntegrityError
from time import sleep
from chain.core.api import BadRequestException
from itertools import izip
from time import sleep


    def get_last_sensor_data(self, sensor_id):
        query = "SELECT LAST(value) FROM {0} WHERE sensor_id = \'{1}\'".format(self._measurement,
                                                                           sensor_id)
        result = self.get_values(self.get(query, True))
        return result

    def get_last_data_from_all_sensors(self, site_id):
        query = "SELECT LAST(*) FROM {0} WHERE site_id = \'{1}\' GROUP BY sensor_id".format(self._measurement,
                                                                                           site_id)
        result = self.get_values(self.get(query, True))
        return result

    def get_databases(self):
        response = self.get('SHOW DATABASES', False)
        series = response.json()['results'][0]['series'][0]
        if 'values' not in series:
            # there's only a values list if there's at least one value
            return []

        return [sub[0] for sub in series['values']]

    # returns a list of dictionaries, with one dictionary for each series in the
    # query result. Each dictionary maps the column name to a list of data
    def get_values(self,response):
        json = response.json()
        try:
            if len(json['results'])==0:
                return []
        except:

            pdb.set_trace()