@@ -3,6 +3,7 @@ import { unitOfTime } from 'moment';
33import * as _ from 'lodash' ;
44import { map , filter , values , groupBy , sortBy , flow , reverse } from 'lodash/fp' ;
55
6+ import { window_events } from '~/util/fakedata' ;
67import queries from '~/queries' ;
78import { getColorFromCategory } from '~/util/color' ;
89import { loadClassesForQuery } from '~/util/classes' ;
@@ -13,8 +14,8 @@ interface TimePeriod {
1314 length : [ number , string ] ;
1415}
1516
16- function dateToTimeperiod ( date : string ) : TimePeriod {
17- return { start : get_day_start_with_offset ( date ) , length : [ 1 , 'day' ] } ;
17+ function dateToTimeperiod ( date : string , duration ?: [ number , string ] ) : TimePeriod {
18+ return { start : get_day_start_with_offset ( date ) , length : duration || [ 1 , 'day' ] } ;
1819}
1920
2021function timeperiodToStr ( tp : TimePeriod ) : string {
@@ -62,6 +63,7 @@ const _state = {
6263
6364 category : {
6465 available : false ,
66+ by_hour : [ ] ,
6567 top : [ ] ,
6668 } ,
6769
@@ -104,6 +106,23 @@ function timeperiodsAroundTimeperiod(timeperiod: TimePeriod): TimePeriod[] {
104106 return periods ;
105107}
106108
109+ function timeperiodsHoursOfDay ( timeperiod : TimePeriod ) : TimePeriod [ ] {
110+ const periods = [ ] ;
111+ const _length : [ number , string ] = [ 1 , 'hour' ] ;
112+ for ( let i = 0 ; i < 24 ; i ++ ) {
113+ const start = moment ( timeperiod . start )
114+ . add ( i * _length [ 0 ] , _length [ 1 ] as moment . unitOfTime . DurationConstructor )
115+ . format ( ) ;
116+ periods . push ( { start, length : _length } ) ;
117+ }
118+ // const periods = _.range(24).map(i => [TimePeriod(moment(i * 1 + dayOffset), [1, 'hour'])]);
119+ return periods ;
120+ }
121+
122+ function timeperiodsStrsHoursOfDay ( timeperiod : TimePeriod ) : string [ ] {
123+ return timeperiodsHoursOfDay ( timeperiod ) . map ( timeperiodToStr ) ;
124+ }
125+
107126function timeperiodStrsAroundTimeperiod ( timeperiod : TimePeriod ) : string [ ] {
108127 return timeperiodsAroundTimeperiod ( timeperiod ) . map ( timeperiodToStr ) ;
109128}
@@ -142,6 +161,7 @@ const actions = {
142161
143162 if ( state . window . available ) {
144163 await dispatch ( 'query_desktop_full' , query_options ) ;
164+ await dispatch ( 'query_category_time_by_hour' , query_options ) ;
145165 } else if ( state . android . available ) {
146166 await dispatch ( 'query_android' , query_options ) ;
147167 } else {
@@ -263,6 +283,32 @@ const actions = {
263283 commit ( 'query_active_history_completed' , { active_history } ) ;
264284 } ,
265285
286+ async query_category_time_by_hour (
287+ { commit, state } ,
288+ { timeperiod, filterCategories, filterAFK } : QueryOptions
289+ ) {
290+ // TODO: Only works for the 1 day timeperiod
291+ // TODO: Needs to be adapted for Android
292+ const periods = timeperiodsStrsHoursOfDay ( timeperiod ) ;
293+ const classes = loadClassesForQuery ( ) ;
294+ const data = await this . _vm . $aw . query (
295+ periods ,
296+ // TODO: Clean up call, pass QueryParams in fullDesktopQuery as well
297+ // TODO: Unify QueryOptions and QueryParams
298+ queries . hourlyCategoryQuery ( {
299+ bid_afk : state . buckets . afk [ 0 ] ,
300+ bid_window : state . buckets . window [ 0 ] ,
301+ bid_browsers : state . buckets . browser ,
302+ // bid_android: state.buckets.android,
303+ classes : classes ,
304+ filter_afk : filterAFK ,
305+ filter_classes : filterCategories ,
306+ } )
307+ ) ;
308+ const category_time_by_hour = _ . zipObject ( periods , data ) ;
309+ commit ( 'query_category_time_by_hour_completed' , { category_time_by_hour } ) ;
310+ } ,
311+
266312 async query_active_history_android ( { commit, state } , { timeperiod } : QueryOptions ) {
267313 const periods = timeperiodStrsAroundTimeperiod ( timeperiod ) . filter ( tp_str => {
268314 return ! _ . includes ( state . active . history , tp_str ) ;
@@ -317,95 +363,6 @@ const actions = {
317363 // A function to load some demo data (for screenshots and stuff)
318364 commit ( 'start_loading' , { } ) ;
319365
320- const window_events = [
321- {
322- duration : 32.1 * 60 ,
323- data : {
324- app : 'Firefox' ,
325- title : 'ActivityWatch/activitywatch: Track how you spend your time - Mozilla Firefox' ,
326- url : 'https://github.com/ActivityWatch/activitywatch' ,
327- $category : [ 'Work' , 'Programming' , 'ActivityWatch' ] ,
328- } ,
329- } ,
330- {
331- duration : 14.6 * 60 ,
332- data : {
333- app : 'Firefox' ,
334- title : 'Inbox - Gmail - Mozilla Firefox' ,
335- url : 'https://mail.google.com' ,
336- $category : [ 'Comms' , 'Email' ] ,
337- } ,
338- } ,
339- {
340- duration : 0.2 * 60 * 60 ,
341- data : {
342- app : 'Firefox' ,
343- title : 'reddit: the front page of the internet - Mozilla Firefox' ,
344- url : 'https://reddit.com' ,
345- $category : [ 'Media' , 'Social Media' ] ,
346- } ,
347- } ,
348- {
349- duration : 0.2 * 60 * 60 ,
350- data : {
351- app : 'Firefox' ,
352- title : 'YouTube - Mozilla Firefox' ,
353- url : 'https://youtube.com' ,
354- $category : [ 'Media' , 'Video' ] ,
355- } ,
356- } ,
357- {
358- duration : 0.15 * 60 * 60 ,
359- data : {
360- app : 'Firefox' ,
361- title : 'Home / Twitter - Mozilla Firefox' ,
362- url : 'https://twitter.com' ,
363- $category : [ 'Media' , 'Social Media' ] ,
364- } ,
365- } ,
366- {
367- duration : 0.15 * 60 * 60 ,
368- data : {
369- app : 'Firefox' ,
370- title : 'Stack Overflow' ,
371- url : 'https://stackoverflow.com' ,
372- $category : [ 'Work' , 'Programming' ] ,
373- } ,
374- } ,
375- {
376- duration : 48.2 * 60 ,
377- data : {
378- app : 'Terminal' ,
379- title : 'vim ~/code/activitywatch/aw-server/aw-webui/src' ,
380- $category : [ 'Work' , 'Programming' , 'ActivityWatch' ] ,
381- } ,
382- } ,
383- {
384- duration : 12.6 * 60 ,
385- data : {
386- app : 'Terminal' ,
387- title : 'bash ~/code/activitywatch' ,
388- $category : [ 'Work' , 'Programming' , 'ActivityWatch' ] ,
389- } ,
390- } ,
391- {
392- duration : 58.1 * 60 ,
393- data : {
394- app : 'zoom' ,
395- title : 'Zoom Meeting' ,
396- $category : [ 'Comms' , 'Video Conferencing' ] ,
397- } ,
398- } ,
399- {
400- duration : 0.4 * 60 * 60 ,
401- data : { app : 'Minecraft' , title : 'Minecraft' , $category : [ 'Media' , 'Games' ] } ,
402- } ,
403- {
404- duration : 3.15 * 60 ,
405- data : { app : 'Spotify' , title : 'Spotify' , $category : [ 'Media' , 'Music' ] } ,
406- } ,
407- ] ;
408-
409366 function groupSumEventsBy ( events , key , f ) {
410367 return flow (
411368 filter ( f ) ,
@@ -544,6 +501,10 @@ const mutations = {
544501 } ;
545502 } ,
546503
504+ query_category_time_by_hour_completed ( state , { category_time_by_hour } ) {
505+ state . category . by_hour = category_time_by_hour ;
506+ } ,
507+
547508 buckets ( state , data ) {
548509 state . buckets = data ;
549510 state . buckets . loaded = true ;
0 commit comments