fix(plugins/isocalendar): Improve padding to support years better
This commit is contained in:
11
.github/workflows/build.yml
vendored
11
.github/workflows/build.yml
vendored
@@ -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:
|
||||
|
||||
34
action/dist/index.js
vendored
34
action/dist/index.js
vendored
File diff suppressed because one or more lines are too long
@@ -8,31 +8,46 @@
|
||||
//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(`
|
||||
query Calendar {
|
||||
user(login: "${login}") {
|
||||
calendar:contributionsCollection(from: "${from.toISOString()}", to: "${(new Date()).toISOString()}") {
|
||||
contributionCalendar {
|
||||
weeks {
|
||||
contributionDays {
|
||||
contributionCount
|
||||
color
|
||||
date
|
||||
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: "${to.toISOString()}") {
|
||||
contributionCalendar {
|
||||
weeks {
|
||||
contributionDays {
|
||||
contributionCount
|
||||
color
|
||||
date
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
)
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user