|
| 1 | +library _reporter; |
| 2 | + |
| 3 | +import 'dart:io'; |
| 4 | +import 'dart:convert' show UTF8, JSON; |
| 5 | + |
| 6 | + |
| 7 | +String getBaseUrl() { |
| 8 | + String ngDashBaseUrl = Platform.environment["NGDASH_BASE_URL"]; |
| 9 | + if (ngDashBaseUrl == null || ngDashBaseUrl.isEmpty) { |
| 10 | + ngDashBaseUrl = "http://ng-dash.gae.localhost"; |
| 11 | + } |
| 12 | + return ngDashBaseUrl; |
| 13 | +} |
| 14 | + |
| 15 | + |
| 16 | +class ResultData { |
| 17 | + String name = ""; |
| 18 | + String description = ""; |
| 19 | + Map<String, dynamic> dimensions = {}; |
| 20 | + Map<String, dynamic> metrics = {}; |
| 21 | + List<ResultData> children = []; |
| 22 | + |
| 23 | + ResultData(String this.name, String this.description); |
| 24 | + |
| 25 | + newChild(String name, String description) { |
| 26 | + ResultData child = new ResultData(name, description); |
| 27 | + children.add(child); |
| 28 | + return child; |
| 29 | + } |
| 30 | + |
| 31 | + toJson() => { |
| 32 | + "name": name, |
| 33 | + "description": description, |
| 34 | + "dimensions_json": JSON.encode(dimensions), |
| 35 | + "metrics_json": JSON.encode(metrics), |
| 36 | + "children": children.map((i) => i.toJson()).toList(), |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +Map<String, String> getTravisDimension() { |
| 42 | + if (Platform.environment["TRAVIS"] != "true") { |
| 43 | + throw "getTravisDimension(): Not called on TRAVIS"; |
| 44 | + } |
| 45 | + Map<String, String> result = {}; |
| 46 | + // Ref: http://docs.travis-ci.com/user/ci-environment/ |
| 47 | + for (String envVar in const [ |
| 48 | + "TRAVIS_BRANCH", // The name of the branch currently being built. |
| 49 | + "TRAVIS_BUILD_ID", // The id of the current build that Travis CI uses internally. |
| 50 | + "TRAVIS_BUILD_NUMBER", // The number of the current build (for example, "4"). |
| 51 | + "TRAVIS_COMMIT", // The commit that the current build is testing. |
| 52 | + "TRAVIS_COMMIT_RANGE", // The range of commits that were included in the push or pull request. |
| 53 | + "TRAVIS_JOB_ID", // The id of the current job that Travis CI uses internally. |
| 54 | + "TRAVIS_JOB_NUMBER", // The number of the current job (for example, "4.1"). |
| 55 | + "TRAVIS_PULL_REQUEST", // The pull request number if the current job is a pull request, "false" if it's not a pull request. |
| 56 | + "TRAVIS_REPO_SLUG", // "owner_name/repo_name" (e.g. "travis-ci/travis-build"). |
| 57 | + "TRAVIS_OS_NAME", // Name of the operating system built on. (e.g. linux or osx) |
| 58 | + "TRAVIS_TAG", // (optional) tag name for current build if relevant. |
| 59 | + ]) { |
| 60 | + String value = Platform.environment[envVar]; |
| 61 | + if (value != null && !value.isEmpty) { |
| 62 | + result[envVar.substring("TRAVIS_".length).toLowerCase()] = value; |
| 63 | + } |
| 64 | + } |
| 65 | + return result; |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | +String getOsType() { |
| 70 | + if (Platform.isMacOS) { |
| 71 | + return "OSX"; |
| 72 | + } else if (Platform.isLinux) { |
| 73 | + return "Linux"; |
| 74 | + } else if (Platform.isWindows) { |
| 75 | + return "Windows"; |
| 76 | + } else if (Platform.Android) { |
| 77 | + return "Android"; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +class Reporter { |
| 83 | + ResultData data = new ResultData("", ""); |
| 84 | + final String baseUrl = getBaseUrl(); |
| 85 | + String commitSha = ""; |
| 86 | + String treeSha = ""; |
| 87 | + String reportId = null; |
| 88 | + List<Cookie> cookies; |
| 89 | + |
| 90 | + Reporter() { |
| 91 | + var dimensions = data.dimensions; |
| 92 | + dimensions["project"] = "AngularDart"; |
| 93 | + dimensions["dart"] = { |
| 94 | + "full_version": Platform.version, |
| 95 | + "version": Platform.version.split(" ")[0], |
| 96 | + }; |
| 97 | + dimensions["os"] = { |
| 98 | + "type": getOsType(), |
| 99 | + }; |
| 100 | + if (Platform.environment["TRAVIS"] == "true") { |
| 101 | + dimensions["travis"] = getTravisDimension(); |
| 102 | + commitSha = dimensions["travis"]["commit"]; |
| 103 | + } |
| 104 | + |
| 105 | + // Auth cookies |
| 106 | + var user_email = Platform.environment["NGDASH_USER_EMAIL"]; |
| 107 | + var user_secret = Platform.environment["NGDASH_USER_SECRET"]; |
| 108 | + if (user_email == null || user_email.isEmpty || |
| 109 | + user_secret == null || user_secret.isEmpty) { |
| 110 | + throw "Please set NGDASH_USER_EMAIL and NGDASH_USER_SECRET credentials."; |
| 111 | + } |
| 112 | + cookies = [new Cookie("user_email", user_email), |
| 113 | + new Cookie("user_secret", user_secret)]; |
| 114 | + |
| 115 | + if (commitSha.isEmpty) { |
| 116 | + throw "Could not detect the commit SHA. (non-travis detection not implemented yet.)"; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + // The following machinery is there just to serialize saving to the server. |
| 122 | + // If we're already in the process of saving the results, then just mark that |
| 123 | + // we need to save again. This is also particularly important because the |
| 124 | + // first time, we create a new report, and in all subsequent calls, we update |
| 125 | + // that same report using the report ID that was received when we created it. |
| 126 | + var _messageQueue = []; |
| 127 | + var _needKick = true; |
| 128 | + |
| 129 | + _sendNextMessage() { |
| 130 | + print("_sendNextMessage: reportId=$reportId"); |
| 131 | + |
| 132 | + if (_messageQueue.length == 0) { |
| 133 | + _needKick = true; |
| 134 | + print("_sendNextMessage: _messageQueue is empty. returning"); |
| 135 | + return; |
| 136 | + } |
| 137 | + String requestData = JSON.encode(_messageQueue.removeAt(0)); |
| 138 | + |
| 139 | + _needKick = false; |
| 140 | + |
| 141 | + if (reportId == null) { |
| 142 | + Uri url = Uri.parse("${baseUrl}/api/run"); |
| 143 | + new HttpClient().postUrl(url) |
| 144 | + .then((HttpClientRequest request) { |
| 145 | + request.headers.contentType = ContentType.JSON; |
| 146 | + request.cookies.addAll(cookies); |
| 147 | + request.write(requestData); |
| 148 | + return request.close(); |
| 149 | + }) |
| 150 | + .then((HttpClientResponse response) { |
| 151 | + var responseData = ""; |
| 152 | + response.transform(UTF8.decoder).listen( |
| 153 | + (partialData) { responseData += partialData; }, |
| 154 | + onDone: () { |
| 155 | + reportId = JSON.decode(responseData)["id"]; |
| 156 | + _sendNextMessage(); |
| 157 | + } |
| 158 | + ); |
| 159 | + }); |
| 160 | + } else { |
| 161 | + Uri url = Uri.parse("${baseUrl}/api/run/id=$reportId"); |
| 162 | + new HttpClient().putUrl(url) |
| 163 | + .then((HttpClientRequest request) { |
| 164 | + request.headers.contentType = ContentType.JSON; |
| 165 | + request.cookies.addAll(cookies); |
| 166 | + request.write(requestData); |
| 167 | + return request.close(); |
| 168 | + }) |
| 169 | + .then((HttpClientResponse response) { |
| 170 | + response.transform(UTF8.decoder).listen(null, onDone: _sendNextMessage); |
| 171 | + }); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + |
| 176 | + void saveReport() { |
| 177 | + print("saveReport: _needKick=$_needKick"); |
| 178 | + _messageQueue.add(this); |
| 179 | + if (_needKick) { |
| 180 | + _sendNextMessage(); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + |
| 185 | + toJson() => { |
| 186 | + "commit_sha": commitSha, |
| 187 | + "tree_sha": treeSha, |
| 188 | + "data": data.toJson(), |
| 189 | + }; |
| 190 | +} |
0 commit comments