1+ //Setup
2+ export default async function ( { login, q, data, imports, graphql, queries, account} , { enabled = false } = { } ) {
3+ //Plugin execution
4+ try {
5+ //Check if plugin is enabled and requirements are met
6+ if ( ( ! enabled ) || ( ! q . calendar ) )
7+ return null
8+
9+ //Load inputs
10+ let { limit} = imports . metadata . plugins . calendar . inputs ( { data, account, q} )
11+
12+ //Compute boundaries
13+ const end = new Date ( ) . getFullYear ( )
14+ const start = new Date ( limit ? end - limit + 1 : data . user . createdAt , 0 ) . getFullYear ( )
15+
16+ //Load contribution calendar
17+ console . debug ( `metrics/compute/${ login } /plugins > calendar > processing years ${ start } to ${ end } ` )
18+ const calendar = { years :[ ] }
19+ for ( let year = start ; year <= end ; year ++ ) {
20+ console . debug ( `metrics/compute/${ login } /plugins > calendar > processing year ${ year } ` )
21+ const weeks = [ ]
22+ const newyear = new Date ( year , 0 , 1 )
23+ const endyear = ( year === end ) ? new Date ( ) : new Date ( year , 11 , 31 )
24+ for ( let from = new Date ( newyear ) ; from < endyear ; ) {
25+ //Set date range and ensure we start on sundays
26+ let to = new Date ( from )
27+ to . setUTCHours ( + 4 * 7 * 24 )
28+ if ( to . getUTCDay ( ) )
29+ to . setUTCHours ( - to . getUTCDay ( ) * 24 )
30+ if ( to > endyear )
31+ to = endyear
32+
33+ //Ensure that date ranges are not overlapping by setting it to previous day at 23:59:59.999
34+ const dto = new Date ( to )
35+ dto . setUTCHours ( - 1 )
36+ dto . setUTCMinutes ( 59 )
37+ dto . setUTCSeconds ( 59 )
38+ dto . setUTCMilliseconds ( 999 )
39+ //Fetch data from api
40+ console . debug ( `metrics/compute/${ login } /plugins > calendar > loading calendar from "${ from . toISOString ( ) } " to "${ dto . toISOString ( ) } "` )
41+ const { user :{ calendar :{ contributionCalendar} } } = await graphql ( queries . isocalendar . calendar ( { login, from :from . toISOString ( ) , to :dto . toISOString ( ) } ) )
42+ weeks . push ( ...contributionCalendar . weeks )
43+ //Set next date range start
44+ from = new Date ( to )
45+ }
46+ calendar . years . unshift ( { year, weeks} )
47+ }
48+
49+ //Results
50+ return calendar
51+ }
52+ //Handle errors
53+ catch ( error ) {
54+ throw { error :{ message :"An error occured" , instance :error } }
55+ }
56+ }
0 commit comments