Skip to content

Commit e30bad4

Browse files
refactor: Made activity_daily vuex more structured and easier to read
1 parent 6379134 commit e30bad4

6 files changed

Lines changed: 120 additions & 82 deletions

File tree

src/store/modules/activity_daily.ts

Lines changed: 92 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,39 @@ interface QueryOptions {
3636

3737
// initial state
3838
const _state = {
39-
top_apps: [],
40-
top_titles: [],
41-
42-
browser_duration: [],
43-
top_domains: [],
44-
top_urls: [],
45-
46-
editor_duration: [],
47-
top_editor_files: [],
48-
top_editor_languages: [],
49-
top_editor_projects: [],
50-
51-
top_categories: [],
52-
active_events: [],
53-
active_duration: 0,
54-
active_history: {},
39+
window: {
40+
available: false,
41+
top_apps: [],
42+
top_titles: [],
43+
},
44+
45+
browser: {
46+
available: false,
47+
duration: [],
48+
top_domains: [],
49+
top_urls: [],
50+
},
51+
52+
editor: {
53+
available: false,
54+
duration: [],
55+
top_files: [],
56+
top_languages: [],
57+
top_projects: [],
58+
},
59+
60+
category: {
61+
available: false,
62+
top: [],
63+
},
64+
65+
active: {
66+
available: false,
67+
duration: 0,
68+
events: [],
69+
history: {},
70+
},
71+
5572
query_options: {
5673
browser_buckets: 'all',
5774
editor_buckets: 'all',
@@ -84,8 +101,8 @@ const getters = {
84101
getActiveHistoryAroundTimeperiod: state => (timeperiod: TimePeriod) => {
85102
const periods = timeperiodStrsAroundTimeperiod(timeperiod);
86103
const _history = periods.map(tp => {
87-
if (_.has(state.active_history, tp)) {
88-
return state.active_history[tp];
104+
if (_.has(state.active.history, tp)) {
105+
return state.active.history[tp];
89106
} else {
90107
// A zero-duration placeholder until new data has been fetched
91108
return [{ timestamp: moment(tp.split('/')[0]).format(), duration: 0, data: {} }];
@@ -109,19 +126,16 @@ const actions = {
109126
await dispatch('get_buckets', query_options);
110127

111128
// TODO: These queries can actually run in parallel, but since server won't process them in parallel anyway we won't.
129+
await dispatch('set_available', query_options);
112130

113-
if (state.buckets.afk_buckets.length > 0 && state.buckets.window_buckets.length > 0) {
131+
if (state.window.available) {
114132
await dispatch('query_window', query_options);
115133
} else {
116134
console.log('Cannot call query_window as we are missing either an afk or window bucket');
117135
await dispatch('query_window_empty', query_options);
118136
}
119137

120-
if (
121-
state.buckets.afk_buckets.length > 0 &&
122-
state.buckets.window_buckets.length > 0 &&
123-
state.buckets.browser_buckets.length > 0
124-
) {
138+
if (state.browser.available) {
125139
await dispatch('query_browser', query_options);
126140
} else {
127141
console.log(
@@ -130,14 +144,14 @@ const actions = {
130144
await dispatch('query_browser_empty', query_options);
131145
}
132146

133-
if (state.buckets.afk_buckets.length > 0) {
147+
if (state.active.available) {
134148
await dispatch('query_active_history', query_options);
135149
} else {
136150
console.log('Cannot call query_active_history as we do not have an afk bucket');
137151
await dispatch('query_active_history_empty', query_options);
138152
}
139153

140-
if (state.buckets.editor_buckets.length > 0) {
154+
if (state.editor.available) {
141155
await dispatch('query_editor', query_options);
142156
} else {
143157
console.log('Cannot call query_editor as we do not have any editor buckets');
@@ -217,7 +231,7 @@ const actions = {
217231

218232
async query_active_history({ commit, state }, { timeperiod }: QueryOptions) {
219233
const periods = timeperiodStrsAroundTimeperiod(timeperiod).filter(tp_str => {
220-
return !_.includes(state.active_history, tp_str);
234+
return !_.includes(state.active.history, tp_str);
221235
});
222236
const bucket_id_afk = state.buckets.afk_buckets[0];
223237
const data = await this._vm.$aw.query(periods, queries.dailyActivityQuery(bucket_id_afk));
@@ -233,6 +247,23 @@ const actions = {
233247
commit('query_active_history_completed', data);
234248
},
235249

250+
async set_available({ commit, state }) {
251+
const window_available =
252+
state.buckets.afk_buckets.length > 0 && state.buckets.window_buckets.length > 0;
253+
const browser_available =
254+
state.buckets.afk_buckets.length > 0 &&
255+
state.buckets.window_buckets.length > 0 &&
256+
state.buckets.browser_buckets.length > 0;
257+
const active_available = state.buckets.afk_buckets.length > 0;
258+
const editor_available = state.buckets.editor_buckets.length > 0;
259+
commit('set_available', {
260+
window_available: window_available,
261+
browser_available: browser_available,
262+
active_available: active_available,
263+
editor_available: editor_available,
264+
});
265+
},
266+
236267
async get_buckets({ commit, rootGetters }, { host }) {
237268
const buckets = {
238269
afk_buckets: rootGetters['buckets/afkBucketsByHost'](host),
@@ -410,54 +441,62 @@ const mutations = {
410441
state.query_options = query_options;
411442

412443
// Resets the store state while waiting for new query to finish
413-
state.top_apps = null;
414-
state.top_titles = null;
444+
state.window.top_apps = null;
445+
state.window.top_titles = null;
415446

416-
state.browser_duration = 0;
447+
state.browser.duration = 0;
417448
state.top_domains = null;
418449
state.top_urls = null;
419450

420-
state.editor_duration = 0;
421-
state.top_editor_files = null;
422-
state.top_editor_languages = null;
423-
state.top_editor_projects = null;
451+
state.editor.duration = 0;
452+
state.editor.top_files = null;
453+
state.editor.top_languages = null;
454+
state.editor.top_projects = null;
455+
456+
state.category.top = null;
424457

425-
state.top_categories = null;
426-
state.active_duration = null;
427-
state.active_events = null;
458+
state.active.duration = null;
459+
state.active.events = null;
460+
461+
state.active.history = {};
462+
},
428463

429-
state.active_history = {};
464+
set_available(state, data) {
465+
state.window.available = data['window_available'];
466+
state.browser.available = data['browser_available'];
467+
state.active.available = data['active_available'];
468+
state.editor.available = data['editor_available'];
469+
state.category.available = data['window_available'];
430470
},
431471

432472
query_window_completed(state, data) {
433-
state.top_apps = data['app_events'];
434-
state.top_titles = data['title_events'];
435-
state.top_categories = data['cat_events'];
436-
state.active_duration = data['duration'];
437-
state.app_chunks = data['app_chunks'];
438-
state.active_events = data['active_events'];
473+
state.window.top_apps = data['app_events'];
474+
state.window.top_titles = data['title_events'];
475+
state.category.top = data['cat_events'];
476+
state.active.duration = data['duration'];
477+
state.active.events = data['active_events'];
439478
},
440479

441480
query_browser_completed(state, data) {
442-
state.top_domains = data['domains'];
443-
state.top_urls = data['urls'];
444-
state.browser_duration = data['duration'];
481+
state.browser.top_domains = data['domains'];
482+
state.browser.top_urls = data['urls'];
483+
state.browser.duration = data['duration'];
445484

446485
// FIXME: This one might take up a lot of size in the request, move it to a seperate request
447486
// (or remove entirely, since we have the other timeline now)
448487
state.web_chunks = data['chunks'];
449488
},
450489

451490
query_editor_completed(state, data) {
452-
state.editor_duration = data['duration'];
453-
state.top_editor_files = data['files'];
454-
state.top_editor_languages = data['languages'];
455-
state.top_editor_projects = data['projects'];
491+
state.editor.duration = data['duration'];
492+
state.editor.top_files = data['files'];
493+
state.editor.top_languages = data['languages'];
494+
state.editor.top_projects = data['projects'];
456495
},
457496

458497
query_active_history_completed(state, { active_history }) {
459-
state.active_history = {
460-
...state.active_history,
498+
state.active.history = {
499+
...state.active.history,
461500
...active_history,
462501
};
463502
},

src/views/activity/daily/ActivityDaily.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ div
55
p
66
| Host: {{ host }}
77
br
8-
| Active time: {{ $store.state.activity_daily.active_duration | friendlyduration }}
8+
| Active time: {{ $store.state.activity_daily.active.duration | friendlyduration }}
99

1010
div.d-flex
1111
div.p-1

src/views/activity/daily/ActivityDailyBrowser.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ div
66
small Make sure you have a browser watcher installed to use this feature
77
div(v-if="browserBuckets.length > 0")
88

9-
h6 Active browser time: {{ $store.state.activity_daily.browser_duration | friendlyduration }}
9+
h6 Active browser time: {{ $store.state.activity_daily.browser.duration | friendlyduration }}
1010

1111
div.row
1212
div.col-md-6
1313
h5 Top Browser Domains
1414
div(v-if="browserBuckets")
15-
aw-summary(:fields="$store.state.activity_daily.top_domains", :namefunc="e => e.data.$domain", :colorfunc="e => e.data.$domain", with_limit)
15+
aw-summary(:fields="$store.state.activity_daily.browser.top_domains", :namefunc="e => e.data.$domain", :colorfunc="e => e.data.$domain", with_limit)
1616

1717
div.col-md-6
1818
h5 Top Browser URLs
1919
div(v-if="browserBuckets")
20-
aw-summary(:fields="$store.state.activity_daily.top_urls", :namefunc="e => e.data.url", :colorfunc="e => e.data.$domain", with_limit)
20+
aw-summary(:fields="$store.state.activity_daily.browser.top_urls", :namefunc="e => e.data.url", :colorfunc="e => e.data.$domain", with_limit)
2121

2222
//div(v-if="periodLength === 'day'")
2323
br

src/views/activity/daily/ActivityDailyEditor.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@ div
55
h6 No editor buckets available
66
small Make sure you have an editor watcher installed to use this feature
77
div(v-if="editorBuckets.length")
8-
h6 Active editor time: {{ $store.state.activity_daily.editor_duration | friendlyduration }}
8+
h6 Active editor time: {{ $store.state.activity_daily.editor.duration | friendlyduration }}
99
div.row(style="padding-top: 0.5em;")
1010
div.col-md-4
1111
h5 Top file activity
12-
aw-summary(:fields="$store.state.activity_daily.top_editor_files",
12+
aw-summary(:fields="$store.state.activity_daily.editor.top_files",
1313
:namefunc="top_editor_files_namefunc",
1414
:colorfunc="top_editor_files_colorfunc", with_limit)
1515

1616
div.col-md-4
1717
h5 Top language activity
18-
aw-summary(:fields="$store.state.activity_daily.top_editor_languages",
18+
aw-summary(:fields="$store.state.activity_daily.editor.top_languages",
1919
:namefunc="top_editor_languages_namefunc",
2020
:colorfunc="top_editor_languages_colorfunc", with_limit)
2121

2222
div.col-md-4
2323
h5 Top project activity
24-
aw-summary(:fields="$store.state.activity_daily.top_editor_projects",
24+
aw-summary(:fields="$store.state.activity_daily.editor.top_projects",
2525
:namefunc="top_editor_projects_namefunc",
2626
:colorfunc="top_editor_projects_colorfunc", with_limit)
2727
br

src/views/activity/daily/ActivityDailySummary.vue

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,16 @@ export default {
9393
},
9494
computed: {
9595
top_apps: function() {
96-
return this.$store.state.activity_daily.top_apps;
96+
return this.$store.state.activity_daily.window.top_apps;
9797
},
9898
top_titles: function() {
99-
return this.$store.state.activity_daily.top_titles;
99+
return this.$store.state.activity_daily.window.top_titles;
100100
},
101101
top_categories: function() {
102-
return this.$store.state.activity_daily.top_categories;
102+
return this.$store.state.activity_daily.category.top;
103103
},
104104
top_domains: function() {
105-
return this.$store.state.activity_daily.top_domains;
106-
},
107-
//top_urls: function() { return this.$store.state.activity_daily.top_urls },
108-
browser_buckets: function() {
109-
return this.$store.state.activity_daily.browser_buckets;
105+
return this.$store.state.activity_daily.browser.top_domains;
110106
},
111107
top_categories_hierarchy: function() {
112108
if (this.top_categories) {
@@ -123,7 +119,7 @@ export default {
123119
}
124120
},
125121
datasets: function() {
126-
const data = split_by_hour_into_data(this.$store.state.activity_daily.active_events);
122+
const data = split_by_hour_into_data(this.$store.state.activity_daily.active.events);
127123
return [
128124
{
129125
label: 'Total time',

src/views/activity/daily/ActivityDailyWindow.vue

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<template lang="pug">
22
div
3+
div.row.mb-6
4+
div.col-md-6
5+
h5 Top Applications
6+
aw-summary(:fields="top_apps", :namefunc="e => e.data.app", :colorfunc="e => e.data.app", with_limit)
7+
div.col-md-6
8+
h5 Top Window Titles
9+
aw-summary(:fields="top_titles", :namefunc="e => e.data.title", :colorfunc="e => e.data.app", with_limit)
10+
311
div(v-if="periodLength == 'day'")
4-
// This is commented out because it requires a special query return which can become huge for periodLength's that are not day.
5-
// It's not very useful anymore anyway since we have the timeline now.
6-
//div
7-
b-form-checkbox(v-model="timelineShowAFK")
8-
| Show AFK time
9-
aw-timeline-inspect(:chunks="app_chunks", :show_afk='timelineShowAFK', :chunkfunc='e => e.data.app', :eventfunc='e => e.data.title')
10-
hr
1112
aw-sunburst-clock(:date="date", :afkBucketId="bucket_id_afk", :windowBucketId="bucket_id_window")
12-
1313
div(v-else)
1414
| Nothing to show here for the current period length: {{ periodLength }}
1515
</template>
@@ -36,14 +36,17 @@ export default {
3636
};
3737
},
3838
computed: {
39-
app_chunks: function() {
40-
return this.$store.state.activity_daily.app_chunks;
39+
top_apps: function() {
40+
return this.$store.state.activity_daily.window.top_apps;
41+
},
42+
top_titles: function() {
43+
return this.$store.state.activity_daily.window.top_titles;
4144
},
4245
bucket_id_window: function() {
43-
return 'aw-watcher-window_' + this.host;
46+
return this.$store.state.activity_daily.buckets.window_buckets[0];
4447
},
4548
bucket_id_afk: function() {
46-
return 'aw-watcher-afk_' + this.host;
49+
return this.$store.state.activity_daily.buckets.afk_buckets[0];
4750
},
4851
},
4952
};

0 commit comments

Comments
 (0)