data.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. demeter database
  5. name:data.py
  6. author:rabin
  7. """
  8. from __load__ import *
  9. import datetime
  10. import calendar
  11. class Data(Model):
  12. __table__ = 'data'
  13. def setTable(self, data):
  14. self.table = self.__table__ + '_' + str(data['farm']) + '_' + str(data['device']) + '1'
  15. def insert(self, data):
  16. self.setTable(data)
  17. json = self.insert_json(data)
  18. self.db.write_points(json)
  19. def insert_json(self, data):
  20. json = [
  21. {
  22. "measurement": self.table,
  23. "tags": {
  24. "hard": data['hard'],
  25. "type":data['type'],
  26. "gateway": data['gateway']
  27. },
  28. #"time": data['time'],
  29. "fields": {
  30. 'source' : float(data['source']),
  31. 'value' : float(data['value'])
  32. }
  33. }]
  34. return json
  35. def get(self, farm, device, col='value', search=''):
  36. data = {}
  37. data['farm'] = farm
  38. data['device'] = device
  39. self.setTable(data)
  40. if search:
  41. search = ' where ' + search
  42. sql = 'select ' + col + ' from ' + self.table + search
  43. data = self.db.query(sql)
  44. value = []
  45. time = []
  46. rows = []
  47. utc_format = '%Y-%m-%dT%H:%M:%S'
  48. local_format = "%Y-%m-%d %H:%M:%S"
  49. if 'series' in data.raw and data.raw['series'][0]['values']:
  50. for values in data.raw['series'][0]['values']:
  51. if values[1]:
  52. v = float(round(values[1], 2))
  53. temp = values[0].split('.')
  54. uformat = utc_format
  55. if len(temp) <= 1:
  56. uformat = utc_format + 'Z'
  57. utc = Demeter.mktime(temp[0], uformat) + 3600*8
  58. date = Demeter.date(utc, local_format)
  59. value.append(v)
  60. time.append(date)
  61. rows.append([date, v])
  62. return {'time':time, 'value':value, 'rows':rows}
  63. def getData(self, method, data, config):
  64. search = ''
  65. col = 'value'
  66. utc_format = '%Y-%m-%dT%H:%M:%SZ'
  67. if ('start' in data and data['start']) or ('end' in data and data['end']):
  68. if 'start' in data and data['start']:
  69. start = Demeter.mktime(data['start'])
  70. startDate = Demeter.date(start - 3600*8, utc_format)
  71. search = 'time > \'' + str(startDate) + '\''
  72. if 'end' in data and data['end']:
  73. end = Demeter.mktime(data['end'])
  74. endDate = Demeter.date(end - 3600*8, utc_format)
  75. search = search + ' and time < \'' + str(endDate) + '\''
  76. search = search + self.group(self.day(start, end))
  77. else:
  78. now = Demeter.time()
  79. search = search + self.group(self.day(start, now))
  80. elif 'group' in data and data['group']:
  81. value = data['group']
  82. time = 'time > now()'
  83. search = time + ' - ' + value + 'd' + self.group(int(value))
  84. result = []
  85. if method == 'avg':
  86. col = 'mean(value) as value'
  87. result.append(self.load(config, search, col, '平均'))
  88. elif method == 'maxmin':
  89. col = 'max(value) as value'
  90. result.append(self.load(config, search, col, '最高'))
  91. col = 'min(value) as value'
  92. result.append(self.load(config, search, col, '最低'))
  93. else:
  94. col = 'value'
  95. if 'group' in search:
  96. temp = search.split('group')
  97. search = temp[0]
  98. result.append(self.load(config, search, col, '明细'))
  99. return result
  100. def load(self, config, search, col, name):
  101. result = {}
  102. result['data'] = self.get(config['farm_id'], config['hardware_id'], search=search, col=col)
  103. result['name'] = name
  104. if result['data']:
  105. result['data']['time'] = json.dumps(result['data']['time'])
  106. result['data']['value'] = json.dumps(result['data']['value'])
  107. return result
  108. def group(self, day):
  109. if day >= 180:
  110. return ' group by time(10d)'
  111. elif day >= 30:
  112. return ' group by time(1d)'
  113. elif day >= 7:
  114. return ' group by time(60m)'
  115. elif day >= 1:
  116. return ' group by time(5m)'
  117. else:
  118. return ' group by time(1m)'
  119. def day(self, start, end):
  120. return math.ceil(round(float((end - start) / (3600*24))))
  121. def dateConfig(self):
  122. return [{'id':'1', 'name':'一天'},{'id':'7', 'name':'一周'},{'id':'30', 'name':'一个月'},{'id':'180', 'name':'半年'}]