# -*- coding: utf-8 -*- # author:Li Mingjie time:2019/2/28 # Brief:Time Series Data Analysis import pandas as pd import matplotlib.pyplot as plt import datetime as dt import bottom_function.data_read as dr import json from flask import Flask from flask import request from flask_cors import CORS def datetime_data_plot(timetype, starttime, endtime, graphtype): gree_data = dr.read_domain_data(datatype="control", starttime=starttime, endtime=endtime) tent_data = dr.read_domain_data(datatype="application", starttime=starttime, endtime=endtime) # gree_data['datetime'] = gree_data['datetime'].apply(lambda x: dt.datetime.strftime(x, "%Y-%m-%d %H ")) # tent_data['datetime'] = tent_data['datetime'].apply(lambda x: dt.datetime.strftime(x, "%Y-%m-%d %H ")) gree_data = gree_data.set_index('datetime', drop=True) tent_data = tent_data.set_index('datetime', drop=True) mg_data = gree_data.apply(sum, axis=1) mt_data = tent_data.apply(sum, axis=1) g_data = pd.DataFrame() t_data = pd.DataFrame() all_data = pd.DataFrame() if timetype == "hour": g_data = mg_data.resample('H').sum() t_data = mt_data.resample('H').sum() all_data = pd.concat([g_data, t_data], axis=1) all_data.columns = ['control', 'application'] index_data = all_data.index.tolist() for i in range(len(index_data)): index_data[i] = dt.datetime.strftime(index_data[i], "%Y-%m-%d %H ") all_data.index = index_data if timetype == "day": g_data = mg_data.resample('D').sum() t_data = mt_data.resample('D').sum() all_data = pd.concat([g_data, t_data], axis=1) all_data.columns = ['control', 'application'] index_data = all_data.index.tolist() for i in range(len(index_data)): index_data[i] = dt.datetime.strftime(index_data[i], "%Y-%m-%d") all_data.index = index_data if timetype == "month": g_data = mg_data.resample('M').sum() t_data = mt_data.resample('M').sum() all_data = pd.concat([g_data, t_data], axis=1) all_data.columns = ['control', 'application'] index_data = all_data.index.tolist() for i in range(len(index_data)): index_data[i] = dt.datetime.strftime(index_data[i], "%Y-%m") all_data.index = index_data if timetype == "year": g_data = mg_data.resample('Y').sum() t_data = mt_data.resample('Y').sum() all_data = pd.concat([g_data, t_data], axis=1) all_data.columns = ['control', 'application'] index_data = all_data.index.tolist() for i in range(len(index_data)): index_data[i] = dt.datetime.strftime(index_data[i], "%Y") all_data.index = index_data fig = plt.figure(figsize=(16, 6)) all_data.plot(kind=graphtype, stacked=True, use_index=True) plt.xticks(rotation=45) plt.title(str(starttime) + ' to ' + str( endtime) + ' ' + timetype + ' datetime domain analysis of ' + graphtype + ' graph', fontsize=10) plt.tight_layout(5) path = '/roobo/soft/phpmyadmin/plot_time.jpg' plt.savefig(path) return path app = Flask(__name__) CORS(app, supports_credentials=True) @app.route('/SPDAS/time_series_analysis1', methods=['POST']) def domain(): param = ({"time_type": [{"value": "hour", "id": 1}, {"value": "day", "id": 2}, {"value": "month", "id": 3}, {"value": "year", "id": 4}], "time": "2018-12-01 00:00:00/2018-12-02 00:00:00", "graph_type": [{"value": "bar"}, {"value": "pie"}]}) return json.JSONEncoder().encode(param) @app.route('/SPDAS/time_series_analysis2', methods=['POST']) def domain_form(): # 需要从request对象读取表单内容: data = request.get_data() json_re = json.loads(data) print(json_re) timetype = json_re['time_type'] m_time = json_re['time'] graphtype = json_re['graph_type'] str_time = str(m_time) m_time = str_time.split('/') starttime = m_time[0] endtime = m_time[1] image_path = datetime_data_plot(timetype=timetype, starttime=starttime, endtime=endtime, graphtype=graphtype) path = ({"time_image": image_path}) return json.JSONEncoder().encode(path) if __name__ == '__main__': app.run(debug=True, host='10.7.19.129', port=5000)