curve-info.ts 1.9 KB
Newer Older
Z
zengqiao 已提交
1 2
import { observable, action } from 'mobx';
import moment = require('moment');
E
eilenexuzhe 已提交
3
import { EChartsOption } from 'echarts';
Z
zengqiao 已提交
4 5 6 7 8 9 10 11 12 13 14 15
import { ICurve } from 'container/common-curve/config';
import { curveKeys, PERIOD_RADIO_MAP } from 'container/admin/data-curve/config';
import { timeFormat } from 'constants/strategy';

class CurveInfo {
  @observable
  public periodKey: string = 'oneHour';

  @observable
  public timeRange: [moment.Moment, moment.Moment] = PERIOD_RADIO_MAP.get(this.periodKey).dateRange;

  @observable
E
eilenexuzhe 已提交
16
  public curveData: { [key: string]: EChartsOption } = {};
Z
zengqiao 已提交
17 18 19 20 21 22 23 24 25 26 27

  @observable
  public curveLoading: { [key: string]: boolean } = {};

  @observable
  public operators: string[] = [];

  @observable
  public currentOperator: string;

  @action.bound
E
eilenexuzhe 已提交
28
  public setCurveData(key: curveKeys | string, data: EChartsOption) {
Z
zengqiao 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    this.curveData[key] = data;
  }

  @action.bound
  public setCurveLoading(key: curveKeys | string, loading: boolean) {
    this.curveLoading[key] = loading;
  }

  @action.bound
  public setTimeRange(newRange: [moment.Moment, moment.Moment]) {
    this.timeRange = newRange;
  }

  @action.bound
  public setOperators(operators: string[]) {
    this.operators = operators;
  }

  @action.bound
  public setCurrentOperator(operator: string) {
    this.currentOperator = operator;
  }

  public getStartTime = () => {
    return this.timeRange[0].format(timeFormat);
  }

  public getEndTime = () => {
    return this.timeRange[1].format(timeFormat);
  }

  public getCommonCurveData = (
    options: ICurve,
E
eilenexuzhe 已提交
62
    parser: (option: ICurve, data: any[]) => EChartsOption,
Z
zengqiao 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75
    reload?: boolean) => {
    const { path } = options;
    this.setCurveData(path, null);
    this.setCurveLoading(path, true);
    return options.api(this.timeRange[0], this.timeRange[1], reload).then((data: any) => {
      this.setCurveData(path, parser(options, data));
    }).finally(() => {
      this.setCurveLoading(path, false);
    });
  }
}

export const curveInfo = new CurveInfo();