bill-information.tsx 3.2 KB
Newer Older
Z
zengqiao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
import * as React from 'react';
import Url from 'lib/url-parser';
import { DatePicker, notification, Icon } from 'component/antd';
import { BarChartComponet } from 'component/chart';
import { SearchAndFilterContainer } from 'container/search-filter';
import { observer } from 'mobx-react';
import { Moment } from 'moment';
import { topic } from 'store/topic';
import { timeMonth } from 'constants/strategy';
import './index.less';
const { RangePicker } = DatePicker;

import moment = require('moment');

@observer
export class BillInformation extends SearchAndFilterContainer {
  public clusterId: number;
  public topicName: string;

  public state = {
    mode: ['month', 'month'] as any,
    value: [moment(new Date()).subtract(6, 'months'), moment()] as any,
  };

  private startTime: number = moment(new Date()).subtract(6, 'months').valueOf();
  private endTime: number = moment().valueOf();
  private chart: any = null;

  constructor(props: any) {
    super(props);
    const url = Url();
    this.clusterId = Number(url.search.clusterId);
    this.topicName = url.search.topic;
  }

  public getData() {
    const { clusterId, topicName, startTime, endTime } = this;
    topic.setLoading(true);
    return topic.getBillInfo(clusterId, topicName, startTime, endTime);
  }

  public renderDatePick() {
    const { value, mode } = this.state;
    return (
      <>
        <div className="op-panel">
          <span>
            <RangePicker
              ranges={{
                近半年: [moment(new Date()).subtract(6, 'months'), moment()],
                近一年: [moment().startOf('year'), moment().endOf('year')],
              }}
              value={value}
              mode={mode}
              format={timeMonth}
              onChange={this.handlePanelChange}
              onPanelChange={this.handlePanelChange}
            />
          </span>
        </div>
      </>
    );
  }

  public disabledDateTime = (current: Moment) => {
    return current && current > moment().endOf('day');
  }

  public handleRefreshChart = () => {
    this.chart.handleRefreshChart();
  }

  public handlePanelChange = (value: any, mode: any) => {
    this.setState({
      value,
      mode: ['month', 'month'] as any,
    });

    this.startTime = value[0].valueOf();
    this.endTime = value[1].valueOf();

    if (this.startTime >= this.endTime) {
      return notification.error({ message: '开始时间不能大于或等于结束时间' });
    }
    this.getData();
    this.handleRefreshChart();
  }

  public renderChart() {
    return (
      <div className="chart-box">
        <BarChartComponet ref={(ref) => this.chart = ref} getChartData={this.getData.bind(this, null)} />
      </div>
    );
  }

  public render() {
孙超 已提交
98
    return (
Z
zengqiao 已提交
99
      <>
孙超 已提交
100 101 102
        <div className="k-row" >
          <ul className="k-tab">
            <li>账单信息&nbsp;
Z
zengqiao 已提交
103
            <a
孙超 已提交
104 105 106 107 108 109 110 111 112 113 114
                // tslint:disable-next-line:max-line-length
                href="https://github.com/didi/kafka-manager"
                target="_blank"
              >
                <Icon type="question-circle" />
              </a>
            </li>
            {this.renderDatePick()}
          </ul>
          {this.renderChart()}
        </div>
Z
zengqiao 已提交
115 116 117 118
      </>
    );
  }
}