dengzedong
3 天以前 730d1944e3a13c517c77df2b0712df05645a38dd
提交 | 用户 | 时间
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      */
8bf553 79     protected Date getStartTime(ColumnItem columnItem, Date originalTime, Map<String, ApiPointDTO> pointMap, Map<String, ApiPlanItemDTO> planMap) {
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:
07890e 85                 ApiPointDTO dataPoint = pointMap.get(columnItem.getParamId());
7fd198 86                 if (dataPoint == null) {
87                     return null;
88                 }
e78121 89                 dateTime = calculateTime(originalTime, true, columnItem.getDataLength(), DataPointFreqEnum.getEumByCode(dataPoint.getMinfreqid()).getValue());
7fd198 90                 break;
19a4fb 91             case NORMALITEM:
D 92             case MERGEITEM:
3ddbb6 93                 dateTime = calendar.getTime();
7fd198 94                 break;
e78121 95             case IND:
7fd198 96                 dateTime = calculateTime(originalTime, true, columnItem.getDataLength(), 60);
97                 break;
e78121 98             case PLAN:
8bf553 99                 ApiPlanItemDTO apiPlanItemDTO = planMap.get(columnItem.getParamId());
D 100                 if (apiPlanItemDTO == null) {
101                     return null;
102                 }
103                 dateTime = calculateTime(originalTime, true, columnItem.getDataLength(), TimeGranularitySecEnum.getEumByCode(apiPlanItemDTO.getTimeGranularity()).getValue());
7fd198 104                 break;
105             default:
106                 break;
107         }
108         return dateTime;
109     }
110
111     /**
112      * 获取结束时间
113      *
114      * @param columnItem
115      * @param originalTime
116      * @return
117      * @throws Exception
118      */
8bf553 119     protected Date getEndTime(ColumnItem columnItem, Date originalTime,Map<String, ApiPointDTO> pointMap, Map<String, ApiPlanItemDTO> planMap) {
7fd198 120         Date dateTime = new Date();
121         Calendar calendar = Calendar.getInstance();
122         calendar.setTime(originalTime);
e78121 123         switch (ModelParamType.getEumByCode(columnItem.getParamType())) {
124             case DATAPOINT:
b2aca2 125                 dateTime = calendar.getTime();
7fd198 126                 break;
19a4fb 127             case NORMALITEM:
D 128             case MERGEITEM:
3ddbb6 129                 dateTime = calculateTime(originalTime, false, columnItem.getDataLength(), 60);
7fd198 130                 break;
e78121 131             case IND:
7fd198 132                 dateTime = calendar.getTime();
133                 break;
e78121 134             case PLAN:
3ddbb6 135                 dateTime = calendar.getTime();
7fd198 136                 break;
137             default:
138                 break;
139         }
140         return dateTime;
141     }
142
143     /**
144      * 获取粒度,s
145      *
146      * @param columnItem
147      * @return
148      * @throws Exception
149      */
8bf553 150     protected Integer getGranularity(ColumnItem columnItem,Map<String, ApiPointDTO> pointMap, Map<String, ApiPlanItemDTO> planMap) {
91343d 151         // 默认60s
7fd198 152         Integer granularity = 60;
91343d 153         switch (ModelParamType.getEumByCode(columnItem.getParamType())) {
154             case DATAPOINT:
8bf553 155                 ApiPointDTO dataPoint = pointMap.get(columnItem.getParamId());
e78121 156                 granularity = DataPointFreqEnum.getEumByCode(dataPoint.getMinfreqid()).getValue();
7fd198 157                 break;
19a4fb 158             case NORMALITEM:
D 159             case MERGEITEM:
50084d 160                 granularity = mmPredictItemService.getItemByOutPutId(columnItem.getParamId()).getGranularity();
7fd198 161                 break;
91343d 162             case IND:
163                 ApiIndItemDTO indItemDTO = indItemApi.getInfoById(columnItem.getParamId());
164                 granularity = TimeGranularitySecEnum.getEumByCode(indItemDTO.getTimeGranularity()).getValue();;
7fd198 165                 break;
91343d 166             case PLAN:
8bf553 167                 ApiPlanItemDTO apiPlanItemDTO = planMap.get(columnItem.getParamId());
91343d 168                 granularity = TimeGranularitySecEnum.getEumByCode(apiPlanItemDTO.getTimeGranularity()).getValue();
7fd198 169                 break;
170             default:
171                 break;
172         }
173         return granularity;
174     }
175
176     /**
177      * 计算取值的时间
178      *
179      * @param originalTime
180      * @param backward
181      * @param dataLength
182      * @param granularity
183      * @return
184      */
185     public Date calculateTime(Date originalTime, Boolean backward, int dataLength, int granularity) {
186         int timeLength;
187         if (backward) {
188             timeLength = (-1) * dataLength;
189         } else {
190             timeLength = dataLength - 1;
191         }
192         Date desTime = originalTime;
193         Calendar calendar = Calendar.getInstance();
194         calendar.setTime(desTime);
195         calendar.set(Calendar.MILLISECOND, 0);
196         // 数据长度 * 粒度
197         calendar.add(Calendar.SECOND, timeLength * granularity);
e78121 198         return calendar.getTime();
199     }
bab433 200
201     protected int getDataLength(Map<Integer, Integer> dynamicDataLength, Integer port, Integer dataLength) {
202         if (CollectionUtils.isEmpty(dynamicDataLength)) {
203             return dataLength;
204         }
205         if (dynamicDataLength.containsKey(port)) {
206             return dynamicDataLength.get(port);
207         }
208         return dataLength;
209     }
7fd198 210 }