import { observable, action, flow } from 'mobx'; import { RESOURCE } from '../common/fetch'; import {appStore} from "./appStore"; import moment from 'moment'; import {Message} from "../common/message"; import {DEV_SITE_ID} from "../common/constant"; class Overall { @observable loading = false; @observable protectedApi = { count:0, gains:'0' }; @observable attackFlow = { count:0, gains:'0' }; @observable totalFlow = { count:0, gains:'0' }; @observable independentIp = { count:0, gains:'0' }; @observable pvInfo = []; @observable topFiveApi = []; @action init = flow(function* () { let tsStart = moment().startOf('day').valueOf(); let tsEnd = moment().valueOf(); let preStart = moment().subtract(1, 'd').startOf('day').valueOf(); let preEnd = moment().subtract(1, 'd').valueOf(); this.loading = true; try { let [protectedApi, attackFlow, totalFlow, independentIp, pvInfo, topFiveApi] = yield Promise.all([ this._getCount('protectedApi', tsStart, tsEnd, preStart, preEnd), this._getCount('attackFlow', tsStart, tsEnd, preStart, preEnd), this._getCount('totalFlow', tsStart, tsEnd, preStart, preEnd), this._getCount('independentIp', tsStart, tsEnd, preStart, preEnd), RESOURCE.post('/api/v1/overview/pvInfo',{ "siteId": DEV_SITE_ID || appStore.selectedSite.siteId }), RESOURCE.post('/api/v1/overview/topFiveApi',{ "siteId": DEV_SITE_ID || appStore.selectedSite.siteId, tsEnd, tsStart }), ]); this.protectedApi = protectedApi.data; this.attackFlow = attackFlow.data; this.totalFlow = totalFlow.data; this.independentIp = independentIp.data; this.pvInfo = pvInfo.data; this.topFiveApi = topFiveApi.data; }catch (e) { Message.alert(e); } this.loading = false; }.bind(this)); // 切换阻断模式状态 @action toggleBlockMode = flow(function* (mode){ this.layoutLoading = true; try { yield RESOURCE.post('/api/v1/config/blockSwitch', { blockSwitch: mode, siteId: DEV_SITE_ID || appStore.selectedSite.siteId }) } catch (e) { Message.alert(e); } this.layoutLoading = false; }) _getCount(type, tsStart, tsEnd, preStart, preEnd) { return new Promise((resolve, reject) => { Promise.all([ RESOURCE.post('/api/v1/overview/count',{ "siteId": DEV_SITE_ID || appStore.selectedSite.siteId, tsEnd, tsStart, type }), RESOURCE.post('/api/v1/overview/count',{ "siteId": DEV_SITE_ID || appStore.selectedSite.siteId, tsEnd:preEnd, tsStart:preStart, type }), ]).then(([ tData, preData ]) => { let tCount = tData.data.count; let preCount = preData.data.count; resolve({data:{ count:tCount, gains:preCount === 0 ? (tCount === 0 ? '0%' : '100%') : (parseFloat((tCount-preCount)/preCount)*100).toFixed(2)+'%'}}) },(e) => { reject(e); }); }); } } export const overallStore = new Overall();