Advertisement
Guest User

Untitled

a guest
May 22nd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. const req = require('request');
  3. const fs = require('fs');
  4. const async = require('async');
  5.  
  6. const lineByLine = require('n-readlines');
  7. let liner = new lineByLine('./SOidsWithGH.txt');
  8. let line;
  9.  
  10. const LANGUAGES_ARRAY = ["JavaScript", "Java", "Python", "Ruby", "PHP"];
  11. const LANGUAGES_ARRAY_LOWCASE = LANGUAGES_ARRAY.map((lang) => {return lang.toLowerCase()})
  12.  
  13. const mysql = require('promise-mysql');
  14. const connection = mysql.createConnection({
  15.     host: 'localhost',
  16.     user: 'ghtorrentuser',
  17.     password: 'ghtorrentpassword',
  18.     database: 'ghtorrent_restore'
  19. });
  20.  
  21. connection.connect();
  22.  
  23. const getUserQuery = function(GHlogin) {
  24.     return [`SELECT id as GHid FROM ghtorrent_restore.users WHERE login="?"`,
  25.         [GHlogin],
  26.     ];
  27. }
  28. const getFollowersCountQuery = function(GHid) {
  29.     return [`SELECT count(user_id) as followersCount
  30.         FROM ghtorrent_restore.followers
  31.         WHERE follower_id IN (?)`,
  32.         [GHid],
  33.     ];
  34.    
  35. }
  36. const getReposList = function(GHid) {
  37.     return [`SELECT t1.project_id as project_id, t2.language as project_lang
  38.         FROM ghtorrent_restore.commits as t1
  39.         JOIN ghtorrent_restore.projects as t2 on t2.id = t1.project_id
  40.         WHERE t1.author_id=? and t2.language IN ('?')
  41.         GROUP BY project_id, project_lang`,
  42.         [GHid, LANGUAGES_ARRAY.join("','")],
  43.     ];
  44. }
  45. const getForksCountQuery = function(GHrepoId) {
  46.     return [`SELECT count(id) as forksCount
  47.         FROM ghtorrent_restore.projects
  48.         WHERE forked_from IN (?)`,
  49.         [GHrepoId],
  50.     ];
  51. }
  52. const getStarsCountQuery = function(GHrepoId) {
  53.     return [`SELECT count(repo_id) as starsCount
  54.         FROM ghtorrent_restore.watchers
  55.         WHERE repo_id IN (?)`,
  56.         [GHrepoId],
  57.     ];
  58. }
  59. const getAllCommitsCountQuery = function(GHrepoId) {
  60.     return [`SELECT count(id) as allCommitsCount
  61.         FROM ghtorrent_restore.commits
  62.         WHERE project_id IN (?)`,
  63.         [GHrepoId],
  64.     ];
  65. }
  66. const getUserCommitsCountQuery = function(GHrepoId, GHid) {
  67.     return [`SELECT count(id) as usersCommitsCount
  68.         FROM ghtorrent_restore.commits
  69.         WHERE project_id IN (?) AND author_id=?`,
  70.         [GHrepoId, GHid],
  71.     ];
  72. }
  73. const getClosedPRCountQuery = function(GHrepoId, GHid) {
  74.     return [`SELECT count(pr.id) as closedPRCount
  75.         FROM ghtorrent_restore.pull_request_history as prh
  76.         JOIN ghtorrent_restore.pull_requests as pr on prh.pull_request_id=pr.id
  77.         where pr.base_repo_id IN (?) and prh.actor_id=? and prh.action="closed"`,
  78.         [GHrepoId, GHid],
  79.     ];
  80. }
  81. const getAllPRCountQuery = function(GHrepoId, GHid) {
  82.     return [`SELECT count(pr.id) as openedPRCount
  83.         FROM [ghtorrent-bq:ght_2017_01_19.pull_request_history] as prh
  84.         JOIN [ghtorrent-bq:ght_2017_01_19.pull_requests] as pr on prh.pull_request_id=pr.id
  85.         where pr.base_repo_id IN (?) and prh.actor_id=?`,
  86.         [GHrepoId, GHid]
  87.     ];
  88. }
  89. const getMergedPRCountQuery = function(GHrepoId, GHid) {
  90.     return [`SELECT count(pr.id) as mergedPRCount
  91.         FROM ghtorrent_restore.pull_request_history as prh
  92.         JOIN ghtorrent_restore.pull_requests as pr on prh.pull_request_id=pr.id
  93.         where pr.base_repo_id IN (?) and prh.actor_id= ? and prh.action="merged"`,
  94.         [GHrepoId, GHid],
  95.     ];
  96. }
  97.  
  98. const mysqlConnect = function (sueryString, queryParams) {
  99.     connection.query(sueryString, queryParams, function (error, results, fields) {
  100.         if (error) throw error;
  101.         // ...
  102.     });
  103. }
  104.  
  105. async function asyncQuery (sqlQuery) {
  106.     const bigquery = BigQuery();
  107.     const options = {
  108.         query: sqlQuery,
  109.         useLegacySql: true
  110.     };
  111.  
  112.     let results = await bigquery.startQuery(options);
  113.     let job = results[0];
  114.     await job.promise();
  115.     let rows = await job.getQueryResults();
  116.     return rows[0];
  117. }
  118.  
  119. let init = function()  {
  120.  
  121. (function loop() {
  122.     if (line = liner.next()) {
  123.         const GHlogin = line.toString("ascii")
  124.         console.log(GHlogin);
  125.  
  126.         mysql.createConnection({
  127.             host: "localhost",
  128.             user: "ghtorrentuser",
  129.             password: "ghtorrentpassword",
  130.             database: "ghtorrent_restore",
  131.         }).then((conn) => {
  132.             connection = conn;
  133.             let queryParams = getUserQuery(GHlogin);
  134.             return connection.query(queryParams[0], queryParams[1]);
  135.         }).then((rows)=>{
  136.             queryParams = getUserQuery(GHlogin);
  137.             const GHid = rows[0].GHid;
  138.             queryParams = getReposList(GHid);
  139.  
  140.             return connection.query(queryParams[0], queryParams[1]);
  141.         }).then(function(rows){
  142.             if(rows.length) {
  143.                 let ghData = {};
  144.                 reposList.forEach((repo) => {
  145.                     if( !ghData.hasOwnProperty(repo.project_lang) ){
  146.                         ghData[repo.project_lang] = [];
  147.                     }
  148.                     ghData[repo.project_lang].push(repo.project_id);
  149.                 });
  150.             }
  151.             console.log(rows);
  152.         });
  153.  
  154.           asyncQuery(getUserQuery(GHlogin)).then((ghUser) => {
  155.               console.log(ghUser);
  156.               if(ghUser.length) {
  157.                   let GHid = ghUser[0].GHid;
  158.                   console.log(GHid);
  159.                   asyncQuery(getReposList(GHid)).then((reposList) => {
  160.                       if(reposList.length) {
  161.                           //return;
  162.                           let ghData = {};
  163.                           console.log(reposList.map((repo) => {return repo.project_lang}))
  164.                           reposList.forEach((repo) => {
  165.                               if( !ghData.hasOwnProperty(repo.project_lang) ){
  166.                                 ghData[repo.project_lang] = [];
  167.                               }
  168.                               ghData[repo.project_lang].push(repo.project_id);
  169.                               let SOresult = {};
  170.                               req.get({ "url": `http://api.stackexchange.com/2.2/users/${SOid}/top-tags?page=1&order=desc&site=stackoverflow`,
  171.                                   json: true,
  172.                                   headers: {'accept-encoding': 'gzip'},
  173.                                   gzip: true
  174.                                 },  (err, httpResponse, body) => {
  175.                                   let tags = body.items;
  176.                                   let i = 0;
  177.                                   async.whilst( () => {
  178.                                       let tagItemsLength = i < tags.length && tags[i].answer_score > 0;
  179.                                       return i < tagItemsLength;
  180.                                   },
  181.                                   (next) => {
  182.                                       req.get({
  183.                                           "url": `http://api.stackexchange.com/2.2/tags/${tags[i].tag_name}/related?site=stackoverflow`,
  184.                                           json: true,
  185.                                           headers: {'accept-encoding': 'gzip'},
  186.                                           gzip: true
  187.                                       }, (err, httpResponse, body) => {
  188.                                           let related_tags = body.items.slice(0, 5);
  189.                                          
  190.                                          
  191.                                           for(let j = 0; j < related_tags.length; j++ ) {
  192.                                               let tag = related_tags[j].name.toLowerCase();
  193.                                               if(LANGUAGES_ARRAY_LOWCASE.indexOf(tag) >=0) {                      
  194.                                                   if( !SOresult.hasOwnProperty(tag) ){
  195.                                                       SOresult[tag] = 0;
  196.                                                   }
  197.                                                   SOresult[tag] += tags[i].answer_score;
  198.                                                   break;
  199.                                               }
  200.                                           }
  201.                                           i++;
  202.                                           next();
  203.                                       });    
  204.                                   },
  205.                                 (err, n) => {
  206.                                       Object.keys(ghData).forEach((lang) => {
  207.                                           let resultObj = {};
  208.                                           let q1 = asyncQuery(getForksCountQuery(ghData[lang].join()));
  209.                                           asyncQuery(getForksCountQuery(ghData[lang].join())).then(forks => {
  210.                                               resultObj.forksCount = forks[0].forksCount;
  211.                                              
  212.                                               asyncQuery(getStarsCountQuery(ghData[lang].join())).then(stars => {
  213.                                                   resultObj.starsCount = stars[0].starsCount;
  214.                                                  
  215.                                                   asyncQuery(getUserCommitsCountQuery(ghData[lang].join(), GHid)).then(allCommits => {
  216.                                                       resultObj.allCommitsCount = allCommits[0].allCommitsCount;
  217.                                                      
  218.                                                       asyncQuery(getUserCommitsCountQuery(ghData[lang].join(), GHid)).then(userCommits => {
  219.                                                           resultObj.userCommitsCount = userCommits[0].userCommitsCount;
  220.                                                          
  221.                                                           asyncQuery(getClosedPRCountQuery(ghData[lang].join(), GHid)).then(closedPR => {
  222.                                                               resultObj.closedPRCount = closedPR[0].closedPRCount;
  223.                                                              
  224.                                                               asyncQuery(getAllPRCountQuery(ghData[lang].join(), GHid)).then(openedPR => {
  225.                                                                   resultObj.openedPRCount = openedPR[0].openedPRCount;
  226.                                                                  
  227.                                                                   asyncQuery(getMergedPRCountQuery(ghData[lang].join(), GHid)).then(mergedPR => {
  228.                                                                       resultObj.mergedPRCount = mergedPR[0].mergedPRCount;
  229.                                                                      
  230.                                                                       asyncQuery(getFollowersCountQuery(ghData[lang].join(), GHid)).then(followers => {
  231.                                                                           resultObj.followersCount = followers[0].followersCount;
  232.                                                                           resultObj.eval = SOresult[lang.toLocaleLowerCase()] || 0;
  233.                                                                           console.log("so ");
  234.                                                                           console.log(SOresult);
  235.                                                                           // console.log(resultObj);
  236.                                                                           // console.log(`${lang}.json`)
  237.                                                                           var json = JSON.stringify(resultObj);
  238.                                                                           fs.appendFile(`${lang}.json`, `${json} \n`, function (err) {
  239.                                                                               if (err) throw err;
  240.                                                                               setTimeout(loop, 1500)
  241.                                                                           });
  242.                                                                       })
  243.                                                                   })
  244.                                                               })
  245.                                                           })
  246.                                                       })
  247.                                                   })
  248.                                               })
  249.                                           })
  250.                                       });
  251.                                   });
  252.                               });
  253.                           });
  254.                       }
  255.                       else {
  256.                           console.log("after if(reposList.length)")
  257.                           loop();
  258.                       }
  259.                       //
  260.               })
  261.             }
  262.             else {
  263.                 console.log("after if(ghUser.length)")
  264.                 loop();
  265.             }
  266.             //console.log("after if(ghUser.length)")
  267.             //loop();
  268.              
  269.             //  setTimeout(() => {
  270.                  
  271.             //  }, 50000)
  272.              
  273.           });
  274.       }
  275.   }());
  276. }
  277.  
  278. init();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement