用户统计
需求分析
- 展示当前总人数,月活跃人数,日活跃人数
- 使用图表的形式展示活跃曲线
实现准备
- 将
static/admin/user_count.html
拖到templates/admin/
目录下
代码实现
实现思路:
- 月新增数:获取到本月第1天0点0分0秒的时间对象,然后查询最后一次登录比其大的所有数据
- 日新增数:获取到当日0点0分0秒时间对象,然后查询最后一次登录比其大的所有数据
- 图表查询:遍历查询数据每一天的数据(当前天数,减去某些天)
后端查询数据实现
@admin_blu.route('/user_count')
def user_count():
# 查询总人数
total_count = 0
try:
total_count = User.query.filter(User.is_admin == False).count()
except Exception as e:
current_app.logger.error(e)
# 查询月新增数
mon_count = 0
try:
now = time.localtime()
mon_begin = '%d-%02d-01' % (now.tm_year, now.tm_mon)
mon_begin_date = datetime.strptime(mon_begin, '%Y-%m-%d')
mon_count = User.query.filter(User.is_admin == False, User.create_time >= mon_begin_date).count()
except Exception as e:
current_app.logger.error(e)
# 查询日新增数
day_count = 0
try:
day_begin = '%d-%02d-%02d' % (now.tm_year, now.tm_mon, now.tm_mday)
day_begin_date = datetime.strptime(day_begin, '%Y-%m-%d')
day_count = User.query.filter(User.is_admin == False, User.create_time > day_begin_date).count()
except Exception as e:
current_app.logger.error(e)
# 查询图表信息
# 获取到当天00:00:00时间
now_date = datetime.strptime(datetime.now().strftime('%Y-%m-%d'), '%Y-%m-%d')
# 定义空数组,保存数据
active_date = []
active_count = []
# 依次添加数据,再反转
for i in range(0, 31):
begin_date = now_date - timedelta(days=i)
end_date = now_date - timedelta(days=(i - 1))
active_date.append(begin_date.strftime('%Y-%m-%d'))
count = 0
try:
count = User.query.filter(User.is_admin == False, User.last_login >= day_begin,
User.last_login < day_end).count()
except Exception as e:
current_app.logger.error(e)
active_count.append(count)
active_date.reverse()
active_count.reverse()
data = {"total_count": total_count, "mon_count": mon_count, "day_count": day_count, "active_date": active_date,
"active_count": active_count}
return render_template('admin/user_count.html', data=data)
- 修改
templates/admin/index.html
中【用户统计】链接
<li><a class="icon022" href="/admin/user_count" target="main_frame">用户统计</a></li>
- 前端页面渲染数据实现
<div class="spannels">
<div class="spannel scolor01">
<em>{{ data.total_count }}</em><span>人</span>
<b>用户总数</b>
</div>
<div class="spannel scolor02">
<em>{{ data.mon_count }}</em><span>人</span>
<b>用户月新增数</b>
</div>
<div class="spannel2 scolor03">
<em>{{ data.day_count }}</em><span>人</span>
<b>用户日新增数</b>
</div>
</div>
...
xAxis : [
{
name: '今天',
type : 'category',
boundaryGap : false,
data : {{ data.active_date | safe }}
}
],
yAxis : [
{
name: '活跃用户数量',
type : 'value'
}
],
series : [
{
name:'active',
type:'line',
smooth:true,
itemStyle: {normal: {areaStyle: {type: 'default'}, color: '#f80'}, lineStyle: {color: '#f80'}},
data: {{ data.active_count }}
}],
运行测试