@@ -56,8 +56,12 @@ const _state = {
5656 browser_buckets : 'all' ,
5757 editor_buckets : 'all' ,
5858 } ,
59- editor_buckets_available : [ ] ,
60- browser_buckets_available : [ ] ,
59+ buckets : {
60+ afk_buckets : [ ] ,
61+ window_buckets : [ ] ,
62+ editor_buckets : [ ] ,
63+ browser_buckets : [ ] ,
64+ } ,
6165} ;
6266
6367function timeperiodsAroundTimeperiod ( timeperiod : TimePeriod ) : TimePeriod [ ] {
@@ -100,95 +104,144 @@ const actions = {
100104 if ( ! query_options . timeperiod ) {
101105 query_options . timeperiod = dateToTimeperiod ( query_options . date ) ;
102106 }
107+
108+ await dispatch ( 'buckets/ensureBuckets' , null , { root : true } ) ;
103109 await dispatch ( 'get_buckets' , query_options ) ;
104110
105111 // TODO: These queries can actually run in parallel, but since server won't process them in parallel anyway we won't.
106- await dispatch ( 'query_window' , query_options ) ;
107- await dispatch ( 'query_browser' , query_options ) ;
108- await dispatch ( 'query_editor' , query_options ) ;
109- await dispatch ( 'query_active_history' , query_options ) ;
112+
113+ if ( state . buckets . afk_buckets . length > 0 && state . buckets . window_buckets . length > 0 ) {
114+ await dispatch ( 'query_window' , query_options ) ;
115+ } else {
116+ console . log ( 'Cannot call query_window as we are missing either an afk or window bucket' ) ;
117+ await dispatch ( 'query_window_empty' , query_options ) ;
118+ }
119+
120+ if (
121+ state . buckets . afk_buckets . length > 0 &&
122+ state . buckets . window_buckets . length > 0 &&
123+ state . buckets . browser_buckets . length > 0
124+ ) {
125+ await dispatch ( 'query_browser' , query_options ) ;
126+ } else {
127+ console . log (
128+ 'Cannot call query_browser as we are missing either an afk, window or browser bucket'
129+ ) ;
130+ await dispatch ( 'query_browser_empty' , query_options ) ;
131+ }
132+
133+ if ( state . buckets . afk_buckets . length > 0 ) {
134+ await dispatch ( 'query_active_history' , query_options ) ;
135+ } else {
136+ console . log ( 'Cannot call query_active_history as we do not have an afk bucket' ) ;
137+ await dispatch ( 'query_active_history_empty' , query_options ) ;
138+ }
139+
140+ if ( state . buckets . editor_buckets . length > 0 ) {
141+ await dispatch ( 'query_editor' , query_options ) ;
142+ } else {
143+ console . log ( 'Cannot call query_editor as we do not have any editor buckets' ) ;
144+ await dispatch ( 'query_editor_empty' , query_options ) ;
145+ }
110146 } else {
111147 console . warn (
112148 'ensure_loaded called twice with same query_options but without query_options.force = true, skipping...'
113149 ) ;
114150 }
115151 } ,
116152
117- async query_window ( { commit } , { host, timeperiod, filterAFK, filterCategories } : QueryOptions ) {
118- const start = moment ( ) ;
153+ async query_window ( { state, commit } , { timeperiod, filterAFK, filterCategories } : QueryOptions ) {
119154 const periods = [ timeperiodToStr ( timeperiod ) ] ;
120155 const classes = loadClassesForQuery ( ) ;
121- const bucket_id_window = 'aw-watcher-window_' + host ;
122- const bucket_id_afk = 'aw-watcher-afk_' + host ;
123156 const q = queries . windowQuery (
124- bucket_id_window ,
125- bucket_id_afk ,
157+ state . buckets . window_buckets [ 0 ] ,
158+ state . buckets . afk_buckets [ 0 ] ,
126159 default_limit , // this.top_apps_count,
127160 default_limit , // this.top_windowtitles_count,
128161 filterAFK ,
129162 classes ,
130163 filterCategories
131164 ) ;
132165 const data = await this . _vm . $aw . query ( periods , q ) ;
133- console . info ( `Completed window query in ${ moment ( ) . diff ( start ) } ms` ) ;
134166 commit ( 'query_window_completed' , data [ 0 ] ) ;
135167 } ,
136168
137- async query_browser ( { state, commit } , { host, timeperiod, filterAFK } : QueryOptions ) {
138- const start = moment ( ) ;
139- if ( state . browser_buckets_available ) {
140- const periods = [ timeperiodToStr ( timeperiod ) ] ;
141- const bucket_id_window = 'aw-watcher-window_' + host ;
142- const bucket_id_afk = 'aw-watcher-afk_' + host ;
143- const q = queries . browserSummaryQuery (
144- state . browser_buckets_available ,
145- bucket_id_window ,
146- bucket_id_afk ,
147- default_limit , // this.top_web_count
148- filterAFK
149- ) ;
150- const data = await this . _vm . $aw . query ( periods , q ) ;
151- console . info ( `Completed browser query in ${ moment ( ) . diff ( start ) } ms` ) ;
152- commit ( 'query_browser_completed' , data [ 0 ] ) ;
153- }
169+ async query_window_empty ( { commit } ) {
170+ const data = {
171+ app_events : [ ] ,
172+ title_events : [ ] ,
173+ cat_events : [ ] ,
174+ duration : 0 ,
175+ active_events : [ ] ,
176+ } ;
177+ commit ( 'query_window_completed' , data ) ;
178+ } ,
179+
180+ async query_browser ( { state, commit } , { timeperiod, filterAFK } : QueryOptions ) {
181+ const periods = [ timeperiodToStr ( timeperiod ) ] ;
182+ const q = queries . browserSummaryQuery (
183+ state . buckets . browser_buckets ,
184+ state . buckets . window_buckets [ 0 ] ,
185+ state . buckets . afk_buckets [ 0 ] ,
186+ default_limit , // this.top_web_count
187+ filterAFK
188+ ) ;
189+ const data = await this . _vm . $aw . query ( periods , q ) ;
190+ commit ( 'query_browser_completed' , data [ 0 ] ) ;
191+ } ,
192+
193+ async query_browser_empty ( { commit } ) {
194+ const data = {
195+ domains : [ ] ,
196+ urls : [ ] ,
197+ duration : 0 ,
198+ } ;
199+ commit ( 'query_browser_completed' , data ) ;
154200 } ,
155201
156202 async query_editor ( { state, commit } , { timeperiod } ) {
157- if ( state . editor_buckets_available ) {
158- const periods = [ timeperiodToStr ( timeperiod ) ] ;
159- const q = queries . editorActivityQuery ( state . editor_buckets_available , 100 ) ;
160- const data = await this . _vm . $aw . query ( periods , q ) . catch ( this . errorHandler ) ;
161- commit ( 'query_editor_completed' , data [ 0 ] ) ;
162- }
203+ const periods = [ timeperiodToStr ( timeperiod ) ] ;
204+ const q = queries . editorActivityQuery ( state . buckets . editor_buckets , 100 ) ;
205+ const data = await this . _vm . $aw . query ( periods , q ) ;
206+ commit ( 'query_editor_completed' , data [ 0 ] ) ;
163207 } ,
164208
165- async query_active_history ( { commit, state } , { host, timeperiod } : QueryOptions ) {
209+ async query_editor_empty ( { commit } ) {
210+ const data = {
211+ files : [ ] ,
212+ projects : [ ] ,
213+ languages : [ ] ,
214+ } ;
215+ commit ( 'query_editor_completed' , data ) ;
216+ } ,
217+
218+ async query_active_history ( { commit, state } , { timeperiod } : QueryOptions ) {
166219 const periods = timeperiodStrsAroundTimeperiod ( timeperiod ) . filter ( tp_str => {
167220 return ! _ . includes ( state . active_history , tp_str ) ;
168221 } ) ;
169- const bucket_id_afk = 'aw-watcher-afk_' + host ;
170- const queryStart = moment ( ) ;
222+ const bucket_id_afk = state . buckets . afk_buckets [ 0 ] ;
171223 const data = await this . _vm . $aw . query ( periods , queries . dailyActivityQuery ( bucket_id_afk ) ) ;
172224 const active_history = _ . zipObject (
173225 periods ,
174226 _ . map ( data , pair => _ . filter ( pair , e => e . data . status == 'not-afk' ) )
175227 ) ;
176- console . info ( `Completed history query in ${ moment ( ) . diff ( queryStart ) } ms` ) ;
177228 commit ( 'query_active_history_completed' , { active_history } ) ;
178229 } ,
179230
180- async get_buckets ( { commit } ) {
181- const buckets = await this . _vm . $aw . getBuckets ( ) ;
182- const browser_buckets = _ . map (
183- _ . filter ( buckets , bucket => bucket [ 'type' ] === 'web.tab.current' ) ,
184- bucket => bucket [ 'id' ]
185- ) ;
186- const editor_buckets = _ . map (
187- _ . filter ( buckets , bucket => bucket [ 'type' ] === 'app.editor.activity' ) ,
188- bucket => bucket [ 'id' ]
189- ) ;
190- commit ( 'browser_buckets' , browser_buckets ) ;
191- commit ( 'editor_buckets' , editor_buckets ) ;
231+ async query_active_history_empty ( { commit } ) {
232+ const data = [ ] ;
233+ commit ( 'query_active_history_completed' , data ) ;
234+ } ,
235+
236+ async get_buckets ( { commit, rootGetters } , { host } ) {
237+ const buckets = {
238+ afk_buckets : rootGetters [ 'buckets/afkBucketsByHost' ] ( host ) ,
239+ window_buckets : rootGetters [ 'buckets/windowBucketsByHost' ] ( host ) ,
240+ browser_buckets : rootGetters [ 'buckets/browserBuckets' ] ,
241+ editor_buckets : rootGetters [ 'buckets/editorBuckets' ] ,
242+ } ;
243+ console . log ( 'Available buckets: ' , buckets ) ;
244+ commit ( 'buckets' , buckets ) ;
192245 } ,
193246
194247 async load_demo ( { commit } ) {
@@ -409,12 +462,8 @@ const mutations = {
409462 } ;
410463 } ,
411464
412- browser_buckets ( state , data ) {
413- state . browser_buckets_available = data ;
414- } ,
415-
416- editor_buckets ( state , data ) {
417- state . editor_buckets_available = data ;
465+ buckets ( state , data ) {
466+ state . buckets = data ;
418467 } ,
419468} ;
420469
0 commit comments