懒人记时 代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 regels
5.6 KiB

2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
  1. # python对多文件编程及其不友好, 白写了这么多文件到处报错, 最后还是要写在一个文件里解决了
  2. import os
  3. import sys
  4. import json
  5. from datetime import datetime, timedelta # 处理时间
  6. from flask import Flask, request
  7. from flask_cors import CORS, cross_origin
  8. from flask_sqlalchemy import SQLAlchemy
  9. app = Flask(__name__)
  10. cors = CORS(app)
  11. app.config['CORS_HEADERS'] = 'Content-Type'
  12. prefix = "sqlite:///"
  13. path = '../appTime/AppTime-Realease/data.db'
  14. app.config['SQLALCHEMY_DATABASE_URI'] = prefix + path
  15. app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
  16. # 在扩展类实例化前加载配置
  17. db = SQLAlchemy(app)
  18. # 数据库模型
  19. class App(db.Model):
  20. __tablename__ = 'app'
  21. id = db.Column(db.Integer, primary_key=True) # App id
  22. process = db.Column(db.Text) # appName
  23. text = db.Column(db.Text) # app窗口名称
  24. tagId = db.Column(db.Integer, unique=True) # 标签id
  25. class Period(db.Model):
  26. __tablename__ = 'period'
  27. timeStart = db.Column(db.DateTime, primary_key=True) # 开始时间
  28. timeEnd = db.Column(db.DateTime) # 结束时间
  29. winId = db.Column(db.Integer) # 活动id
  30. class Tag(db.Model):
  31. __tablename__ = 'tag'
  32. id = db.Column(db.Integer, primary_key=True) # 标签id
  33. text = db.Column(db.Text) # 标签文本
  34. class Win(db.Model):
  35. __tablename__ = 'win'
  36. id = db.Column(db.Integer, primary_key=True) # 活动id
  37. appId = db.Column(db.Integer) # App id
  38. text = db.Column(db.Text, nullable=True) # 活动窗口名称
  39. tagId = db.Column(db.Integer, nullable=True) # 标签id
  40. # 后端API
  41. # 获取每日屏幕使用总时间
  42. @app.route('/getDayScreenUseTime', methods=['POST'])
  43. @cross_origin()
  44. def getDayScreenUseTime():
  45. datetimeStr1 = json.loads(request.data)
  46. datetimeStr = datetimeStr1['datetimeStr']
  47. struct_time = datetime.strptime(datetimeStr, "%Y-%m-%d").date() # 获取请求日期, struct_time数据类型为dateTime
  48. activityTimeList = Period.query.all()
  49. selectedTimeList = [] # 选中的对应今天日期的时间
  50. for time in activityTimeList:
  51. if time.timeStart.date() == struct_time and time.timeEnd.date() == struct_time:
  52. selectedTimeList.append(time)
  53. if (len(selectedTimeList) == 0):
  54. sendJson = {'getDayScreenUseTimeH': 0, 'getDayScreenUseTimeM': 0, 'getDayScreenUseTimeS': 0,
  55. 'firstScreenTime': '', 'lastScreenTime': '', 'getScreenTimeSpanH': 0, 'getScreenTimeSpanM': 0,
  56. 'getScreenTimeSpanS': 0}
  57. else:
  58. timeAmount = 0
  59. for time in selectedTimeList:
  60. if time.timeEnd.date() == time.timeStart.date():
  61. time_diff = time.timeEnd - time.timeStart
  62. timeAmount = timeAmount + time_diff.total_seconds()
  63. timeAmount = int(timeAmount)
  64. getDayScreenUseTimeM, getDayScreenUseTimeS = divmod(timeAmount, 60) # 每日屏幕使用时长, 分时秒
  65. getDayScreenUseTimeH, getDayScreenUseTimeM = divmod(getDayScreenUseTimeM, 60)
  66. firstScreenTime = selectedTimeList[0].timeStart.strftime('%H时%M分%S秒') # 第一次屏幕使用时刻,
  67. lastScreenTime = selectedTimeList[-1].timeEnd.strftime('%H时%M分%S秒') # 最后一次屏幕使用时刻
  68. screenTimeSpan = int((selectedTimeList[-1].timeEnd - selectedTimeList[0].timeStart).total_seconds()) # 持续时间
  69. getScreenTimeSpanM, getScreenTimeSpanS = divmod(screenTimeSpan, 60) # 每日屏幕使用时长, 分时秒
  70. getScreenTimeSpanH, getScreenTimeSpanM = divmod(getScreenTimeSpanM, 60)
  71. sendJson = {'getDayScreenUseTimeH': getDayScreenUseTimeH, 'getDayScreenUseTimeM': getDayScreenUseTimeM,
  72. 'getDayScreenUseTimeS': getDayScreenUseTimeS, 'firstScreenTime': firstScreenTime,
  73. 'lastScreenTime': lastScreenTime, 'getScreenTimeSpanH': getScreenTimeSpanH,
  74. 'getScreenTimeSpanM': getScreenTimeSpanM, 'getScreenTimeSpanS': getScreenTimeSpanS}
  75. return json.dumps(sendJson, indent=4)
  76. @app.route('/getDayScreenUse', methods=['POST'])
  77. @cross_origin()
  78. def getDayScreenUse():
  79. datetimeStr1 = json.loads(request.data)
  80. datetimeStr = datetimeStr1['datetimeStr']
  81. struct_time = datetime.strptime(datetimeStr, "%Y-%m-%d").date() # dateTime数据类型
  82. struct_time_tomorrow = struct_time + timedelta(days=1)
  83. # 连接多张表
  84. joinTables = Period.query.filter(Period.timeStart > struct_time).filter(
  85. Period.timeStart < struct_time_tomorrow).filter(Period.timeEnd > struct_time).filter(
  86. Period.timeEnd < struct_time_tomorrow).all()
  87. resultTables = []
  88. for i in joinTables:
  89. sampleDict = {}
  90. sampleDict['timeStart'] = i.timeStart.strftime('%H:%M:%S')
  91. sampleDict['timeEnd'] = i.timeEnd.strftime('%H:%M:%S')
  92. sampleDict['totolSeconds'] = (i.timeEnd - i.timeStart).seconds
  93. queryWin = Win.query.filter(Win.id == i.winId).first()
  94. sampleDict['activity'] = queryWin.text
  95. sampleDict['appId'] = queryWin.appId
  96. queryApp = App.query.filter(queryWin.appId == App.id).first()
  97. sampleDict['appName'] = queryApp.text
  98. queryTag = Tag.query.filter(queryApp.tagId == Tag.id).first()
  99. sampleDict['tag'] = queryTag.text
  100. resultTables.append(sampleDict)
  101. returnJson = json.dumps(resultTables)
  102. return returnJson
  103. @app.route('/getAppId', methods=['POST'])
  104. @cross_origin()
  105. def getAppId():
  106. queryAppId = App.query.all()
  107. resultTables = []
  108. for i in queryAppId:
  109. sampleDict = {}
  110. sampleDict['id'] = i.id
  111. sampleDict['text'] = i.text
  112. resultTables.append(sampleDict)
  113. return json.dumps(resultTables)