Skip to content

Commit

Permalink
fix: See host if only editor buckets exist
Browse files Browse the repository at this point in the history
  • Loading branch information
johan-bjareholt committed Apr 21, 2020
1 parent 3451196 commit 59197ce
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 66 deletions.
2 changes: 1 addition & 1 deletion src/components/Header.vue
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export default {
//console.log(types_by_host);
_.each(types_by_host, (types, hostname) => {
if (types.afk && types.window) {
if (hostname != 'unknown') {
this.activityViews.push({
name: hostname,
hostname: hostname,
Expand Down
165 changes: 107 additions & 58 deletions src/store/modules/activity_daily.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ const _state = {
browser_buckets: 'all',
editor_buckets: 'all',
},
editor_buckets_available: [],
browser_buckets_available: [],
buckets: {
afk_buckets: [],
window_buckets: [],
editor_buckets: [],
browser_buckets: [],
},
};

function timeperiodsAroundTimeperiod(timeperiod: TimePeriod): TimePeriod[] {
Expand Down Expand Up @@ -100,95 +104,144 @@ const actions = {
if (!query_options.timeperiod) {
query_options.timeperiod = dateToTimeperiod(query_options.date);
}

await dispatch('buckets/ensureBuckets', null, { root: true });
await dispatch('get_buckets', query_options);

// TODO: These queries can actually run in parallel, but since server won't process them in parallel anyway we won't.
await dispatch('query_window', query_options);
await dispatch('query_browser', query_options);
await dispatch('query_editor', query_options);
await dispatch('query_active_history', query_options);

if (state.buckets.afk_buckets.length > 0 && state.buckets.window_buckets.length > 0) {
await dispatch('query_window', query_options);
} else {
console.log('Cannot call query_window as we are missing either an afk or window bucket');
await dispatch('query_window_empty', query_options);
}

if (
state.buckets.afk_buckets.length > 0 &&
state.buckets.window_buckets.length > 0 &&
state.buckets.browser_buckets.length > 0
) {
await dispatch('query_browser', query_options);
} else {
console.log(
'Cannot call query_browser as we are missing either an afk, window or browser bucket'
);
await dispatch('query_browser_empty', query_options);
}

if (state.buckets.afk_buckets.length > 0) {
await dispatch('query_active_history', query_options);
} else {
console.log('Cannot call query_active_history as we do not have an afk bucket');
await dispatch('query_active_history_empty', query_options);
}

if (state.buckets.editor_buckets.length > 0) {
await dispatch('query_editor', query_options);
} else {
console.log('Cannot call query_editor as we do not have any editor buckets');
await dispatch('query_editor_empty', query_options);
}
} else {
console.warn(
'ensure_loaded called twice with same query_options but without query_options.force = true, skipping...'
);
}
},

async query_window({ commit }, { host, timeperiod, filterAFK, filterCategories }: QueryOptions) {
const start = moment();
async query_window({ state, commit }, { timeperiod, filterAFK, filterCategories }: QueryOptions) {
const periods = [timeperiodToStr(timeperiod)];
const classes = loadClassesForQuery();
const bucket_id_window = 'aw-watcher-window_' + host;
const bucket_id_afk = 'aw-watcher-afk_' + host;
const q = queries.windowQuery(
bucket_id_window,
bucket_id_afk,
state.buckets.window_buckets[0],
state.buckets.afk_buckets[0],
default_limit, // this.top_apps_count,
default_limit, // this.top_windowtitles_count,
filterAFK,
classes,
filterCategories
);
const data = await this._vm.$aw.query(periods, q);
console.info(`Completed window query in ${moment().diff(start)}ms`);
commit('query_window_completed', data[0]);
},

async query_browser({ state, commit }, { host, timeperiod, filterAFK }: QueryOptions) {
const start = moment();
if (state.browser_buckets_available) {
const periods = [timeperiodToStr(timeperiod)];
const bucket_id_window = 'aw-watcher-window_' + host;
const bucket_id_afk = 'aw-watcher-afk_' + host;
const q = queries.browserSummaryQuery(
state.browser_buckets_available,
bucket_id_window,
bucket_id_afk,
default_limit, // this.top_web_count
filterAFK
);
const data = await this._vm.$aw.query(periods, q);
console.info(`Completed browser query in ${moment().diff(start)}ms`);
commit('query_browser_completed', data[0]);
}
async query_window_empty({ commit }) {
const data = {
app_events: [],
title_events: [],
cat_events: [],
duration: 0,
active_events: [],
};
commit('query_window_completed', data);
},

async query_browser({ state, commit }, { timeperiod, filterAFK }: QueryOptions) {
const periods = [timeperiodToStr(timeperiod)];
const q = queries.browserSummaryQuery(
state.buckets.browser_buckets,
state.buckets.window_buckets[0],
state.buckets.afk_buckets[0],
default_limit, // this.top_web_count
filterAFK
);
const data = await this._vm.$aw.query(periods, q);
commit('query_browser_completed', data[0]);
},

async query_browser_empty({ commit }) {
const data = {
domains: [],
urls: [],
duration: 0,
};
commit('query_browser_completed', data);
},

async query_editor({ state, commit }, { timeperiod }) {
if (state.editor_buckets_available) {
const periods = [timeperiodToStr(timeperiod)];
const q = queries.editorActivityQuery(state.editor_buckets_available, 100);
const data = await this._vm.$aw.query(periods, q).catch(this.errorHandler);
commit('query_editor_completed', data[0]);
}
const periods = [timeperiodToStr(timeperiod)];
const q = queries.editorActivityQuery(state.buckets.editor_buckets, 100);
const data = await this._vm.$aw.query(periods, q);
commit('query_editor_completed', data[0]);
},

async query_active_history({ commit, state }, { host, timeperiod }: QueryOptions) {
async query_editor_empty({ commit }) {
const data = {
files: [],
projects: [],
languages: [],
};
commit('query_editor_completed', data);
},

async query_active_history({ commit, state }, { timeperiod }: QueryOptions) {
const periods = timeperiodStrsAroundTimeperiod(timeperiod).filter(tp_str => {
return !_.includes(state.active_history, tp_str);
});
const bucket_id_afk = 'aw-watcher-afk_' + host;
const queryStart = moment();
const bucket_id_afk = state.buckets.afk_buckets[0];
const data = await this._vm.$aw.query(periods, queries.dailyActivityQuery(bucket_id_afk));
const active_history = _.zipObject(
periods,
_.map(data, pair => _.filter(pair, e => e.data.status == 'not-afk'))
);
console.info(`Completed history query in ${moment().diff(queryStart)}ms`);
commit('query_active_history_completed', { active_history });
},

async get_buckets({ commit }) {
const buckets = await this._vm.$aw.getBuckets();
const browser_buckets = _.map(
_.filter(buckets, bucket => bucket['type'] === 'web.tab.current'),
bucket => bucket['id']
);
const editor_buckets = _.map(
_.filter(buckets, bucket => bucket['type'] === 'app.editor.activity'),
bucket => bucket['id']
);
commit('browser_buckets', browser_buckets);
commit('editor_buckets', editor_buckets);
async query_active_history_empty({ commit }) {
const data = [];
commit('query_active_history_completed', data);
},

async get_buckets({ commit, rootGetters }, { host }) {
const buckets = {
afk_buckets: rootGetters['buckets/afkBucketsByHost'](host),
window_buckets: rootGetters['buckets/windowBucketsByHost'](host),
browser_buckets: rootGetters['buckets/browserBuckets'],
editor_buckets: rootGetters['buckets/editorBuckets'],
};
console.log('Available buckets: ', buckets);
commit('buckets', buckets);
},

async load_demo({ commit }) {
Expand Down Expand Up @@ -409,12 +462,8 @@ const mutations = {
};
},

browser_buckets(state, data) {
state.browser_buckets_available = data;
},

editor_buckets(state, data) {
state.editor_buckets_available = data;
buckets(state, data) {
state.buckets = data;
},
};

Expand Down
35 changes: 30 additions & 5 deletions src/store/modules/buckets.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,39 @@ const state = {
buckets: [],
};

function get_buckets_by_type(buckets, type) {
return _.map(
_.filter(buckets, bucket => bucket['type'] === type),
bucket => bucket['id']
);
}

function get_buckets_by_host_and_type(buckets, host, type) {
return _.map(
_.filter(buckets, bucket => bucket['type'] === type && bucket['hostname'] == host),
bucket => bucket['id']
);
}

// getters
const getters = {
afkBuckets(state) {
return get_buckets_by_type(state.buckets, 'afkstatus');
},
afkBucketsByHost: state => host => {
return get_buckets_by_host_and_type(state.buckets, host, 'afkstatus');
},
windowBuckets(state) {
return get_buckets_by_type(state.buckets, 'currentwindow');
},
windowBucketsByHost: state => host => {
return get_buckets_by_host_and_type(state.buckets, host, 'currentwindow');
},
editorBuckets(state) {
return get_buckets_by_type(state.buckets, 'app.editor.activity');
},
browserBuckets(state) {
return _.map(
_.filter(state.buckets, bucket => bucket['type'] === 'web.tab.current'),
bucket => bucket['id']
);
return get_buckets_by_type(state.buckets, 'web.tab.current');
},
getBucket: state => id => _.filter(state.buckets, b => b.id === id)[0],
bucketsByHostname: state => _.groupBy(state.buckets, 'hostname'),
Expand All @@ -25,7 +51,6 @@ const actions = {

async loadBuckets({ commit }) {
const buckets = await this._vm.$aw.getBuckets();
console.info('Received buckets: ', buckets);
commit('update_buckets', buckets);
},

Expand Down
2 changes: 1 addition & 1 deletion src/views/activity/daily/ActivityDailyBrowser.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default {
},
computed: {
browserBuckets: function() {
return this.$store.state.activity_daily.browser_buckets_available;
return this.$store.state.activity_daily.buckets.browser_buckets;
},
},
};
Expand Down
2 changes: 1 addition & 1 deletion src/views/activity/daily/ActivityDailyEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default {
computed: {
editorBuckets: function() {
return this.$store.state.activity_daily.editor_buckets_available;
return this.$store.state.activity_daily.buckets.editor_buckets;
},
dateEnd: function() {
return moment(this.date)
Expand Down

0 comments on commit 59197ce

Please sign in to comment.