Welcome to mirror list, hosted at ThFree Co, Russian Federation.

history.js « incremental_webpack_compiler « helpers « config - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 24599900011a0e96d58a74098116d553da02966d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* eslint-disable max-classes-per-file, no-underscore-dangle */

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.
 */
class History {
  constructor(historyFilePath) {
    this._historyFilePath = historyFilePath;
    this._history = {};

    this._loadHistoryFile();
  }

  onRequestEntryPoint(entryPoint) {
    const wasVisitedRecently = this.isRecentlyVisited(entryPoint);

    this._addEntryPoint(entryPoint);

    this._writeHistoryFile();

    return wasVisitedRecently;
  }

  // eslint-disable-next-line class-methods-use-this
  isRecentlyVisited() {
    return true;
  }

  // eslint-disable-next-line class-methods-use-this
  get size() {
    return 0;
  }

  // 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 (error) {
      log('Warning – Could not write to history', error.message);
    }
  }

  _loadHistoryFile() {
    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 {
      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.message);
    }
  }

  /**
   * 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();
  }
}

const MS_PER_DAY = 1000 * 60 * 60 * 24;

/**
 * The HistoryWithTTL class adds LRU-like behaviour onto the base History
 * behaviour. Entry points visited within the last `ttl` days are considered
 * "recent", and therefore should be eagerly compiled.
 */
class HistoryWithTTL extends History {
  constructor(historyFilePath, ttl) {
    super(historyFilePath);
    this._ttl = ttl;
    this._calculateRecentEntryPoints();
  }

  onRequestEntryPoint(entryPoint) {
    const wasVisitedRecently = super.onRequestEntryPoint(entryPoint);

    this._calculateRecentEntryPoints();

    return wasVisitedRecently;
  }

  isRecentlyVisited(entryPoint) {
    return this._recentEntryPoints.has(entryPoint);
  }

  get size() {
    return this._recentEntryPoints.size;
  }

  // Private methods

  _calculateRecentEntryPoints() {
    const oldestVisitAllowed = Date.now() - MS_PER_DAY * this._ttl;

    const recentEntryPoints = Object.entries(this._history).reduce(
      (acc, [entryPoint, { lastVisit }]) => {
        if (lastVisit > oldestVisitAllowed) {
          acc.push(entryPoint);
        }

        return acc;
      },
      [],
    );

    this._recentEntryPoints = new Set([...ESSENTIAL_ENTRY_POINTS, ...recentEntryPoints]);
  }
}

module.exports = {
  History,
  HistoryWithTTL,
};