# all files / src/lib/traces/ devtools-timeline-model.js

92.11% Statements 35/38
75% Branches 3/4
80% Functions 8/10
92.11% Lines 35/38

    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

    1×

    1×

    5×

    5×

    5×

    5×
    5×
    5×
    5×

    5×

    5×

    5×

    5×
    5×
    5×
    5×

    5×

    5×

    1×
    1×
    1×
    1×

    2×

    2×
    2×
    2×

    2×
    2×

    1×
    1×

    1×

    1×
    1×
    1×

    1×
     /**
     * @license
     * Copyright 2016 Google Inc. All Rights Reserved.
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */

    'use strict';

    const WebInspector = require('../web-inspector');

    // Polyfill the bottom-up and topdown tree sorting.
    const TimelineModelTreeView =
        require('devtools-timeline-model/lib/timeline-model-treeview.js')(WebInspector);

    class TimelineModel {

      constructor(events) {
        this.init(events);
      }

      init(events) {
        // (devtools) tracing model
        this._tracingModel =
            new WebInspector.TracingModel(new WebInspector.TempFileBackingStorage('tracing'));
        // timeline model
        this._timelineModel =
            new WebInspector.TimelineModel(WebInspector.TimelineUIUtils.visibleEventsFilter());

        // populate with events
        this._tracingModel.reset();
        this._tracingModel.addEvents(typeof events === 'string' ? JSON.parse(events) : events);
        this._tracingModel.tracingComplete();
        this._timelineModel.setEvents(this._tracingModel);

        this._aggregator = new WebInspector.TimelineAggregator(event => {
          return WebInspector.TimelineUIUtils.eventStyle(event).category.name;
        });

        return this;
      }

      timelineModel() {
        return this._timelineModel;
      }

      tracingModel() {
        return this._tracingModel;
      }

      topDown() {
        var filters = [];
        filters.push(WebInspector.TimelineUIUtils.visibleEventsFilter());
        filters.push(new WebInspector.ExcludeTopLevelFilter());
        var nonessentialEvents = [
          WebInspector.TimelineModel.RecordType.EventDispatch,
          WebInspector.TimelineModel.RecordType.FunctionCall,
          WebInspector.TimelineModel.RecordType.TimerFire
        ];
        filters.push(new WebInspector.ExclusiveNameFilter(nonessentialEvents));

        return WebInspector.TimelineProfileTree.buildTopDown(this._timelineModel.mainThreadEvents(),
            filters, /* startTime */ 0, /* endTime */ Infinity,
            WebInspector.TimelineAggregator.eventId);
      }

      bottomUp() {
        var topDown = this.topDown();
        var noGrouping = WebInspector.TimelineAggregator.GroupBy.None;
        var noGroupAggregator = this._aggregator.groupFunction(noGrouping);
        return WebInspector.TimelineProfileTree.buildBottomUp(topDown, noGroupAggregator);
      }

     /**
      * @param  {!string} grouping Allowed values: None Category Subdomain Domain URL EventName
      * @return {!WebInspector.TimelineProfileTree.Node} A grouped and sorted tree
      */
      bottomUpGroupBy(grouping) {
        var topDown = this.topDown();

        var groupSetting = WebInspector.TimelineAggregator.GroupBy[grouping];
        var groupingAggregator = this._aggregator.groupFunction(groupSetting);
        var bottomUpGrouped =
            WebInspector.TimelineProfileTree.buildBottomUp(topDown, groupingAggregator);

        // sort the grouped tree, in-place
        new TimelineModelTreeView(bottomUpGrouped).sortingChanged('self', 'desc');
        return bottomUpGrouped;
      }

      frameModel() {
        var frameModel = new WebInspector.TracingTimelineFrameModel();
        frameModel.addTraceEvents({ /* target */ },
          this._timelineModel.inspectedTargetEvents(), this._timelineModel.sessionId() || '');
        return frameModel;
      }

      filmStripModel() {
        return new WebInspector.FilmStripModel(this._tracingModel);
      }

      interactionModel() {
        var irModel = new WebInspector.TimelineIRModel();
        irModel.populate(this._timelineModel);
        return irModel;
      }

    }

    module.exports = TimelineModel;