-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
80 lines (61 loc) · 2.15 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from random import randrange
import threading
from flask.json import jsonify
from flask import Flask, render_template, request
from jinja2 import Markup, Environment, FileSystemLoader
from pyecharts import options as opts
from pyecharts.charts import Bar, Line
from pyecharts.globals import CurrentConfig
import ecs
import cms
CurrentConfig.GLOBAL_ENV = Environment(loader=FileSystemLoader("./templates"))
app = Flask(__name__, static_folder="templates")
def line_base(): # -> Line:
line = (
Line()
.add_xaxis(["{}".format(i) for i in range(10)])
.add_yaxis(
series_name="",
y_axis=[randrange(50, 80) for _ in range(10)],
is_smooth=True,
label_opts=opts.LabelOpts(is_show=False),
)
.set_global_opts(
title_opts=opts.TitleOpts(title="动态数据"),
xaxis_opts=opts.AxisOpts(type_="value"),
yaxis_opts=opts.AxisOpts(type_="value"),
)
)
return line
@app.route("/")
def index():
return render_template("index.html")
# from charts import build_cpu_chart
# c = build_cpu_chart('i-2ze99ni7tsf7pgz6y62e', period=120)
# return Markup(c.render_embed())
# @app.route("/lineChart")
@app.route("/instanceChart")
def get_line_chart():
c = line_base()
return c.dump_options_with_quotes()
# @app.route("/instanceChart")
@app.route("/lineChart")
def get_instance_chart():
from charts import build_cpu_chart
instanceId = request.args['machineIdOrIp']
instanceName = request.args['instanceName']
c = build_cpu_chart(instanceId, instanceName, period=300)
return c.dump_options_with_quotes()
@app.route("/aliyun/ecs/instances0")
def get_aliyun_ecs_instances0():
instances = ecs.get_instances()
return jsonify(instances)
@app.route("/aliyun/ecs/instances")
def get_aliyun_ecs_instances():
instances = ecs.get_instances()
metrics = cms.get_metric_infos([ins['InstanceId'] for ins in instances['Instances']], top_n=1)
for instance in instances['Instances']:
instance["topN_metric"] = metrics[instance['InstanceId']]
return jsonify(instances)
if __name__ == "__main__":
app.run()