Commit 41dcd764 by Peter Holmberg

removed unused mobx state

parent 22510be4
import { SearchStore } from './../stores/SearchStore/SearchStore';
import { ServerStatsStore } from './../stores/ServerStatsStore/ServerStatsStore';
import { NavStore } from './../stores/NavStore/NavStore';
import { PermissionsStore } from './../stores/PermissionsStore/PermissionsStore';
import { AlertListStore } from './../stores/AlertListStore/AlertListStore';
import { ViewStore } from './../stores/ViewStore/ViewStore';
import { FolderStore } from './../stores/FolderStore/FolderStore';
interface ContainerProps {
search: typeof SearchStore.Type;
serverStats: typeof ServerStatsStore.Type;
nav: typeof NavStore.Type;
alertList: typeof AlertListStore.Type;
permissions: typeof PermissionsStore.Type;
view: typeof ViewStore.Type;
folder: typeof FolderStore.Type;
......
import { AlertListStore } from './AlertListStore';
import { backendSrv } from 'test/mocks/common';
import moment from 'moment';
function getRule(name, state, info) {
return {
id: 11,
dashboardId: 58,
panelId: 3,
name: name,
state: state,
newStateDate: moment()
.subtract(5, 'minutes')
.format(),
evalData: {},
executionError: '',
url: 'db/mygool',
stateText: state,
stateIcon: 'fa',
stateClass: 'asd',
stateAge: '10m',
info: info,
canEdit: true,
};
}
describe('AlertListStore', () => {
let store;
beforeAll(() => {
store = AlertListStore.create(
{
rules: [
getRule('Europe', 'OK', 'backend-01'),
getRule('Google', 'ALERTING', 'backend-02'),
getRule('Amazon', 'PAUSED', 'backend-03'),
getRule('West-Europe', 'PAUSED', 'backend-03'),
],
search: '',
},
{
backendSrv: backendSrv,
}
);
});
it('search should filter list on name', () => {
store.setSearchQuery('urope');
expect(store.filteredRules).toHaveLength(2);
});
it('search should filter list on state', () => {
store.setSearchQuery('ale');
expect(store.filteredRules).toHaveLength(1);
});
it('search should filter list on info', () => {
store.setSearchQuery('-0');
expect(store.filteredRules).toHaveLength(4);
});
it('search should be equal', () => {
store.setSearchQuery('alert');
expect(store.search).toBe('alert');
});
});
import { types, getEnv, flow } from 'mobx-state-tree';
import { AlertRule as AlertRuleModel } from './AlertRule';
import { setStateFields } from './helpers';
type AlertRuleType = typeof AlertRuleModel.Type;
export interface AlertRule extends AlertRuleType {}
export const AlertListStore = types
.model('AlertListStore', {
rules: types.array(AlertRuleModel),
stateFilter: types.optional(types.string, 'all'),
search: types.optional(types.string, ''),
})
.views(self => ({
get filteredRules() {
const regex = new RegExp(self.search, 'i');
return self.rules.filter(alert => {
return regex.test(alert.name) || regex.test(alert.stateText) || regex.test(alert.info);
});
},
}))
.actions(self => ({
loadRules: flow(function* load(filters) {
const backendSrv = getEnv(self).backendSrv;
self.stateFilter = filters.state; // store state filter used in api query
const apiRules = yield backendSrv.get('/api/alerts', filters);
self.rules.clear();
for (const rule of apiRules) {
setStateFields(rule, rule.state);
if (rule.state !== 'paused') {
if (rule.executionError) {
rule.info = 'Execution Error: ' + rule.executionError;
}
if (rule.evalData && rule.evalData.noData) {
rule.info = 'Query returned no data';
}
}
self.rules.push(AlertRuleModel.create(rule));
}
}),
setSearchQuery(query: string) {
self.search = query;
},
}));
import { types, getEnv, flow } from 'mobx-state-tree';
import { setStateFields } from './helpers';
export const AlertRule = types
.model('AlertRule', {
id: types.identifier(types.number),
dashboardId: types.number,
panelId: types.number,
name: types.string,
state: types.string,
stateText: types.string,
stateIcon: types.string,
stateClass: types.string,
stateAge: types.string,
info: types.optional(types.string, ''),
url: types.string,
})
.views(self => ({
get isPaused() {
return self.state === 'paused';
},
}))
.actions(self => ({
/**
* will toggle alert rule paused state
*/
togglePaused: flow(function* togglePaused() {
const backendSrv = getEnv(self).backendSrv;
const payload = { paused: !self.isPaused };
const res = yield backendSrv.post(`/api/alerts/${self.id}/pause`, payload);
setStateFields(self, res.state);
self.info = '';
}),
}));
import moment from 'moment';
import alertDef from 'app/features/alerting/state/alertDef';
export function setStateFields(rule, state) {
const stateModel = alertDef.getStateDisplayModel(state);
rule.state = state;
rule.stateText = stateModel.text;
rule.stateIcon = stateModel.iconClass;
rule.stateClass = stateModel.stateClass;
rule.stateAge = moment(rule.newStateDate)
.fromNow()
.replace(' ago', '');
}
import { types } from 'mobx-state-tree';
import { SearchStore } from './../SearchStore/SearchStore';
import { ServerStatsStore } from './../ServerStatsStore/ServerStatsStore';
import { NavStore } from './../NavStore/NavStore';
import { AlertListStore } from './../AlertListStore/AlertListStore';
import { ViewStore } from './../ViewStore/ViewStore';
import { FolderStore } from './../FolderStore/FolderStore';
import { PermissionsStore } from './../PermissionsStore/PermissionsStore';
......@@ -12,13 +10,7 @@ export const RootStore = types.model({
search: types.optional(SearchStore, {
sections: [],
}),
serverStats: types.optional(ServerStatsStore, {
stats: [],
}),
nav: types.optional(NavStore, {}),
alertList: types.optional(AlertListStore, {
rules: [],
}),
permissions: types.optional(PermissionsStore, {
fetching: false,
items: [],
......
import { types } from 'mobx-state-tree';
export const ServerStat = types.model('ServerStat', {
name: types.string,
value: types.optional(types.number, 0),
});
import { types, getEnv, flow } from 'mobx-state-tree';
import { ServerStat } from './ServerStat';
export const ServerStatsStore = types
.model('ServerStatsStore', {
stats: types.array(ServerStat),
error: types.optional(types.string, ''),
})
.actions(self => ({
load: flow(function* load() {
const backendSrv = getEnv(self).backendSrv;
const res = yield backendSrv.get('/api/admin/stats');
self.stats.clear();
self.stats.push(ServerStat.create({ name: 'Total dashboards', value: res.dashboards }));
self.stats.push(ServerStat.create({ name: 'Total users', value: res.users }));
self.stats.push(ServerStat.create({ name: 'Active users (seen last 30 days)', value: res.activeUsers }));
self.stats.push(ServerStat.create({ name: 'Total orgs', value: res.orgs }));
self.stats.push(ServerStat.create({ name: 'Total playlists', value: res.playlists }));
self.stats.push(ServerStat.create({ name: 'Total snapshots', value: res.snapshots }));
self.stats.push(ServerStat.create({ name: 'Total dashboard tags', value: res.tags }));
self.stats.push(ServerStat.create({ name: 'Total starred dashboards', value: res.stars }));
self.stats.push(ServerStat.create({ name: 'Total alerts', value: res.alerts }));
}),
}));
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment