| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2009 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 /** | |
| 6 * Represents a record in the tree status database. | |
| 7 * | |
| 8 * @param {int} timestamp Unix timestamp in milliseconds. | |
| 9 * @param {string} author | |
| 10 * @param {string} message | |
| 11 * @param {string} general_state | |
| 12 * @constructor | |
| 13 */ | |
| 14 function Entry(timestamp, author, message, general_state) { | |
| 15 this.timestamp = timestamp; | |
| 16 this.author = author; | |
| 17 this.message = message; | |
| 18 this.general_state = general_state; | |
| 19 } | |
| 20 | |
| 21 /** | |
| 22 * Gets the tree status enumeration for this entry. | |
| 23 * @return {string} One of the possible tree states. | |
| 24 * | |
| 25 * See Entry.TREE_STATES for the enumeration of possible values. | |
| 26 */ | |
| 27 Entry.prototype.GetTreeState = function() { | |
| 28 return this.general_state; | |
| 29 } | |
| 30 | |
| 31 Entry.TREE_STATES = [ | |
| 32 "open", | |
| 33 "closed", | |
| 34 "throttled", | |
| 35 "maintenance", // Tree is closed (for maintenance) | |
| 36 "unknown" | |
| 37 ]; | |
| 38 | |
| 39 // When building runs for display, we may insert a fake entry to fill the gaps | |
| 40 // remaining in a day. These fake entries will use the magic author of "oracle" | |
| 41 // to indicate that they are not real. | |
| 42 Entry.AUTHOR_ORACLE = "The oracle"; | |
| 43 | |
| 44 Entry.prototype.IsOracle = function() { | |
| 45 return this.author == Entry.AUTHOR_ORACLE; | |
| 46 } | |
| 47 | |
| 48 /** | |
| 49 * This class implements a counter for each of the tree status types. | |
| 50 * Always favor this over a raw dictionary, since it is easier to fix | |
| 51 * callers when new status types are added. | |
| 52 */ | |
| 53 function StatusTotals() { | |
| 54 // Init all totals to 0. | |
| 55 this.totals_ = {}; | |
| 56 for (var i = 0; i < Entry.TREE_STATES.length; ++i) { | |
| 57 this.totals_[Entry.TREE_STATES[i]] = 0; | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 StatusTotals.prototype.Increment = function(type, value) { | |
| 62 this.totals_[type] += value; | |
| 63 } | |
| 64 | |
| 65 StatusTotals.prototype.GetOpen = function(type) { | |
| 66 return this.totals_["open"]; | |
| 67 } | |
| 68 | |
| 69 StatusTotals.prototype.GetClosed = function(type) { | |
| 70 return this.totals_["closed"] + this.totals_["maintenance"]; | |
| 71 } | |
| 72 | |
| 73 StatusTotals.prototype.GetClosedForMaintenance = function(type) { | |
| 74 return this.totals_["maintenance"]; | |
| 75 } | |
| 76 | |
| 77 StatusTotals.prototype.GetThrottled = function(type) { | |
| 78 return this.totals_["throttled"]; | |
| 79 } | |
| 80 | |
| 81 StatusTotals.prototype.GetUnknown = function(type) { | |
| 82 return this.totals_["unknown"]; | |
| 83 } | |
| 84 | |
| 85 StatusTotals.prototype.GetTotal = function(type) { | |
| 86 var total = 0; | |
| 87 for (var key in this.totals_) { | |
| 88 total += this.totals_[key]; | |
| 89 } | |
| 90 return total; | |
| 91 } | |
| 92 | |
| 93 StatusTotals.prototype.GetTotalKnown = function(type) { | |
| 94 return this.GetTotal() - this.GetUnknown(); | |
| 95 } | |
| 96 | |
| 97 /** | |
| 98 * A "run" shows the time range that an entry was active for. | |
| 99 * | |
| 100 * In particular, it describes |entry| as having lasted from for | |
| 101 * [startTime - duration, startTime). | |
| 102 * | |
| 103 * Note that entry.timestamp may be earlier than (startTime - duration) when we | |
| 104 * are cutting runs at day boundaries. | |
| 105 * | |
| 106 * @param {Entry} entry | |
| 107 * @param {int} startTime Unix timestamp in milliseconds when the entry *ENDS*. | |
| 108 * @param {int} duration Number of milliseconds the entry is active for, | |
| 109 * starting from startTime. | |
| 110 * @constructor | |
| 111 */ | |
| 112 function Run(entry, startTime, duration) { | |
| 113 this.entry = entry; | |
| 114 this.startTime = startTime; | |
| 115 this.duration = duration; | |
| 116 } | |
| 117 | |
| 118 /** | |
| 119 * Returns the end timestamp of the run (inclusive). | |
| 120 * @return {int} | |
| 121 */ | |
| 122 Run.prototype.GetEndTime = function() { | |
| 123 return this.startTime - this.duration; | |
| 124 } | |
| 125 | |
| 126 /** | |
| 127 * Builds a list of "runs" that span |timeRange|, pulling data from |entries|. | |
| 128 * | |
| 129 * @param {array<Entry>} entries Records sorted from most recent to oldest. | |
| 130 * @param {TimeRange} timeRange | |
| 131 * @return {array<Run>} | |
| 132 */ | |
| 133 function MakeRuns(entries, timeRange) { | |
| 134 var runs = []; | |
| 135 | |
| 136 for (var i = 0; i < entries.length; ++i) { | |
| 137 var prevEntry = i == 0 ? null : entries[i-1]; | |
| 138 var entry = entries[i]; | |
| 139 var nextEntry = i + 1 == entries.length ? null : entries[i + 1]; | |
| 140 | |
| 141 if (entry.timestamp > timeRange.startTime) | |
| 142 continue; | |
| 143 | |
| 144 var runStartTime; | |
| 145 var duration; | |
| 146 | |
| 147 if (runs.length == 0) { | |
| 148 // Connect the startTime to this entry. | |
| 149 runStartTime = timeRange.startTime; | |
| 150 | |
| 151 if (!prevEntry) { | |
| 152 // We don't know what the future holds... | |
| 153 // Extrapolate current status only until the current time. | |
| 154 var curTime = (new Date()).getTime(); | |
| 155 if (curTime >= timeRange.endTime && curTime < timeRange.startTime) { | |
| 156 runStartTime = Math.min(curTime, timeRange.startTime); | |
| 157 } | |
| 158 } | |
| 159 } else { | |
| 160 runStartTime = prevEntry.timestamp; | |
| 161 } | |
| 162 | |
| 163 var runEndTime = entry.timestamp < timeRange.endTime ? | |
| 164 timeRange.endTime : entry.timestamp; | |
| 165 | |
| 166 if (runs.length == 0 && runStartTime != timeRange.startTime) { | |
| 167 var unknownEntry = new Entry(runStartTime, Entry.AUTHOR_ORACLE, | |
| 168 "Your future is uncertain...", "unknown"); | |
| 169 // Add an unknown filler. | |
| 170 runs.push(new Run(unknownEntry, timeRange.startTime, | |
| 171 timeRange.startTime - runStartTime)); | |
| 172 } | |
| 173 | |
| 174 runs.push(new Run(entry, runStartTime, runStartTime - runEndTime)); | |
| 175 | |
| 176 if (runEndTime == timeRange.endTime) { | |
| 177 break; | |
| 178 } | |
| 179 } | |
| 180 | |
| 181 // The runs are supposed to span the entire time range. If any data was | |
| 182 // missing add a filler run. | |
| 183 var lastEndTime = runs.length == 0 ? | |
| 184 timeRange.startTime : runs[runs.length - 1].GetEndTime(); | |
| 185 if (lastEndTime != timeRange.endTime) { | |
| 186 var unknownEntry = new Entry(timeRange.endTime, Entry.AUTHOR_ORACLE, | |
| 187 "Missing data!", "unknown"); | |
| 188 runs.push(new Run(unknownEntry, lastEndTime, | |
| 189 lastEndTime - timeRange.endTime)); | |
| 190 } | |
| 191 | |
| 192 return runs; | |
| 193 } | |
| OLD | NEW |