|
| 1 | +//Setup |
| 2 | + export default async function ({login, graphql, data, q, queries, imports}, {enabled = false} = {}) { |
| 3 | + //Plugin execution |
| 4 | + try { |
| 5 | + //Check if plugin is enabled and requirements are met |
| 6 | + if ((!enabled)||(!q.stargazers)) |
| 7 | + return null |
| 8 | + //Retrieve stargazers from graphql api |
| 9 | + console.debug(`metrics/compute/${login}/plugins > stargazers > querying api`) |
| 10 | + const repositories = data.user.repositories.nodes.map(({name}) => name).slice(0, 2) |
| 11 | + const dates = [] |
| 12 | + for (const repository of repositories) { |
| 13 | + //Iterate through stargazers |
| 14 | + console.debug(`metrics/compute/${login}/plugins > stargazers > retrieving stargazers of ${repository}`) |
| 15 | + let cursor = null |
| 16 | + let pushed = 0 |
| 17 | + do { |
| 18 | + console.debug(`metrics/compute/${login}/plugins > stargazers > retrieving stargazers of ${repository} after ${cursor}`) |
| 19 | + const {repository:{stargazers:{edges}}} = await graphql(queries.stargazers({login, repository, after:cursor ? `after: "${cursor}"` : ""})) |
| 20 | + cursor = edges?.[edges?.length-1]?.cursor |
| 21 | + console.log(edges) |
| 22 | + dates.push(...edges.map(({starredAt}) => new Date(starredAt))) |
| 23 | + pushed = edges.length |
| 24 | + } while ((pushed)&&(cursor)) |
| 25 | + //Limit repositories |
| 26 | + console.debug(`metrics/compute/${login}/plugins > stargazers > loaded ${dates.length} stargazers for ${repository}`) |
| 27 | + } |
| 28 | + console.debug(`metrics/compute/${login}/plugins > stargazers > loaded ${dates.length} stargazers in total`) |
| 29 | + //Compute stargazers increments |
| 30 | + const days = 14 |
| 31 | + const increments = {dates:Object.fromEntries([...new Array(days).fill(null).map((_, i) => [new Date(Date.now()-i*24*60*60*1000).toISOString().slice(0, 10), 0]).reverse()]), max:NaN, min:NaN} |
| 32 | + dates |
| 33 | + .map(date => date.toISOString().slice(0, 10)) |
| 34 | + .filter(date => date in increments.dates) |
| 35 | + .map(date => increments.dates[date]++) |
| 36 | + increments.min = Math.min(...Object.values(increments.dates)) |
| 37 | + increments.max = Math.max(...Object.values(increments.dates)) |
| 38 | + //Compute total stargazers |
| 39 | + let stargazers = data.computed.repositories.stargazers |
| 40 | + const total = {dates:{...increments.dates}, max:NaN, min:NaN} |
| 41 | + { |
| 42 | + const dates = Object.keys(total.dates) |
| 43 | + for (let i = dates.length-1; i >= 0; i--) { |
| 44 | + const date = dates[i], tomorrow = dates[i+1] |
| 45 | + stargazers -= (increments.dates[tomorrow] ?? 0) |
| 46 | + total.dates[date] = stargazers |
| 47 | + } |
| 48 | + } |
| 49 | + total.min = Math.min(...Object.values(total.dates)) |
| 50 | + total.max = Math.max(...Object.values(total.dates)) |
| 51 | + //Format values |
| 52 | + for (const date in increments.dates) |
| 53 | + increments.dates[date] = `${increments.dates[date] > 0 ? "+" : ""}${imports.format(increments.dates[date])}` |
| 54 | + for (const date in total.dates) |
| 55 | + total.dates[date] = imports.format(total.dates[date]) |
| 56 | + //Months name |
| 57 | + const months = ["", "Jan.", "Feb.", "Mar.", "Apr.", "May", "June", "July", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."] |
| 58 | + //Results |
| 59 | + return {total, increments, months} |
| 60 | + } |
| 61 | + //Handle errors |
| 62 | + catch (error) { |
| 63 | + throw {error:{message:"An error occured", instance:error}} |
| 64 | + } |
| 65 | + } |
0 commit comments