# -*- coding: utf-8 -*-
from pyecharts import options as opts
from pyecharts.charts import Pie
import pymysql
from snapshot_phantomjs import snapshot
from pyecharts.render import make_snapshot
import datetime
l_mysql_server="192.168.1.14"
l_user_name="root"
l_password= "123456"
l_db_name="db_admin"
l_port = 3306
sqltext = "select table_schema as dbname,round(sum(data_length+index_length)/1024/1024/1024,2) as 'dbsize' " \
"from information_schema.tables " \
"where table_schema not in ('db_admin','sys','information_schema','mysql','performance_schema') " \
"group by table_schema order by 2 desc limit 10"
def gente_dbsize_image(image_name,title) :
db = pymysql.connect(host=l_mysql_server, user=l_user_name, password=l_password, database=l_db_name,port=l_port)
cursor = db.cursor()
pie_chart = Pie(init_opts=opts.InitOpts(bg_color="#ffffff"))
data = {"dbname":[], "dbsize":[]}
l_title = title
try:
cursor.execute(sqltext)
results = cursor.fetchall()
for row in results:
data["dbname"].append(row[0])
data["dbsize"].append(row[1])
pie_chart.add("", [list(z) for z in zip(data["dbname"], data["dbsize"])])
pie_chart.set_global_opts(title_opts=opts.TitleOpts(title = l_title,
pos_left="40%",
pos_bottom = "4%",
##pos_left = 'bottom',
title_textstyle_opts=opts.TextStyleOpts(font_family="Microsoft YaHei",font_size=20)),
legend_opts = opts.LegendOpts(type_="scroll", pos_left="1%", orient='vertical', pos_top="20%",is_show=False),)
pie_chart.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}",font_size=12))
##pie_chart.render("pie_chart02.html")
make_snapshot(snapshot, pie_chart.render(), "image/" + image_name,output="jpeg")
except Exception as e:
print(e)
db.close()
return 0
if __name__ == '__main__':
now_time = datetime.datetime.now()
yes_time = now_time + datetime.timedelta(days=-1)
today = datetime.datetime.now().weekday() ##0 表示週一
image_name = "1_rds_yimiao.jpeg"
title = "my例項(GB)"
if today == 2:
gente_dbsize_image(image_name,title)