sql求平均日活_杨学峰博客 | Flask Sqlarchemy实现按日、周、月统计并图表展示
def test():
"""統(tǒng)計用戶同步分布情況"""
from pyecharts.charts import Bar, Line, Pie
from pyecharts import options as opts
from pyecharts.globals import ThemeType
from sqlalchemy.sql import func, extract
from modules.database.models import ComputerTarget
# 根據(jù)最后同步時間計算每一天的活躍量(2020-05-15是服務(wù)給數(shù)據(jù)的時間加一天)
target_date = datetime.strptime('2020-05-15', '%Y-%m-%d')
with session_maker() as session:
stats_query = session.query(
func.date_format(ComputerTarget.LastSyncTime, '%Y-%m-%d').label('date'),
func.count(ComputerTarget.TargetID).label('count')
).filter(ComputerTarget.LastSyncTime <= target_date, ComputerTarget.OSVersion == 'CMGE V0-G').order_by('date').group_by('date').all()
date_list, count_list = [], []
for item in stats_query:
date_list.append(item.date)
count_list.append(item.count)
# 計算機日活柱狀圖
bar1 = (
Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
.add_xaxis(date_list)
.add_yaxis("", count_list)
# .set_global_opts(title_opts=opts.TitleOpts(title="計算機日活躍度統(tǒng)計"))
)
bar1.render("mycharts.html")
# 計算機日活折線圖
line1 = (
Line(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
.add_xaxis(date_list)
.add_yaxis("", count_list)
# .set_global_opts(title_opts=opts.TitleOpts(title="計算機日活躍度統(tǒng)計"))
)
line1.render("mycharts2.html")
# 計算機周活柱狀圖
week_stats = ComputerTarget.query.with_entities(
func.count(ComputerTarget.TargetID).label('count'),
extract('week', ComputerTarget.LastSyncTime).label('week')
).filter(ComputerTarget.LastSyncTime <= target_date).order_by('week').group_by('week').all()
week_list, week_count_list = [], []
for item in week_stats:
week_list.append(item.week)
week_count_list.append(item.count)
bar2 = (
Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
.add_xaxis(week_list)
.add_yaxis("周活躍數(shù)", week_count_list)
.set_global_opts(title_opts=opts.TitleOpts(title="計算機周活躍度統(tǒng)計"), tooltip_opts=opts.ToolboxOpts(is_show=True))
)
bar2.render("mycharts6.html")
# 計算機月活餅圖
month_stats = session.query(
func.date_format(ComputerTarget.LastSyncTime, '%Y年%m月').label('month'),
func.count(ComputerTarget.TargetID).label('count')
).filter(ComputerTarget.LastSyncTime <= target_date).order_by('month').group_by('month').all()
pie1 = (
Pie(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
)
pie1.add(series_name='', data_pair=list(month_stats))
pie1.render("mycharts4.html")
# 計算機月活柱狀圖
month_list, month_count_list = [], []
for item in month_stats:
month_list.append(item.month)
month_count_list.append(item.count)
bar3 = (
Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
.add_xaxis(month_list)
.add_yaxis("月活躍數(shù)", month_count_list)
.set_global_opts(title_opts=opts.TitleOpts(title="計算機月活躍度統(tǒng)計"))
)
bar3.render("mycharts5.html")
# OS餅圖統(tǒng)計
os_stats_query = session.query(
ComputerTarget.OSVersion.label('os'),
func.count(ComputerTarget.TargetID).label('count')
).filter(ComputerTarget.LastSyncTime < target_date).group_by('os').all()
pie2 = (
Pie(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
.set_global_opts(title_opts=opts.TitleOpts(title='OS餅圖統(tǒng)計'), legend_opts=opts.LegendOpts(orient='vertical', pos_left='0%', pos_top='50%'))
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}", position='inside'))
)
pie2.add(series_name='', data_pair=list(os_stats_query), center=['50%', '60%'])
pie2.render("mycharts3.html")
# OS折線圖統(tǒng)計
os_list, os_count_list = [], []
for item in os_stats_query:
os_list.append(item.os)
os_count_list.append(item.count)
(
Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
.add_xaxis(os_list)
.add_yaxis("OS數(shù)量統(tǒng)計", os_count_list)
.set_global_opts(title_opts=opts.TitleOpts(title="OS數(shù)量統(tǒng)計"))
.render("mycharts7.html")
)
return http_response()
總結(jié)
以上是生活随笔為你收集整理的sql求平均日活_杨学峰博客 | Flask Sqlarchemy实现按日、周、月统计并图表展示的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 编译安装_Unbound编译安装
- 下一篇: computed get set 传参_