日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

sql求平均日活_杨学峰博客 | Flask Sqlarchemy实现按日、周、月统计并图表展示

發(fā)布時間:2024/9/3 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 sql求平均日活_杨学峰博客 | Flask Sqlarchemy实现按日、周、月统计并图表展示 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。