xinflux.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. # update: 2021-8-23-14
  2. # see: https://github.com/influxdata/influxdb-python - pip install influxdb
  3. # see: https://pypi.org/project/influxdb/#history
  4. # see: https://registry.hub.docker.com/_/influxdb
  5. # see: https://influxdb-python.readthedocs.io/en/latest/include-readme.html
  6. # see: https://github.com/unaizalakain/qinfluxdb - influxdb orm
  7. # see: https://blog.csdn.net/liuming690452074/article/details/115719884 - delete
  8. # see: https://blog.csdn.net/xiligey1/article/details/104530893/
  9. # see: https://blog.csdn.net/yue530tomtom/article/details/82688453 - func
  10. """
  11. """
  12. from influxdb import InfluxDBClient
  13. import datetime
  14. import time
  15. class Client(InfluxDBClient):
  16. def __init__(self, host='fra-middleware-influx', port=8086, database='vms'):
  17. """
  18. 内部访问:
  19. host: fra-middleware-influx
  20. port: 8086
  21. 远程访问:
  22. host: 192.168.30.59
  23. port: 7080
  24. """
  25. """
  26. def filter
  27. def remove
  28. def append
  29. def add
  30. def write
  31. def read
  32. def save
  33. def add
  34. def set
  35. def get
  36. def put
  37. def craete
  38. def delete
  39. def update
  40. def select
  41. """
  42. self.database = database
  43. super().__init__(host, port, 'admin', 'admin', database)
  44. self.create_database(database)
  45. def add_item(self, table, key_dict=dict(), value_dict=dict()):
  46. """保存单条"""
  47. _points = [
  48. {
  49. 'measurement': table, # 表名
  50. 'time': datetime.datetime.now().isoformat('T'), # iso标准格式
  51. 'tags': key_dict,
  52. 'fields': value_dict
  53. }
  54. ]
  55. return self.write_points(_points, database=self.database)
  56. def add_items(self, table, key_list=list(), value_list=list()):
  57. """保存多条"""
  58. _points = list()
  59. for key, value in zip(key_list, value_list):
  60. _point = {
  61. 'measurement': table, # 表名
  62. 'time': datetime.datetime.now().isoformat('T'), # iso标准格式
  63. 'tags': key,
  64. 'fields': value
  65. }
  66. _points.append(_point)
  67. return self.write_points(_points, database=self.database)
  68. def filter_by_time_range(self, table, start_ts, end_ts):
  69. """按时时间筛选"""
  70. start_at = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.localtime(start_ts))
  71. end_at = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.localtime(end_ts))
  72. sql = f"select * from {table} "
  73. sql += f"where time >= '{start_at}' and time <= '{end_at}'"
  74. sql += f'order by time desc;'
  75. result = self.query(sql, database=self.database)
  76. return result.get_points()
  77. def remove_one(self, table, k, v): # todo 不可用
  78. """按时时间筛选"""
  79. sql = f"delete from {table} where \"{k}\"=\"{v}\";"
  80. return self.query(sql, database=self.database)
  81. def delete_table(self, table):
  82. return self.drop_measurement(table)
  83. if __name__ == '__main__':
  84. """
  85. sudo python3 -m pip install influxdb
  86. sudo python3 /home/server/projects/taiwuict/cscec-8bur-vms/supplement-python/clients/db_influx.py
  87. """
  88. import time
  89. # --- init ---
  90. # xdb = Client(host='192.168.30.59', port=7080, database='vms')
  91. # xdb = Client(host='192.168.30.169', port=7080, database='vms')
  92. # xdb = Client(host='192.168.30.13', port=7080, database='vms')
  93. # xdb = Client(host='192.168.20.231', port=7080, database='vms')
  94. # xdb = Client(host='192.168.0.15', port=7080, database='vms')
  95. xdb = Client(host='192.168.1.242', port=7080, database='vms') # 第4现场
  96. # --- test ---
  97. s = time.time() - (86400 * 100)
  98. e = time.time()
  99. out = xdb.filter_by_time_range('FaceLog', s, e)
  100. for i in out:
  101. print(i)
  102. break
  103. # --- test ---
  104. # s = 1649606400
  105. # e = 1650297600
  106. # out = xdb.filter_by_time_range('FaceLog', s, e)
  107. # for i in out:
  108. # print(i)
  109. # break
  110. # --- test filter_by_time_range ---
  111. # s = time.mktime(time.strptime('2020-01-09 13:45:00', '%Y-%m-%d %H:%M:%S'))
  112. # e = time.mktime(time.strptime('2022-01-01 17:00:00', '%Y-%m-%d %H:%M:%S'))
  113. # out = xdb.filter_by_time_range('FaceLog', s, e)
  114. # for i in out:
  115. # print(i)
  116. # break
  117. # --- test 添加数据 ---
  118. # out = xdb.add_item('FaceLog', {}, {'aaa': 111, 'bbb': 222})
  119. # print(out)
  120. # --- test delete_table ---
  121. # xdb.delete_table('FaceLog')
  122. # --- test remove_one ---
  123. # xdb.remove_one('FaceLog', 'face_uuid', 'aaabbbccc')
  124. # --- test 获取数据库列表 ---
  125. # out = xdb.get_list_database()
  126. # print(out)
  127. # --- test 创建数据库 ---
  128. # xdb.create_database('vms')
  129. # out = xdb.get_list_database()
  130. # print(out)
  131. # --- test 删除数据库 ---
  132. # out1 = xdb.drop_database('vms1')
  133. # out = xdb.get_list_database()
  134. # print(out1, out)
  135. # --- test 显示数据库中的表 ---
  136. # out = xdb.query('show measurements;', database=xdb.database)
  137. # print(out)
  138. # --- test 添加数据 ---
  139. # points = [
  140. # {
  141. # # "time": datetime.datetime.utcnow().isoformat('T'), # iso标准时间
  142. # "time": datetime.datetime.now().isoformat('T'), # iso标准时间
  143. # "measurement": "table23", # 表名
  144. # "tags": {
  145. # "key1": "server01",
  146. # "key2": "us-west"
  147. # },
  148. # "fields": {
  149. # "field1": 0.64,
  150. # "field2": 0.42,
  151. # }
  152. # }
  153. # ] # 待写入数据库的点组成的列表
  154. # out = xdb.write_points(points, database=xdb.database) # 将这些点写入指定database
  155. # print(out)
  156. # --- test 删除表数据 ---
  157. # table_name = 'students'
  158. # xdb.query(f"drop measurement {table_name}") # 删除表
  159. # --- test query ---
  160. # sql = f"select <字段名> from <数据库.表名> "
  161. # sql += f"where time >= '<开始时间>' and time <= '<结束时间>' "
  162. # sql += f"TZ('Asia/Shanghai')"
  163. # out = xdb.query(sql, database='vms')
  164. # print(out)
  165. # --- test ---
  166. # now = datetime.datetime.utcnow().isoformat('T')
  167. # print(now, type(now))
  168. # --- test ---
  169. # _sql = f"select field1, field2 from table23 "
  170. # _sql += f"where time >= '{start_at}' and time <= '{end_at}' "
  171. # _sql += f"TZ('Asia/Shanghai');"
  172. # out = xdb.query(_sql, database=xdb.database)
  173. # print(out)
  174. # --- test add_item ---
  175. # _key_dict = {
  176. # "key1": "server01",
  177. # "key2": "server02",
  178. # }
  179. # _value_dict = {
  180. # "field1": 0.64,
  181. # "field2": 0.55,
  182. # }
  183. # out = xdb.add_item('table123', _key_dict, _value_dict)
  184. # print(out)
  185. # --- test add_item ---
  186. # _key_dict = {
  187. # 'face_uuid': 'aaabbbccc',
  188. # }
  189. # _value_dict = {
  190. # 'count': 1,
  191. # }
  192. # xdb.add_item('FaceLog', _key_dict, _value_dict)