25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

176 satır
5.1KB

  1. import datetime
  2. import os
  3. from flask import Blueprint, request, send_from_directory, json
  4. from flask_classy import FlaskView, route
  5. from modules.core.core import cbpi
  6. class LogView(FlaskView):
  7. @route('/', methods=['GET'])
  8. def get_all_logfiles(self):
  9. """
  10. Get a list of all Log Files
  11. ---
  12. tags:
  13. - logs
  14. responses:
  15. 200:
  16. description: List of all log files
  17. """
  18. result = []
  19. for filename in os.listdir("./logs"):
  20. if filename.endswith(".log"):
  21. result.append(filename)
  22. return json.dumps(result)
  23. @route('/actions')
  24. def actions(self):
  25. """
  26. Get a list of all brewing actions
  27. ---
  28. tags:
  29. - logs
  30. responses:
  31. 200:
  32. description: List of all log files
  33. """
  34. filename = "./logs/action.log"
  35. if os.path.isfile(filename) == False:
  36. return
  37. import csv
  38. array = []
  39. with open(filename, 'rb') as f:
  40. reader = csv.reader(f)
  41. for row in reader:
  42. try:
  43. array.append([int((datetime.datetime.strptime(row[0], "%Y-%m-%d %H:%M:%S") - datetime.datetime(1970, 1, 1)).total_seconds()) * 1000, row[1]])
  44. except:
  45. pass
  46. return json.dumps(array)
  47. @route('/<file>', methods=["DELETE"])
  48. def clearlog(self, file):
  49. """
  50. Delete a log file by name
  51. ---
  52. tags:
  53. - logs
  54. parameters:
  55. - in: path
  56. name: file
  57. schema:
  58. type: string
  59. required: true
  60. description: File name
  61. responses:
  62. 204:
  63. description: Log deleted
  64. """
  65. if not self.check_filename(file):
  66. return ('File Not Found', 404)
  67. filename = "./logs/%s" % file
  68. if os.path.isfile(filename) == True:
  69. os.remove(filename)
  70. cbpi.notify("log deleted succesfully", "")
  71. return ('', 204)
  72. else:
  73. cbpi.notify("Failed to delete log", "", type="danger")
  74. return ('', 404)
  75. def read_log_as_json(self, type, id):
  76. filename = "./logs/%s_%s.log" % (type, id)
  77. if os.path.isfile(filename) == False:
  78. return
  79. import csv
  80. array = []
  81. with open(filename, 'rb') as f:
  82. reader = csv.reader(f)
  83. for row in reader:
  84. try:
  85. array.append([int((datetime.datetime.strptime(row[0], "%Y-%m-%d %H:%M:%S") - datetime.datetime(1970, 1, 1)).total_seconds()) * 1000, float(row[1])])
  86. except:
  87. pass
  88. return array
  89. def convert_chart_data_to_json(self, chart_data):
  90. return {"name": chart_data["name"], "data": self.read_log_as_json(chart_data["data_type"], chart_data["data_id"])}
  91. @route('/<t>/<int:id>', methods=["POST"])
  92. def get_logs_as_json(self, t, id):
  93. """
  94. Get Log as json
  95. ---
  96. tags:
  97. - logs
  98. parameters:
  99. - in: path
  100. name: id
  101. schema:
  102. type: string
  103. required: true
  104. description: id of the file
  105. responses:
  106. 200:
  107. description: Log File Data
  108. """
  109. data = request.json
  110. result = []
  111. if t == "s":
  112. name = cbpi.cache.get("sensors").get(id).name
  113. result.append({"name": name, "data": self.read_log_as_json("sensor", id)})
  114. if t == "k":
  115. kettle = cbpi.cache.get("kettle").get(id)
  116. result = map(self.convert_chart_data_to_json, cbpi.brewing.get_controller(kettle.logic).get("class").chart(kettle))
  117. if t == "f":
  118. fermenter = cbpi.cache.get("fermenter").get(id)
  119. result = map(self.convert_chart_data_to_json, cbpi.fermentation.get_controller(fermenter.logic).get("class").chart(fermenter))
  120. return json.dumps(result)
  121. @route('/download/<file>')
  122. @cbpi.nocache
  123. def download(self, file):
  124. """
  125. Download a log file by name
  126. ---
  127. tags:
  128. - logs
  129. parameters:
  130. - in: path
  131. name: file
  132. schema:
  133. type: string
  134. required: true
  135. description: filename
  136. responses:
  137. 200:
  138. description: Log file downloaded
  139. """
  140. if not self.check_filename(file):
  141. return ('File Not Found', 404)
  142. return send_from_directory('../../logs', file, as_attachment=True, attachment_filename=file)
  143. def check_filename(self, name):
  144. import re
  145. pattern = re.compile('^([A-Za-z0-9-_])+.log$')
  146. return True if pattern.match(name) else False
  147. @cbpi.addon.core.initializer()
  148. def init(cbpi):
  149. """
  150. Initializer for the message module
  151. :param app: the flask app
  152. :return: None
  153. """
  154. LogView.register(cbpi._app, route_base='/api/logs')