workbench.py 4.3 KB
Newer Older
1 2 3
'''
Author: your name
Date: 2021-07-22 04:03:24
4
LastEditTime: 2021-07-23 00:51:58
5 6 7 8
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: \evm-store\tools\build_out\controllers\workbench.py
'''
9
import datetime
10
from models.user import UserModel
11 12 13 14 15
from models.device import DeviceModel
from models.app import AppModel
from models.package import PackageModel
from webcreator.log import logger
from webcreator.response import ResponseCode
16

17 18 19 20
# 今日上传应用数 、历史应用总数
# 今日打包次数、历史打包总数
# 今日新增设备数,绑定设备数
# 今日新增开发者,开发者总数
21

22
'''
23 24
time_now = datetime.now()
#最近30天数据
25
result = UserModel.query.filter(UserModel.create_time >= time_now - timedelta(days=30)).all()
26
#最近一周数据
27
result = UserModel.query.filter(UserModel.create_time >= time_now - timedelta(days=7)).all()
28
#最近1天数据
29
result = UserModel.query.filter(UserModel.create_time >= time_now - timedelta(days=1)).all()
30
#最近12小时
31
result = UserModel.query.filter(UserModel.create_time >= time_now - timedelta(hours=12)).all()
32
#最近半小时
33
result = UserModel.query.filter(UserModel.create_time >= time_now - timedelta(seconds=30)).all()
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
'''


class WorkbenchResource(object):
    def __init__(self):
        super().__init__()

    def get(self, jwt={}):
        if not jwt:
            return None, ResponseCode.HTTP_INVAILD_REQUEST

        user = UserModel.query.filter(UserModel.uuid==jwt.get("uuid")).one_or_none()
        if not user:
            return None, ResponseCode.USER_NOT_EXISTS

        today = datetime.date.today()
        tomorrow = today + datetime.timedelta(days=1)

        logger.info(user.role)

        if user.role == 1:
            total_user = UserModel.query.filter().count()
            today_user = UserModel.query.filter(UserModel.create_at >= today.strftime("%Y-%m-%d 00:00:00"), UserModel.create_at <= tomorrow.strftime("%Y-%m-%d 00:00:00")).count()

            total_device = DeviceModel.query.filter().count()
            today_device = DeviceModel.query.filter(DeviceModel.create_at >= today.strftime("%Y-%m-%d 00:00:00"), DeviceModel.create_at <= tomorrow.strftime("%Y-%m-%d 00:00:00")).count()

            total_app = AppModel.query.filter().count()
            today_app = AppModel.query.filter(AppModel.create_at >= today.strftime("%Y-%m-%d 00:00:00"), AppModel.create_at <= tomorrow.strftime("%Y-%m-%d 00:00:00")).count()

            total_package = PackageModel.query.filter().count()
            today_package = PackageModel.query.filter(PackageModel.create_at >= today.strftime("%Y-%m-%d 00:00:00"), PackageModel.create_at <= tomorrow.strftime("%Y-%m-%d 00:00:00")).count()
        else:
            total_user = UserModel.query.filter(UserModel.create_by==user.id).count()
            today_user = UserModel.query.filter(UserModel.create_by==user.id, UserModel.create_at >= today.strftime("%Y-%m-%d 00:00:00"), UserModel.create_at <= tomorrow.strftime("%Y-%m-%d 00:00:00")).count()

            total_device = DeviceModel.query.filter(DeviceModel.create_by==user.id).count()
            today_device = DeviceModel.query.filter(DeviceModel.create_by==user.id, DeviceModel.create_at >= today.strftime("%Y-%m-%d 00:00:00"), DeviceModel.create_at <= tomorrow.strftime("%Y-%m-%d 00:00:00")).count()

            total_app = AppModel.query.filter(AppModel.create_by==user.id).count()
            today_app = AppModel.query.filter(AppModel.create_by==user.id, AppModel.create_at >= today.strftime("%Y-%m-%d 00:00:00"), AppModel.create_at <= tomorrow.strftime("%Y-%m-%d 00:00:00")).count()

            total_package = PackageModel.query.filter(PackageModel.create_by==user.id).count()
            today_package = PackageModel.query.filter(PackageModel.create_by==user.id, PackageModel.create_at >= today.strftime("%Y-%m-%d 00:00:00"), PackageModel.create_at <= tomorrow.strftime("%Y-%m-%d 00:00:00")).count()

        result = {
            'total_user': total_user,
            'today_user': today_user,
            'total_device': total_device,
            'today_device': today_device,
            'total_app': total_app,
            'today_app': today_app,
            'total_package': total_package,
            'today_package': today_package
        }

        logger.info(result)

        return result, ResponseCode.HTTP_SUCCESS


workbenchResource = WorkbenchResource()