#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
    demeter database
    name:data.py
    author:rabin
"""
from __load__ import *
import datetime
import calendar

class Data(Model):
	__table__ = 'data'

	def setTable(self, data):
		self.table = self.__table__ + '_' + str(data['farm']) + '_' + str(data['device']) + '1'

	def insert(self, data):
		self.setTable(data)
		json = self.insert_json(data)
		self.db.write_points(json)

	def insert_json(self, data):
		json = [
		{
			"measurement": self.table,
			"tags": {
				"hard": data['hard'],
				"type":data['type'],
				"gateway": data['gateway']
			},
			#"time": data['time'],
			"fields": {
				'source' : float(data['source']),
				'value' : float(data['value'])
			}
		}]
		return json

	def get(self, farm, device, col='value', search=''):
		data = {}
		data['farm'] = farm
		data['device'] = device
		self.setTable(data)
		if search:
			search = ' where ' + search
		sql = 'select ' + col + ' from ' + self.table + search
		data = self.db.query(sql)
		value = []
		time = []
		rows = []
		utc_format = '%Y-%m-%dT%H:%M:%S'
		local_format = "%Y-%m-%d %H:%M:%S"
		if 'series' in data.raw and data.raw['series'][0]['values']:
			for values in data.raw['series'][0]['values']:
				if values[1]:
					v = float(round(values[1], 2))
					temp = values[0].split('.')
					uformat = utc_format
					if len(temp) <= 1:
						uformat = utc_format + 'Z'
					utc = Demeter.mktime(temp[0], uformat) + 3600*8
					date = Demeter.date(utc, local_format)
					value.append(v)
					time.append(date)
					rows.append([date, v])
		return {'time':time, 'value':value, 'rows':rows}

	def getData(self, method, data, config):
		search = ''
		col = 'value'
		utc_format = '%Y-%m-%dT%H:%M:%SZ'
		if ('start' in data and data['start']) or ('end' in data and data['end']):
			if 'start' in data and data['start']:
				start = Demeter.mktime(data['start'])
				startDate = Demeter.date(start - 3600*8, utc_format)
				search = 'time > \'' + str(startDate) + '\''

			if 'end' in data and data['end']:
				end = Demeter.mktime(data['end'])
				endDate = Demeter.date(end - 3600*8, utc_format)
				search = search + ' and time < \'' + str(endDate) + '\''
				search = search + self.group(self.day(start, end))
			else:
				now = Demeter.time()
				search = search + self.group(self.day(start, now))

		elif 'group' in data and data['group']:
			value = data['group']
			time = 'time > now()'
			search = time + ' - ' + value + 'd' + self.group(int(value))

		result = []
		if method == 'avg':
			col = 'mean(value) as value'
			result.append(self.load(config, search, col, '平均'))
		elif method == 'maxmin':
			col = 'max(value) as value'
			result.append(self.load(config, search, col, '最高'))
			col = 'min(value) as value'
			result.append(self.load(config, search, col, '最低'))
		else:
			col = 'value'
			if 'group' in search:
				temp = search.split('group')
				search = temp[0]
			result.append(self.load(config, search, col, '明细'))
		return result

	def load(self, config, search, col, name):
		result = {}
		result['data'] = self.get(config['farm_id'], config['hardware_id'], search=search, col=col)
		result['name'] = name
		if result['data']:
			result['data']['time'] = json.dumps(result['data']['time'])
			result['data']['value'] = json.dumps(result['data']['value'])
		return result

	def group(self, day):
		if day >= 180:
			return ' group by time(10d)'
		elif day >= 30:
			return ' group by time(1d)'
		elif day >= 7:
			return ' group by time(60m)'
		elif day >= 1:
			return ' group by time(5m)'
		else:
			return ' group by time(1m)'

	def day(self, start, end):
		return math.ceil(round(float((end - start) / (3600*24))))

	def dateConfig(self):
		return [{'id':'1', 'name':'一天'},{'id':'7', 'name':'一周'},{'id':'30', 'name':'一个月'},{'id':'180', 'name':'半年'}]