timeline.mjs 7.05 KB
Newer Older
1 2 3 4
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import {groupBy} from './helper.mjs'

7
class Timeline {
8
  // Class:
9
  _model;
10
  // Array of #model instances:
11
  _values;
12
  // Current selection, subset of #values:
13
  _selection;
14
  _breakdown;
15

16
  constructor(model, values = [], startTime = null, endTime = null) {
17
    this._model = model;
18 19 20
    this._values = values;
    this.startTime = startTime;
    this.endTime = endTime;
21
    if (values.length > 0) {
22 23 24 25 26
      if (startTime === null) this.startTime = values[0].time;
      if (endTime === null) this.endTime = values[values.length - 1].time;
    } else {
      if (startTime === null) this.startTime = 0;
      if (endTime === null) this.endTime = 0;
27
    }
28
  }
29 30

  get model() {
31
    return this._model;
32 33
  }

34
  get all() {
35
    return this._values;
36
  }
37

38
  get selection() {
39
    return this._selection;
40
  }
41

42 43 44 45
  get selectionOrSelf() {
    return this._selection ?? this;
  }

46
  set selection(value) {
47
    this._selection = value;
48
  }
49

50
  selectTimeRange(startTime, endTime) {
51 52 53 54 55 56
    const items = this.range(startTime, endTime);
    this._selection = new Timeline(this._model, items, startTime, endTime);
  }

  clearSelection() {
    this._selection = undefined;
57
  }
58

59
  getChunks(windowSizeMs) {
60 61
    return this.chunkSizes(windowSizeMs);
  }
62

63
  get values() {
64
    return this._values;
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  }

  count(filter) {
    return this.all.reduce((sum, each) => {
      return sum + (filter(each) === true ? 1 : 0);
    }, 0);
  }

  filter(predicate) {
    return this.all.filter(predicate);
  }

  push(event) {
    let time = event.time;
    if (!this.isEmpty() && this.last().time > time) {
      // Invalid insertion order, might happen without --single-process,
      // finding insertion point.
      let insertionPoint = this.find(time);
83
      this._values.splice(insertionPoint, event);
84
    } else {
85
      this._values.push(event);
86 87 88 89 90 91 92 93 94 95 96 97
    }
    if (time > 0) {
      this.endTime = Math.max(this.endTime, time);
      if (this.startTime === 0) {
        this.startTime = time;
      } else {
        this.startTime = Math.min(this.startTime, time);
      }
    }
  }

  at(index) {
98
    return this._values[index];
99 100 101 102 103 104 105
  }

  isEmpty() {
    return this.size() === 0;
  }

  size() {
106
    return this._values.length;
107 108
  }

109 110 111 112
  get length() {
    return this._values.length;
  }

113 114 115 116
  slice(startIndex, endIndex) {
    return this._values.slice(startIndex, endIndex);
  }

117
  first() {
118
    return this._values[0];
119 120 121
  }

  last() {
122
    return this._values[this._values.length - 1];
123 124
  }

125 126 127 128
  * [Symbol.iterator]() {
    yield* this._values;
  }

129
  duration() {
130
    return this.endTime - this.startTime;
131 132 133
  }

  forEachChunkSize(count, fn) {
134
    if (this.isEmpty()) return;
135
    const increment = this.duration() / count;
136
    let currentTime = this.startTime;
137
    let index = 0;
138
    for (let i = 0; i < count - 1; i++) {
139
      const nextTime = currentTime + increment;
140
      const nextIndex = this.findLast(nextTime, index);
141
      fn(index, nextIndex, currentTime, nextTime);
142
      index = nextIndex + 1;
143 144
      currentTime = nextTime;
    }
145
    fn(index, this._values.length - 1, currentTime, this.endTime);
146 147 148
  }

  chunkSizes(count) {
149
    const chunks = [];
150 151 152 153
    this.forEachChunkSize(count, (start, end) => chunks.push(end - start));
    return chunks;
  }

154 155
  chunks(count, predicate = undefined) {
    const chunks = [];
156
    this.forEachChunkSize(count, (start, end, startTime, endTime) => {
157
      let items = this._values.slice(start, end + 1);
158
      if (predicate !== undefined) items = items.filter(predicate);
159 160 161 162 163
      chunks.push(new Chunk(chunks.length, startTime, endTime, items));
    });
    return chunks;
  }

164 165 166 167 168 169
  // Return all entries in ({startTime}, {endTime}]
  range(startTime, endTime) {
    const firstIndex = this.find(startTime);
    if (firstIndex < 0) return [];
    const lastIndex = this.find(endTime, firstIndex + 1);
    return this._values.slice(firstIndex, lastIndex);
170 171
  }

172
  // Return the first index with element.time >= time.
173
  find(time, offset = 0) {
174 175 176 177
    return this.findFirst(time, offset);
  }

  findFirst(time, offset = 0) {
178
    return this._find(this._values, each => each.time - time, offset);
179 180
  }

181 182 183 184 185 186 187 188 189 190 191 192 193 194
  // Return the last index with element.time <= time.
  findLast(time, offset = 0) {
    const nextTime = time + 1;
    let index = (this.last().time <= nextTime) ?
        this.length :
        this.findFirst(nextTime + 1, offset);
    // Typically we just have to look at the previous element.
    while (index > 0) {
      index--;
      if (this._values[index].time <= time) return index;
    }
    return -1;
  }

195 196 197 198 199 200 201 202
  // Return the first index for which compareFn(item) is >= 0;
  _find(array, compareFn, offset = 0) {
    let minIndex = offset;
    let maxIndex = array.length - 1;
    while (minIndex < maxIndex) {
      const midIndex = minIndex + (((maxIndex - minIndex) / 2) | 0);
      if (compareFn(array[midIndex]) < 0) {
        minIndex = midIndex + 1;
203
      } else {
204
        maxIndex = midIndex;
205 206
      }
    }
207
    return minIndex;
208 209
  }

210 211 212 213 214 215 216
  getBreakdown(keyFunction, collect = false) {
    if (keyFunction || collect) {
      if (!keyFunction) {
        keyFunction = each => each.type;
      }
      return groupBy(this._values, keyFunction, collect);
    }
217
    if (this._breakdown === undefined) {
218
      this._breakdown = groupBy(this._values, each => each.type);
219
    }
220
    return this._breakdown;
221 222
  }

223
  depthHistogram() {
224
    return this._values.histogram(each => each.depth);
225 226 227
  }

  fanOutHistogram() {
228
    return this._values.histogram(each => each.children.length);
229 230 231
  }

  forEach(fn) {
232
    return this._values.forEach(fn);
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
  }
}

// ===========================================================================
class Chunk {
  constructor(index, start, end, items) {
    this.index = index;
    this.start = start;
    this.end = end;
    this.items = items;
    this.height = 0;
  }

  isEmpty() {
    return this.items.length === 0;
  }

  last() {
    return this.at(this.size() - 1);
  }

  first() {
    return this.at(0);
  }

  at(index) {
    return this.items[index];
  }

  size() {
    return this.items.length;
  }

266 267 268 269
  get length() {
    return this.items.length;
  }

270 271 272
  yOffset(event) {
    // items[0]   == oldest event, displayed at the top of the chunk
    // items[n-1] == youngest event, displayed at the bottom of the chunk
273
    return ((this.indexOf(event) + 0.5) / this.size()) * this.height;
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
  }

  indexOf(event) {
    return this.items.indexOf(event);
  }

  has(event) {
    if (this.isEmpty()) return false;
    return this.first().time <= event.time && event.time <= this.last().time;
  }

  next(chunks) {
    return this.findChunk(chunks, 1);
  }

  prev(chunks) {
    return this.findChunk(chunks, -1);
  }

  findChunk(chunks, delta) {
    let i = this.index + delta;
    let chunk = chunks[i];
    while (chunk && chunk.size() === 0) {
      i += delta;
      chunk = chunks[i];
    }
    return chunk;
  }

303
  getBreakdown(keyFunction) {
304
    return groupBy(this.items, keyFunction);
305 306
  }

307
  filter() {
308
    return this.items.filter(map => !map.parent || !this.has(map.parent));
309 310 311
  }
}

312
export {Timeline, Chunk};