utils.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import dayjs, { Dayjs } from 'dayjs';
  2. import { uid } from 'uid';
  3. import NP from 'number-precision';
  4. import { isEqual } from 'lodash-es';
  5. import { TimeRangeItem, TimePeriodItem } from './types';
  6. import { CameraAlgoItem } from '@/api/camera/camera-preview';
  7. // export const createDefaultTime = (): TimeRangeItem => {
  8. // return { id: uid(), value: [dayjs(), dayjs().add(1, 'hour')] as [Dayjs, Dayjs] };
  9. // };
  10. export const createDefaultTime = (): TimePeriodItem => {
  11. return {
  12. id: uid(),
  13. startDay: null,
  14. endDay: null,
  15. timeRangeList: [{ id: uid(), startTime: '', endTime: '' }],
  16. };
  17. };
  18. export enum FrequencyEnum {
  19. second = 1,
  20. miniute = 60,
  21. hour = 3600,
  22. }
  23. export const frequencyOptions = [
  24. { label: '秒', value: FrequencyEnum.second },
  25. { label: '分钟', value: FrequencyEnum.miniute },
  26. { label: '小时', value: FrequencyEnum.hour },
  27. ];
  28. export interface DetectionJSON {
  29. detectionNum: number;
  30. detectionUnit: FrequencyEnum;
  31. }
  32. /** 根据后端返回的时间(单位是秒),拆分成单位和数值 */
  33. export const getDetectionJSON = (time: number | undefined | null): DetectionJSON => {
  34. if (time && time > 0) {
  35. for (let i = frequencyOptions.length - 1; i >= 0; i--) {
  36. const unit = frequencyOptions[i].value;
  37. if (time >= unit) {
  38. return { detectionNum: NP.divide(time, unit), detectionUnit: unit };
  39. }
  40. }
  41. }
  42. return { detectionNum: 5, detectionUnit: FrequencyEnum.miniute };
  43. };
  44. export const getDetectionTimeJSON = (time?: string): TimeRangeItem[] | null => {
  45. if (!time) return null;
  46. const timeArr = time.split(';');
  47. const nowStr = dayjs().format('YYYY-MM-DD');
  48. const timeStrArr = timeArr
  49. .map((x) => {
  50. const [startDate, endDate] = x.split('-');
  51. return [dayjs(`${nowStr} ${startDate}`), dayjs(`${nowStr} ${endDate}`)] as [Dayjs, Dayjs];
  52. })
  53. .map((x) => {
  54. return { id: uid(), value: x };
  55. });
  56. return timeStrArr;
  57. };
  58. export const getMetaValues = (extra: string | undefined | null) => {
  59. if (!extra) return [];
  60. const extraObj = JSON.parse(extra);
  61. const params = extraObj?.inferParams;
  62. if (!params || (params && params.length == 0)) return [];
  63. const metaObjs = params[0]?.metaObjs;
  64. if (!metaObjs || (metaObjs && metaObjs.length == 0)) return [];
  65. const metaArr = metaObjs.map((item: any) => {
  66. const val = {
  67. id: uid(),
  68. label: item.label,
  69. confidence: Number((item.confidence * 100).toFixed(0)),
  70. min_width: item['min_width'],
  71. min_height: item['min_height'],
  72. } as any;
  73. item.nextObjs.forEach((next) => {
  74. val[`${next.label}.confidence`] = Number((next.confidence * 100).toFixed(0));
  75. val[next.label + '.' + 'min_width'] = next['min_width'];
  76. val[next.label + '.' + 'min_height'] = next['min_height'];
  77. });
  78. return val;
  79. });
  80. return metaArr;
  81. };
  82. export const getDetectionTime = (time: string | undefined | null) => {
  83. if (!time) return [];
  84. const timeList = JSON.parse(time);
  85. if (!timeList || (timeList && timeList.length === 0)) {
  86. return [];
  87. }
  88. return timeList;
  89. };
  90. export const getInferCode = (extra: string | undefined | null) => {
  91. if (!extra) return '';
  92. const extraObj = JSON.parse(extra);
  93. return extraObj?.inferCode || '';
  94. };
  95. export const getAlgoType = (extra: string | undefined | null) => {
  96. if (!extra) return 0;
  97. const extraObj = JSON.parse(extra);
  98. const infers = extraObj?.inferParams;
  99. if (!infers || infers.length === 0) return 0;
  100. return infers[0]?.algoType || 0;
  101. };
  102. export const getCriticalCounts = (extra: string | undefined | null) => {
  103. if (!extra) return [];
  104. const extraObj = JSON.parse(extra);
  105. const infers = extraObj?.inferParams;
  106. if (!infers || infers.length === 0) return [];
  107. return infers[0]?.criticalCounts || [];
  108. };
  109. export const getExtraCommonInfo = (detail: CameraAlgoItem | undefined | null) => {
  110. if (!detail) return {};
  111. let extraValue = getCommonInfo(detail.extra);
  112. if (isEqual(extraValue, {})) {
  113. extraValue = getCommonInfo(detail.algoInfo?.extra);
  114. }
  115. return extraValue;
  116. };
  117. interface CommonInfo {
  118. regionJudge?: number;
  119. judge?: number;
  120. timeWindow?: number;
  121. }
  122. const getCommonInfo = (extra: string | undefined | null): CommonInfo => {
  123. if (!extra) return {};
  124. const extraObj = JSON.parse(extra);
  125. const params = extraObj?.inferParams;
  126. if (!params || (params && params.length == 0)) return {};
  127. const regionJudge = params[0]?.regionJudge;
  128. const judge = params[0]?.judge;
  129. const timeWindow = params[0]?.timeWindow;
  130. const ret = {} as CommonInfo;
  131. if (regionJudge || regionJudge == 0) {
  132. ret.regionJudge = regionJudge;
  133. }
  134. if (judge || judge == 0) {
  135. ret.judge = judge;
  136. }
  137. if (timeWindow) {
  138. ret.timeWindow = timeWindow;
  139. }
  140. return ret;
  141. };
  142. export const getTimeCompletion = (time: TimePeriodItem) => {
  143. if (!time.startDay || !time.endDay) {
  144. return false;
  145. }
  146. time.timeRangeList.forEach((item) => {
  147. if (!item.startTime || !item.endTime) {
  148. return false;
  149. }
  150. });
  151. return true;
  152. };