Skip to content

Commit

Permalink
Remove legacy code
Browse files Browse the repository at this point in the history
  • Loading branch information
Folyd committed May 25, 2024
1 parent 98c6f74 commit d787033
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 81 deletions.
2 changes: 0 additions & 2 deletions extension/manage/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,6 @@ async function renderYearList() {
}

(async () => {
await tryMigrateLegacyStatisticsToTimeline();

const now = moment().valueOf();
const yearAgo = moment().startOf('day').subtract(1, 'year').valueOf();
await renderCharts(now, yearAgo);
Expand Down
79 changes: 0 additions & 79 deletions extension/statistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,83 +172,4 @@ class Statistics {
return search.replace(/-/gi, "_");
}
}
}

/**
* Migrate the legacy statistics storage format to timeline format.
*/
async function tryMigrateLegacyStatisticsToTimeline() {
const statistics = await storage.getItem("statistics");
if (!statistics) return;
// Those calendar, crates, hours, type data are legacy.
const { timeline = [], calendarData, cratesData, hoursData, typeData } = statistics;
// If no legacy data, we needn't migrate.
if (!calendarData) return;

const migratedData = [];

// Get the minimum timestamp in the timeline data,default is the timestamp of the current date
const minTime = timeline.reduce((pre, [time]) => {
return Math.min(pre, time)
}, timeline[0] ? timeline[0][0] : moment().valueOf());

for (let [date, value] of Object.entries(calendarData)) {
const time = moment(date).valueOf();
if (time < minTime) {
for (let i = 1; i <= value; i++) {
migratedData.push([time, null, null]);
}
}
}

/**
* Generate an array based on keys of the object and the size of each key's value.
*
* For example, unfoldObjectKeysIntoArray({"a": 3, "b": 2}) will get
* this: ["a", "a", "a", "b", "b"]
*/
function unfoldObjectKeysIntoArray(obj) {
const arr = [];
for (let [item, value] of Object.entries(obj)) {
if (value) {
for (let i = 1; i <= value; i++) {
arr.push(item);
}
}
}
return arr;
}

const typeArr = unfoldObjectKeysIntoArray(typeData);
const hoursArr = unfoldObjectKeysIntoArray(hoursData);
const cratesArr = unfoldObjectKeysIntoArray(cratesData);

migratedData.forEach((item) => {
if (hoursArr.length) {
const hourIndex = Math.floor(Math.random() * hoursArr.length)
item[0] = moment(item[0]).set('hour', hoursArr[hourIndex]).valueOf();
hoursArr.splice(hourIndex, 1);
}

if (typeArr.length) {
const typeIndex = Math.floor(Math.random() * typeArr.length);
const typeObj = STATS_PATTERNS.find(item => item.name === typeArr[typeIndex]);
if (typeObj) {
item[1] = typeObj.type
typeArr.splice(typeIndex, 1);
}
}

if (cratesArr.length) {
const cratesIndex = Math.floor(Math.random() * cratesArr.length);
item[2] = cratesArr[cratesIndex];
cratesArr.splice(cratesIndex, 1);
}
})

statistics.timeline = statistics.timeline || [];
// Prepend migrated data into timeline.
statistics.timeline.unshift(...migratedData);

await Statistics.prototype.save.apply(statistics)
}

0 comments on commit d787033

Please sign in to comment.