File size: 2,254 Bytes
d684338
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { TaskExecutorElapsed } from '@/interfaces/database/user-setting';
import { Divider, Flex } from 'antd';
import { max } from 'lodash';
import {
  Bar,
  BarChart,
  CartesianGrid,
  ResponsiveContainer,
  Tooltip,
} from 'recharts';

import styles from './index.less';

interface IProps {
  data: TaskExecutorElapsed;
}

const getColor = (value: number) => {
  if (value > 120) {
    return 'red';
  } else if (value <= 120 && value > 50) {
    return '#faad14';
  }
  return '#52c41a';
};

const getMaxLength = (data: TaskExecutorElapsed) => {
  const lengths = Object.keys(data).reduce<number[]>((pre, cur) => {
    pre.push(data[cur].length);
    return pre;
  }, []);
  return max(lengths) ?? 0;
};

const fillEmptyElementByMaxLength = (list: any[], maxLength: number) => {
  if (list.length === maxLength) {
    return list;
  }
  return list.concat(
    new Array(maxLength - list.length).fill({
      value: 0,
      actualValue: 0,
      fill: getColor(0),
    }),
  );
};

const CustomTooltip = ({ active, payload }: any) => {
  if (active && payload && payload.length) {
    return (
      <div className="custom-tooltip">
        <p
          className={styles.taskBarTooltip}
        >{`${payload[0].payload.actualValue}`}</p>
      </div>
    );
  }

  return null;
};

const TaskBarChat = ({ data }: IProps) => {
  const maxLength = getMaxLength(data);
  return (
    <Flex gap="middle" vertical>
      {Object.keys(data).map((key) => {
        const list = data[key].map((x) => ({
          value: x > 120 ? 120 : x,
          actualValue: x,
          fill: getColor(x),
        }));
        const nextList = fillEmptyElementByMaxLength(list, maxLength);
        return (
          <Flex key={key} className={styles.taskBar} vertical>
            <b className={styles.taskBarTitle}>ID: {key}</b>
            <ResponsiveContainer>
              <BarChart data={nextList} barSize={20}>
                <CartesianGrid strokeDasharray="3 3" />
                <Tooltip content={<CustomTooltip></CustomTooltip>} />
                <Bar dataKey="value" />
              </BarChart>
            </ResponsiveContainer>
            <Divider></Divider>
          </Flex>
        );
      })}
    </Flex>
  );
};

export default TaskBarChat;