From 30dd90769c0984144229438490260da74030d7b2 Mon Sep 17 00:00:00 2001 From: Rishabh Rawat Date: Mon, 3 Jun 2019 20:22:52 +0530 Subject: [PATCH] Refactor the codebase (#213) * refactor the codebase. * rename recent contributors utility file. --- dist/community-toolbox.js | 867 ++++++++++-------- examples/demo.js | 96 ++ index.html | 101 +- src/UI/contributorsUI.js | 26 + src/UI/issuesUI.js | 37 + src/UI/recentContributorsUI.js | 24 + src/UI/ui.js | 76 -- src/models/{index.js => crud.js} | 68 +- src/models/initialize.js | 61 ++ src/models/utils.js | 10 +- src/scripts/community-toolbox.js | 187 ++-- src/utils/contributorsUtil.js | 185 ++++ ...AllContribsUtility.js => fetchRepoUtil.js} | 13 +- ...ommitsUtility.js => recentContribsUtil.js} | 81 +- src/utils/repoContributorsUtility.js | 97 -- 15 files changed, 1031 insertions(+), 898 deletions(-) create mode 100644 examples/demo.js create mode 100644 src/UI/contributorsUI.js create mode 100644 src/UI/issuesUI.js create mode 100644 src/UI/recentContributorsUI.js delete mode 100644 src/UI/ui.js rename src/models/{index.js => crud.js} (54%) create mode 100644 src/models/initialize.js create mode 100644 src/utils/contributorsUtil.js rename src/utils/{getAllContribsUtility.js => fetchRepoUtil.js} (75%) rename src/utils/{getRecentCommitsUtility.js => recentContribsUtil.js} (73%) delete mode 100644 src/utils/repoContributorsUtility.js diff --git a/dist/community-toolbox.js b/dist/community-toolbox.js index 4d7991db..b0b5b32f 100644 --- a/dist/community-toolbox.js +++ b/dist/community-toolbox.js @@ -26696,7 +26696,7 @@ module.exports={ "_args": [ [ "elliptic@6.4.1", - "/home/stephqian/community-toolbox" + "/home/rishabh570/community-toolbox" ] ], "_development": true, @@ -26722,7 +26722,7 @@ module.exports={ ], "_resolved": "https://registry.npmjs.org/elliptic/-/elliptic-6.4.1.tgz", "_spec": "6.4.1", - "_where": "/home/stephqian/community-toolbox", + "_where": "/home/rishabh570/community-toolbox", "author": { "name": "Fedor Indutny", "email": "fedor@indutny.com" @@ -27584,7 +27584,7 @@ module.exports={ ], "_resolved": "git://github.com/jywarren/github-api-simple.git#cb5b7f778ea9c8b65641b64b8c02f43cedf6672e", "_spec": "github-api-simple@git://github.com/jywarren/github-api-simple.git#patch-2", - "_where": "/home/stephqian/community-toolbox", + "_where": "/home/rishabh570/community-toolbox", "author": { "name": "Michiel van der Velde", "email": "michiel@michielvdvelde.nl" @@ -77485,7 +77485,7 @@ module.exports={ "_args": [ [ "tough-cookie@2.4.3", - "/home/stephqian/community-toolbox" + "/home/rishabh570/community-toolbox" ] ], "_development": true, @@ -77510,7 +77510,7 @@ module.exports={ ], "_resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-2.4.3.tgz", "_spec": "2.4.3", - "_where": "/home/stephqian/community-toolbox", + "_where": "/home/rishabh570/community-toolbox", "author": { "name": "Jeremy Stashewsky", "email": "jstash@gmail.com" @@ -81613,138 +81613,108 @@ function extend() { } },{}],399:[function(require,module,exports){ +var insertContributorsExec = false; + + +function insertContributors(AllContributors){ + // This removes the spinner icon as soon as contributors list is loaded + document.getElementById("spinner-icon").style.display = "none"; + + let totalContributors = 0; + var usernames = AllContributors.map(function getContributorUsername(c) { + return `@${c.login}`; + }); + var avatars = AllContributors.map(function getContributorAvatarURL(c) { + return ``; + }); + totalContributors += AllContributors.length; + if(insertContributorsExec) $('.contributors > .usernames').append(', '); + $('.contributors-head').html('Contributors ('+totalContributors+'+)'); + $('.contributors > .usernames').append(usernames.join(', ')); + $('.contributors > .avatars').append(avatars.join('')); + insertContributorsExec=true; + } + + +module.exports = { + insertContributors: insertContributors, +}; +},{}],400:[function(require,module,exports){ var moment = require('moment'); -function generateIssueHtml(title, body, githubUrl, repo) { - var repoName = githubUrl.split('/')[4], - repoUrl = githubUrl.split('/').slice(0, 5).join('/'), - html = '
\ - \ -
\ - ' + body + '\ -
\ -
'; - return html; -} + +function generateIssueHtml(title, body, githubUrl, repo) { + var repoName = githubUrl.split('/')[4], + repoUrl = githubUrl.split('/').slice(0, 5).join('/'), + html = '
\ + \ +
\ + ' + body + '\ +
\ +
'; + return html; + } + function insertIssue(issue, el) { - var body = ""; - body += "
" - issue.labels.forEach(function(label) { - body += "" + label.name + " "; - }); - body += "
"; - body += "#" + issue.number + " opened " + moment(issue.updated_at).fromNow() + " "; - body += "by " + issue.user.login + ""; - body += " " + issue.comments; - $(el).append(generateIssueHtml(issue.title, body, issue.html_url, issue)); + var body = ""; + body += "
" + issue.labels.forEach(function(label) { + body += "" + label.name + " "; + }); + body += "
"; + body += "#" + issue.number + " opened " + moment(issue.updated_at).fromNow() + " "; + body += "by " + issue.user.login + ""; + body += " " + issue.comments; + $(el).append(generateIssueHtml(issue.title, body, issue.html_url, issue)); } -//Check if function executed so we can add a comma -var insertContributorsExec = false; -var insertRecentContributorsExec = false; -function insertContributors(AllContributors){ - // This removes the spinner icon as soon as contributors list is loaded - document.getElementById("spinner-icon").style.display = "none"; +module.exports = { + generateIssueHtml: generateIssueHtml, + insertIssue: insertIssue, +}; +},{"moment":271}],401:[function(require,module,exports){ +var insertRecentContributorsExec = false; - let totalContributors = 0; - var usernames = AllContributors.map(function getContributorUsername(c) { - return `@${c.login}`; - }); - var avatars = AllContributors.map(function getContributorAvatarURL(c) { - return ``; - }); - totalContributors += AllContributors.length; - if(insertContributorsExec) $('.contributors > .usernames').append(', '); - $('.contributors-head').html('Contributors ('+totalContributors+'+)'); - $('.contributors > .usernames').append(usernames.join(', ')); - $('.contributors > .avatars').append(avatars.join('')); - insertContributorsExec=true; -} function insertRecentContributors(AllContributors){ - let recentContributors = 0; - let usernames = AllContributors.map((commit, i) => { - return `@${commit.author.login}`; - }) - let avatars = AllContributors.map((commit, i) => { - return ``; - }) - recentContributors += AllContributors.length; - - if(insertRecentContributorsExec) $('.recent-contributors > .usernames').append(', '); - $('.recent-contributors-head').html('Recent Contributors ('+recentContributors+'+)'); - $('.recent-contributors > .usernames').html(usernames.join(', ')); - $('.recent-contributors > .avatars').html(avatars.join('')); - insertRecentContributorsExec=true; -} + let recentContributors = 0; + let usernames = AllContributors.map((commit, i) => { + return `@${commit.author.login}`; + }) + let avatars = AllContributors.map((commit, i) => { + return ``; + }) + recentContributors += AllContributors.length; + + if(insertRecentContributorsExec) $('.recent-contributors > .usernames').append(', '); + $('.recent-contributors-head').html('Recent Contributors ('+recentContributors+'+)'); + $('.recent-contributors > .usernames').html(usernames.join(', ')); + $('.recent-contributors > .avatars').html(avatars.join('')); + insertRecentContributorsExec=true; + } + module.exports = { - generateIssueHtml: generateIssueHtml, - insertIssue: insertIssue, - insertContributors: insertContributors, insertRecentContributors: insertRecentContributors, }; - -},{"moment":271}],400:[function(require,module,exports){ - +},{}],402:[function(require,module,exports){ let db; +let init = require('../models/initialize') -function initialize() { - let dbReq = indexedDB.open('publiclabDB'); - - // Fires when upgrade needed - dbReq.onupgradeneeded = function(event) { - // Set the db variable to our database so we can use it - db = event.target.result; - - // Create an object store named toolbox, or retrieve it if it already exists. - // Object stores in databases are where data are stored. - let toolbox; - if (!db.objectStoreNames.contains('toolbox')) { - toolbox = db.createObjectStore('toolbox', {autoIncrement: true, keyPath: "keys"}); - } else { - toolbox = db.transaction("toolbox", "readwrite").objectStore("toolbox"); - } - - // If there isn't already a KEY index, make one so we can query toolbox - // by their KEY - if (!toolbox.indexNames.contains('keys')) { - toolbox.createIndex('keys', 'keys', { unique: true }); - }else { - console.log("KEY index is already created"); - } - - // If there isn't already a CONTENT index, make one so we can query toolbox - // by their CONTENT - if (!toolbox.indexNames.contains('content')) { - toolbox.createIndex('content', 'content', { unique: false }); - } else { - console.log("content index is already created"); - } - } - - // Fires once the database is opened (and onupgradeneeded completes, if - // onupgradeneeded was called) - dbReq.onsuccess = function(event) { - // Set the db variable to our database so we can use it - db = event.target.result; - } - - // Fires when we can't open the database - dbReq.onerror = function(event) { - console.log('error opening database'); - } +function populateDb() { + return init.dbInit().then((response) => { + db = response; + return; + }); } -// We make sure that initialize function gets fired once the DOM content is loaded -window.addEventListener('DOMContentLoaded', initialize()); - +// Stores items to the database function saveContentToDb(queryKey, queryContent) { // Start a database transaction and get the toolbox object store let tx = db.transaction(["toolbox"], 'readwrite'); @@ -81766,6 +81736,7 @@ function saveContentToDb(queryKey, queryContent) { } +// Fetches items from the database function getContentFromDb(query) { let tx = db.transaction(["toolbox"], 'readonly'); let store = tx.objectStore("toolbox"); @@ -81790,7 +81761,7 @@ function getContentFromDb(query) { } -// This is not implemented fully yet...(this is WIP) +// Deletes items from the database function deleteItemFromDb(query) { let tx = db.transaction(["toolbox"], 'readwrite'); let store = tx.objectStore("toolbox"); @@ -81831,13 +81802,76 @@ function deleteItemFromDb(query) { // EXPORTS +module.exports.saveContentToDb = saveContentToDb; +module.exports.getContentFromDb = getContentFromDb; +module.exports.deleteItemFromDb = deleteItemFromDb; +module.exports.populateDb = populateDb; + +},{"../models/initialize":403}],403:[function(require,module,exports){ +// This function is responsible for setting up the database +function dbInit() { + let db; + let dbReq = indexedDB.open('publiclabDB'); + return new Promise((resolve, reject) => { + // Fires when upgrade needed + dbReq.onupgradeneeded = function(event) { + // Set the db variable to our database so we can use it + db = event.target.result; + + // Create an object store named toolbox, or retrieve it if it already exists. + // Object stores in databases are where data are stored. + let toolbox; + if (!db.objectStoreNames.contains('toolbox')) { + toolbox = db.createObjectStore('toolbox', {autoIncrement: true, keyPath: "keys"}); + } else { + toolbox = db.transaction("toolbox", "readwrite").objectStore("toolbox"); + } + + // If there isn't already a KEY index, make one so we can query toolbox + // by their KEY + if (!toolbox.indexNames.contains('keys')) { + toolbox.createIndex('keys', 'keys', { unique: true }); + }else { + console.log("KEY index is already created"); + } + + // If there isn't already a CONTENT index, make one so we can query toolbox + // by their CONTENT + if (!toolbox.indexNames.contains('content')) { + toolbox.createIndex('content', 'content', { unique: false }); + } else { + console.log("content index is already created"); + } + + } + + // Fires once the database is opened (and onupgradeneeded completes, if + // onupgradeneeded was called) + dbReq.onsuccess = function(event) { + // Set the db variable to our database so we can use it + db = event.target.result; + resolve(db); + } + + // Fires when we can't open the database + dbReq.onerror = function(event) { + console.log('error opening database'); + resolve(null); + } + + }) + + +} + + module.exports = { - saveContentToDb: saveContentToDb, - getContentFromDb: getContentFromDb, - deleteItemFromDb: deleteItemFromDb, + dbInit: dbInit, } -},{}],401:[function(require,module,exports){ -var model = require('./index'); + + +},{}],404:[function(require,module,exports){ +var model = require('./crud'); function setItem(queryKey, queryContent) { @@ -81861,12 +81895,11 @@ function deleteItem(query) { // EXPORTS -module.exports = { - setItem: setItem, - getItem: getItem, - deleteItem: deleteItem, -} -},{"./index":400}],402:[function(require,module,exports){ +module.exports.setItem = setItem; +module.exports.getItem = getItem; +module.exports.deleteItem = deleteItem; + +},{"./crud":402}],405:[function(require,module,exports){ // view-source:http://www.chartjs.org/samples/latest/charts/bar/vertical.html function generateChart(args) { @@ -81919,22 +81952,28 @@ function generateChart(args) { module.exports = generateChart; -},{}],403:[function(require,module,exports){ -CommunityToolbox = function CommunityToolbox(org, repo) { +},{}],406:[function(require,module,exports){ - var SimpleApi = require("github-api-simple") +CommunityToolbox = function CommunityToolbox(org, repo) { + + + var SimpleApi = require('github-api-simple') var api = new SimpleApi(); - var ui = require('../UI/ui'); - var getAllContribsUtility = require('../utils/getAllContribsUtility'); - var repoContributorsUtility = require('../utils/repoContributorsUtility'); - var getRecentCommitsUtility = require('../utils/getRecentCommitsUtility'); - - var model_utils = require('../models/utils'); - - const requestP = require('request-promise'); - var parse = require('parse-link-header'); - - var options = { + + var crud = require('../models/crud') + var issuesUI = require('../UI/issuesUI') + var model_utils = require('../models/utils') + var fetchReposUtil = require('../utils/fetchRepoUtil') + var contributorsUI = require('../UI/contributorsUI') + var contributorsUtil = require('../utils/contributorsUtil') + var recentContributorsUI = require('../UI/recentContributorsUI') + var recentContribsUtil = require('../utils/recentContribsUtil') + + const requestP = require('request-promise') + var parse = require('parse-link-header') + var chart = require('./chart'); + + var options = { 'qs': { 'sort': 'pushed', 'direction': 'desc', // optional, GitHub API uses 'desc' by default for 'pushed' @@ -81966,60 +82005,33 @@ CommunityToolbox = function CommunityToolbox(org, repo) { .then(callback); } - // This runs at the very start of page load and stores all the repos and all the - // contributors in the database - function storeAllContributorsInDatabase(org) { - let AllContributors = []; - let promises = []; - var contributorSet = new Set([]); - return new Promise((resolve, reject) => { - model_utils.getItem('allContributors').then((allContributors) => { - // If all contributors list is not in the database, it makes a fresh call to Github API - if(allContributors == null || allContributors == undefined || allContributors.length == 0) { - getAllContribsUtility.getAllRepos(org) - .then(function gotRepos(res) { - let splicedRepos = res.splice(0,20); - splicedRepos.map(function mappingToEachRepo(Repo, i) { - let promise = repoContributorsUtility.fetchRepoContributorsUtil(org, Repo) - .then(function gotRepoContributorsInStorage(contributors) { - if(contributors!=undefined && contributors.length>0) { - contributors.map((contributor, i)=> { - if(!contributorSet.has(contributor.login)) { - contributorSet.add(contributor.login); - AllContributors.push(contributor); - } - }) - } - }); - promises.push(promise); - }); - return Promise.all(promises) - .then(()=> { - // Storing array containing all the contributors list across 20 most active - // repos to database - model_utils.setItem('allContributors', AllContributors); - // Saves current time in epoch, used for flushing out the stored data - // after 24 hours - let currentTime = (new Date).getTime(); - model_utils.setItem('allContributorsExpiry', currentTime); - resolve(AllContributors); - }) + + function initialize(org, repo) { + return new Promise((resolve, reject) => { + return crud.populateDb() + .then((res) => { + return true; }) - } - // If all contributors list is in the database, it simply returns that as a resolved promise - else { - resolve(allContributors); - console.log("allContributors already in database..."); - } - }) - }); - } + .then((dummy) => { + return model_utils.getItem('repos').then((response) => { + if(response==null || response==undefined) { + // Fetches and stores the list of repositories when the page loads + return fetchReposUtil.getAllRepos(org) + .then((resp) => { + resolve(true); + }); + } + resolve(true); + }); + }); + }); + } // This function is responsible for showing contributors // on a multi-repository view - function getAllContributors(org) { - return storeAllContributorsInDatabase(org).then((allContributors) => { + function showAllContributors(org) { + return contributorsUtil.storeAllContributorsInDatabase(org).then((allContributors) => { // If the stored data is not undefined or null, execution goes here if(allContributors!=null && allContributors!=undefined && allContributors.length>0) { // Flushes contributors list from the database after every single day @@ -82039,15 +82051,15 @@ CommunityToolbox = function CommunityToolbox(org, repo) { // If the data is not in the database, it gets fetched from storeAllContributorsInDatabase function if(AllContributors == null || AllContributors == undefined || AllContributors.length==0) { - storeAllContributorsInDatabase(org).then(function gotAllContributors(AllContributors) { + contributorsUtil.storeAllContributorsInDatabase(org).then(function gotAllContributors(AllContributors) { // Provides fetched contributors list to UI function for rendering it // to the user - ui.insertContributors(AllContributors); + contributorsUI.insertContributors(AllContributors); }) } // If stored data is not null and undefined, process it else { - ui.insertContributors(AllContributors); + contributorsUI.insertContributors(AllContributors); } }) }); @@ -82063,7 +82075,7 @@ CommunityToolbox = function CommunityToolbox(org, repo) { // This function is responsible for showing all the contributors for a particular repository function showRepoContributors(org, repo) { - return storeAllContributorsInDatabase(org).then((allContributors) => { + return contributorsUtil.storeAllContributorsInDatabase(org).then((allContributors) => { // If the stored data is not undefined or null, execution goes here if(allContributors != null && allContributors!=undefined && allContributors.length>0) { // Flushes repoContributors from the database after every single day @@ -82082,15 +82094,15 @@ CommunityToolbox = function CommunityToolbox(org, repo) { model_utils.getItem(repo).then((repoContributors) => { // If we don't have repoContributors in the database, we fetch them from Github if (repoContributors == null || repoContributors == undefined) { - repoContributorsUtility.fetchRepoContributorsUtil(org, repo) + contributorsUtil.fetchRepoContributorsUtil(org, repo) .then(function gotRepoContributorsInStorage (contributors) { - ui.insertContributors(contributors); + contributorsUI.insertContributors(contributors); return; }) } // If we have repoContributors in the database, we save a network call :) else { - ui.insertContributors(repoContributors); + contributorsUI.insertContributors(repoContributors); return; } }) @@ -82103,59 +82115,22 @@ CommunityToolbox = function CommunityToolbox(org, repo) { }) } - // Stores all the Recent Contributors in the database - function storeAllRecentContribsInitially(org, repo) { - // We make queryTime 1 month behind the current time, to pass it as query in the request - let d = (new Date); - d.setDate(d.getDate() - 30); - let queryTime = d.toISOString(); - model_utils.getItem('repos').then((repos) => { - if(repos!=null && repos!=undefined) { - return model_utils.getItem('recent-present').then((result)=> { - if(result!=null && result!=undefined) { - return result; - } - else { - if(repos!=null || repos!=undefined) { - return getRecentCommitsUtility.fetchAllRecentMonthCommits(org, repos, queryTime) - .then((result) => { - model_utils.setItem('recent-present', 'true'); - return result; - }) - } - else { - getAllContribsUtility.getAllRepos(org).then((repos) => { - if(repos!=null || repos!=undefined || repos.length>0) { - return getRecentCommitsUtility.fetchAllRecentMonthCommits(org, repos, queryTime) - .then((result) => { - model_utils.setItem('recent-present', 'true'); - return result; - }) - } - }); - } - } - }); - } - }) - } - // Function for fetching and showing recent contributors - function getRecentCommits(org, repo, recencyLabel) { - return storeAllRecentContribsInitially(org, repo).then((result)=>{ + function showRecentContributors(org, repo, recencyLabel) { + return contributorsUtil.storeAllRecentContribsInitially(org, repo).then((result)=>{ if(recencyLabel==='month') { - return getRecentCommitsUtility.getCommitsLastMonth(org, repo) + return recentContribsUtil.getCommitsLastMonth(org, repo) .then(function gotCommits(commits) { // Push data to UI - ui.insertRecentContributors(commits); + recentContributorsUI.insertRecentContributors(commits); return; }); } else { - return getRecentCommitsUtility.getCommitsLastWeek(org, repo) + return recentContribsUtil.getCommitsLastWeek(org, repo) .then((weekly_contribs) => { // Push data to UI - ui.insertRecentContributors(weekly_contribs); + recentContributorsUI.insertRecentContributors(weekly_contribs); return; }); } @@ -82167,35 +82142,229 @@ CommunityToolbox = function CommunityToolbox(org, repo) { .getIssuesForRepo(org, repo, { qs: { labels: label } }) .then(function onGotIssues(issues) { issues.forEach(function(issue) { - toolbox.ui.insertIssue(issue, selector); + toolbox.issuesUI.insertIssue(issue, selector); }); }); } - var chart = require('./chart'); + + + + // externally available API return { api: api, - ui: ui, + issuesUI: issuesUI, + contributorsUI: contributorsUI, + recentContributorsUI: recentContributorsUI, parse: parse, chart: chart, options: options, getIssuesForRepo: getIssuesForRepo, getIssuesForOrg: getIssuesForOrg, - getRecentCommits: getRecentCommits, - getCommitsForRepo: getCommitsForRepo, - storeAllContributorsInDatabase: storeAllContributorsInDatabase, - getAllContributors: getAllContributors, + showRecentContributors: showRecentContributors, + getCommitsForRepo: getCommitsForRepo, // storeAllContributorsInDatabase: storeAllContributorsInDatabase, + showAllContributors: showAllContributors, showRepoContributors: showRepoContributors, - displayIssuesForRepo: displayIssuesForRepo + displayIssuesForRepo: displayIssuesForRepo, + initialize: initialize } } module.exports = CommunityToolbox; -},{"../UI/ui":399,"../models/utils":401,"../utils/getAllContribsUtility":404,"../utils/getRecentCommitsUtility":405,"../utils/repoContributorsUtility":406,"./chart":402,"github-api-simple":151,"parse-link-header":278,"request-promise":323}],404:[function(require,module,exports){ +},{"../UI/contributorsUI":399,"../UI/issuesUI":400,"../UI/recentContributorsUI":401,"../models/crud":402,"../models/utils":404,"../utils/contributorsUtil":407,"../utils/fetchRepoUtil":408,"../utils/recentContribsUtil":409,"./chart":405,"github-api-simple":151,"parse-link-header":278,"request-promise":323}],407:[function(require,module,exports){ + +var SimpleApi = require("github-api-simple") +var api = new SimpleApi() +var parse = require('parse-link-header') +var model_utils = require('../models/utils') +var fetchRepoUtil = require('./fetchRepoUtil') +var recentContribsUtil = require('../utils/recentContribsUtil') + +// This is a utility function which decides whether to make a single request for fetching +// each repository's contributors or multiple ones. +function fetchRepoContributorsUtil(org, repo) { + return new Promise((resolve, reject) => { + if(repo === 'plots2') { + resolve(fetchAllRepoContributors(org, repo)); + }else { + resolve(fetchRepoContributors(org, repo)); + } + }) +} + + +// This utility helps us in getting CONTRIBUTORS for a particular repository +function fetchRepoContributors(org, repo) { + // This array is used to store the contributors from all of the repositories + let contributorsArray = []; + + return api.Repositories + .getRepoContributors(org, repo, { method:"GET", qs: { sort: 'pushed', direction: 'desc', per_page: 100 }}) + .then(function gotRepoContributors(contributors) { + if (contributors!=undefined && (contributors != null || contributors.length > 0)) { + contributors.map((contributor, i) => contributorsArray.push(contributor)); + } + }) + .then(() => { + let now = (new Date).getTime(); + model_utils.setItem(repo, contributorsArray); + model_utils.setItem(`${repo}Expiry`, now); + return contributorsArray; + }) +} + + + + +// This utility helps us in getting all the contributors for a particular repository +function fetchAllRepoContributors(org, repo) { + // This array is used to store the contributors from all of the repositories + let contributorsArray = []; + + return api.Repositories + .getRepoContributors(org, repo, {method: "HEAD", qs: { sort: 'pushed', direction: 'desc', per_page: 100 } }) + .then(function gotContribData(contribData) { + var headers = contribData; + if (headers.hasOwnProperty("link")) { + var parsed = parse(headers['link']); + if(parsed.last.page!=undefined) { + totalPages = parseInt(parsed.last.page); + } + } else { + totalPages = 1; + } + return totalPages; + }) + .then(function gotTotalPages(totalPages) { + // This array is used to store all of the promises + let promises = []; + + for(let i = 1; i <= totalPages; i++) { + var currentPromise = api.Repositories + .getRepoContributors(org, repo, { method:"GET", qs: { sort: 'pushed', direction: 'desc', per_page: 100, page:i } }) + .then(function gotRepoContributors(contributors) { + if (contributors!=undefined && (contributors != null || contributors.length > 0)) { + contributors.map((contributor, i) => contributorsArray.push(contributor)); + } + }); + // Push currentPromise to promises array + promises.push(currentPromise); + } + + // Waits for all of the promises to resolve first, sets localStorage after that... + return Promise.all(promises) + .then(()=> { + let now = (new Date).getTime(); + model_utils.setItem(repo, contributorsArray); + model_utils.setItem(`${repo}Expiry`, now); + return contributorsArray; + }); + }); +} + + +// This runs at the very start of page load and stores all the repositories and all the +// contributors in the database on initial page load +function storeAllContributorsInDatabase(org) { + let AllContributors = []; + let promises = []; + var contributorSet = new Set([]); + return new Promise((resolve, reject) => { + model_utils.getItem('allContributors').then((allContributors) => { + // If all contributors list is not in the database, it makes a fresh call to Github API + if(allContributors == null || allContributors == undefined || allContributors.length == 0) { + return model_utils.getItem('repos').then((res) => { + let splicedRepos = res.splice(0,20); + splicedRepos.map(function mappingToEachRepo(Repo, i) { + let promise = fetchRepoContributorsUtil(org, Repo) + .then(function gotRepoContributorsInStorage(contributors) { + if(contributors!=undefined && contributors.length>0) { + contributors.map((contributor, i)=> { + if(!contributorSet.has(contributor.login)) { + contributorSet.add(contributor.login); + AllContributors.push(contributor); + } + }) + } + }); + promises.push(promise); + }); + return Promise.all(promises) + .then(()=> { + // Storing array containing all the contributors' list across 20 most active + // repos to database + model_utils.setItem('allContributors', AllContributors); + // Saves current time in epoch, used for flushing out the stored data + // after 24 hours + let currentTime = (new Date).getTime(); + model_utils.setItem('allContributorsExpiry', currentTime); + resolve(AllContributors); + }) + }) + } + // If all contributors list is in the database, it simply returns that as a resolved promise + else { + resolve(allContributors); + } + }) + }); +} + + +// Stores all the Recent Contributors in the database +function storeAllRecentContribsInitially(org, repo) { + // We make queryTime 1 month behind the current time, to pass it as query in the request + let d = (new Date); + d.setDate(d.getDate() - 30); + let queryTime = d.toISOString(); + return model_utils.getItem('repos').then((repos) => { + return model_utils.getItem('recent-present').then((result)=> { + if(result!=null && result!=undefined) { + return result; + } + else { + if(repos!=null || repos!=undefined) { + return recentContribsUtil.fetchAllRecentMonthCommits(org, repos, queryTime) + .then((result) => { + model_utils.setItem('recent-present', 'true'); + return result; + }) + } else { + fetchRepoUtil.getAllRepos(org).then((repos) => { + if(repos!=null || repos!=undefined) { + return recentContribsUtil.fetchAllRecentMonthCommits(org, repos, queryTime) + .then((result) => { + model_utils.setItem('recent-present', 'true'); + return result; + }) + } + }); + } + } + }); + }) + +} + + + +// EXPORTS +module.exports = { + fetchAllRepoContributors: fetchAllRepoContributors, + fetchRepoContributors: fetchRepoContributors, + fetchRepoContributorsUtil: fetchRepoContributorsUtil, + storeAllRecentContribsInitially: storeAllRecentContribsInitially, + storeAllContributorsInDatabase: storeAllContributorsInDatabase, +} + + +},{"../models/utils":404,"../utils/recentContribsUtil":409,"./fetchRepoUtil":408,"github-api-simple":151,"parse-link-header":278}],408:[function(require,module,exports){ +let model_utils = require('../models/utils') + // Fetches all the publiclab's repositories function getAllRepos(org) { @@ -82212,20 +82381,21 @@ function getAllRepos(org) { results.map(function mappingToEachRepo(repo, index) { return repos[index] = repo.name; }); - // Storing the repos in localStorage + return(repos); + }) + .then((repos) => { + // Storing the repos in the database model_utils.setItem('repos', repos); return(repos); - }); + }) } // EXPORTS -module.exports = { - getAllRepos: getAllRepos, -} +module.exports.getAllRepos = getAllRepos; -},{}],405:[function(require,module,exports){ -var getAllContribsUtility = require('../utils/getAllContribsUtility'); +},{"../models/utils":404}],409:[function(require,module,exports){ +var fetchRepoUtil = require('./fetchRepoUtil'); var model_utils = require('../models/utils'); // Fetches recent month commits for a particular repository @@ -82241,7 +82411,7 @@ function fetchRecentMonthCommits(org, repo, queryTime) { .then(function gotResponseJson(response) { if(response!=null) { response.map(function mappingToCommits(commit, i) { - if(commit!=null) { + if(commit.author!=null) { if(!commitersSet.has(commit.author.login)) { commitersSet.add(commit.author.login); result.push(commit); @@ -82280,7 +82450,7 @@ function fetchAllRecentMonthCommits(org, repos, queryTime) { if(response!=null) { let partialResult = []; response.map(function mappingToCommits(commit, i) { - if(commit!=null) { + if(commit.author!=null) { if(!commitersSet.has(commit.author.login)) { commitersSet.add(commit.author.login); partialResult.push(commit); @@ -82293,9 +82463,6 @@ function fetchAllRecentMonthCommits(org, repos, queryTime) { let currTime = (new Date).getTime(); model_utils.setItem(`recent-${repo}-month-commits`, partialResult); model_utils.setItem(`recent-${repo}-month-expiry`, currTime); - - // Push every repo's contribs to results array - // results.push(partialResult); } return true; }); @@ -82312,6 +82479,7 @@ function fetchAllRecentMonthCommits(org, repos, queryTime) { } +// Fetches recent week's commits for a particular repo function getCommitsLastWeek(org, repo) { let contribs = []; @@ -82320,7 +82488,6 @@ function getCommitsLastWeek(org, repo) { let timeToday = (new Date).getTime(); // If recent month's commits expiry time is 1 day behind the current time, flush them out. if(recentCommitsWeekExpiry!=null && recentCommitsWeekExpiry!=undefined && ((timeToday-recentCommitsWeekExpiry)/1000)>=86400) { - console.log("deleting"); return Promise.all([model_utils.deleteItem(`recent-${repo}-week-expiry`), model_utils.deleteItem(`recent-${repo}-week-commits`)]) .then(()=> { return true; @@ -82333,6 +82500,7 @@ function getCommitsLastWeek(org, repo) { return result; } else { + // We save extra request by filtering commits-made-last-week from commits-made-last month return getCommitsLastMonth(org, repo) .then((commits_last_month) => { commits_last_month.map((commit_last_month, index) => { @@ -82369,48 +82537,49 @@ function within_this_week(date) { // Fetches recent month's commits for a particular repo or all of the repos (10 repos) function getCommitsLastMonth(org, repo) { - model_utils.getItem('repos').then((repos) => { + return model_utils.getItem('repos').then((repos) => { if(repos!=null && repos!=undefined) { return model_utils.getItem(`recent-${repo}-month-expiry`) .then((recentCommitsMonthExpiry) => { - let timeToday = (new Date).getTime(); - // If recentCommits expiry time is 1 day behind the current time, flush them out. - if(recentCommitsMonthExpiry!=null && recentCommitsMonthExpiry!=undefined && ((timeToday-recentCommitsMonthExpiry)/1000)>=86400) { - console.log("Deleting month contribs"); - return Promise.all([model_utils.deleteItem(`recent-${repo}-month-commits`), model_utils.deleteItem(`recent-${repo}-month-expiry`)]) - .then(() => { - return true; - }) - } - return true; - }) - .then((boolean) => { - return model_utils.getItem(`recent-${repo}-month-commits`).then((result) => { - if(result!=null && result!=undefined) { - return result; + let timeToday = (new Date).getTime(); + // If recentCommits expiry time is 1 day behind the current time, flush them out. + if(recentCommitsMonthExpiry!=null && recentCommitsMonthExpiry!=undefined && ((timeToday-recentCommitsMonthExpiry)/1000)>=86400) { + return Promise.all([model_utils.deleteItem(`recent-${repo}-month-commits`), model_utils.deleteItem(`recent-${repo}-month-expiry`)]) + .then(() => { + return true; + }) } - else { - // We make queryTime 1 month behind the current time, to pass it as query in the request - let d = (new Date); - d.setDate(d.getDate() - 30); - let queryTime = d.toISOString(); - if(repo==='all') { - return fetchAllRecentMonthCommits(org, repos, queryTime) - .then(function gotRecentCommitsInStorage(month_commits) { - console.log("got all monthly contribs from fetchAllRecentMonthCommits"); - return month_commits; - }); + return true; + }) + .then((boolean) => { + return model_utils.getItem(`recent-${repo}-month-commits`).then((result) => { + if(result!=null && result!=undefined) { + return result; } else { - return fetchRecentMonthCommits(org, repo, queryTime) - .then(function gotRecentCommitsInStorage(month_commits) { - console.log("got repo's month commits from fetch"); - return month_commits; - }) + // We make queryTime 1 month behind the current time, to pass it as query in the request + let d = (new Date); + d.setDate(d.getDate() - 30); + let queryTime = d.toISOString(); + if(repo==='all') { + return fetchAllRecentMonthCommits(org, repos, queryTime) + .then(function gotRecentCommitsInStorage(month_commits) { + console.log("got all monthly contribs from fetchAllRecentMonthCommits"); + return month_commits; + }); + } + else { + return fetchRecentMonthCommits(org, repo, queryTime) + .then(function gotRecentCommitsInStorage(month_commits) { + console.log("got repo's month commits from fetch"); + return month_commits; + }) + } } - } + }) }) - }) + } else { + console.log("repos are not there yet!!!"); } }) } @@ -82426,102 +82595,4 @@ module.exports = { within_this_week: within_this_week } -},{"../models/utils":401,"../utils/getAllContribsUtility":404}],406:[function(require,module,exports){ -var SimpleApi = require("github-api-simple") -var api = new SimpleApi(); -var parse = require('parse-link-header'); -var model_utils = require('../models/utils'); - -// This is a utility function which decides whether to make a single request for fetching -// each repo's contributors or multiple ones. -function fetchRepoContributorsUtil(org, repo) { - return new Promise((resolve, reject) => { - if(repo === 'plots2') { - resolve(fetchAllRepoContributors(org, repo)); - }else { - resolve(fetchRepoContributors(org, repo)); - } - }) -} - - -// This utility helps us in getting CONTRIBUTORS for a particular repository -function fetchRepoContributors(org, repo) { - // This array is used to store the contributors from all of the repositories - let contributorsArray = []; - - return api.Repositories - .getRepoContributors(org, repo, { method:"GET", qs: { sort: 'pushed', direction: 'desc', per_page: 100 }}) - .then(function gotRepoContributors(contributors) { - if (contributors!=undefined && (contributors != null || contributors.length > 0)) { - contributors.map((contributor, i) => contributorsArray.push(contributor)); - } - }) - .then(() => { - let now = (new Date).getTime(); - model_utils.setItem(repo, contributorsArray); - console.log("saving ",repo,"'s all contribs"); - model_utils.setItem(`${repo}Expiry`, now); - return contributorsArray; - }) - -} - - - - -// This utility helps us in getting all the contributors for a particular repository -function fetchAllRepoContributors(org, repo) { - // This array is used to store the contributors from all of the repositories - let contributorsArray = []; - - return api.Repositories - .getRepoContributors(org, repo, {method: "HEAD", qs: { sort: 'pushed', direction: 'desc', per_page: 100 } }) - .then(function gotContribData(contribData) { - var headers = contribData; - if (headers.hasOwnProperty("link")) { - var parsed = parse(headers['link']); - if(parsed.last.page!=undefined) { - totalPages = parseInt(parsed.last.page); - } - } else { - totalPages = 1; - } - return totalPages; - }) - .then(function gotTotalPages(totalPages) { - // This array is used to store all of the promises - let promises = []; - - for(let i = 1; i <= totalPages; i++) { - var currentPromise = api.Repositories - .getRepoContributors(org, repo, { method:"GET", qs: { sort: 'pushed', direction: 'desc', per_page: 100, page:i } }) - .then(function gotRepoContributors(contributors) { - if (contributors!=undefined && (contributors != null || contributors.length > 0)) { - contributors.map((contributor, i) => contributorsArray.push(contributor)); - } - }); - // Push currentPromise to promises array - promises.push(currentPromise); - } - - // Waits for all of the promises to resolve first, sets localStorage after that... - return Promise.all(promises) - .then(()=> { - let now = (new Date).getTime(); - model_utils.setItem(repo, contributorsArray); - model_utils.setItem(`${repo}Expiry`, now); - return contributorsArray; - }); - }); - } - - - -// EXPORTS -module.exports = { - fetchAllRepoContributors: fetchAllRepoContributors, - fetchRepoContributors: fetchRepoContributors, - fetchRepoContributorsUtil: fetchRepoContributorsUtil, -} -},{"../models/utils":401,"github-api-simple":151,"parse-link-header":278}]},{},[403]); +},{"../models/utils":404,"./fetchRepoUtil":408}]},{},[406]); diff --git a/examples/demo.js b/examples/demo.js new file mode 100644 index 00000000..7e19afca --- /dev/null +++ b/examples/demo.js @@ -0,0 +1,96 @@ +document.addEventListener('DOMContentLoaded', function () { + + /*Scroll to top when arrow up clicked BEGIN*/ + $(window).scroll(function() { + var height = $(window).scrollTop(); + if (height > 100) { + $('#back2Top').fadeIn(); + } else { + $('#back2Top').fadeOut(); + } + }); + + $("#back2Top").click(function(event) { + event.preventDefault(); + $("html, body").animate({ scrollTop: 0 }, "slow"); + return false; + }); + /*Scroll to top when arrow up clicked END*/ + + var toolbox; + + (function() { + + var org = urlHash().getUrlHashParameter('o') || 'publiclab'; + var repo = urlHash().getUrlHashParameter('r') || 'plots2'; + var ftoLabel = urlHash().getUrlHashParameter('f') || 'first-timers-only'; + var candidateLabel = urlHash().getUrlHashParameter('c') || 'fto-candidate'; + var recencyLabel = urlHash().getUrlHashParameter('l') || 'week'; + + toolbox = CommunityToolbox(org, repo); + + let d = document.getElementById('toggle-contributors'); + let recency_label = document.getElementById('recency-label'); + d.addEventListener("click", (e) => { + e.preventDefault(); + if (recencyLabel=="week") { + toolbox.showRecentContributors(org, repo, recencyLabel); + d.innerHTML = "Show monthly"; + recency_label.innerHTML = "last week: "; + recencyLabel = "month"; + }else if (recencyLabel=="month") { + toolbox.showRecentContributors(org, repo, recencyLabel); + d.innerHTML = "Show weekly"; + recency_label.innerHTML = "last month: "; + recencyLabel = "week"; + } + }) + + if (repo === 'all') { + + toolbox.getIssuesForOrg(org, { qs: { labels: ftoLabel } }) + .then(displayIssues('.first-timers-only')); + + toolbox.getIssuesForOrg(org, { qs: { labels: candidateLabel } }) + .then(displayIssues('.candidates')); + + toolbox.initialize(org, repo).then((res)=> { + if(res) { + // compile and display all contributors for given org + toolbox.showAllContributors(org, repo); + // Makes the toggle contributors list button click + d.click(); + } + }) + + } else { + + toolbox.api.Issues + .getIssuesForRepo(org, repo, { qs: { labels: ftoLabel } }) + .then(displayIssues('.first-timers-only')); + + toolbox.api.Issues + .getIssuesForRepo(org, repo, { qs: { labels: candidateLabel } }) + .then(displayIssues('.candidates')); + + toolbox.initialize(org, repo).then((res)=> { + if(res) { + // compile and display all contributors for given repo + toolbox.showRepoContributors(org, repo); + // Makes the toggle contributors list button click + d.click(); + } + }) + } + + function displayIssues(selector) { + return function displayIssues(issues) { + if (typeof issues === "string") issues = JSON.parse(issues).items; + issues.forEach(function(issue) { + toolbox.issuesUI.insertIssue(issue, selector); + }); + } + } + + })(); +}) diff --git a/index.html b/index.html index 87fc49cb..a2693227 100644 --- a/index.html +++ b/index.html @@ -15,7 +15,7 @@ - + @@ -169,105 +169,6 @@

Connect with us

- diff --git a/src/UI/contributorsUI.js b/src/UI/contributorsUI.js new file mode 100644 index 00000000..fe0a97da --- /dev/null +++ b/src/UI/contributorsUI.js @@ -0,0 +1,26 @@ +var insertContributorsExec = false; + + +function insertContributors(AllContributors){ + // This removes the spinner icon as soon as contributors list is loaded + document.getElementById("spinner-icon").style.display = "none"; + + let totalContributors = 0; + var usernames = AllContributors.map(function getContributorUsername(c) { + return `@${c.login}`; + }); + var avatars = AllContributors.map(function getContributorAvatarURL(c) { + return ``; + }); + totalContributors += AllContributors.length; + if(insertContributorsExec) $('.contributors > .usernames').append(', '); + $('.contributors-head').html('Contributors ('+totalContributors+'+)'); + $('.contributors > .usernames').append(usernames.join(', ')); + $('.contributors > .avatars').append(avatars.join('')); + insertContributorsExec=true; + } + + +module.exports = { + insertContributors: insertContributors, +}; \ No newline at end of file diff --git a/src/UI/issuesUI.js b/src/UI/issuesUI.js new file mode 100644 index 00000000..d779d816 --- /dev/null +++ b/src/UI/issuesUI.js @@ -0,0 +1,37 @@ +var moment = require('moment'); + + + +function generateIssueHtml(title, body, githubUrl, repo) { + var repoName = githubUrl.split('/')[4], + repoUrl = githubUrl.split('/').slice(0, 5).join('/'), + html = '
\ + \ +
\ + ' + body + '\ +
\ +
'; + return html; + } + +function insertIssue(issue, el) { + var body = ""; + body += "
" + issue.labels.forEach(function(label) { + body += "" + label.name + " "; + }); + body += "
"; + body += "#" + issue.number + " opened " + moment(issue.updated_at).fromNow() + " "; + body += "by " + issue.user.login + ""; + body += " " + issue.comments; + $(el).append(generateIssueHtml(issue.title, body, issue.html_url, issue)); +} + + +module.exports = { + generateIssueHtml: generateIssueHtml, + insertIssue: insertIssue, +}; \ No newline at end of file diff --git a/src/UI/recentContributorsUI.js b/src/UI/recentContributorsUI.js new file mode 100644 index 00000000..19cd5948 --- /dev/null +++ b/src/UI/recentContributorsUI.js @@ -0,0 +1,24 @@ +var insertRecentContributorsExec = false; + + +function insertRecentContributors(AllContributors){ + let recentContributors = 0; + let usernames = AllContributors.map((commit, i) => { + return `@${commit.author.login}`; + }) + let avatars = AllContributors.map((commit, i) => { + return ``; + }) + recentContributors += AllContributors.length; + + if(insertRecentContributorsExec) $('.recent-contributors > .usernames').append(', '); + $('.recent-contributors-head').html('Recent Contributors ('+recentContributors+'+)'); + $('.recent-contributors > .usernames').html(usernames.join(', ')); + $('.recent-contributors > .avatars').html(avatars.join('')); + insertRecentContributorsExec=true; + } + + +module.exports = { + insertRecentContributors: insertRecentContributors, +}; \ No newline at end of file diff --git a/src/UI/ui.js b/src/UI/ui.js deleted file mode 100644 index 8b12a5f1..00000000 --- a/src/UI/ui.js +++ /dev/null @@ -1,76 +0,0 @@ -var moment = require('moment'); - -function generateIssueHtml(title, body, githubUrl, repo) { - var repoName = githubUrl.split('/')[4], - repoUrl = githubUrl.split('/').slice(0, 5).join('/'), - html = '
\ - \ -
\ - ' + body + '\ -
\ -
'; - return html; -} - -function insertIssue(issue, el) { - var body = ""; - body += "
" - issue.labels.forEach(function(label) { - body += "" + label.name + " "; - }); - body += "
"; - body += "#" + issue.number + " opened " + moment(issue.updated_at).fromNow() + " "; - body += "by " + issue.user.login + ""; - body += " " + issue.comments; - $(el).append(generateIssueHtml(issue.title, body, issue.html_url, issue)); -} - -//Check if function executed so we can add a comma -var insertContributorsExec = false; -var insertRecentContributorsExec = false; - -function insertContributors(AllContributors){ - // This removes the spinner icon as soon as contributors list is loaded - document.getElementById("spinner-icon").style.display = "none"; - - let totalContributors = 0; - var usernames = AllContributors.map(function getContributorUsername(c) { - return `@${c.login}`; - }); - var avatars = AllContributors.map(function getContributorAvatarURL(c) { - return ``; - }); - totalContributors += AllContributors.length; - if(insertContributorsExec) $('.contributors > .usernames').append(', '); - $('.contributors-head').html('Contributors ('+totalContributors+'+)'); - $('.contributors > .usernames').append(usernames.join(', ')); - $('.contributors > .avatars').append(avatars.join('')); - insertContributorsExec=true; -} - -function insertRecentContributors(AllContributors){ - let recentContributors = 0; - let usernames = AllContributors.map((commit, i) => { - return `@${commit.author.login}`; - }) - let avatars = AllContributors.map((commit, i) => { - return ``; - }) - recentContributors += AllContributors.length; - - if(insertRecentContributorsExec) $('.recent-contributors > .usernames').append(', '); - $('.recent-contributors-head').html('Recent Contributors ('+recentContributors+'+)'); - $('.recent-contributors > .usernames').html(usernames.join(', ')); - $('.recent-contributors > .avatars').html(avatars.join('')); - insertRecentContributorsExec=true; -} - -module.exports = { - generateIssueHtml: generateIssueHtml, - insertIssue: insertIssue, - insertContributors: insertContributors, - insertRecentContributors: insertRecentContributors, -}; diff --git a/src/models/index.js b/src/models/crud.js similarity index 54% rename from src/models/index.js rename to src/models/crud.js index 35fc89d5..62dca9c2 100644 --- a/src/models/index.js +++ b/src/models/crud.js @@ -1,57 +1,15 @@ - let db; +let init = require('../models/initialize') -function initialize() { - let dbReq = indexedDB.open('publiclabDB'); - - // Fires when upgrade needed - dbReq.onupgradeneeded = function(event) { - // Set the db variable to our database so we can use it - db = event.target.result; - - // Create an object store named toolbox, or retrieve it if it already exists. - // Object stores in databases are where data are stored. - let toolbox; - if (!db.objectStoreNames.contains('toolbox')) { - toolbox = db.createObjectStore('toolbox', {autoIncrement: true, keyPath: "keys"}); - } else { - toolbox = db.transaction("toolbox", "readwrite").objectStore("toolbox"); - } - - // If there isn't already a KEY index, make one so we can query toolbox - // by their KEY - if (!toolbox.indexNames.contains('keys')) { - toolbox.createIndex('keys', 'keys', { unique: true }); - }else { - console.log("KEY index is already created"); - } - - // If there isn't already a CONTENT index, make one so we can query toolbox - // by their CONTENT - if (!toolbox.indexNames.contains('content')) { - toolbox.createIndex('content', 'content', { unique: false }); - } else { - console.log("content index is already created"); - } - } - - // Fires once the database is opened (and onupgradeneeded completes, if - // onupgradeneeded was called) - dbReq.onsuccess = function(event) { - // Set the db variable to our database so we can use it - db = event.target.result; - } - - // Fires when we can't open the database - dbReq.onerror = function(event) { - console.log('error opening database'); - } +function populateDb() { + return init.dbInit().then((response) => { + db = response; + return; + }); } -// We make sure that initialize function gets fired once the DOM content is loaded -window.addEventListener('DOMContentLoaded', initialize()); - +// Stores items to the database function saveContentToDb(queryKey, queryContent) { // Start a database transaction and get the toolbox object store let tx = db.transaction(["toolbox"], 'readwrite'); @@ -73,6 +31,7 @@ function saveContentToDb(queryKey, queryContent) { } +// Fetches items from the database function getContentFromDb(query) { let tx = db.transaction(["toolbox"], 'readonly'); let store = tx.objectStore("toolbox"); @@ -97,7 +56,7 @@ function getContentFromDb(query) { } -// This is not implemented fully yet...(this is WIP) +// Deletes items from the database function deleteItemFromDb(query) { let tx = db.transaction(["toolbox"], 'readwrite'); let store = tx.objectStore("toolbox"); @@ -138,8 +97,7 @@ function deleteItemFromDb(query) { // EXPORTS -module.exports = { - saveContentToDb: saveContentToDb, - getContentFromDb: getContentFromDb, - deleteItemFromDb: deleteItemFromDb, -} \ No newline at end of file +module.exports.saveContentToDb = saveContentToDb; +module.exports.getContentFromDb = getContentFromDb; +module.exports.deleteItemFromDb = deleteItemFromDb; +module.exports.populateDb = populateDb; diff --git a/src/models/initialize.js b/src/models/initialize.js new file mode 100644 index 00000000..1ba6dfca --- /dev/null +++ b/src/models/initialize.js @@ -0,0 +1,61 @@ +// This function is responsible for setting up the database +function dbInit() { + let db; + let dbReq = indexedDB.open('publiclabDB'); + return new Promise((resolve, reject) => { + // Fires when upgrade needed + dbReq.onupgradeneeded = function(event) { + // Set the db variable to our database so we can use it + db = event.target.result; + + // Create an object store named toolbox, or retrieve it if it already exists. + // Object stores in databases are where data are stored. + let toolbox; + if (!db.objectStoreNames.contains('toolbox')) { + toolbox = db.createObjectStore('toolbox', {autoIncrement: true, keyPath: "keys"}); + } else { + toolbox = db.transaction("toolbox", "readwrite").objectStore("toolbox"); + } + + // If there isn't already a KEY index, make one so we can query toolbox + // by their KEY + if (!toolbox.indexNames.contains('keys')) { + toolbox.createIndex('keys', 'keys', { unique: true }); + }else { + console.log("KEY index is already created"); + } + + // If there isn't already a CONTENT index, make one so we can query toolbox + // by their CONTENT + if (!toolbox.indexNames.contains('content')) { + toolbox.createIndex('content', 'content', { unique: false }); + } else { + console.log("content index is already created"); + } + + } + + // Fires once the database is opened (and onupgradeneeded completes, if + // onupgradeneeded was called) + dbReq.onsuccess = function(event) { + // Set the db variable to our database so we can use it + db = event.target.result; + resolve(db); + } + + // Fires when we can't open the database + dbReq.onerror = function(event) { + console.log('error opening database'); + resolve(null); + } + + }) + + +} + + +module.exports = { + dbInit: dbInit, +} + diff --git a/src/models/utils.js b/src/models/utils.js index 77be8bfb..2345121b 100644 --- a/src/models/utils.js +++ b/src/models/utils.js @@ -1,4 +1,4 @@ -var model = require('./index'); +var model = require('./crud'); function setItem(queryKey, queryContent) { @@ -22,8 +22,6 @@ function deleteItem(query) { // EXPORTS -module.exports = { - setItem: setItem, - getItem: getItem, - deleteItem: deleteItem, -} \ No newline at end of file +module.exports.setItem = setItem; +module.exports.getItem = getItem; +module.exports.deleteItem = deleteItem; diff --git a/src/scripts/community-toolbox.js b/src/scripts/community-toolbox.js index 4692c5c3..3c273778 100644 --- a/src/scripts/community-toolbox.js +++ b/src/scripts/community-toolbox.js @@ -1,18 +1,24 @@ -CommunityToolbox = function CommunityToolbox(org, repo) { - var SimpleApi = require("github-api-simple") +CommunityToolbox = function CommunityToolbox(org, repo) { + + + var SimpleApi = require('github-api-simple') var api = new SimpleApi(); - var ui = require('../UI/ui'); - var getAllContribsUtility = require('../utils/getAllContribsUtility'); - var repoContributorsUtility = require('../utils/repoContributorsUtility'); - var getRecentCommitsUtility = require('../utils/getRecentCommitsUtility'); - - var model_utils = require('../models/utils'); - - const requestP = require('request-promise'); - var parse = require('parse-link-header'); - - var options = { + + var crud = require('../models/crud') + var issuesUI = require('../UI/issuesUI') + var model_utils = require('../models/utils') + var fetchReposUtil = require('../utils/fetchRepoUtil') + var contributorsUI = require('../UI/contributorsUI') + var contributorsUtil = require('../utils/contributorsUtil') + var recentContributorsUI = require('../UI/recentContributorsUI') + var recentContribsUtil = require('../utils/recentContribsUtil') + + const requestP = require('request-promise') + var parse = require('parse-link-header') + var chart = require('./chart'); + + var options = { 'qs': { 'sort': 'pushed', 'direction': 'desc', // optional, GitHub API uses 'desc' by default for 'pushed' @@ -44,60 +50,33 @@ CommunityToolbox = function CommunityToolbox(org, repo) { .then(callback); } - // This runs at the very start of page load and stores all the repos and all the - // contributors in the database - function storeAllContributorsInDatabase(org) { - let AllContributors = []; - let promises = []; - var contributorSet = new Set([]); - return new Promise((resolve, reject) => { - model_utils.getItem('allContributors').then((allContributors) => { - // If all contributors list is not in the database, it makes a fresh call to Github API - if(allContributors == null || allContributors == undefined || allContributors.length == 0) { - getAllContribsUtility.getAllRepos(org) - .then(function gotRepos(res) { - let splicedRepos = res.splice(0,20); - splicedRepos.map(function mappingToEachRepo(Repo, i) { - let promise = repoContributorsUtility.fetchRepoContributorsUtil(org, Repo) - .then(function gotRepoContributorsInStorage(contributors) { - if(contributors!=undefined && contributors.length>0) { - contributors.map((contributor, i)=> { - if(!contributorSet.has(contributor.login)) { - contributorSet.add(contributor.login); - AllContributors.push(contributor); - } - }) - } - }); - promises.push(promise); - }); - return Promise.all(promises) - .then(()=> { - // Storing array containing all the contributors list across 20 most active - // repos to database - model_utils.setItem('allContributors', AllContributors); - // Saves current time in epoch, used for flushing out the stored data - // after 24 hours - let currentTime = (new Date).getTime(); - model_utils.setItem('allContributorsExpiry', currentTime); - resolve(AllContributors); - }) + + function initialize(org, repo) { + return new Promise((resolve, reject) => { + return crud.populateDb() + .then((res) => { + return true; }) - } - // If all contributors list is in the database, it simply returns that as a resolved promise - else { - resolve(allContributors); - console.log("allContributors already in database..."); - } - }) - }); - } + .then((dummy) => { + return model_utils.getItem('repos').then((response) => { + if(response==null || response==undefined) { + // Fetches and stores the list of repositories when the page loads + return fetchReposUtil.getAllRepos(org) + .then((resp) => { + resolve(true); + }); + } + resolve(true); + }); + }); + }); + } // This function is responsible for showing contributors // on a multi-repository view - function getAllContributors(org) { - return storeAllContributorsInDatabase(org).then((allContributors) => { + function showAllContributors(org) { + return contributorsUtil.storeAllContributorsInDatabase(org).then((allContributors) => { // If the stored data is not undefined or null, execution goes here if(allContributors!=null && allContributors!=undefined && allContributors.length>0) { // Flushes contributors list from the database after every single day @@ -117,15 +96,15 @@ CommunityToolbox = function CommunityToolbox(org, repo) { // If the data is not in the database, it gets fetched from storeAllContributorsInDatabase function if(AllContributors == null || AllContributors == undefined || AllContributors.length==0) { - storeAllContributorsInDatabase(org).then(function gotAllContributors(AllContributors) { + contributorsUtil.storeAllContributorsInDatabase(org).then(function gotAllContributors(AllContributors) { // Provides fetched contributors list to UI function for rendering it // to the user - ui.insertContributors(AllContributors); + contributorsUI.insertContributors(AllContributors); }) } // If stored data is not null and undefined, process it else { - ui.insertContributors(AllContributors); + contributorsUI.insertContributors(AllContributors); } }) }); @@ -141,7 +120,7 @@ CommunityToolbox = function CommunityToolbox(org, repo) { // This function is responsible for showing all the contributors for a particular repository function showRepoContributors(org, repo) { - return storeAllContributorsInDatabase(org).then((allContributors) => { + return contributorsUtil.storeAllContributorsInDatabase(org).then((allContributors) => { // If the stored data is not undefined or null, execution goes here if(allContributors != null && allContributors!=undefined && allContributors.length>0) { // Flushes repoContributors from the database after every single day @@ -160,15 +139,15 @@ CommunityToolbox = function CommunityToolbox(org, repo) { model_utils.getItem(repo).then((repoContributors) => { // If we don't have repoContributors in the database, we fetch them from Github if (repoContributors == null || repoContributors == undefined) { - repoContributorsUtility.fetchRepoContributorsUtil(org, repo) + contributorsUtil.fetchRepoContributorsUtil(org, repo) .then(function gotRepoContributorsInStorage (contributors) { - ui.insertContributors(contributors); + contributorsUI.insertContributors(contributors); return; }) } // If we have repoContributors in the database, we save a network call :) else { - ui.insertContributors(repoContributors); + contributorsUI.insertContributors(repoContributors); return; } }) @@ -181,59 +160,22 @@ CommunityToolbox = function CommunityToolbox(org, repo) { }) } - // Stores all the Recent Contributors in the database - function storeAllRecentContribsInitially(org, repo) { - // We make queryTime 1 month behind the current time, to pass it as query in the request - let d = (new Date); - d.setDate(d.getDate() - 30); - let queryTime = d.toISOString(); - model_utils.getItem('repos').then((repos) => { - if(repos!=null && repos!=undefined) { - return model_utils.getItem('recent-present').then((result)=> { - if(result!=null && result!=undefined) { - return result; - } - else { - if(repos!=null || repos!=undefined) { - return getRecentCommitsUtility.fetchAllRecentMonthCommits(org, repos, queryTime) - .then((result) => { - model_utils.setItem('recent-present', 'true'); - return result; - }) - } - else { - getAllContribsUtility.getAllRepos(org).then((repos) => { - if(repos!=null || repos!=undefined || repos.length>0) { - return getRecentCommitsUtility.fetchAllRecentMonthCommits(org, repos, queryTime) - .then((result) => { - model_utils.setItem('recent-present', 'true'); - return result; - }) - } - }); - } - } - }); - } - }) - } - // Function for fetching and showing recent contributors - function getRecentCommits(org, repo, recencyLabel) { - return storeAllRecentContribsInitially(org, repo).then((result)=>{ + function showRecentContributors(org, repo, recencyLabel) { + return contributorsUtil.storeAllRecentContribsInitially(org, repo).then((result)=>{ if(recencyLabel==='month') { - return getRecentCommitsUtility.getCommitsLastMonth(org, repo) + return recentContribsUtil.getCommitsLastMonth(org, repo) .then(function gotCommits(commits) { // Push data to UI - ui.insertRecentContributors(commits); + recentContributorsUI.insertRecentContributors(commits); return; }); } else { - return getRecentCommitsUtility.getCommitsLastWeek(org, repo) + return recentContribsUtil.getCommitsLastWeek(org, repo) .then((weekly_contribs) => { // Push data to UI - ui.insertRecentContributors(weekly_contribs); + recentContributorsUI.insertRecentContributors(weekly_contribs); return; }); } @@ -245,28 +187,33 @@ CommunityToolbox = function CommunityToolbox(org, repo) { .getIssuesForRepo(org, repo, { qs: { labels: label } }) .then(function onGotIssues(issues) { issues.forEach(function(issue) { - toolbox.ui.insertIssue(issue, selector); + toolbox.issuesUI.insertIssue(issue, selector); }); }); } - var chart = require('./chart'); + + + + // externally available API return { api: api, - ui: ui, + issuesUI: issuesUI, + contributorsUI: contributorsUI, + recentContributorsUI: recentContributorsUI, parse: parse, chart: chart, options: options, getIssuesForRepo: getIssuesForRepo, getIssuesForOrg: getIssuesForOrg, - getRecentCommits: getRecentCommits, - getCommitsForRepo: getCommitsForRepo, - storeAllContributorsInDatabase: storeAllContributorsInDatabase, - getAllContributors: getAllContributors, + showRecentContributors: showRecentContributors, + getCommitsForRepo: getCommitsForRepo, // storeAllContributorsInDatabase: storeAllContributorsInDatabase, + showAllContributors: showAllContributors, showRepoContributors: showRepoContributors, - displayIssuesForRepo: displayIssuesForRepo + displayIssuesForRepo: displayIssuesForRepo, + initialize: initialize } } diff --git a/src/utils/contributorsUtil.js b/src/utils/contributorsUtil.js new file mode 100644 index 00000000..0a154f03 --- /dev/null +++ b/src/utils/contributorsUtil.js @@ -0,0 +1,185 @@ + +var SimpleApi = require("github-api-simple") +var api = new SimpleApi() +var parse = require('parse-link-header') +var model_utils = require('../models/utils') +var fetchRepoUtil = require('./fetchRepoUtil') +var recentContribsUtil = require('../utils/recentContribsUtil') + +// This is a utility function which decides whether to make a single request for fetching +// each repository's contributors or multiple ones. +function fetchRepoContributorsUtil(org, repo) { + return new Promise((resolve, reject) => { + if(repo === 'plots2') { + resolve(fetchAllRepoContributors(org, repo)); + }else { + resolve(fetchRepoContributors(org, repo)); + } + }) +} + + +// This utility helps us in getting CONTRIBUTORS for a particular repository +function fetchRepoContributors(org, repo) { + // This array is used to store the contributors from all of the repositories + let contributorsArray = []; + + return api.Repositories + .getRepoContributors(org, repo, { method:"GET", qs: { sort: 'pushed', direction: 'desc', per_page: 100 }}) + .then(function gotRepoContributors(contributors) { + if (contributors!=undefined && (contributors != null || contributors.length > 0)) { + contributors.map((contributor, i) => contributorsArray.push(contributor)); + } + }) + .then(() => { + let now = (new Date).getTime(); + model_utils.setItem(repo, contributorsArray); + model_utils.setItem(`${repo}Expiry`, now); + return contributorsArray; + }) +} + + + + +// This utility helps us in getting all the contributors for a particular repository +function fetchAllRepoContributors(org, repo) { + // This array is used to store the contributors from all of the repositories + let contributorsArray = []; + + return api.Repositories + .getRepoContributors(org, repo, {method: "HEAD", qs: { sort: 'pushed', direction: 'desc', per_page: 100 } }) + .then(function gotContribData(contribData) { + var headers = contribData; + if (headers.hasOwnProperty("link")) { + var parsed = parse(headers['link']); + if(parsed.last.page!=undefined) { + totalPages = parseInt(parsed.last.page); + } + } else { + totalPages = 1; + } + return totalPages; + }) + .then(function gotTotalPages(totalPages) { + // This array is used to store all of the promises + let promises = []; + + for(let i = 1; i <= totalPages; i++) { + var currentPromise = api.Repositories + .getRepoContributors(org, repo, { method:"GET", qs: { sort: 'pushed', direction: 'desc', per_page: 100, page:i } }) + .then(function gotRepoContributors(contributors) { + if (contributors!=undefined && (contributors != null || contributors.length > 0)) { + contributors.map((contributor, i) => contributorsArray.push(contributor)); + } + }); + // Push currentPromise to promises array + promises.push(currentPromise); + } + + // Waits for all of the promises to resolve first, sets localStorage after that... + return Promise.all(promises) + .then(()=> { + let now = (new Date).getTime(); + model_utils.setItem(repo, contributorsArray); + model_utils.setItem(`${repo}Expiry`, now); + return contributorsArray; + }); + }); +} + + +// This runs at the very start of page load and stores all the repositories and all the +// contributors in the database on initial page load +function storeAllContributorsInDatabase(org) { + let AllContributors = []; + let promises = []; + var contributorSet = new Set([]); + return new Promise((resolve, reject) => { + model_utils.getItem('allContributors').then((allContributors) => { + // If all contributors list is not in the database, it makes a fresh call to Github API + if(allContributors == null || allContributors == undefined || allContributors.length == 0) { + return model_utils.getItem('repos').then((res) => { + let splicedRepos = res.splice(0,20); + splicedRepos.map(function mappingToEachRepo(Repo, i) { + let promise = fetchRepoContributorsUtil(org, Repo) + .then(function gotRepoContributorsInStorage(contributors) { + if(contributors!=undefined && contributors.length>0) { + contributors.map((contributor, i)=> { + if(!contributorSet.has(contributor.login)) { + contributorSet.add(contributor.login); + AllContributors.push(contributor); + } + }) + } + }); + promises.push(promise); + }); + return Promise.all(promises) + .then(()=> { + // Storing array containing all the contributors' list across 20 most active + // repos to database + model_utils.setItem('allContributors', AllContributors); + // Saves current time in epoch, used for flushing out the stored data + // after 24 hours + let currentTime = (new Date).getTime(); + model_utils.setItem('allContributorsExpiry', currentTime); + resolve(AllContributors); + }) + }) + } + // If all contributors list is in the database, it simply returns that as a resolved promise + else { + resolve(allContributors); + } + }) + }); +} + + +// Stores all the Recent Contributors in the database +function storeAllRecentContribsInitially(org, repo) { + // We make queryTime 1 month behind the current time, to pass it as query in the request + let d = (new Date); + d.setDate(d.getDate() - 30); + let queryTime = d.toISOString(); + return model_utils.getItem('repos').then((repos) => { + return model_utils.getItem('recent-present').then((result)=> { + if(result!=null && result!=undefined) { + return result; + } + else { + if(repos!=null || repos!=undefined) { + return recentContribsUtil.fetchAllRecentMonthCommits(org, repos, queryTime) + .then((result) => { + model_utils.setItem('recent-present', 'true'); + return result; + }) + } else { + fetchRepoUtil.getAllRepos(org).then((repos) => { + if(repos!=null || repos!=undefined) { + return recentContribsUtil.fetchAllRecentMonthCommits(org, repos, queryTime) + .then((result) => { + model_utils.setItem('recent-present', 'true'); + return result; + }) + } + }); + } + } + }); + }) + +} + + + +// EXPORTS +module.exports = { + fetchAllRepoContributors: fetchAllRepoContributors, + fetchRepoContributors: fetchRepoContributors, + fetchRepoContributorsUtil: fetchRepoContributorsUtil, + storeAllRecentContribsInitially: storeAllRecentContribsInitially, + storeAllContributorsInDatabase: storeAllContributorsInDatabase, +} + diff --git a/src/utils/getAllContribsUtility.js b/src/utils/fetchRepoUtil.js similarity index 75% rename from src/utils/getAllContribsUtility.js rename to src/utils/fetchRepoUtil.js index e1eb835f..3a44be2d 100644 --- a/src/utils/getAllContribsUtility.js +++ b/src/utils/fetchRepoUtil.js @@ -1,3 +1,5 @@ +let model_utils = require('../models/utils') + // Fetches all the publiclab's repositories function getAllRepos(org) { @@ -14,14 +16,15 @@ function getAllRepos(org) { results.map(function mappingToEachRepo(repo, index) { return repos[index] = repo.name; }); - // Storing the repos in localStorage + return(repos); + }) + .then((repos) => { + // Storing the repos in the database model_utils.setItem('repos', repos); return(repos); - }); + }) } // EXPORTS -module.exports = { - getAllRepos: getAllRepos, -} +module.exports.getAllRepos = getAllRepos; diff --git a/src/utils/getRecentCommitsUtility.js b/src/utils/recentContribsUtil.js similarity index 73% rename from src/utils/getRecentCommitsUtility.js rename to src/utils/recentContribsUtil.js index f0b05961..707d5b0e 100644 --- a/src/utils/getRecentCommitsUtility.js +++ b/src/utils/recentContribsUtil.js @@ -1,4 +1,4 @@ -var getAllContribsUtility = require('../utils/getAllContribsUtility'); +var fetchRepoUtil = require('./fetchRepoUtil'); var model_utils = require('../models/utils'); // Fetches recent month commits for a particular repository @@ -14,7 +14,7 @@ function fetchRecentMonthCommits(org, repo, queryTime) { .then(function gotResponseJson(response) { if(response!=null) { response.map(function mappingToCommits(commit, i) { - if(commit!=null) { + if(commit.author!=null) { if(!commitersSet.has(commit.author.login)) { commitersSet.add(commit.author.login); result.push(commit); @@ -53,7 +53,7 @@ function fetchAllRecentMonthCommits(org, repos, queryTime) { if(response!=null) { let partialResult = []; response.map(function mappingToCommits(commit, i) { - if(commit!=null) { + if(commit.author!=null) { if(!commitersSet.has(commit.author.login)) { commitersSet.add(commit.author.login); partialResult.push(commit); @@ -66,9 +66,6 @@ function fetchAllRecentMonthCommits(org, repos, queryTime) { let currTime = (new Date).getTime(); model_utils.setItem(`recent-${repo}-month-commits`, partialResult); model_utils.setItem(`recent-${repo}-month-expiry`, currTime); - - // Push every repo's contribs to results array - // results.push(partialResult); } return true; }); @@ -85,6 +82,7 @@ function fetchAllRecentMonthCommits(org, repos, queryTime) { } +// Fetches recent week's commits for a particular repo function getCommitsLastWeek(org, repo) { let contribs = []; @@ -93,7 +91,6 @@ function getCommitsLastWeek(org, repo) { let timeToday = (new Date).getTime(); // If recent month's commits expiry time is 1 day behind the current time, flush them out. if(recentCommitsWeekExpiry!=null && recentCommitsWeekExpiry!=undefined && ((timeToday-recentCommitsWeekExpiry)/1000)>=86400) { - console.log("deleting"); return Promise.all([model_utils.deleteItem(`recent-${repo}-week-expiry`), model_utils.deleteItem(`recent-${repo}-week-commits`)]) .then(()=> { return true; @@ -106,6 +103,7 @@ function getCommitsLastWeek(org, repo) { return result; } else { + // We save extra request by filtering commits-made-last-week from commits-made-last month return getCommitsLastMonth(org, repo) .then((commits_last_month) => { commits_last_month.map((commit_last_month, index) => { @@ -142,48 +140,49 @@ function within_this_week(date) { // Fetches recent month's commits for a particular repo or all of the repos (10 repos) function getCommitsLastMonth(org, repo) { - model_utils.getItem('repos').then((repos) => { + return model_utils.getItem('repos').then((repos) => { if(repos!=null && repos!=undefined) { return model_utils.getItem(`recent-${repo}-month-expiry`) .then((recentCommitsMonthExpiry) => { - let timeToday = (new Date).getTime(); - // If recentCommits expiry time is 1 day behind the current time, flush them out. - if(recentCommitsMonthExpiry!=null && recentCommitsMonthExpiry!=undefined && ((timeToday-recentCommitsMonthExpiry)/1000)>=86400) { - console.log("Deleting month contribs"); - return Promise.all([model_utils.deleteItem(`recent-${repo}-month-commits`), model_utils.deleteItem(`recent-${repo}-month-expiry`)]) - .then(() => { - return true; - }) - } - return true; - }) - .then((boolean) => { - return model_utils.getItem(`recent-${repo}-month-commits`).then((result) => { - if(result!=null && result!=undefined) { - return result; + let timeToday = (new Date).getTime(); + // If recentCommits expiry time is 1 day behind the current time, flush them out. + if(recentCommitsMonthExpiry!=null && recentCommitsMonthExpiry!=undefined && ((timeToday-recentCommitsMonthExpiry)/1000)>=86400) { + return Promise.all([model_utils.deleteItem(`recent-${repo}-month-commits`), model_utils.deleteItem(`recent-${repo}-month-expiry`)]) + .then(() => { + return true; + }) } - else { - // We make queryTime 1 month behind the current time, to pass it as query in the request - let d = (new Date); - d.setDate(d.getDate() - 30); - let queryTime = d.toISOString(); - if(repo==='all') { - return fetchAllRecentMonthCommits(org, repos, queryTime) - .then(function gotRecentCommitsInStorage(month_commits) { - console.log("got all monthly contribs from fetchAllRecentMonthCommits"); - return month_commits; - }); + return true; + }) + .then((boolean) => { + return model_utils.getItem(`recent-${repo}-month-commits`).then((result) => { + if(result!=null && result!=undefined) { + return result; } else { - return fetchRecentMonthCommits(org, repo, queryTime) - .then(function gotRecentCommitsInStorage(month_commits) { - console.log("got repo's month commits from fetch"); - return month_commits; - }) + // We make queryTime 1 month behind the current time, to pass it as query in the request + let d = (new Date); + d.setDate(d.getDate() - 30); + let queryTime = d.toISOString(); + if(repo==='all') { + return fetchAllRecentMonthCommits(org, repos, queryTime) + .then(function gotRecentCommitsInStorage(month_commits) { + console.log("got all monthly contribs from fetchAllRecentMonthCommits"); + return month_commits; + }); + } + else { + return fetchRecentMonthCommits(org, repo, queryTime) + .then(function gotRecentCommitsInStorage(month_commits) { + console.log("got repo's month commits from fetch"); + return month_commits; + }) + } } - } + }) }) - }) + } else { + console.log("repos are not there yet!!!"); } }) } diff --git a/src/utils/repoContributorsUtility.js b/src/utils/repoContributorsUtility.js deleted file mode 100644 index 6e7d5f74..00000000 --- a/src/utils/repoContributorsUtility.js +++ /dev/null @@ -1,97 +0,0 @@ -var SimpleApi = require("github-api-simple") -var api = new SimpleApi(); -var parse = require('parse-link-header'); -var model_utils = require('../models/utils'); - -// This is a utility function which decides whether to make a single request for fetching -// each repo's contributors or multiple ones. -function fetchRepoContributorsUtil(org, repo) { - return new Promise((resolve, reject) => { - if(repo === 'plots2') { - resolve(fetchAllRepoContributors(org, repo)); - }else { - resolve(fetchRepoContributors(org, repo)); - } - }) -} - - -// This utility helps us in getting CONTRIBUTORS for a particular repository -function fetchRepoContributors(org, repo) { - // This array is used to store the contributors from all of the repositories - let contributorsArray = []; - - return api.Repositories - .getRepoContributors(org, repo, { method:"GET", qs: { sort: 'pushed', direction: 'desc', per_page: 100 }}) - .then(function gotRepoContributors(contributors) { - if (contributors!=undefined && (contributors != null || contributors.length > 0)) { - contributors.map((contributor, i) => contributorsArray.push(contributor)); - } - }) - .then(() => { - let now = (new Date).getTime(); - model_utils.setItem(repo, contributorsArray); - console.log("saving ",repo,"'s all contribs"); - model_utils.setItem(`${repo}Expiry`, now); - return contributorsArray; - }) - -} - - - - -// This utility helps us in getting all the contributors for a particular repository -function fetchAllRepoContributors(org, repo) { - // This array is used to store the contributors from all of the repositories - let contributorsArray = []; - - return api.Repositories - .getRepoContributors(org, repo, {method: "HEAD", qs: { sort: 'pushed', direction: 'desc', per_page: 100 } }) - .then(function gotContribData(contribData) { - var headers = contribData; - if (headers.hasOwnProperty("link")) { - var parsed = parse(headers['link']); - if(parsed.last.page!=undefined) { - totalPages = parseInt(parsed.last.page); - } - } else { - totalPages = 1; - } - return totalPages; - }) - .then(function gotTotalPages(totalPages) { - // This array is used to store all of the promises - let promises = []; - - for(let i = 1; i <= totalPages; i++) { - var currentPromise = api.Repositories - .getRepoContributors(org, repo, { method:"GET", qs: { sort: 'pushed', direction: 'desc', per_page: 100, page:i } }) - .then(function gotRepoContributors(contributors) { - if (contributors!=undefined && (contributors != null || contributors.length > 0)) { - contributors.map((contributor, i) => contributorsArray.push(contributor)); - } - }); - // Push currentPromise to promises array - promises.push(currentPromise); - } - - // Waits for all of the promises to resolve first, sets localStorage after that... - return Promise.all(promises) - .then(()=> { - let now = (new Date).getTime(); - model_utils.setItem(repo, contributorsArray); - model_utils.setItem(`${repo}Expiry`, now); - return contributorsArray; - }); - }); - } - - - -// EXPORTS -module.exports = { - fetchAllRepoContributors: fetchAllRepoContributors, - fetchRepoContributors: fetchRepoContributors, - fetchRepoContributorsUtil: fetchRepoContributorsUtil, -} \ No newline at end of file