Skip to content

Commit 01dbdad

Browse files
authoredSep 16, 2024
Improve history import performance and fix some bugs (#5666)
* Improve history import performance and fix some bugs * Make the view count optional * Add lastViewedPlaylistItemId to the optional keys * I forgot lastViewedPlaylistType
1 parent 621d518 commit 01dbdad

File tree

8 files changed

+80
-6
lines changed

8 files changed

+80
-6
lines changed
 

‎src/constants.js

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const DBActions = {
5151
},
5252

5353
HISTORY: {
54+
OVERWRITE: 'db-action-history-overwrite',
5455
UPDATE_WATCH_PROGRESS: 'db-action-history-update-watch-progress',
5556
UPDATE_PLAYLIST: 'db-action-history-update-playlist',
5657
},
@@ -78,6 +79,7 @@ const SyncEvents = {
7879
},
7980

8081
HISTORY: {
82+
OVERWRITE: 'sync-history-overwrite',
8183
UPDATE_WATCH_PROGRESS: 'sync-history-update-watch-progress',
8284
UPDATE_PLAYLIST: 'sync-history-update-playlist',
8385
},

‎src/datastores/handlers/base.js

+6
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ class History {
5656
return db.history.updateAsync({ videoId: record.videoId }, record, { upsert: true })
5757
}
5858

59+
static async overwrite(records) {
60+
await db.history.removeAsync({}, { multi: true })
61+
62+
await db.history.insertAsync(records)
63+
}
64+
5965
static updateWatchProgress(videoId, watchProgress) {
6066
return db.history.updateAsync({ videoId }, { $set: { watchProgress } }, { upsert: true })
6167
}

‎src/datastores/handlers/electron.js

+7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ class History {
3232
)
3333
}
3434

35+
static overwrite(records) {
36+
return ipcRenderer.invoke(
37+
IpcChannels.DB_HISTORY,
38+
{ action: DBActions.HISTORY.OVERWRITE, data: records }
39+
)
40+
}
41+
3542
static updateWatchProgress(videoId, watchProgress) {
3643
return ipcRenderer.invoke(
3744
IpcChannels.DB_HISTORY,

‎src/datastores/handlers/web.js

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ class History {
2929
return baseHandlers.history.upsert(record)
3030
}
3131

32+
static overwrite(records) {
33+
return baseHandlers.history.overwrite(records)
34+
}
35+
3236
static updateWatchProgress(videoId, watchProgress) {
3337
return baseHandlers.history.updateWatchProgress(videoId, watchProgress)
3438
}

‎src/main/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,15 @@ function runApp() {
10751075
)
10761076
return null
10771077

1078+
case DBActions.HISTORY.OVERWRITE:
1079+
await baseHandlers.history.overwrite(data)
1080+
syncOtherWindows(
1081+
IpcChannels.SYNC_HISTORY,
1082+
event,
1083+
{ event: SyncEvents.HISTORY.OVERWRITE, data }
1084+
)
1085+
return null
1086+
10781087
case DBActions.HISTORY.UPDATE_WATCH_PROGRESS:
10791088
await baseHandlers.history.updateWatchProgress(data.videoId, data.watchProgress)
10801089
syncOtherWindows(

‎src/renderer/components/data-settings/data-settings.js

+19-6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ export default defineComponent({
5050
allPlaylists: function () {
5151
return this.$store.getters.getAllPlaylists
5252
},
53+
historyCacheById: function () {
54+
return this.$store.getters.getHistoryCacheById
55+
},
5356
historyCacheSorted: function () {
5457
return this.$store.getters.getHistoryCacheSorted
5558
},
@@ -616,7 +619,7 @@ export default defineComponent({
616619
})
617620
},
618621

619-
importFreeTubeHistory(textDecode) {
622+
async importFreeTubeHistory(textDecode) {
620623
textDecode.pop()
621624

622625
const requiredKeys = [
@@ -630,20 +633,24 @@ export default defineComponent({
630633
'title',
631634
'type',
632635
'videoId',
633-
'viewCount',
634636
'watchProgress',
635637
]
636638

637639
const optionalKeys = [
638640
// `_id` absent if marked as watched manually
639641
'_id',
640642
'lastViewedPlaylistId',
643+
'lastViewedPlaylistItemId',
644+
'lastViewedPlaylistType',
645+
'viewCount',
641646
]
642647

643648
const ignoredKeys = [
644649
'paid',
645650
]
646651

652+
const historyItems = new Map(Object.entries(this.historyCacheById))
653+
647654
textDecode.forEach((history) => {
648655
const historyData = JSON.parse(history)
649656
// We would technically already be done by the time the data is parsed,
@@ -667,14 +674,16 @@ export default defineComponent({
667674
showToast(this.$t('Settings.Data Settings.History object has insufficient data, skipping item'))
668675
console.error('Missing Keys: ', missingKeys, historyData)
669676
} else {
670-
this.updateHistory(historyObject)
677+
historyItems.set(historyObject.videoId, historyObject)
671678
}
672679
})
673680

681+
await this.overwriteHistory(historyItems)
682+
674683
showToast(this.$t('Settings.Data Settings.All watched history has been successfully imported'))
675684
},
676685

677-
importYouTubeHistory(historyData) {
686+
async importYouTubeHistory(historyData) {
678687
const filterPredicate = item =>
679688
item.products.includes('YouTube') &&
680689
item.titleUrl != null && // removed video doesnt contain url...
@@ -722,6 +731,8 @@ export default defineComponent({
722731
'activityControls',
723732
].concat(Object.keys(keyMapping))
724733

734+
const historyItems = new Map(Object.entries(this.historyCacheById))
735+
725736
filteredHistoryData.forEach(element => {
726737
const historyObject = {}
727738

@@ -750,10 +761,12 @@ export default defineComponent({
750761
historyObject.watchProgress = 1
751762
historyObject.isLive = false
752763

753-
this.updateHistory(historyObject)
764+
historyItems.set(historyObject.videoId, historyObject)
754765
}
755766
})
756767

768+
await this.overwriteHistory(historyItems)
769+
757770
showToast(this.$t('Settings.Data Settings.All watched history has been successfully imported'))
758771
},
759772

@@ -1069,10 +1082,10 @@ export default defineComponent({
10691082
...mapActions([
10701083
'updateProfile',
10711084
'updateShowProgressBar',
1072-
'updateHistory',
10731085
'addPlaylist',
10741086
'addVideo',
10751087
'updatePlaylist',
1088+
'overwriteHistory'
10761089
]),
10771090

10781091
...mapMutations([

‎src/renderer/store/modules/history.js

+21
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,27 @@ const actions = {
4545
}
4646
},
4747

48+
/**
49+
* @param {any} param0
50+
* @param {Map<string, any>} historyItems
51+
*/
52+
async overwriteHistory({ commit }, historyItems) {
53+
try {
54+
const sortedRecords = Array.from(historyItems.values())
55+
56+
// sort before sending saving to the database and passing to other windows
57+
// so that the other windows can use it as is, without having to sort the array themselves
58+
sortedRecords.sort((a, b) => b.timeWatched - a.timeWatched)
59+
60+
await DBHistoryHandlers.overwrite(sortedRecords)
61+
62+
commit('setHistoryCacheSorted', sortedRecords)
63+
commit('setHistoryCacheById', Object.fromEntries(historyItems))
64+
} catch (errMessage) {
65+
console.error(errMessage)
66+
}
67+
},
68+
4869
async removeFromHistory({ commit }, videoId) {
4970
try {
5071
await DBHistoryHandlers.delete(videoId)

‎src/renderer/store/modules/settings.js

+12
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,18 @@ const customActions = {
476476
commit('upsertToHistoryCache', data)
477477
break
478478

479+
case SyncEvents.HISTORY.OVERWRITE: {
480+
const byId = {}
481+
data.forEach(video => {
482+
byId[video.videoId] = video
483+
})
484+
485+
// It comes pre-sorted, so we don't have to sort it here
486+
commit('setHistoryCacheSorted', data)
487+
commit('setHistoryCacheById', byId)
488+
break
489+
}
490+
479491
case SyncEvents.HISTORY.UPDATE_WATCH_PROGRESS:
480492
commit('updateRecordWatchProgressInHistoryCache', data)
481493
break

0 commit comments

Comments
 (0)