fix(plugins/isocalendar): Improve padding to support years better

This commit is contained in:
lowlighter
2020-11-26 20:13:21 +01:00
parent eaa844e275
commit 441e05b2dd
3 changed files with 62 additions and 36 deletions

View File

@@ -110,6 +110,17 @@ jobs:
plugins_errors_fatal: yes
dryrun: yes
- name: Isocalendar plugin (full year)
uses: lowlighter/metrics@master
with:
token: ${{ secrets.METRICS_TOKEN }}
base: ""
repositories: 1
plugin_isocalendar: yes
plugin_isocalendar_duration: full-year
plugins_errors_fatal: yes
dryrun: yes
- name: Habits plugin
uses: lowlighter/metrics@master
with:

16
action/dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -8,16 +8,25 @@
//Parameters override
let {"isocalendar.duration":duration = "half-year"} = q
//Duration in days
const leap = (new Date(new Date().getYear(), 1, 29).getDate() === 29)
const days = {"half-year":180, "full-year":365 + leap}[duration] ?? 180
//Compute start day (to ensure last row is complete, we'll retrieve one more week that we'll shift later)
const from = new Date(Date.now()-days*24*60*60*1000)
from.setHours(-24*7)
duration = ["full-year", "half-year"].includes(duration) ? duration : "full-year"
//Compute start day
const now = new Date()
const start = new Date(now)
if (duration === "full-year")
start.setFullYear(now.getFullYear()-1)
else
start.setHours(-24*180)
//Compute padding to ensure last row is complete
const padding = new Date(start)
padding.setHours(-14*24)
//Retrieve contribution calendar from graphql api
const {user:{calendar:{contributionCalendar:calendar}}} = await graphql(`
const calendar = {}
for (const [name, from, to] of [["padding", padding, start], ["weeks", start, now]]) {
console.debug(`metrics/compute/${login}/plugins > isocalendar > loading "${name}" from "${from.toISOString()}" to "${to.toISOString()}"`)
const {user:{calendar:{contributionCalendar:{weeks}}}} = await graphql(`
query Calendar {
user(login: "${login}") {
calendar:contributionsCollection(from: "${from.toISOString()}", to: "${(new Date()).toISOString()}") {
calendar:contributionsCollection(from: "${from.toISOString()}", to: "${to.toISOString()}") {
contributionCalendar {
weeks {
contributionDays {
@@ -32,7 +41,13 @@
}
`
)
calendar.weeks.shift()
calendar[name] = weeks
}
//Apply padding
const firstweek = calendar.weeks[0].contributionDays
const padded = calendar.padding.flatMap(({contributionDays}) => contributionDays).filter(({date}) => !firstweek.map(({date}) => date).includes(date))
while (firstweek.length < 7)
firstweek.unshift(padded.pop())
//Compute the highest contributions in a day, streaks and average commits per day
let max = 0, streak = {max:0, current:0}, values = [], average = 0
for (const week of calendar.weeks) {