Move refresh logic into parent_bot store

This commit is contained in:
Matthias 2021-08-29 14:39:52 +02:00
parent 2cca4c9765
commit 0bdf73e63b
3 changed files with 92 additions and 84 deletions

View File

@ -1,24 +1,26 @@
<template> <template>
<div class="container-fluid"> <div class="d-flex flex-align-center">
<div class="row"> <b-button class="m-1 mr-3" variant="secondary" size="sm" @click="refreshAll(true)">
<button class="m-1 btn btn-primary" @click="refreshAll(true)"><RefreshIcon /></button> <RefreshIcon :size="16" />
</b-button>
<b-form-checkbox <b-form-checkbox
v-model="autoRefreshLoc" v-model="autoRefreshLoc"
class="ml-auto float-right mr-2 my-auto" class="ml-auto float-right mr-2 my-auto"
title="AutoRefresh" title="AutoRefresh"
switch variant="secondary"
>AutoRefresh</b-form-checkbox >AutoRefresh</b-form-checkbox
> >
</div> </div>
</div>
</template> </template>
<script lang="ts"> <script lang="ts">
import { Component, Vue, Watch } from 'vue-property-decorator'; import { Component, Vue, Watch } from 'vue-property-decorator';
import { Action, Getter, State } from 'vuex-class'; import { Getter, State, namespace } from 'vuex-class';
import RefreshIcon from 'vue-material-design-icons/Refresh.vue'; import RefreshIcon from 'vue-material-design-icons/Refresh.vue';
const ftbot = namespace('ftbot');
@Component({ components: { RefreshIcon } }) @Component({ components: { RefreshIcon } })
export default class ReloadControl extends Vue { export default class ReloadControl extends Vue {
refreshInterval: number | null = null; refreshInterval: number | null = null;
@ -43,18 +45,18 @@ export default class ReloadControl extends Vue {
// TODO-multi: This should be per bot! // TODO-multi: This should be per bot!
@Getter loggedIn; @Getter loggedIn;
@State autoRefresh!: boolean; @ftbot.Getter autoRefresh!: boolean;
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
@Action setAutoRefresh!: (newValue: boolean) => void; @ftbot.Action setAutoRefresh!: (newValue: boolean) => void;
@Action refreshSlow; @ftbot.Action refreshSlow;
@Action refreshFrequent; @ftbot.Action refreshFrequent;
@Action refreshAll; @ftbot.Action refreshAll;
@Action refreshOnce; @ftbot.Action refreshOnce;
get autoRefreshLoc() { get autoRefreshLoc() {
return this.autoRefresh; return this.autoRefresh;

View File

@ -2,16 +2,13 @@ import Vue from 'vue';
import Vuex from 'vuex'; import Vuex from 'vuex';
import { getCurrentTheme, getTheme, storeCurrentTheme } from '@/shared/themes'; import { getCurrentTheme, getTheme, storeCurrentTheme } from '@/shared/themes';
import axios, { AxiosInstance } from 'axios'; import axios from 'axios';
import { UserService } from '@/shared/userService'; import { UserService } from '@/shared/userService';
import createBotStore from './modules/botStoreWrapper'; import createBotStore from './modules/botStoreWrapper';
import { BotStoreGetters } from './modules/ftbot';
import alertsModule from './modules/alerts'; import alertsModule from './modules/alerts';
import layoutModule from './modules/layout'; import layoutModule from './modules/layout';
import settingsModule from './modules/settings'; import settingsModule from './modules/settings';
const AUTO_REFRESH = 'ft_auto_refresh';
Vue.use(Vuex); Vue.use(Vuex);
const initCurrentTheme = getCurrentTheme(); const initCurrentTheme = getCurrentTheme();
@ -22,8 +19,6 @@ const store = new Vuex.Store({
uiSettings: settingsModule, uiSettings: settingsModule,
}, },
state: { state: {
refreshing: false,
autoRefresh: JSON.parse(localStorage.getItem(AUTO_REFRESH) || '{}'),
currentTheme: initCurrentTheme, currentTheme: initCurrentTheme,
uiVersion: 'dev', uiVersion: 'dev',
}, },
@ -46,13 +41,6 @@ const store = new Vuex.Store({
}, },
}, },
mutations: { mutations: {
setAutoRefresh(state, newRefreshValue: boolean) {
state.autoRefresh = newRefreshValue;
},
setRefreshing(state, refreshing: boolean) {
state.refreshing = refreshing;
},
mutateCurrentTheme(state, newTheme: string) { mutateCurrentTheme(state, newTheme: string) {
storeCurrentTheme(newTheme); storeCurrentTheme(newTheme);
state.currentTheme = newTheme; state.currentTheme = newTheme;
@ -65,10 +53,7 @@ const store = new Vuex.Store({
setCurrentTheme({ commit }, newTheme: string) { setCurrentTheme({ commit }, newTheme: string) {
commit('mutateCurrentTheme', newTheme); commit('mutateCurrentTheme', newTheme);
}, },
setAutoRefresh({ commit }, newRefreshValue) {
commit('setAutoRefresh', newRefreshValue);
localStorage.setItem(AUTO_REFRESH, JSON.stringify(newRefreshValue));
},
setLoggedIn({ commit }, loggedin: boolean) { setLoggedIn({ commit }, loggedin: boolean) {
commit('setLoggedIn', loggedin); commit('setLoggedIn', loggedin);
}, },
@ -76,12 +61,9 @@ const store = new Vuex.Store({
commit('setIsBotOnline', isOnline); commit('setIsBotOnline', isOnline);
if (isOnline === false) { if (isOnline === false) {
console.log('disabling autorun'); console.log('disabling autorun');
dispatch('setAutoRefresh', false); dispatch('ftbot/setAutoRefresh', false);
} }
}, },
refreshOnce({ dispatch }) {
dispatch('ftbot/getVersion');
},
async loadUIVersion({ commit }) { async loadUIVersion({ commit }) {
if (process.env.NODE_ENV !== 'development') { if (process.env.NODE_ENV !== 'development') {
try { try {
@ -94,51 +76,6 @@ const store = new Vuex.Store({
} }
} }
}, },
async refreshAll({ dispatch, state, commit }, forceUpdate = false) {
if (state.refreshing) {
return;
}
commit('setRefreshing', true);
try {
const updates: Promise<AxiosInstance>[] = [];
updates.push(dispatch('refreshFrequent', false));
updates.push(dispatch('refreshSlow', forceUpdate));
updates.push(dispatch('ftbot/getDaily'));
updates.push(dispatch('ftbot/getBalance'));
await Promise.all(updates);
console.log('refreshing_end');
} finally {
commit('setRefreshing', false);
}
},
async refreshSlow({ dispatch, getters, state }, forceUpdate = false) {
if (state.refreshing && !forceUpdate) {
return;
}
// Refresh data only when needed
if (forceUpdate || getters[`ftbot/${BotStoreGetters.refreshRequired}`]) {
const updates: Promise<AxiosInstance>[] = [];
updates.push(dispatch('ftbot/getPerformance'));
updates.push(dispatch('ftbot/getProfit'));
updates.push(dispatch('ftbot/getTrades'));
/* white/blacklist might be refreshed more often as they are not expensive on the backend */
updates.push(dispatch('ftbot/getWhitelist'));
updates.push(dispatch('ftbot/getBlacklist'));
await Promise.all(updates);
dispatch('ftbot/setRefreshRequired', false);
}
},
refreshFrequent({ dispatch }, slow = true) {
if (slow) {
dispatch('refreshSlow', false);
}
// Refresh data that's needed in near realtime
dispatch('ftbot/getOpenTrades');
dispatch('ftbot/getState');
dispatch('ftbot/getLocks');
},
}, },
}); });

View File

@ -1,9 +1,14 @@
import { BotDescriptor, BotDescriptors } from '@/types'; import { BotDescriptor, BotDescriptors } from '@/types';
import { AxiosInstance } from 'axios';
import { BotStoreActions, BotStoreGetters, createBotSubStore } from './ftbot'; import { BotStoreActions, BotStoreGetters, createBotSubStore } from './ftbot';
const AUTO_REFRESH = 'ft_auto_refresh';
interface FTMultiBotState { interface FTMultiBotState {
selectedBot: string; selectedBot: string;
availableBots: BotDescriptors; availableBots: BotDescriptors;
autoRefresh: boolean;
refreshing: boolean;
} }
export enum MultiBotStoreGetters { export enum MultiBotStoreGetters {
@ -13,12 +18,15 @@ export enum MultiBotStoreGetters {
allAvailableBotsList = 'allAvailableBotsList', allAvailableBotsList = 'allAvailableBotsList',
allIsBotOnline = 'allIsBotOnline', allIsBotOnline = 'allIsBotOnline',
nextBotId = 'nextBotId', nextBotId = 'nextBotId',
autoRefresh = 'autoRefresh',
} }
export default function createBotStore(store) { export default function createBotStore(store) {
const state: FTMultiBotState = { const state: FTMultiBotState = {
selectedBot: '', selectedBot: '',
availableBots: {}, availableBots: {},
autoRefresh: JSON.parse(localStorage.getItem(AUTO_REFRESH) || '{}'),
refreshing: false,
}; };
// All getters working on all bots should be prefixed with all. // All getters working on all bots should be prefixed with all.
@ -50,6 +58,9 @@ export default function createBotStore(store) {
} }
return `ftbot.${botCount}`; return `ftbot.${botCount}`;
}, },
[MultiBotStoreGetters.autoRefresh](state: FTMultiBotState): boolean {
return state.autoRefresh;
},
}; };
// Autocreate getters from botStores // Autocreate getters from botStores
Object.keys(BotStoreGetters).forEach((e) => { Object.keys(BotStoreGetters).forEach((e) => {
@ -66,6 +77,12 @@ export default function createBotStore(store) {
console.warn(`Botid ${botId} not available, but selected.`); console.warn(`Botid ${botId} not available, but selected.`);
} }
}, },
setAutoRefresh(state: FTMultiBotState, newRefreshValue: boolean) {
state.autoRefresh = newRefreshValue;
},
setRefreshing(state, refreshing: boolean) {
state.refreshing = refreshing;
},
addBot(state: FTMultiBotState, bot: BotDescriptor) { addBot(state: FTMultiBotState, bot: BotDescriptor) {
state.availableBots[bot.botId] = bot; state.availableBots[bot.botId] = bot;
}, },
@ -108,6 +125,58 @@ export default function createBotStore(store) {
selectBot({ commit }, botId: string) { selectBot({ commit }, botId: string) {
commit('selectBot', botId); commit('selectBot', botId);
}, },
setAutoRefresh({ commit }, newRefreshValue) {
commit('setAutoRefresh', newRefreshValue);
localStorage.setItem(AUTO_REFRESH, JSON.stringify(newRefreshValue));
},
async refreshAll({ dispatch, state, commit }, forceUpdate = false) {
if (state.refreshing) {
return;
}
commit('setRefreshing', true);
try {
const updates: Promise<AxiosInstance>[] = [];
updates.push(dispatch('refreshFrequent', false));
updates.push(dispatch('refreshSlow', forceUpdate));
updates.push(dispatch('getDaily'));
updates.push(dispatch('getBalance'));
await Promise.all(updates);
console.log('refreshing_end');
} finally {
commit('setRefreshing', false);
}
},
async refreshSlow({ dispatch, getters, state }, forceUpdate = false) {
if (state.refreshing && !forceUpdate) {
return;
}
// Refresh data only when needed
if (forceUpdate || getters[`${BotStoreGetters.refreshRequired}`]) {
const updates: Promise<AxiosInstance>[] = [];
updates.push(dispatch('getPerformance'));
updates.push(dispatch('getProfit'));
updates.push(dispatch('getTrades'));
/* white/blacklist might be refreshed more often as they are not expensive on the backend */
updates.push(dispatch('getWhitelist'));
updates.push(dispatch('getBlacklist'));
await Promise.all(updates);
dispatch('setRefreshRequired', false);
}
},
refreshFrequent({ dispatch }, slow = true) {
if (slow) {
dispatch('refreshSlow', false);
}
// Refresh data that's needed in near realtime
dispatch('getOpenTrades');
dispatch('getState');
dispatch('getLocks');
},
refreshOnce({ dispatch }) {
dispatch('getVersion');
},
pingAll({ getters, dispatch }) { pingAll({ getters, dispatch }) {
getters.allAvailableBotsList.forEach((e) => { getters.allAvailableBotsList.forEach((e) => {
dispatch(`${e}/ping`); dispatch(`${e}/ping`);