summaryrefslogtreecommitdiffstats
path: root/tools/sheriffing/functions.js
blob: 4e9ea7f98891f9c3b18b1725da7336dc4fd806ee (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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/** Random constants. */
var MAX_BUILD_STATUS_LENGTH = 50;
var TICKS_BETWEEN_REFRESHES = 60;
var NUM_PREVIOUS_BUILDS_TO_SHOW = 3;
var MAX_MILLISECONDS_TO_WAIT = 5 * 60 * 1000;

/** Parsed JSON data. */
var gWaterfallData = [];
var gStatusData = [];
var gWaterfallDataIsDirty = true;

/** Global state. */
var gTicksUntilRefresh = TICKS_BETWEEN_REFRESHES;

/** Statistics. */
var gNumRequestsInFlight = 0;
var gNumRequestsIgnored = 0;
var gNumRequestsRetried = 0;
var gStartTimestamp = 0;

/** Cut the status message down so it doesn't hog the whole screen. */
function truncateStatusText(text) {
  if (text.length > MAX_BUILD_STATUS_LENGTH) {
    return text.substr(0, MAX_BUILD_STATUS_LENGTH) + '...';
  }
  return text;
}

/** Queries all of the servers for their latest statuses. */
function queryServersForInfo() {
  for (var index = 0; index < gWaterfallData.length; ++index) {
    gWaterfallData[index].requestJson();
  }

  for (var index = 0; index < gStatusData.length; ++index) {
    gStatusData[index].requestJson();
  }
}

/** Updates the sidebar's contents. */
function updateSidebarHTML() {
  // Update all of the project info.
  var divElement = document.getElementById('sidebar-contents');
  while (divElement.firstChild) {
    divElement.removeChild(divElement.firstChild);
  }

  for (var i = 0; i < gStatusData.length; ++i) {
    divElement.appendChild(gStatusData[i].createHtml());
  }

  // Debugging stats.
  document.getElementById('num-ticks-until-refresh').innerHTML =
      gTicksUntilRefresh;
  document.getElementById('num-requests-in-flight').innerHTML =
      gNumRequestsInFlight;
  document.getElementById('num-requests-ignored').innerHTML =
      gNumRequestsIgnored;
  document.getElementById('num-requests-retried').innerHTML =
      gNumRequestsRetried;
}

/**
 * Organizes all of the bots by category, then alphabetically within their
 * categories.
 */
function sortBotNamesByCategory(botInfo) {
  // Bucket all of the bots according to their category.
  var allBotNames = Object.keys(botInfo);
  var bucketedNames = {};
  for (var i = 0; i < allBotNames.length; ++i) {
    var botName = allBotNames[i];
    var category = botInfo[botName].category;

    if (!bucketedNames[category]) bucketedNames[category] = [];
    bucketedNames[category].push(botName);
  }

  // Alphabetically sort bots within their buckets, then append them to the
  // current list.
  var sortedBotNames = [];
  var allCategories = Object.keys(bucketedNames);
  allCategories.sort();
  for (var i = 0; i < allCategories.length; ++i) {
    var category = allCategories[i];
    var bucketBots = bucketedNames[category];
    bucketBots.sort();

    for (var j = 0; j < bucketBots.length; ++j) {
      sortedBotNames.push(bucketBots[j]);
    }
  }

  return sortedBotNames;
}

/** Update all the waterfall data. */
function updateStatusHTML() {
  var table = document.getElementById('build-info');
  while (table.rows.length > 0) {
    table.deleteRow(-1);
  }

  for (var i = 0; i < gWaterfallData.length; ++i) {
    gWaterfallData[i].updateWaterfallStatusHTML();
  }
}

/** Marks the waterfall data as dirty due to updated filter. */
function filterUpdated() {
  gWaterfallDataIsDirty = true;
}

/** Update the page content. */
function updateContent() {
  if (--gTicksUntilRefresh <= 0) {
    gTicksUntilRefresh = TICKS_BETWEEN_REFRESHES;
    queryServersForInfo();
  }

  // Redraw the page content.
  if (gWaterfallDataIsDirty) {
    gWaterfallDataIsDirty = false;
    updateStatusHTML();

    if (document.getElementById('failure-info')) {
      updateCorrelationsHTML();
    }
  }
  updateSidebarHTML();
}

/** Initialize all the things. */
function initialize() {
  var gStartTimestamp = new Date().getTime();

  // Initialize the waterfall pages.
  for (var i = 0; i < kBuilderPages.length; ++i) {
    gWaterfallData.push(new WaterfallInfo(kBuilderPages[i]));
  }

  // Initialize the status pages.
  for (var i = 0; i < kStatusPages.length; ++i) {
    gStatusData.push(new StatusPageInfo(kStatusPages[i][0],
                                        kStatusPages[i][1]));
  }

  // Kick off the main loops.
  queryServersForInfo();
  updateStatusHTML();
  setInterval('updateContent()', 1000);
}