| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 * Helper that fetches the tree status data from a server. | |
| 7 * | |
| 8 * The public method it exports is: | |
| 9 * DataFetcher.GetTreeStatusEntries(timeRange, callback) method. | |
| 10 */ | |
| 11 var DataFetcher = {}; | |
| 12 | |
| 13 // ----------------------------------------------------------------------------- | |
| 14 // Private implementation | |
| 15 // ----------------------------------------------------------------------------- | |
| 16 (function() { | |
| 17 | |
| 18 /** | |
| 19 * Downloads a single url and calls callback(text,error) on completion. | |
| 20 * | |
| 21 * @param {string} url | |
| 22 * @param {function} callback | |
| 23 */ | |
| 24 function Fetch(url, callback) { | |
| 25 var r = new XMLHttpRequest(); | |
| 26 r.open("GET", url, true); | |
| 27 r.setRequestHeader("pragma", "no-cache"); | |
| 28 r.setRequestHeader("cache-control", "no-cache"); | |
| 29 r.onreadystatechange = function() { | |
| 30 if (r.readyState == 4) { | |
| 31 var error; | |
| 32 var text = r.responseText; | |
| 33 if (r.status != 200) { | |
| 34 error = url + ": " + r.status + ": " + r.statusText; | |
| 35 } else if (! text) { | |
| 36 error = url + ": null response"; | |
| 37 } | |
| 38 callback(text, error); | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 r.send(null); | |
| 43 } | |
| 44 | |
| 45 /** | |
| 46 * Parses response (text, error) and appends the parsed entry to |entries|. | |
| 47 * | |
| 48 * /allstatus returns entries as "<author>, <date>,<message>\n" lines, with | |
| 49 * a header line reading "Who,When,Message\n". | |
| 50 * | |
| 51 */ | |
| 52 function ParseDataResponseAndAppend(entries, text, error) { | |
| 53 if (error) { | |
| 54 // If we failed to download the file, error. | |
| 55 Log("Failed to retrieve the data from server. Error:\n" + error); | |
| 56 return false; // failure. | |
| 57 } | |
| 58 | |
| 59 var content = JSON.parse(text); | |
| 60 if (!content.length) { | |
| 61 Log("Content seems to be invalid"); | |
| 62 return false; | |
| 63 } | |
| 64 | |
| 65 for (var i = 0; i < content.length; ++i) { | |
| 66 // The time has a trailing ".XXXXX" ms component we don't care for. | |
| 67 // Also append "UTC" so we are left with a string resembling: | |
| 68 // "2009-10-14 21:59:18 UTC" | |
| 69 var item = content[i]; | |
| 70 dateStr = item.date.split(".")[0] + " UTC"; | |
| 71 entries.push( | |
| 72 new Entry(DateUtil.ParseUTCDateTimeString(dateStr), item.username, | |
| 73 item.message, item.general_state)); | |
| 74 | |
| 75 } | |
| 76 | |
| 77 return true; // success. | |
| 78 } | |
| 79 | |
| 80 /** | |
| 81 * Fetches all of the tree status data relating to |range|. | |
| 82 * On completion |callback(entries)| is invoked, where |entries| is a list of | |
| 83 * Entry instances. | |
| 84 * | |
| 85 * @param {TimeRange} timeRange | |
| 86 * @param {function} callback | |
| 87 */ | |
| 88 DataFetcher.GetTreeStatusEntries = function(timeRange, callback) { | |
| 89 // Convert milliseconds to seconds, since the server is epxecting | |
| 90 // seconds. | |
| 91 var startTime = DateUtil.MillisToSeconds(timeRange.startTime); | |
| 92 var endTime = DateUtil.MillisToSeconds(timeRange.endTime); | |
| 93 | |
| 94 // The peak hours view may need extra time for its day view. | |
| 95 // Lets go ahead and optimisitically ask for it, just in case... | |
| 96 startTime += DateUtil.MillisToSeconds(DateUtil.MILLIS_PER_DAY); | |
| 97 endTime -= DateUtil.MillisToSeconds(DateUtil.MILLIS_PER_DAY); | |
| 98 | |
| 99 var url = "/allstatus?format=json&startTime=" + startTime + "&endTime=" + | |
| 100 endTime + "&limit=1000"; | |
| 101 | |
| 102 Fetch(url, OnFetchedDataComplete.bind(this, callback)); | |
| 103 } | |
| 104 | |
| 105 /** | |
| 106 * Callback for when the data has been fetched. | |
| 107 * | |
| 108 * @param {function} callback The user's callback. | |
| 109 * @param {string} text The contents of the response. | |
| 110 * @param {string} error Any error message, or undefined on success. | |
| 111 */ | |
| 112 function OnFetchedDataComplete(callback, text, error) { | |
| 113 var entries = []; | |
| 114 var ok = ParseDataResponseAndAppend(entries, text, error); | |
| 115 // TODO(eroman): Fix login situation. | |
| 116 if (!ok) | |
| 117 alert("TODO: You probably aren't logged in; try doing that first."); | |
| 118 callback(entries); | |
| 119 } | |
| 120 | |
| 121 })(); // Private implementation. | |
| OLD | NEW |