From 4b3de36a5ace1b9a3ba3ec6351da16dbbca098f8 Mon Sep 17 00:00:00 2001
From: Mark Florian <mflorian@gitlab.com>
Date: Thu, 26 Aug 2021 18:10:56 +0100
Subject: [PATCH] Seed history if it doesn't exist

---
 .../incremental_webpack_compiler/history.js   | 93 ++++++++++++++-----
 1 file changed, 72 insertions(+), 21 deletions(-)

diff --git a/config/helpers/incremental_webpack_compiler/history.js b/config/helpers/incremental_webpack_compiler/history.js
index 5ff2a5292a39a..24599900011a0 100644
--- a/config/helpers/incremental_webpack_compiler/history.js
+++ b/config/helpers/incremental_webpack_compiler/history.js
@@ -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]);
   }
 }
 
-- 
GitLab