1 | var verify = require('check-types').verify;
|
2 | var request = require('request');
|
3 | var Q = require('q');
|
4 | var colors = require('cli-color');
|
5 | var colorAvailable = process.stdout.isTTY;
|
6 |
|
7 | var nextUpdateStatsUrl = require('../package.json')['next-update-stats'] ||
|
8 | 'http://next-update.herokuapp.com';
|
9 |
|
10 | function sendUpdateResult(options) {
|
11 | verify.unemptyString(options.name, 'missing name');
|
12 | verify.unemptyString(options.from, 'missing from version');
|
13 | verify.unemptyString(options.to, 'missing to version');
|
14 | if (options.success) {
|
15 | options.success = !!options.success;
|
16 | }
|
17 |
|
18 | verify.webUrl(nextUpdateStatsUrl, 'missing next update stats server url');
|
19 | var sendOptions = {
|
20 | uri: nextUpdateStatsUrl + '/update',
|
21 | method: 'POST',
|
22 | json: options
|
23 | };
|
24 | request(sendOptions, function ignoreResponse() {});
|
25 | }
|
26 |
|
27 | function getSuccessStats(options) {
|
28 | verify.unemptyString(options.name, 'missing name');
|
29 | verify.unemptyString(options.from, 'missing from version');
|
30 | verify.unemptyString(options.to, 'missing to version');
|
31 |
|
32 | verify.webUrl(nextUpdateStatsUrl, 'missing next update stats server url');
|
33 | var opts = {
|
34 | uri: nextUpdateStatsUrl + '/package/' + options.name + '/' + options.from +
|
35 | '/' + options.to,
|
36 | method: 'GET',
|
37 | json: options
|
38 | };
|
39 | var defer = Q.defer();
|
40 | request(opts, function (err, response, stats) {
|
41 | if (err || response.statusCode !== 200) {
|
42 | if (response) {
|
43 | if (response.statusCode !== 404) {
|
44 | console.error('could not get success stats for', options.name);
|
45 | console.error(opts);
|
46 | }
|
47 | console.error('response status:', response.statusCode);
|
48 | }
|
49 | if (err) {
|
50 | console.error(err);
|
51 | }
|
52 | defer.reject(err);
|
53 | return;
|
54 | }
|
55 | defer.resolve(stats);
|
56 | });
|
57 | return defer.promise;
|
58 | }
|
59 |
|
60 | function colorProbability(probability, options) {
|
61 | if (probability === null) {
|
62 | return '';
|
63 | }
|
64 |
|
65 | options = options || {};
|
66 | var useColors = !!options.color && colorAvailable;
|
67 | if (probability < 0 || probability > 1) {
|
68 | throw new Error('Expected probability between 0 and 1, not ' + probability);
|
69 | }
|
70 | var probabilityStr = (probability * 100).toFixed(0) + '%';
|
71 | if (useColors) {
|
72 | if (probability > 0.8) {
|
73 | probabilityStr = colors.greenBright(probabilityStr);
|
74 | } else if (probability > 0.5) {
|
75 | probabilityStr = colors.yellowBright(probabilityStr);
|
76 | } else {
|
77 | probabilityStr = colors.redBright(probabilityStr);
|
78 | }
|
79 | }
|
80 | return probabilityStr;
|
81 | }
|
82 |
|
83 | function printStats(options, stats) {
|
84 | verify.object(stats, 'missing stats object');
|
85 | verify.unemptyString(stats.name, 'missing name');
|
86 | verify.unemptyString(stats.from, 'missing from version');
|
87 | verify.unemptyString(stats.to, 'missing to version');
|
88 | stats.success = +stats.success || 0;
|
89 | stats.failure = +stats.failure || 0;
|
90 | var total = stats.success + stats.failure;
|
91 | var probability = (total > 0 ? stats.success / total: 0);
|
92 | var probabilityStr = colorProbability(probability, options);
|
93 | console.log('stats:', stats.name, stats.from, '->', stats.to,
|
94 | 'success probability', probabilityStr,
|
95 | stats.success, 'success(es)', stats.failure, 'failure(s)');
|
96 | }
|
97 |
|
98 | module.exports = {
|
99 | sendUpdateResult: sendUpdateResult,
|
100 | getSuccessStats: getSuccessStats,
|
101 | printStats: printStats,
|
102 | colorProbability: colorProbability,
|
103 | url: nextUpdateStatsUrl
|
104 | };
|