1 // Important Add your access token here default rate of github is limited to 60 API calls per hour
2 var access_token = '523825265638c8f2b5b9f1d1f5af31d71bceb49c';
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;
9 var github = require('octonode');
10 db_pool = require('/usr/src/app/database').pool;
11 async = require('async');
13 var current_time = Math.floor(new Date().getTime() / 1000);//toISOString().slice(0, 19).replace('T', ' ');
14 console.log(current_time);
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]);
25 var get_stargazers = function(result, ghrepo, orig_repo, primary_callback, cb) {
26 ghrepo.stargazers({per_page: 1}, function(err, data, headers) {
27 //console.log(JSON.stringify(data));
29 result['no_of_stars'] = get_val_from_header(headers['link']);
30 cb(null, result, ghrepo, orig_repo, primary_callback);
32 result['no_of_stars'] = null;
33 cb(null, result, ghrepo, orig_repo, primary_callback);
38 var get_branches = function(result, ghrepo, orig_repo, primary_callback, cb) {
39 ghrepo.branches({per_page: 1}, function(err, data, headers) {
41 result['versions'] = get_val_from_header(headers['link']);
42 cb(null, result, ghrepo, orig_repo, primary_callback);
44 result['versions'] = null;
45 cb(null, result, ghrepo, orig_repo, primary_callback);
50 var get_contributors = function(result, ghrepo, orig_repo, primary_callback, cb) {
51 ghrepo.contributors({per_page: 1}, function(err, data, headers) {
53 result['no_of_developers'] = get_val_from_header(headers['link']);
54 cb(null, result, ghrepo, orig_repo, primary_callback);
56 result['no_of_developers'] = null;
57 cb(null, result, ghrepo, orig_repo, primary_callback);
62 var get_lines_of_code = function(result, ghrepo, orig_repo, primary_callback, cb) {
66 console.log(orig_repo);
68 spawn = require( 'child_process' ).spawn,
69 ls = spawn( 'bash', ['/usr/src/app/git_count_loc.sh', orig_repo] );
71 ls.stdout.on( 'data', data => {
72 console.log( `stdout: ${data}` );
73 result['lines_of_code'] = parseInt(`${data}`.replace(/\n$/, ''), 10);
74 //cb(null, result, primary_callback);
77 ls.stderr.on( 'data', data => {
78 //console.log( `stderr: ${data}` );
79 //cb(null, result, primary_callback);
82 ls.on( 'close', code => {
83 console.log( `child process exited with code ${code}` );
84 cb(null, result, primary_callback);
88 var secondary_callback = function (err, result, primary_callback) {
90 if((result['last_updated'] == null) || (current_time - result['last_updated'] > delta)) {
91 db_pool.getConnection(function(err, connection) {
93 var last_updated = current_time;
94 var no_of_stars = result['no_of_stars'];
95 var versions = result['versions'];
96 var no_of_developers = result['no_of_developers'];
97 var lines_of_code = result['lines_of_code'];
99 console.log(lines_of_code);
100 sql_query = 'update vnf set last_updated = FROM_UNIXTIME(' + last_updated;
101 sql_query += '), no_of_stars = ' + no_of_stars + ', versions = ' + versions;
102 sql_query += ', lines_of_code = ' + lines_of_code;
103 sql_query += ', no_of_developers = ' + no_of_developers + ' where vnf_id = ';
104 sql_query += result['vnf_id'];
105 console.log(sql_query);
106 connection.query(sql_query, function (error, results, fields) {
107 //if (error) throw error;
108 //And done with the connection.
109 connection.release();
111 primary_callback(null, result['vnf_id'] + ' not updated');
113 primary_callback(null, result['vnf_id'] + ' updated');
115 // Handle error after the release.
116 // Don't use the connection here, it has been returned to the pool.
120 primary_callback(null, result['vnf_id'] + ' not updated');
124 var get_stats = function(vnf_details, callback) {
125 repo = vnf_details['repo_url'];
127 repo = repo.split("/");
128 github_id = repo[repo.length - 2] + '/' + repo[repo.length - 1];
130 var async = require('async');
131 var client = github.client(access_token);
132 var ghrepo = client.repo(github_id);
135 result['vnf_id'] = vnf_details['vnf_id'];
136 result['last_updated'] = vnf_details['last_updated'];
139 async.apply(get_stargazers, result, ghrepo, orig_repo, callback),
143 ], secondary_callback);
146 db_pool.getConnection(function(err, connection) {
147 sql_query = 'select vnf_id, repo_url, UNIX_TIMESTAMP(last_updated) last_updated from vnf';
148 console.log(sql_query);
149 connection.query(sql_query, function (error, results, fields) {
150 if (error) throw error;
151 async.map(results, get_stats, function(error, results) {
152 //console.log(results);
153 console.log(results);