懒人记时 代码仓库
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 

133 строки
5.6 KiB

# python对多文件编程及其不友好, 白写了这么多文件到处报错, 最后还是要写在一个文件里解决了
import os
import sys
import json
from datetime import datetime, timedelta # 处理时间
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):
__tablename__ = 'app'
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):
__tablename__ = 'period'
timeStart = db.Column(db.DateTime, primary_key=True) # 开始时间
timeEnd = db.Column(db.DateTime) # 结束时间
winId = db.Column(db.Integer) # 活动id
class Tag(db.Model):
__tablename__ = 'tag'
id = db.Column(db.Integer, primary_key=True) # 标签id
text = db.Column(db.Text) # 标签文本
class Win(db.Model):
__tablename__ = 'win'
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('/getDayScreenUse', methods=['POST'])
@cross_origin()
def getDayScreenUse():
datetimeStr1 = json.loads(request.data)
datetimeStr = datetimeStr1['datetimeStr']
struct_time = datetime.strptime(datetimeStr, "%Y-%m-%d").date() # dateTime数据类型
struct_time_tomorrow = struct_time + timedelta(days=1)
# 连接多张表
joinTables = Period.query.filter(Period.timeStart > struct_time).filter(
Period.timeStart < struct_time_tomorrow).filter(Period.timeEnd > struct_time).filter(
Period.timeEnd < struct_time_tomorrow).all()
resultTables = []
for i in joinTables:
sampleDict = {}
sampleDict['timeStart'] = i.timeStart.strftime('%H:%M:%S')
sampleDict['timeEnd'] = i.timeEnd.strftime('%H:%M:%S')
sampleDict['totolSeconds'] = (i.timeEnd - i.timeStart).seconds
queryWin = Win.query.filter(Win.id == i.winId).first()
sampleDict['activity'] = queryWin.text
sampleDict['appId'] = queryWin.appId
queryApp = App.query.filter(queryWin.appId == App.id).first()
sampleDict['appName'] = queryApp.text
queryTag = Tag.query.filter(queryApp.tagId == Tag.id).first()
sampleDict['tag'] = queryTag.text
resultTables.append(sampleDict)
returnJson = json.dumps(resultTables)
return returnJson
@app.route('/getAppId', methods=['POST'])
@cross_origin()
def getAppId():
queryAppId = App.query.all()
resultTables = []
for i in queryAppId:
sampleDict = {}
sampleDict['id'] = i.id
sampleDict['text'] = i.text
resultTables.append(sampleDict)
return json.dumps(resultTables)