Commit 4b3de36a authored by Mark Florian's avatar Mark Florian

Seed history if it doesn't exist

parent 3a7a35fb
......@@ -3,6 +3,41 @@
const fs = require('fs');
const log = require('./log');
const ESSENTIAL_ENTRY_POINTS = [
// Login page
'pages.sessions.new',
// Explore page
'pages.root',
];
// TODO: Find a way to keep this list up-to-date/relevant.
const COMMON_ENTRY_POINTS = [
...ESSENTIAL_ENTRY_POINTS,
'pages.admin',
'pages.admin.dashboard',
'pages.dashboard.groups.index',
'pages.dashboard.projects.index',
'pages.groups.new',
'pages.groups.show',
'pages.profiles.preferences.show',
'pages.projects.commit.show',
'pages.projects.edit',
'pages.projects.issues.index',
'pages.projects.issues.new',
'pages.projects.issues.show',
'pages.projects.jobs.show',
'pages.projects.merge_requests.index',
'pages.projects.merge_requests.show',
'pages.projects.milestones.index',
'pages.projects.new',
'pages.projects.pipelines.index',
'pages.projects.pipelines.show',
'pages.projects.settings.ci_cd.show',
'pages.projects.settings.repository.show',
'pages.projects.show',
'pages.users',
];
/**
* The History class is responsible for tracking which entry points have been
* requested, and persisting/loading the history to/from disk.
......@@ -10,18 +45,15 @@ const log = require('./log');
class History {
constructor(historyFilePath) {
this._historyFilePath = historyFilePath;
this._history = this._loadHistoryFile();
this._history = {};
this._loadHistoryFile();
}
onRequestEntryPoint(entryPoint) {
const wasVisitedRecently = this.isRecentlyVisited(entryPoint);
if (!this._history[entryPoint]) {
this._history[entryPoint] = { lastVisit: null, count: 0 };
}
this._history[entryPoint].lastVisit = Date.now();
this._history[entryPoint].count += 1;
this._addEntryPoint(entryPoint);
this._writeHistoryFile();
......@@ -40,26 +72,51 @@ class History {
// Private methods
_addEntryPoint(entryPoint) {
if (!this._history[entryPoint]) {
this._history[entryPoint] = { lastVisit: null, count: 0 };
}
this._history[entryPoint].lastVisit = Date.now();
this._history[entryPoint].count += 1;
}
_writeHistoryFile() {
try {
fs.writeFileSync(this._historyFilePath, JSON.stringify(this._history), 'utf8');
} catch (e) {
log('Warning – Could not write to history', e.message);
} catch (error) {
log('Warning – Could not write to history', error.message);
}
}
_loadHistoryFile() {
let history = {};
try {
fs.accessSync(this._historyFilePath);
} catch (e) {
// History file doesn't exist; attempt to seed it, and return early
this._seedHistory();
return;
}
// History file already exists; attempt to load its contents into memory
try {
history = JSON.parse(fs.readFileSync(this._historyFilePath, 'utf8'));
const historySize = Object.keys(history).length;
this._history = JSON.parse(fs.readFileSync(this._historyFilePath, 'utf8'));
const historySize = Object.keys(this._history).length;
log(`Successfully loaded history containing ${historySize} entry points`);
} catch (error) {
log(`Could not load history: ${error}`);
log('Could not load history', error.message);
}
}
return history;
/**
* Seeds a reasonable set of approximately the most common entry points to
* seed the history. This helps to avoid fresh GDK installs showing the
* compiling overlay too often.
*/
_seedHistory() {
log('Seeding history...');
COMMON_ENTRY_POINTS.forEach((entryPoint) => this._addEntryPoint(entryPoint));
this._writeHistoryFile();
}
}
......@@ -109,13 +166,7 @@ class HistoryWithTTL extends History {
[],
);
this._recentEntryPoints = new Set([
// Login page
'pages.sessions.new',
// Explore page
'pages.root',
...recentEntryPoints,
]);
this._recentEntryPoints = new Set([...ESSENTIAL_ENTRY_POINTS, ...recentEntryPoints]);
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment