|
|
@@ -1,6 +1,5 @@ |
|
|
import datetime |
|
|
import datetime |
|
|
import os |
|
|
import os |
|
|
import time |
|
|
|
|
|
import requests |
|
|
import requests |
|
|
import logging |
|
|
import logging |
|
|
from flask import request, send_from_directory, json |
|
|
from flask import request, send_from_directory, json |
|
|
@@ -22,11 +21,8 @@ class LogView(FlaskView): |
|
|
|
|
|
|
|
|
@route('/actions') |
|
|
@route('/actions') |
|
|
def actions(self): |
|
|
def actions(self): |
|
|
filename = "./logs/action.log" |
|
|
|
|
|
array = self.query_log(filename, "string") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
array = self.read_log_as_json("action") |
|
|
json_dumps = json.dumps(array) |
|
|
json_dumps = json.dumps(array) |
|
|
self.logger.debug("Loaded action.log [%s]", json_dumps) |
|
|
|
|
|
|
|
|
|
|
|
return json_dumps |
|
|
return json_dumps |
|
|
|
|
|
|
|
|
@@ -48,13 +44,13 @@ class LogView(FlaskView): |
|
|
cbpi.notify("Failed to delete log", "", type="danger") |
|
|
cbpi.notify("Failed to delete log", "", type="danger") |
|
|
return ('', 204) |
|
|
return ('', 204) |
|
|
|
|
|
|
|
|
def query_tsdb(self, type, id): |
|
|
|
|
|
|
|
|
def query_tsdb(self, sensor_name): |
|
|
kairosdb_server = "http://127.0.0.1:" + cbpi.cache["config"]["kairos_db_port"].__dict__["value"] |
|
|
kairosdb_server = "http://127.0.0.1:" + cbpi.cache["config"]["kairos_db_port"].__dict__["value"] |
|
|
|
|
|
|
|
|
data = dict(metrics=[ |
|
|
data = dict(metrics=[ |
|
|
{ |
|
|
{ |
|
|
"tags": {}, |
|
|
"tags": {}, |
|
|
"name": "cbpi.%s_%s" % (type, id), |
|
|
|
|
|
|
|
|
"name": "cbpi.%s" % sensor_name, |
|
|
"aggregators": [ |
|
|
"aggregators": [ |
|
|
{ |
|
|
{ |
|
|
"name": "avg", |
|
|
"name": "avg", |
|
|
@@ -81,11 +77,11 @@ class LogView(FlaskView): |
|
|
|
|
|
|
|
|
response = requests.post(kairosdb_server + "/api/v1/datapoints/query", json.dumps(data)) |
|
|
response = requests.post(kairosdb_server + "/api/v1/datapoints/query", json.dumps(data)) |
|
|
if response.ok: |
|
|
if response.ok: |
|
|
self.logger.debug("Fetching time series for [%s_%s] took [%s]", type, id, response.elapsed) |
|
|
|
|
|
self.logger.debug("Time series for [%s_%s] is [%s]", type, id, response.json()) |
|
|
|
|
|
|
|
|
self.logger.debug("Fetching time series for [%s] took [%s]", sensor_name, response.elapsed) |
|
|
|
|
|
self.logger.debug("Time series for [%s] is [%s]", sensor_name, response.json()) |
|
|
return response.json()["queries"][0]["results"][0]["values"] |
|
|
return response.json()["queries"][0]["results"][0]["values"] |
|
|
else: |
|
|
else: |
|
|
self.logger.warning("Failed to fetch time series for [%s_%s]. Response [%s]", type, id, response) |
|
|
|
|
|
|
|
|
self.logger.warning("Failed to fetch time series for [%s]. Response [%s]", sensor_name, response) |
|
|
|
|
|
|
|
|
def query_log(self, filename, value_type): |
|
|
def query_log(self, filename, value_type): |
|
|
array = [] |
|
|
array = [] |
|
|
@@ -114,19 +110,19 @@ class LogView(FlaskView): |
|
|
pass |
|
|
pass |
|
|
return array |
|
|
return array |
|
|
|
|
|
|
|
|
def read_log_as_json(self, type, id): |
|
|
|
|
|
|
|
|
def read_log_as_json(self, sensor_name): |
|
|
use_kairosdb = (cbpi.cache["config"]["kairos_db"].__dict__["value"] == "YES") |
|
|
use_kairosdb = (cbpi.cache["config"]["kairos_db"].__dict__["value"] == "YES") |
|
|
|
|
|
|
|
|
if use_kairosdb: |
|
|
if use_kairosdb: |
|
|
return self.query_tsdb(type, id) |
|
|
|
|
|
|
|
|
return self.query_tsdb(sensor_name) |
|
|
else: |
|
|
else: |
|
|
filename = "./logs/%s_%s.log" % (type, id) |
|
|
|
|
|
|
|
|
filename = "./logs/%s.log" % sensor_name |
|
|
return self.query_log(filename, "float") |
|
|
return self.query_log(filename, "float") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def convert_chart_data_to_json(self, chart_data): |
|
|
def convert_chart_data_to_json(self, chart_data): |
|
|
return {"name": chart_data["name"], |
|
|
return {"name": chart_data["name"], |
|
|
"data": self.read_log_as_json(chart_data["data_type"], chart_data["data_id"])} |
|
|
|
|
|
|
|
|
"data": self.read_log_as_json(chart_data["data_type"] + "_" + str(chart_data["data_id"]))} |
|
|
|
|
|
|
|
|
@route('/<t>/<int:id>', methods=["POST"]) |
|
|
@route('/<t>/<int:id>', methods=["POST"]) |
|
|
def get_logs_as_json(self, t, id): |
|
|
def get_logs_as_json(self, t, id): |
|
|
@@ -134,7 +130,8 @@ class LogView(FlaskView): |
|
|
result = [] |
|
|
result = [] |
|
|
if t == "s": |
|
|
if t == "s": |
|
|
name = cbpi.cache.get("sensors").get(id).name |
|
|
name = cbpi.cache.get("sensors").get(id).name |
|
|
result.append({"name": name, "data": self.read_log_as_json("sensor", id)}) |
|
|
|
|
|
|
|
|
sensor_name = "%s_%s" % ("sensor", str(id)) |
|
|
|
|
|
result.append({"name": name, "data": self.read_log_as_json(sensor_name)}) |
|
|
|
|
|
|
|
|
if t == "k": |
|
|
if t == "k": |
|
|
kettle = cbpi.cache.get("kettle").get(id) |
|
|
kettle = cbpi.cache.get("kettle").get(id) |
|
|
|