1- import { Bucket , createBucket , listBuckets , updateBucket } from "@api/quantified-resume"
1+ import { batchCreateItems , Bucket , createBucket , Item , listAllItems , listBuckets , removeBucket , updateBucket } from "@api/quantified-resume"
22import metaMessages , { } from "@i18n/message/common/meta"
33import { t } from "@i18n"
44import { groupBy } from "@util/array"
5+ import { formatTimeYMD , parseTime } from "@util/time"
56
67export type QuantifiedResumeCache = {
78 bucketIds : {
@@ -23,15 +24,16 @@ async function createNewBucket(context: timer.backup.CoordinatorContext<Quantifi
2324 return createBucket ( { endpoint } , bucket )
2425}
2526
26- async function getBucketId ( context : timer . backup . CoordinatorContext < QuantifiedResumeCache > ) : Promise < number > {
27- const { cid, cache } = context || { }
27+ async function getBucketId ( context : timer . backup . CoordinatorContext < QuantifiedResumeCache > , specificCid ?: string ) : Promise < number > {
28+ const cid = specificCid || context ?. cid
29+ const { cache } = context || { }
2830 // 1. query from cache
2931 let bucketId = cache ?. bucketIds ?. [ cid ]
30- if ( ! bucketId ) return bucketId
32+ if ( bucketId ) return bucketId
3133
3234 const { endpoint } = context ?. ext || { }
3335 // 2. query again
34- bucketId = ( await listBuckets ( { endpoint } , cid ) ) ?. [ 0 ] ?. id
36+ bucketId = ( await listBuckets ( { endpoint } , cid ) ) ?. filter ( b => b . builtinRefId === cid ) ?. [ 0 ] ?. id
3537 if ( ! bucketId ) {
3638 // 3. create one
3739 bucketId = await createNewBucket ( context )
@@ -82,15 +84,44 @@ export default class QuantifiedResumeCoordinator implements timer.backup.Coordin
8284 return result
8385 }
8486
85- download ( context : timer . backup . CoordinatorContext < QuantifiedResumeCache > , dateStart : Date , dateEnd : Date , targetCid ?: string ) : Promise < timer . stat . RowBase [ ] > {
86- throw new Error ( "Method not implemented." ) ;
87+ async download ( context : timer . backup . CoordinatorContext < QuantifiedResumeCache > , dateStart : Date , dateEnd : Date , targetCid ?: string ) : Promise < timer . stat . RowBase [ ] > {
88+ let bucketId = await getBucketId ( context , targetCid )
89+ if ( ! bucketId ) return [ ]
90+ const items = await listAllItems ( { endpoint : context ?. ext ?. endpoint } , bucketId )
91+ return items ?. map ( ( { name, timestamp, metrics } ) => ( {
92+ host : name ,
93+ date : formatTimeYMD ( timestamp ) ,
94+ focus : metrics ?. focus ,
95+ time : metrics ?. visit ,
96+ } satisfies timer . stat . RowBase ) ) || [ ]
8797 }
8898
8999 async upload ( context : timer . backup . CoordinatorContext < QuantifiedResumeCache > , rows : timer . stat . RowBase [ ] ) : Promise < void > {
90- const bucketId = await getBucketId ( context )
91- rows . forEach ( row => {
100+ if ( ! rows ?. length ) return
92101
102+ const bucketId = await getBucketId ( context )
103+ let items = rows . map ( ( { host, date, focus, time : visit } ) => {
104+ const time = parseTime ( date )
105+ time . setHours ( 0 )
106+ time . setMinutes ( 0 )
107+ time . setSeconds ( 0 )
108+ time . setMilliseconds ( 0 )
109+ const item : Item = {
110+ refId : `${ date } ${ host } ` ,
111+ timestamp : time . getTime ( ) ,
112+ metrics : { visit, focus } ,
113+ action : "web_time" ,
114+ name : host ,
115+ payload : { date, host, cid : context . cid } ,
116+ }
117+ return item
93118 } )
119+ const groups = groupBy ( items , ( _ , idx ) => Math . floor ( idx / 2000 ) , l => l )
120+
121+ const { endpoint } = context ?. ext || { }
122+ for ( const group of Object . values ( groups ) ) {
123+ await batchCreateItems ( { endpoint } , bucketId , group )
124+ }
94125 }
95126
96127 async testAuth ( _auth : timer . backup . Auth , ext : timer . backup . TypeExt ) : Promise < string > {
@@ -102,7 +133,9 @@ export default class QuantifiedResumeCoordinator implements timer.backup.Coordin
102133 }
103134 }
104135
105- clear ( context : timer . backup . CoordinatorContext < QuantifiedResumeCache > , client : timer . backup . Client ) : Promise < void > {
106- throw new Error ( "Method not implemented." ) ;
136+ async clear ( context : timer . backup . CoordinatorContext < QuantifiedResumeCache > , client : timer . backup . Client ) : Promise < void > {
137+ const bucketId = await getBucketId ( context , client . id )
138+ if ( ! bucketId ) return
139+ await removeBucket ( { endpoint : context ?. ext ?. endpoint } , bucketId )
107140 }
108141}
0 commit comments