Upstream version 9.37.197.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / trace_viewer / tracing / trace_model / counter_sample.js
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 'use strict';
6 tvcm.require('tvcm.iteration_helpers');
7 tvcm.require('tvcm.sorted_array_utils');
8 tvcm.require('tracing.trace_model.event');
9
10 tvcm.exportTo('tracing.trace_model', function() {
11   function CounterSample(series, timestamp, value) {
12     tracing.trace_model.Event.call(this);
13     this.series_ = series;
14     this.timestamp_ = timestamp;
15     this.value_ = value;
16   }
17
18   CounterSample.groupByTimestamp = function(samples) {
19     var samplesByTimestamp = {};
20     for (var i = 0; i < samples.length; i++) {
21       var sample = samples[i];
22       var ts = sample.timestamp;
23       if (!samplesByTimestamp[ts])
24         samplesByTimestamp[ts] = [];
25       samplesByTimestamp[ts].push(sample);
26     }
27     var timestamps = tvcm.dictionaryKeys(samplesByTimestamp);
28     timestamps.sort();
29     var groups = [];
30     for (var i = 0; i < timestamps.length; i++) {
31       var ts = timestamps[i];
32       var group = samplesByTimestamp[ts];
33       group.sort(function(x, y) {
34         return x.series.seriesIndex - y.series.seriesIndex;
35       });
36       groups.push(group);
37     }
38     return groups;
39   }
40
41   CounterSample.prototype = {
42     __proto__: tracing.trace_model.Event.prototype,
43
44     get series() {
45       return this.series_;
46     },
47
48     get timestamp() {
49       return this.timestamp_;
50     },
51
52     get value() {
53       return this.value_;
54     },
55
56     set timestamp(timestamp) {
57       this.timestamp_ = timestamp;
58     },
59
60     addBoundsToRange: function(range) {
61       range.addValue(this.timestamp);
62     },
63
64     toJSON: function() {
65       var obj = new Object();
66       var keys = Object.keys(this);
67       for (var i = 0; i < keys.length; i++) {
68         var key = keys[i];
69         if (typeof this[key] == 'function')
70           continue;
71         if (key == 'series_') {
72           obj[key] = this[key].guid;
73           continue;
74         }
75         obj[key] = this[key];
76       }
77       return obj;
78     },
79
80     getSampleIndex: function() {
81       return tvcm.findLowIndexInSortedArray(
82           this.series.timestamps,
83           function(x) { return x; },
84           this.timestamp_);
85     }
86   };
87
88   return {
89     CounterSample: CounterSample
90   };
91 });