odnil
New Coder
This takes ages to retrieve data. Is there a more efficient way to do this?
Per_page limit is 100 & I needed the function to be able to find results for any date range using pagination. So I added a do while loop and this is what I ended up with:
Per_page limit is 100 & I needed the function to be able to find results for any date range using pagination. So I added a do while loop and this is what I ended up with:
JavaScript:
async function foo(repoOwner, repository, startingDate, endingDate){
const listOfUserObjects = [];
let pages, page = 1;
do{
await axios({
method: 'get',
url : `https://api.github.com/repos/${repoOwner}/${repository}/pulls?state=all&per_page=100&page=${page}`,
}).then(response => {
const users = response.data, startAt = new Date(startingDate), endAt = new Date(endingDate.valueOf());
endAt.setDate(endAt.getDate() + 1);
if(page === 1) pages = (Math.floor((users[0].number)/100))/2, console.log("collecting data, please wait...");
for(let i = 0; i < users.length; i++) {
const createdDate = new Date(users[i].created_at), updatedDate = new Date(users[i].updated_at), closedDate = new Date(users[i].closed_at), mergedDate = new Date(users[i].merged_at);
if(((createdDate || updatedDate || closedDate || mergedDate) >= startAt) && ((createdDate || updatedDate || closedDate || mergedDate) <= endAt)) {
const userObj = {};
userObj.id = users[i].id;
userObj.user = users[i].user.login;
userObj.title = users[i].title;
userObj.state = users[i].state;
userObj.created_at = users[i].created_at.slice(0,10);
listOfUserObjects.push(userObj);
} else {
continue;
};
}
page++
}).catch(error => {
throw error.response.status === 404 ? Error("Error 404 User or Repo Not Found") : Error(error);
});
} while(page < pages);
console.log(listOfUserObjects);
}