Skip to content

Commit 8d79124

Browse files
committed
perf(database): remove records older than 12 hours
1 parent 267658c commit 8d79124

File tree

6 files changed

+61
-58
lines changed

6 files changed

+61
-58
lines changed

app/common/RouterInfo.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ def get_month_statistics_dic(self):
481481
return self.month_statistics_dic
482482

483483
def get_battery_history_dic(self):
484+
self.db.remove_battery_history()
484485
battery_history_list = self.db.read_battery_history()
485486
battery_history_dic = {
486487
"time": [x[0] for x in battery_history_list],

app/common/config.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ class Config(QConfig):
3434
""" Config of application """
3535

3636
# folders
37-
musicFolders = ConfigItem(
38-
"Folders", "LocalMusic", [], FolderListValidator())
39-
downloadFolder = ConfigItem(
40-
"Folders", "Download", "app/download", FolderValidator())
37+
# musicFolders = ConfigItem(
38+
# "Folders", "LocalMusic", [], FolderListValidator())
39+
# downloadFolder = ConfigItem(
40+
# "Folders", "Download", "app/download", FolderValidator())
4141

4242
# main window
4343
dpiScale = OptionsConfigItem(

app/common/database.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import time
23
import sqlite3
34
from typing import Dict
45

@@ -49,19 +50,14 @@ def write_battery_history(self, battery_history_record: Dict[int, int]):
4950

5051
def remove_battery_history(self):
5152
try:
52-
# return a list of tuple, each representing a record
53-
records = self.read_battery_history()
54-
55-
# Check if the number of records is greater than 1440
56-
if len(records) > 1440:
57-
# Get the timestamps of the latest 1440 records
58-
latest_timestamps = [record[0] for record in records]
53+
# Calculate the timestamp 12 hours ago
54+
twelve_hours_ago = int(time.time()) - (12 * 60 * 60)
5955

60-
# Delete records with timestamps not in the latest 1440
61-
self.db_cache_cursor.execute('''
62-
DELETE FROM battery WHERE timestamp NOT IN ({})
63-
'''.format(','.join('?' for _ in latest_timestamps)), latest_timestamps)
64-
self.db_cache_conn.commit()
56+
# Delete records with timestamps older than twelve_hours_ago
57+
self.db_cache_cursor.execute('''
58+
DELETE FROM battery WHERE timestamp < ?
59+
''', (twelve_hours_ago,))
60+
self.db_cache_conn.commit()
6561

6662
return True
6763
except Exception as e:

app/components/battery_history_card.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# coding:utf-8
22
from datetime import datetime
3+
from time import perf_counter
34

45
from PyQt5.QtCore import Qt
56
from PyQt5.QtWidgets import QWidget, QFrame, QLabel, QVBoxLayout, QHBoxLayout
@@ -78,28 +79,29 @@ def updateBatteryHistory(self, battery_history_dic):
7879
self.graphWidget.axes.set_facecolor(background_color)
7980
self.graphWidget.figure.patch.set_facecolor(background_color)
8081

81-
x_ticks = [int(x / 60) for x in battery_history_dic["time"]]
82+
x_list = [int(x / 60) for x in battery_history_dic["time"]]
8283
y_labels_list = [10 * x for x in range(11)]
83-
# vertical lines with color indicating charging status
84-
for x, y, charging in zip(x_ticks, battery_history_dic["battery"], battery_history_dic["charging"]):
84+
battery_history_time_list = []
85+
x_ticks = []
86+
for x, y, charging, time in zip(x_list, battery_history_dic["battery"], battery_history_dic["charging"], battery_history_dic["time"]):
87+
# vertical lines with color indicating charging status
8588
if charging:
8689
# green
8790
color = (0 / 255, 255 / 255, 54 / 255)
8891
else:
8992
# blue
9093
color = (53 / 255, 193 / 255, 241 / 255)
9194
self.graphWidget.axes.axvline(x=x, color=color, linestyle='-', linewidth=1, ymax=y / 100)
92-
self.graphWidget.axes.plot(x_ticks, battery_history_dic["battery"], color=front_color)
9395

94-
battery_history_time_list = []
95-
x_ticks = []
96-
for x in battery_history_dic["time"]:
97-
timestamp_xx00, timestamp_xx30 = self.calculate_x_ticks(x)
96+
# calculate x ticks
97+
timestamp_xx00, timestamp_xx30 = self.calculate_x_ticks(time)
9898
if timestamp_xx00 not in battery_history_dic["time"]:
9999
battery_history_time_list.append(self.convert_time(timestamp_xx00))
100100
battery_history_time_list.append(self.convert_time(timestamp_xx30))
101101
x_ticks.append(int(timestamp_xx00 / 60))
102102
x_ticks.append(int(timestamp_xx30 / 60))
103+
self.graphWidget.axes.plot(x_list, battery_history_dic["battery"], color=front_color)
104+
103105
self.graphWidget.axes.set_xticks(x_ticks)
104106
self.graphWidget.axes.set_xticklabels(battery_history_time_list, rotation=45, ha='right', color=front_color)
105107

@@ -116,7 +118,7 @@ def updateBatteryHistory(self, battery_history_dic):
116118
spine.set_visible(False)
117119
else:
118120
spine.set_edgecolor(front_color)
119-
121+
120122
self.graphWidget.draw()
121123

122124
class MplCanvas(FigureCanvasQTAgg):

app/view/setting_interface.py

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class SettingInterface(ScrollArea):
1919
""" Setting interface """
2020

2121
checkUpdateSig = pyqtSignal()
22-
musicFoldersChanged = pyqtSignal(list)
22+
# musicFoldersChanged = pyqtSignal(list)
2323
acrylicEnableChanged = pyqtSignal(bool)
24-
downloadFolderChanged = pyqtSignal(str)
24+
# downloadFolderChanged = pyqtSignal(str)
2525
minimizeToTrayChanged = pyqtSignal(bool)
2626

2727
def __init__(self, parent=None):
@@ -77,22 +77,22 @@ def __init__(self, parent=None):
7777
)
7878

7979
# music folders
80-
self.musicInThisPCGroup = SettingCardGroup(
81-
self.tr("Music on this PC"), self.scrollWidget)
82-
self.musicFolderCard = FolderListSettingCard(
83-
cfg.musicFolders,
84-
self.tr("Local music library"),
85-
directory=QStandardPaths.writableLocation(
86-
QStandardPaths.MusicLocation),
87-
parent=self.musicInThisPCGroup
88-
)
89-
self.downloadFolderCard = PushSettingCard(
90-
self.tr('Choose folder'),
91-
FIF.DOWNLOAD,
92-
self.tr("Download directory"),
93-
cfg.get(cfg.downloadFolder),
94-
self.musicInThisPCGroup
95-
)
80+
# self.musicInThisPCGroup = SettingCardGroup(
81+
# self.tr("Music on this PC"), self.scrollWidget)
82+
# self.musicFolderCard = FolderListSettingCard(
83+
# cfg.musicFolders,
84+
# self.tr("Local music library"),
85+
# directory=QStandardPaths.writableLocation(
86+
# QStandardPaths.MusicLocation),
87+
# parent=self.musicInThisPCGroup
88+
# )
89+
# self.downloadFolderCard = PushSettingCard(
90+
# self.tr('Choose folder'),
91+
# FIF.DOWNLOAD,
92+
# self.tr("Download directory"),
93+
# cfg.get(cfg.downloadFolder),
94+
# self.musicInThisPCGroup
95+
# )
9696

9797
# personalization
9898
self.personalGroup = SettingCardGroup(
@@ -224,8 +224,8 @@ def __initLayout(self):
224224
self.deviceGroup.addSettingCard(self.deviceNameCard)
225225

226226
# add cards to group
227-
self.musicInThisPCGroup.addSettingCard(self.musicFolderCard)
228-
self.musicInThisPCGroup.addSettingCard(self.downloadFolderCard)
227+
# self.musicInThisPCGroup.addSettingCard(self.musicFolderCard)
228+
# self.musicInThisPCGroup.addSettingCard(self.downloadFolderCard)
229229

230230
self.personalGroup.addSettingCard(self.themeCard)
231231
self.personalGroup.addSettingCard(self.themeColorCard)
@@ -247,7 +247,7 @@ def __initLayout(self):
247247
self.expandLayout.setContentsMargins(36, 10, 36, 0)
248248
self.expandLayout.addWidget(self.batteryGroup)
249249
self.expandLayout.addWidget(self.deviceGroup)
250-
self.expandLayout.addWidget(self.musicInThisPCGroup)
250+
# self.expandLayout.addWidget(self.musicInThisPCGroup)
251251
self.expandLayout.addWidget(self.personalGroup)
252252
self.expandLayout.addWidget(self.materialGroup)
253253
self.expandLayout.addWidget(self.updateSoftwareGroup)
@@ -263,26 +263,26 @@ def __showRestartTooltip(self):
263263
parent=self
264264
)
265265

266-
def __onDownloadFolderCardClicked(self):
267-
""" download folder card clicked slot """
268-
folder = QFileDialog.getExistingDirectory(
269-
self, self.tr("Choose folder"), "./")
270-
if not folder or cfg.get(cfg.downloadFolder) == folder:
271-
return
266+
# def __onDownloadFolderCardClicked(self):
267+
# """ download folder card clicked slot """
268+
# folder = QFileDialog.getExistingDirectory(
269+
# self, self.tr("Choose folder"), "./")
270+
# if not folder or cfg.get(cfg.downloadFolder) == folder:
271+
# return
272272

273-
cfg.set(cfg.downloadFolder, folder)
274-
self.downloadFolderCard.setContent(folder)
273+
# cfg.set(cfg.downloadFolder, folder)
274+
# self.downloadFolderCard.setContent(folder)
275275

276276
def __connectSignalToSlot(self):
277277
""" connect signal to slot """
278278
cfg.appRestartSig.connect(self.__showRestartTooltip)
279279
cfg.themeChanged.connect(setTheme)
280280

281281
# music in the pc
282-
self.musicFolderCard.folderChanged.connect(
283-
self.musicFoldersChanged)
284-
self.downloadFolderCard.clicked.connect(
285-
self.__onDownloadFolderCardClicked)
282+
# self.musicFolderCard.folderChanged.connect(
283+
# self.musicFoldersChanged)
284+
# self.downloadFolderCard.clicked.connect(
285+
# self.__onDownloadFolderCardClicked)
286286

287287
# personalization
288288
self.themeColorCard.colorChanged.connect(setThemeColor)

main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import sys
33
import asyncio
4+
from time import perf_counter
45

56
from PyQt5.QtCore import Qt, QTranslator, QTimer, QTime
67
from PyQt5.QtGui import QFont, QIcon
@@ -51,9 +52,12 @@
5152

5253
def show_window(button):
5354
if button == QSystemTrayIcon.Trigger:
55+
# t1_start = perf_counter()
5456
w.show()
5557
w.update_monitoring_status()
5658
# w.update_month_statistics()
59+
# t1_stop = perf_counter()
60+
# logger.info(f"Elapsed time during the whole program in seconds: {t1_stop-t1_start}")
5761
else:
5862
pass
5963

0 commit comments

Comments
 (0)