|
|
- # python对多文件编程及其不友好, 白写了这么多文件到处报错, 最后还是要写在一个文件里解决了
- import os
- import sys
- import json
- from datetime import datetime # 处理时间
- from flask import Flask, request
- from flask_cors import CORS, cross_origin
- from flask_sqlalchemy import SQLAlchemy
-
- app = Flask(__name__)
- cors = CORS(app)
- app.config['CORS_HEADERS'] = 'Content-Type'
-
- prefix = "sqlite:///"
- path = '../appTime/AppTime-Realease/data.db'
- app.config['SQLALCHEMY_DATABASE_URI'] = prefix + path
- app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
- # 在扩展类实例化前加载配置
- db = SQLAlchemy(app)
-
-
- # 数据库模型
- class App(db.Model):
- id = db.Column(db.Integer, primary_key=True) # App id
- process = db.Column(db.Text) # appName
- text = db.Column(db.Text) # app窗口名称
- tagId = db.Column(db.Integer, unique=True) # 标签id
-
-
- class Period(db.Model):
- timeStart = db.Column(db.DateTime, primary_key=True) # 开始时间
- timeEnd = db.Column(db.DateTime) # 结束时间
- winId = db.Column(db.Integer) # 活动id
-
-
- class Tag(db.Model):
- id = db.Column(db.Integer, primary_key=True) # 标签id
- text = db.Column(db.Text) # 标签文本
-
-
- class Win(db.Model):
- id = db.Column(db.Integer, primary_key=True) # 活动id
- appId = db.Column(db.Integer) # App id
- text = db.Column(db.Text, nullable=True) # 活动窗口名称
- tagId = db.Column(db.Integer, nullable=True) # 标签id
-
-
- # 后端API
- # 获取每日屏幕使用总时间
- @app.route('/getDayScreenUseTime', methods=['POST'])
- @cross_origin()
- def getDayScreenUseTime():
- datetimeStr1 = json.loads(request.data)
- datetimeStr = datetimeStr1['datetimeStr']
- struct_time = datetime.strptime(datetimeStr, "%Y-%m-%d").date() # 获取请求日期, struct_time数据类型为dateTime
- activityTimeList = Period.query.all()
- selectedTimeList = [] # 选中的对应今天日期的时间
- for time in activityTimeList:
- if time.timeStart.date() == struct_time and time.timeEnd.date() == struct_time:
- selectedTimeList.append(time)
-
- if (len(selectedTimeList) == 0):
- sendJson = {'getDayScreenUseTimeH': 0, 'getDayScreenUseTimeM': 0, 'getDayScreenUseTimeS': 0, 'firstScreenTime': '', 'lastScreenTime': '', 'getScreenTimeSpanH': 0, 'getScreenTimeSpanM':0, 'getScreenTimeSpanS': 0 }
- else:
- timeAmount = 0
- for time in selectedTimeList:
- if time.timeEnd.date() == time.timeStart.date():
- time_diff = time.timeEnd - time.timeStart
- timeAmount = timeAmount + time_diff.total_seconds()
-
- timeAmount = int(timeAmount)
- getDayScreenUseTimeM, getDayScreenUseTimeS = divmod(timeAmount, 60) # 每日屏幕使用时长, 分时秒
- getDayScreenUseTimeH, getDayScreenUseTimeM = divmod(getDayScreenUseTimeM, 60)
- firstScreenTime = selectedTimeList[0].timeStart.strftime('%H时%M分%S秒') # 第一次屏幕使用时刻,
- lastScreenTime = selectedTimeList[-1].timeEnd.strftime('%H时%M分%S秒') # 最后一次屏幕使用时刻
- screenTimeSpan = int((selectedTimeList[-1].timeEnd - selectedTimeList[0].timeStart).total_seconds()) # 持续时间
- getScreenTimeSpanM, getScreenTimeSpanS = divmod(screenTimeSpan, 60) # 每日屏幕使用时长, 分时秒
- getScreenTimeSpanH, getScreenTimeSpanM = divmod(getScreenTimeSpanM, 60)
- sendJson = {'getDayScreenUseTimeH': getDayScreenUseTimeH, 'getDayScreenUseTimeM': getDayScreenUseTimeM, 'getDayScreenUseTimeS': getDayScreenUseTimeS, 'firstScreenTime': firstScreenTime, 'lastScreenTime': lastScreenTime, 'getScreenTimeSpanH': getScreenTimeSpanH, 'getScreenTimeSpanM': getScreenTimeSpanM, 'getScreenTimeSpanS': getScreenTimeSpanS }
- return json.dumps(sendJson, indent=4)
-
- @app.route('/getDayScreenUseTime', methods=['POST'])
- @cross_origin()
- def getDayScreenUseTime():
- datetimeStr1 = json.loads(request.data)
- datetimeStr = datetimeStr1['datetimeStr']
- struct_time = datetime.strptime(datetimeStr, "%Y-%m-%d").date() # 获取请求日期, struct_time数据类型为dateTime
- return "1"
|