index.ts 3.8 KB
import { EChartsOption } from 'echarts';
import { formatClassifyText } from '../../api/config';
import { ClassifyDtsItemType, ClassifyItemType } from '/@/api/application/model/record';
import { DescItem } from '/@/components/Description';
import { BasicColumn } from '/@/components/Table';
import { useI18n } from '/@/hooks/web/useI18n';
import { dateUtil } from '/@/utils/dateUtil';

export type DateInterval = 'hour' | 'day' | 'week' | 'month';

const { t } = useI18n();

export const columns: BasicColumn[] = [
  {
    title: t('application.record.text.applicationName'),
    dataIndex: 'name',
    width: 120,
  },
  {
    title: t('application.record.text.accumulatedNumberOfCalls'),
    dataIndex: 'recordNum',
    width: 120,
  },
  {
    title: t('application.record.text.numberOfSuccessfulAttempts'),
    dataIndex: 'successCount',
    width: 180,
  },
  {
    title: t('application.record.text.numberOfFailedAttempts'),
    dataIndex: 'failureCount',
    width: 180,
  },
];

export const formSchema: DescItem[] = [
  {
    field: 'name',
    label: t('application.record.text.applicationName'),
  },
  {
    field: 'recordNum',
    label: t('application.record.text.accumulatedNumberOfCalls'),
  },
  {
    field: 'successCount',
    label: t('application.record.text.numberOfSuccessfulAttempts'),
  },
  {
    field: 'failureCount',
    label: t('application.record.text.numberOfFailedAttempts'),
  },
];

function generateFullXAxis(type: DateInterval) {
  if (type === 'hour') {
    let minute = dateUtil().minute();
    minute = Math.floor(minute / 5) * 5;

    return Array.from({ length: 60 / 5 }, (_, index) =>
      dateUtil()
        .minute(minute)
        .subtract(index * 5, 'minute')
        .startOf('minute')
        .valueOf()
    );
  } else if (type === 'day') {
    let hour = dateUtil().hour();
    hour = Math.floor(hour / 2) * 2;
    return Array.from({ length: 24 / 2 }, (_, index) =>
      dateUtil()
        .hour(hour)
        .subtract(index * 2, 'hour')
        .startOf('hour')
        .valueOf()
    );
  } else if (type === 'week') {
    return Array.from({ length: 7 }, (_, index) =>
      dateUtil().subtract(index, 'day').startOf('day').valueOf()
    );
  } else {
    return Array.from({ length: 30 }, (_, index) =>
      dateUtil().subtract(index, 'day').startOf('day').valueOf()
    );
  }
}

export function formatDateFromType(date: number, type: DateInterval) {
  if (type === 'hour') {
    return dateUtil(date).format('YYYY-MM-DD HH:mm');
  } else if (type === 'day') {
    return dateUtil(date).format('YYYY-MM-DD HH:mm');
  } else {
    return dateUtil(date).format('YYYY-MM-DD');
  }
}

export function transformClassifyItem(data: ClassifyItemType[], type: DateInterval) {
  const res: ClassifyItemType[] = [];
  const fullXAxis = generateFullXAxis(type);

  for (const { dts, classify } of data) {
    const valueMap = dts.reduce((prev, next) => {
      const key = dateUtil(next.time).valueOf();

      return { ...prev, [key]: next };
    }, {} as Record<number, ClassifyDtsItemType>);

    const transformDts: ClassifyDtsItemType[] = fullXAxis.map((timeSpan) => {
      const exist = valueMap[timeSpan];

      return exist || { time: formatDateFromType(timeSpan, type), count: 0, classify };
    });

    res.push({ classify, dts: transformDts });
  }

  return res;
}

export function transformChartsOptionsByClassifyRecord(
  data: ClassifyItemType[],
  type: DateInterval
): Partial<EChartsOption> {
  const series: EChartsOption['series'] = data.map(({ classify, dts }) => {
    return {
      type: 'line',
      name: formatClassifyText[classify],
      data: dts.map(({ count }) => count).reverse(),
    };
  });

  const xAxisData = generateFullXAxis(type).map((timeSpan) => formatDateFromType(timeSpan, type));

  return {
    series,
    xAxis: {
      type: 'category',
      data: xAxisData.reverse(),
    },
  };
}