summaryrefslogtreecommitdiff
path: root/Source/WebInspectorUI/UserInterface/TimelineOverview.js
blob: a17eb83e7a614fa31ac03c785cb4bfa2414e8e61 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 * Copyright (C) 2013 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

WebInspector.TimelineOverview = function(dataSource, recordTypes)
{
    WebInspector.Object.call(this);

    console.assert(dataSource);
    console.assert(recordTypes instanceof Array);

    this._element = document.createElement("div");
    this._element.className = WebInspector.TimelineOverview.StyleClassName;

    this._recordTypes = recordTypes;

    this._timelineDecorations = new WebInspector.TimelineDecorations;

    this._timelineElements = [];
    for (var i = 0; i < this._recordTypes.length; ++i) {
        var timelineElement = document.createElement("div");
        timelineElement.classList.add(WebInspector.TimelineOverview.TimelineElementStyleClassName);
        timelineElement.classList.add(this._recordTypes[i]);
        this._element.appendChild(timelineElement);

        var barContainerElement = document.createElement("div");
        barContainerElement.className = WebInspector.TimelineOverview.BarContainerElementStyleClassName;
        timelineElement.appendChild(barContainerElement);
        timelineElement._barContainerElement = barContainerElement;

        this._timelineElements.push(timelineElement);
    }

    this._element.appendChild(this._timelineDecorations.element);

    this._dataSource = dataSource;
};

WebInspector.TimelineOverview.StyleClassName = "timeline-overview";
WebInspector.TimelineOverview.TimelineElementStyleClassName = "timeline";
WebInspector.TimelineOverview.BarContainerElementStyleClassName = "bar-container";
WebInspector.TimelineOverview.BarElementStyleClassName = "bar";
WebInspector.TimelineOverview.WaitingBarElementStyleClassName = "waiting";

WebInspector.TimelineOverview.prototype = {
    constructor: WebInspector.TimelineOverview,

    // Public

    get element()
    {
        return this._element;
    },

    get recordTypes()
    {
        return this._recordTypes;
    },

    clear: function()
    {
        this._minimumBoundary = NaN;
        this._maximumBoundary = NaN;
        this._boundarySpan = NaN;

        this._timelineDecorations.clear();
        this._timelineDecorations.updateHeaderTimes(this._boundarySpan);

        for (var i = 0; i < this._timelineElements.length; ++i)
            this._timelineElements[i]._barContainerElement.removeChildren();
    },

    update: function()
    {
        var minimumBoundary = NaN;
        var maximumBoundary = NaN;

        var records = [];
        for (var i = 0; i < this._recordTypes.length; ++i)
            records.push(this._dataSource.timelineOverviewRecordsWithType(this._recordTypes[i]));

        for (var i = 0; i < records.length; ++i) {
            var currentRecords = records[i];
            if (!currentRecords.length)
                continue;

            // The first record can be assumed to be the oldest start time.
            var firstRecord = currentRecords[0];
            if (isNaN(minimumBoundary) || firstRecord.startTime < minimumBoundary)
                minimumBoundary = firstRecord.startTime;

            if (this._recordTypes[i] === WebInspector.TimelineRecord.Type.Network) {
                // For the Network timeline we need to look at all the records since
                // long loading resources can be anywhere, not just at the end. The other
                // timelines are static, so the last record is always the newest.
                for (var j = currentRecords.length - 1; j >= 0; --j) {
                    var record = currentRecords[j];
                    if (isNaN(maximumBoundary) || record.endTime > maximumBoundary)
                        maximumBoundary = record.endTime;
                }
            } else {
                // The newest record will always be at the end.
                var lastRecord = currentRecords.lastValue;
                if (isNaN(maximumBoundary) || lastRecord.endTime > maximumBoundary)
                    maximumBoundary = lastRecord.endTime;
            }
        }

        this._minimumBoundary = minimumBoundary;
        this._maximumBoundary = maximumBoundary;
        this._boundarySpan = maximumBoundary - minimumBoundary;

        this._timelineDecorations.updateHeaderTimes(this._boundarySpan);
        this._timelineDecorations.updateEventMarkers(this._minimumBoundary, this._maximumBoundary);

        for (var i = 0; i < records.length; ++i)
            this._updateTimelineBars(this._timelineElements[i], this._recordTypes[i], records[i]);
    },

    updateLayout: function()
    {
        this._timelineDecorations.updateHeaderTimes(this._boundarySpan);
    },

    addTimelineEventMarker: function(eventMarker)
    {
        this._timelineDecorations.addTimelineEventMarker(eventMarker);
    },

    // Private

    _updateTimelineBars: function(timelineElement, type, records)
    {
        function computeActivePercentages(record)
        {
            // Compute percentages for endTime and the start of activeDuration.

            var activeStartTime = record.endTime - record.activeDuration;
            if (isNaN(activeStartTime))
                activeStartTime = record.endTime;

            if (isNaN(activeStartTime))
                var start = 0;
            else
                var start = ((activeStartTime - this._minimumBoundary) / this._boundarySpan) * 100;

            if (isNaN(record.endTime))
                var end = 100;
            else
                var end = ((record.endTime - this._minimumBoundary) / this._boundarySpan) * 100;

            return {start: start, end: end};
        }

        function computePercentages(record)
        {
            // Compute percentages for startTime and endTime.

            if (isNaN(record.startTime))
                var start = 0;
            else
                var start = ((record.startTime - this._minimumBoundary) / this._boundarySpan) * 100;

            if (isNaN(record.endTime))
                var end = 100;
            else
                var end = ((record.endTime - this._minimumBoundary) / this._boundarySpan) * 100;

            return {start: start, end: end};
        }

        function addBar(startPercentage, endPercentage, extraBarStyleClass)
        {
            var barElement = document.createElement("div");
            barElement.className = WebInspector.TimelineOverview.BarElementStyleClassName;
            if (extraBarStyleClass)
                barElement.classList.add(extraBarStyleClass);

            if (startPercentage === 0 || startPercentage !== endPercentage)
                barElement.style.left = startPercentage + "%";
            barElement.style.right = (100 - endPercentage) + "%";

            timelineElement._barContainerElement.appendChild(barElement);
        }

        function createBars(percentageCalculator, extraBarStyleClass)
        {
            // Iterate over the records and find the percentage distribution on the graph.
            // The percentages are recorded in a sparse array from 0-100.

            var timelineSlots = new Array(101);
            for (var i = 0; i < records.length; ++i) {
                var percentages = percentageCalculator.call(this, records[i]);
                var end = Math.round(percentages.end);
                for (var j = Math.round(percentages.start); j <= end; ++j)
                    timelineSlots[j] = true;
            }

            var barStart = NaN;
            for (var i = 0; i < timelineSlots.length; ++i) {
                if (timelineSlots[i]) {
                    if (isNaN(barStart))
                        barStart = i;
                } else {
                    if (!isNaN(barStart)) {
                        addBar.call(this, barStart, i, extraBarStyleClass);
                        barStart = NaN;
                    }
                }
            }

            if (!isNaN(barStart)) {
                addBar.call(this, barStart, 100, extraBarStyleClass);
                barStart = NaN;
            }
        }

        timelineElement._barContainerElement.removeChildren();

        // The Network timeline shows two bars. The first is the total time (waiting for the resource until it finishes).
        // The second bar is the active download time (first first response to finish.)
        if (type === WebInspector.TimelineRecord.Type.Network) {
            createBars.call(this, computePercentages, WebInspector.TimelineOverview.WaitingBarElementStyleClassName);
            createBars.call(this, computeActivePercentages);
        } else
            createBars.call(this, computePercentages);
    }
};

WebInspector.TimelineOverview.prototype.__proto__ = WebInspector.Object.prototype;