Jay
2025-01-20 280ca0c6a4a1e73ab4516d4850dedb5a43541594
提交 | 用户 | 时间
7fd198 1 package com.iailab.module.model.mdk.sample;
2
91343d 3 import com.iailab.module.data.api.ind.IndItemApi;
4 import com.iailab.module.data.api.ind.dto.ApiIndItemDTO;
e78121 5 import com.iailab.module.data.api.plan.dto.ApiPlanItemDTO;
7fd198 6 import com.iailab.module.data.api.point.dto.ApiPointDTO;
e78121 7 import com.iailab.module.data.enums.DataPointFreqEnum;
91343d 8 import com.iailab.module.data.enums.TimeGranularitySecEnum;
9 import com.iailab.module.model.mcs.pre.service.MmPredictItemService;
e78121 10 import com.iailab.module.model.mdk.common.enums.ModelParamType;
7fd198 11 import com.iailab.module.model.mdk.sample.dto.ColumnItem;
12 import com.iailab.module.model.mdk.sample.dto.SampleInfo;
13 import org.springframework.beans.factory.annotation.Autowired;
bab433 14 import org.springframework.util.CollectionUtils;
7fd198 15
16 import java.util.Calendar;
17 import java.util.Date;
07890e 18 import java.util.Map;
7fd198 19
20 /**
21  * @author PanZhibao
22  * @Description
23  * @createTime 2024年09月03日
24  */
25 abstract class SampleInfoConstructor {
e78121 26
27     @Autowired
91343d 28     private IndItemApi indItemApi;
29
30     @Autowired
31     private MmPredictItemService mmPredictItemService;
7fd198 32
33     /**
34      * prepareSampleInfo
35      *
36      * @param modelId
37      * @param predictTime
38      * @return
39      */
bab433 40     protected SampleInfo prepareSampleInfo(String modelId, Date predictTime, Map<Integer, Integer> dynamicDataLength) {
7fd198 41         //样本的列信息
bab433 42         return getColumnInfo(modelId, predictTime, dynamicDataLength);
7fd198 43     }
44
45     /**
46      * 返回样本矩阵的列数
47      *
48      * @param modelId
49      * @return
50      */
51     protected abstract Integer getSampleColumn(String modelId);
45520a 52
D 53
54     /**
55      * 样本的列信息
56      *
57      * @param modelId
58      * @param predictTime
59      * @return
60      */
bab433 61     protected abstract SampleInfo getColumnInfo(String modelId, Date predictTime, Map<Integer, Integer> dynamicDataLength);
45520a 62
D 63     /**
64      * 样本的采样周期
65      *
66      * @param modelId
67      * @return
68      */
69     protected abstract Integer getSampleCycle(String modelId);
7fd198 70
71     /**
72      * 获取开始时间
73      *
74      * @param columnItem
75      * @param originalTime
76      * @return
77      * @throws Exception
78      */
e691b9 79     protected Date getStartTime(ColumnItem columnItem, Date originalTime) {
7fd198 80         Date dateTime = new Date();
81         Calendar calendar = Calendar.getInstance();
82         calendar.setTime(originalTime);
e78121 83         switch (ModelParamType.getEumByCode(columnItem.getParamType())) {
84             case DATAPOINT:
e691b9 85             case PLAN:
D 86                 dateTime = calculateTime(originalTime, true, columnItem.getDataLength(), columnItem.getGranularity());
75a848 87                 break;
D 88             case IND:
89                 dateTime = calculateTime(originalTime, true, columnItem.getDataLength()-1, columnItem.getGranularity());
7fd198 90                 break;
19a4fb 91             case NORMALITEM:
D 92             case MERGEITEM:
3ddbb6 93                 dateTime = calendar.getTime();
7fd198 94                 break;
95             default:
96                 break;
97         }
98         return dateTime;
99     }
100
101     /**
102      * 获取结束时间
103      *
104      * @param columnItem
105      * @param originalTime
106      * @return
107      * @throws Exception
108      */
e691b9 109     protected Date getEndTime(ColumnItem columnItem, Date originalTime) {
7fd198 110         Date dateTime = new Date();
111         Calendar calendar = Calendar.getInstance();
112         calendar.setTime(originalTime);
e78121 113         switch (ModelParamType.getEumByCode(columnItem.getParamType())) {
114             case DATAPOINT:
e691b9 115             case IND:
D 116             case PLAN:
b2aca2 117                 dateTime = calendar.getTime();
7fd198 118                 break;
19a4fb 119             case NORMALITEM:
D 120             case MERGEITEM:
e691b9 121                 dateTime = calculateTime(originalTime, false, columnItem.getDataLength(), columnItem.getGranularity());
7fd198 122                 break;
123             default:
124                 break;
125         }
126         return dateTime;
127     }
128
129     /**
130      * 获取粒度,s
131      *
132      * @param columnItem
133      * @return
134      * @throws Exception
135      */
75a848 136     protected Integer getGranularity(ColumnItem columnItem,Map<String, ApiPointDTO> pointMap, Map<String, ApiPlanItemDTO> planMap,Map<String, ApiIndItemDTO> indMap) {
91343d 137         // 默认60s
7fd198 138         Integer granularity = 60;
91343d 139         switch (ModelParamType.getEumByCode(columnItem.getParamType())) {
140             case DATAPOINT:
8bf553 141                 ApiPointDTO dataPoint = pointMap.get(columnItem.getParamId());
e78121 142                 granularity = DataPointFreqEnum.getEumByCode(dataPoint.getMinfreqid()).getValue();
7fd198 143                 break;
19a4fb 144             case NORMALITEM:
D 145             case MERGEITEM:
50084d 146                 granularity = mmPredictItemService.getItemByOutPutId(columnItem.getParamId()).getGranularity();
7fd198 147                 break;
91343d 148             case IND:
75a848 149                 ApiIndItemDTO indItemDTO = indMap.get(columnItem.getParamId());
91343d 150                 granularity = TimeGranularitySecEnum.getEumByCode(indItemDTO.getTimeGranularity()).getValue();;
7fd198 151                 break;
91343d 152             case PLAN:
8bf553 153                 ApiPlanItemDTO apiPlanItemDTO = planMap.get(columnItem.getParamId());
91343d 154                 granularity = TimeGranularitySecEnum.getEumByCode(apiPlanItemDTO.getTimeGranularity()).getValue();
7fd198 155                 break;
156             default:
157                 break;
158         }
159         return granularity;
160     }
161
162     /**
163      * 计算取值的时间
164      *
165      * @param originalTime
166      * @param backward
167      * @param dataLength
168      * @param granularity
169      * @return
170      */
171     public Date calculateTime(Date originalTime, Boolean backward, int dataLength, int granularity) {
172         int timeLength;
173         if (backward) {
174             timeLength = (-1) * dataLength;
175         } else {
033e69 176             timeLength = dataLength;
7fd198 177         }
178         Date desTime = originalTime;
179         Calendar calendar = Calendar.getInstance();
180         calendar.setTime(desTime);
181         calendar.set(Calendar.MILLISECOND, 0);
182         // 数据长度 * 粒度
183         calendar.add(Calendar.SECOND, timeLength * granularity);
e78121 184         return calendar.getTime();
185     }
bab433 186
187     protected int getDataLength(Map<Integer, Integer> dynamicDataLength, Integer port, Integer dataLength) {
188         if (CollectionUtils.isEmpty(dynamicDataLength)) {
189             return dataLength;
190         }
191         if (dynamicDataLength.containsKey(port)) {
192             return dynamicDataLength.get(port);
193         }
194         return dataLength;
195     }
7fd198 196 }