11import moment from 'moment' ;
2- import { unitOfTime } from 'moment' ;
32import * as _ from 'lodash' ;
43import { map , filter , values , groupBy , sortBy , flow , reverse } from 'lodash/fp' ;
54
@@ -8,23 +7,14 @@ import queries from '~/queries';
87import { getColorFromCategory } from '~/util/color' ;
98import { loadClassesForQuery } from '~/util/classes' ;
109import { get_day_start_with_offset } from '~/util/time' ;
11-
12- interface TimePeriod {
13- start : string ;
14- length : [ number , string ] ;
15- }
16-
17- function dateToTimeperiod ( date : string , duration ?: [ number , string ] ) : TimePeriod {
18- return { start : get_day_start_with_offset ( date ) , length : duration || [ 1 , 'day' ] } ;
19- }
20-
21- function timeperiodToStr ( tp : TimePeriod ) : string {
22- const start = moment ( tp . start ) . format ( ) ;
23- const end = moment ( start )
24- . add ( tp . length [ 0 ] , tp . length [ 1 ] as moment . unitOfTime . DurationConstructor )
25- . format ( ) ;
26- return [ start , end ] . join ( '/' ) ;
27- }
10+ import {
11+ TimePeriod ,
12+ dateToTimeperiod ,
13+ timeperiodToStr ,
14+ timeperiodsHoursOfPeriod ,
15+ timeperiodsDaysOfPeriod ,
16+ timeperiodsAroundTimeperiod ,
17+ } from '~/util/timeperiod' ;
2818
2919interface QueryOptions {
3020 host : string ;
@@ -64,7 +54,7 @@ const _state = {
6454
6555 category : {
6656 available : false ,
67- by_hour : [ ] ,
57+ by_period : [ ] ,
6858 top : [ ] ,
6959 } ,
7060
@@ -96,32 +86,12 @@ const _state = {
9686 } ,
9787} ;
9888
99- function timeperiodsAroundTimeperiod ( timeperiod : TimePeriod ) : TimePeriod [ ] {
100- const periods = [ ] ;
101- for ( let i = - 15 ; i <= 15 ; i ++ ) {
102- const start = moment ( timeperiod . start )
103- . add ( i * timeperiod . length [ 0 ] , timeperiod . length [ 1 ] as moment . unitOfTime . DurationConstructor )
104- . format ( ) ;
105- periods . push ( { ...timeperiod , start } ) ;
106- }
107- return periods ;
89+ function timeperiodsStrsHoursOfPeriod ( timeperiod : TimePeriod ) : string [ ] {
90+ return timeperiodsHoursOfPeriod ( timeperiod ) . map ( timeperiodToStr ) ;
10891}
10992
110- function timeperiodsHoursOfDay ( timeperiod : TimePeriod ) : TimePeriod [ ] {
111- const periods = [ ] ;
112- const _length : [ number , string ] = [ 1 , 'hour' ] ;
113- for ( let i = 0 ; i < 24 ; i ++ ) {
114- const start = moment ( timeperiod . start )
115- . add ( i * _length [ 0 ] , _length [ 1 ] as moment . unitOfTime . DurationConstructor )
116- . format ( ) ;
117- periods . push ( { start, length : _length } ) ;
118- }
119- // const periods = _.range(24).map(i => [TimePeriod(moment(i * 1 + dayOffset), [1, 'hour'])]);
120- return periods ;
121- }
122-
123- function timeperiodsStrsHoursOfDay ( timeperiod : TimePeriod ) : string [ ] {
124- return timeperiodsHoursOfDay ( timeperiod ) . map ( timeperiodToStr ) ;
93+ function timeperiodsStrsDaysOfPeriod ( timeperiod : TimePeriod ) : string [ ] {
94+ return timeperiodsDaysOfPeriod ( timeperiod ) . map ( timeperiodToStr ) ;
12595}
12696
12797function timeperiodStrsAroundTimeperiod ( timeperiod : TimePeriod ) : string [ ] {
@@ -162,15 +132,15 @@ const actions = {
162132
163133 if ( state . window . available ) {
164134 await dispatch ( 'query_desktop_full' , query_options ) ;
165- await dispatch ( 'query_category_time_by_hour ' , query_options ) ;
135+ await dispatch ( 'query_category_time_by_period ' , query_options ) ;
166136 } else if ( state . android . available ) {
167137 await dispatch ( 'query_android' , query_options ) ;
168138 } else {
169139 console . log (
170140 'Cannot query windows as we are missing either an afk/window bucket pair or an android bucket'
171141 ) ;
172- await dispatch ( 'query_window_empty' , query_options ) ;
173- await dispatch ( 'query_browser_empty' , query_options ) ;
142+ await dispatch ( 'reset_window' ) ;
143+ await dispatch ( 'reset_category' ) ;
174144 }
175145
176146 if ( state . active . available ) {
@@ -186,7 +156,7 @@ const actions = {
186156 await dispatch ( 'query_editor' , query_options ) ;
187157 } else {
188158 console . log ( 'Cannot call query_editor as we do not have any editor buckets' ) ;
189- await dispatch ( 'query_editor_empty' , query_options ) ;
159+ await dispatch ( 'reset_editor' ) ;
190160 }
191161 } else {
192162 console . warn (
@@ -203,7 +173,14 @@ const actions = {
203173 commit ( 'query_window_completed' , data [ 0 ] ) ;
204174 } ,
205175
206- async query_window_empty ( { commit } ) {
176+ async reset ( { dispatch } ) {
177+ await dispatch ( 'reset_window' ) ;
178+ await dispatch ( 'reset_browser' ) ;
179+ await dispatch ( 'reset_editor' ) ;
180+ await dispatch ( 'reset_category' ) ;
181+ } ,
182+
183+ async reset_window ( { commit } ) {
207184 const data = {
208185 app_events : [ ] ,
209186 title_events : [ ] ,
@@ -214,6 +191,32 @@ const actions = {
214191 commit ( 'query_window_completed' , data ) ;
215192 } ,
216193
194+ async reset_browser ( { commit } ) {
195+ const data = {
196+ domains : [ ] ,
197+ urls : [ ] ,
198+ duration : 0 ,
199+ } ;
200+ commit ( 'query_browser_completed' , data ) ;
201+ } ,
202+
203+ async reset_editor ( { commit } ) {
204+ const data = {
205+ files : [ ] ,
206+ projects : [ ] ,
207+ languages : [ ] ,
208+ } ;
209+ commit ( 'query_editor_completed' , data ) ;
210+ } ,
211+
212+ async reset_category ( { commit } ) {
213+ const data = {
214+ by_period : [ ] ,
215+ } ;
216+
217+ commit ( 'query_category_time_by_period_completed' , data ) ;
218+ } ,
219+
217220 async query_desktop_full (
218221 { state, commit, rootState, rootGetters } ,
219222 { timeperiod, filterCategories, filterAFK, includeAudible } : QueryOptions
@@ -245,31 +248,13 @@ const actions = {
245248 commit ( 'query_browser_completed' , data_browser ) ;
246249 } ,
247250
248- async query_browser_empty ( { commit } ) {
249- const data = {
250- domains : [ ] ,
251- urls : [ ] ,
252- duration : 0 ,
253- } ;
254- commit ( 'query_browser_completed' , data ) ;
255- } ,
256-
257251 async query_editor ( { state, commit } , { timeperiod } ) {
258252 const periods = [ timeperiodToStr ( timeperiod ) ] ;
259253 const q = queries . editorActivityQuery ( state . buckets . editor ) ;
260254 const data = await this . _vm . $aw . query ( periods , q ) ;
261255 commit ( 'query_editor_completed' , data [ 0 ] ) ;
262256 } ,
263257
264- async query_editor_empty ( { commit } ) {
265- const data = {
266- files : [ ] ,
267- projects : [ ] ,
268- languages : [ ] ,
269- } ;
270- commit ( 'query_editor_completed' , data ) ;
271- } ,
272-
273258 async query_active_history ( { commit, state } , { timeperiod } : QueryOptions ) {
274259 const periods = timeperiodStrsAroundTimeperiod ( timeperiod ) . filter ( tp_str => {
275260 return ! _ . includes ( state . active . history , tp_str ) ;
@@ -285,13 +270,20 @@ const actions = {
285270 commit ( 'query_active_history_completed' , { active_history } ) ;
286271 } ,
287272
288- async query_category_time_by_hour (
273+ async query_category_time_by_period (
289274 { commit, state } ,
290275 { timeperiod, filterCategories, filterAFK } : QueryOptions
291276 ) {
292277 // TODO: Only works for the 1 day timeperiod
293278 // TODO: Needs to be adapted for Android
294- const periods = timeperiodsStrsHoursOfDay ( timeperiod ) ;
279+ let periods ;
280+ if ( timeperiod . length [ 1 ] == 'day' ) {
281+ periods = timeperiodsStrsHoursOfPeriod ( timeperiod ) ;
282+ } else if ( timeperiod . length [ 1 ] == 'week' || timeperiod . length [ 1 ] == 'month' ) {
283+ periods = timeperiodsStrsDaysOfPeriod ( timeperiod ) ;
284+ } else {
285+ console . error ( 'Unknown timeperiod' ) ;
286+ }
295287 const classes = loadClassesForQuery ( ) ;
296288 const data = await this . _vm . $aw . query (
297289 periods ,
@@ -307,8 +299,7 @@ const actions = {
307299 filter_classes : filterCategories ,
308300 } )
309301 ) ;
310- const category_time_by_hour = _ . zipObject ( periods , data ) ;
311- commit ( 'query_category_time_by_hour_completed' , { category_time_by_hour } ) ;
302+ commit ( 'query_category_time_by_period_completed' , { by_period : _ . zipObject ( periods , data ) } ) ;
312303 } ,
313304
314305 async query_active_history_android ( { commit, state } , { timeperiod } : QueryOptions ) {
@@ -451,6 +442,7 @@ const mutations = {
451442 state . editor . top_projects = null ;
452443
453444 state . category . top = null ;
445+ state . category . by_period = null ;
454446
455447 state . active . duration = null ;
456448
@@ -503,8 +495,8 @@ const mutations = {
503495 } ;
504496 } ,
505497
506- query_category_time_by_hour_completed ( state , { category_time_by_hour } ) {
507- state . category . by_hour = category_time_by_hour ;
498+ query_category_time_by_period_completed ( state , { by_period } ) {
499+ state . category . by_period = by_period ;
508500 } ,
509501
510502 buckets ( state , data ) {
0 commit comments