Jobs on Compass often timeout
[releng.git] / utils / test / vnfcatalogue / cronjobs / github.js
1 // Important Add your access token here default rate of github is limited to 60 API calls per hour
2 var access_token = '*';
3 // Important set the delta threshold for repo details updation. For instance if the threshold is
4 // set to 1 day(60 * 60 * 24), the cronjob will only update the row if the difference between current
5 // time and last_updated time stamp of a repo is greater than one day
6 var delta = 60 * 60 * 24;
7
8
9 var github = require('octonode');
10 db_pool = require('./database').pool;
11 async = require('async');
12
13 var current_time = Math.floor(new Date().getTime() / 1000);//toISOString().slice(0, 19).replace('T', ' ');
14 console.log(current_time);
15
16 var get_val_from_header = function(header_link) {
17     // small hack by parsing the header and setting per_page = 1, hence no pagination fetch required
18     result_intermediate = header_link.split(';');
19     result_intermediate = result_intermediate[result_intermediate.length - 2];
20     var reg = /&page=([0-9].*)>/g;
21     var match = reg.exec(result_intermediate);
22     return parseInt(match[1]);
23 }
24
25 var get_stargazers = function(result, ghrepo, primary_callback, cb) {
26     ghrepo.stargazers({per_page: 1}, function(err, data, headers) {
27     //console.log(JSON.stringify(data));
28         try {
29             result['no_of_stars'] = get_val_from_header(headers['link']);
30             cb(null, result, ghrepo, primary_callback);
31         } catch(err) {
32             result['no_of_stars'] = null;
33             cb(null, result, ghrepo, primary_callback);
34         }
35     });
36 }
37
38 var get_branches = function(result, ghrepo, primary_callback, cb) {
39     ghrepo.branches({per_page: 1}, function(err, data, headers) {
40         try {
41             result['versions'] = get_val_from_header(headers['link']);
42             cb(null, result, ghrepo, primary_callback);
43         } catch(err) {
44             result['versions'] = null;
45             cb(null, result, ghrepo, primary_callback);
46         }
47     });
48 }
49
50 var get_contributors = function(result, ghrepo, primary_callback, cb) {
51     ghrepo.contributors({per_page: 1}, function(err, data, headers) {
52         try {
53             result['no_of_developers'] = get_val_from_header(headers['link']);
54             cb(null, result, primary_callback);
55         } catch(err) {
56             result['no_of_developers'] = null;
57             cb(null, result, primary_callback);
58
59         }
60     });
61 }
62
63 var get_lines_of_code = function(result, cb) {
64     //    #TODO
65 }
66
67 var secondary_callback = function (err, result, primary_callback) {
68     console.log(result);
69     if((result['last_updated'] == null) || (current_time - result['last_updated'] > delta)) {
70         db_pool.getConnection(function(err, connection) {
71             //Use the connection
72             var last_updated = current_time;
73             var no_of_stars = result['no_of_stars'];
74             var versions = result['versions'];
75             var no_of_developers = result['no_of_developers'];
76             sql_query = 'update vnf set last_updated = FROM_UNIXTIME(' + last_updated;
77             sql_query += '), no_of_stars =  ' + no_of_stars + ', versions = ' + versions;
78             sql_query += ', no_of_developers = ' + no_of_developers + ' where vnf_id = ';
79             sql_query += result['vnf_id'];
80             console.log(sql_query);
81             connection.query(sql_query, function (error, results, fields) {
82                 if (error) throw error;
83                 //And done with the connection.
84                 primary_callback(null, result['vnf_id'] + ' updated');
85                 connection.release();
86                 // Handle error after the release.
87                 // Don't use the connection here, it has been returned to the pool.
88             });
89         });
90     } else {
91         primary_callback(null, result['vnf_id'] + ' not updated');
92     }
93 }
94
95 var get_stats = function(vnf_details, callback) {
96     repo = vnf_details['repo_url'];
97     repo = repo.split("/");
98     github_id = repo[repo.length - 2] + '/' + repo[repo.length - 1];
99
100     var async = require('async');
101     var client = github.client(access_token);
102     var ghrepo = client.repo(github_id);
103
104     result = {}
105     result['vnf_id'] = vnf_details['vnf_id'];
106     result['last_updated'] = vnf_details['last_updated'];
107
108     async.waterfall([
109             async.apply(get_stargazers, result, ghrepo, callback),
110             get_branches,
111             get_contributors,
112             //get_lines_of_code,
113         ], secondary_callback);
114 }
115
116 db_pool.getConnection(function(err, connection) {
117     sql_query = 'select vnf_id, repo_url, UNIX_TIMESTAMP(last_updated) last_updated from vnf';
118     console.log(sql_query);
119     connection.query(sql_query, function (error, results, fields) {
120         if (error) throw error;
121         async.map(results, get_stats, function(error, results) {
122             //console.log(results);
123             console.log(results);
124             process.exit();
125
126         });
127     });
128 });
129