liriming
2024-11-29 15732a55b862cdc423475f4fb6b45fb8a7b55ed3
提交 | 用户 | 时间
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.PlanItemApi;
6 import com.iailab.module.data.api.plan.dto.ApiPlanItemDTO;
7fd198 7 import com.iailab.module.data.api.point.DataPointApi;
8 import com.iailab.module.data.api.point.dto.ApiPointDTO;
e78121 9 import com.iailab.module.data.enums.DataPointFreqEnum;
10 import com.iailab.module.data.enums.TimeGranularityEnum;
91343d 11 import com.iailab.module.data.enums.TimeGranularitySecEnum;
12 import com.iailab.module.model.mcs.pre.service.MmPredictItemService;
e78121 13 import com.iailab.module.model.mdk.common.enums.ModelParamType;
7fd198 14 import com.iailab.module.model.mdk.sample.dto.ColumnItem;
15 import com.iailab.module.model.mdk.sample.dto.ColumnItemPort;
16 import com.iailab.module.model.mdk.sample.dto.SampleInfo;
17 import org.springframework.beans.factory.annotation.Autowired;
18
19 import java.util.Calendar;
20 import java.util.Date;
21 import java.util.List;
22
23 /**
24  * @author PanZhibao
25  * @Description
26  * @createTime 2024年09月03日
27  */
28 abstract class SampleInfoConstructor {
29
30     @Autowired
31     private DataPointApi dataPointApi;
32
33     @Autowired
91343d 34     private PlanItemApi planItemApi;
e78121 35
36     @Autowired
91343d 37     private IndItemApi indItemApi;
38
39     @Autowired
40     private MmPredictItemService mmPredictItemService;
7fd198 41
42     /**
43      * prepareSampleInfo
44      *
45      * @param modelId
46      * @param predictTime
47      * @return
48      */
49     public SampleInfo prepareSampleInfo(String modelId, Date predictTime) {
50         SampleInfo sampleInfo = new SampleInfo();
51         //调用样本列数的方法
52         sampleInfo.setSampleColumn(getSampleColumn(modelId));
53         //样本的列信息
54         sampleInfo.setColumnInfo(getColumnInfo(modelId, predictTime));
55         //样本的采样周期
56         sampleInfo.setSampleCycle(getSampleCycle(modelId));
57         return sampleInfo;
58     }
59
60     /**
61      * 返回样本矩阵的列数
62      *
63      * @param modelId
64      * @return
65      */
66     protected abstract Integer getSampleColumn(String modelId);
67
68     /**
69      * 获取开始时间
70      *
71      * @param columnItem
72      * @param originalTime
73      * @return
74      * @throws Exception
75      */
76     protected Date getStartTime(ColumnItem columnItem, Date originalTime) {
77         Date dateTime = new Date();
78         Calendar calendar = Calendar.getInstance();
79         calendar.setTime(originalTime);
e78121 80         switch (ModelParamType.getEumByCode(columnItem.getParamType())) {
81             case DATAPOINT:
536b8e 82                 ApiPointDTO dataPoint = dataPointApi.getInfoById(columnItem.getParamId());
7fd198 83                 if (dataPoint == null) {
84                     return null;
85                 }
e78121 86                 dateTime = calculateTime(originalTime, true, columnItem.getDataLength(), DataPointFreqEnum.getEumByCode(dataPoint.getMinfreqid()).getValue());
7fd198 87                 break;
19a4fb 88             case NORMALITEM:
D 89             case MERGEITEM:
7fd198 90                 dateTime = calendar.getTime();
91                 break;
e78121 92             case IND:
7fd198 93                 dateTime = calculateTime(originalTime, true, columnItem.getDataLength(), 60);
94                 break;
e78121 95             case PLAN:
7fd198 96                 dateTime = calendar.getTime();
97                 break;
98             default:
99                 break;
100         }
101         return dateTime;
102     }
103
104     /**
105      * 获取结束时间
106      *
107      * @param columnItem
108      * @param originalTime
109      * @return
110      * @throws Exception
111      */
112     protected Date getEndTime(ColumnItem columnItem, Date originalTime) {
113         Date dateTime = new Date();
114         Calendar calendar = Calendar.getInstance();
115         calendar.setTime(originalTime);
e78121 116         switch (ModelParamType.getEumByCode(columnItem.getParamType())) {
117             case DATAPOINT:
536b8e 118                 ApiPointDTO dataPoint = dataPointApi.getInfoById(columnItem.getParamId());
7fd198 119                 if (dataPoint == null) {
120                     return null;
121                 }
b2aca2 122                 dateTime = calendar.getTime();
7fd198 123                 break;
19a4fb 124             case NORMALITEM:
D 125             case MERGEITEM:
b2aca2 126                 dateTime = calendar.getTime();
7fd198 127                 break;
e78121 128             case IND:
7fd198 129                 dateTime = calendar.getTime();
130                 break;
e78121 131             case PLAN:
132                 ApiPlanItemDTO planItem = planItemApi.getInfoByNo(columnItem.getParamId());
133                 if (planItem == null) {
134                     return null;
135                 }
136                 dateTime = calculateDate(originalTime, false, columnItem.getDataLength(),
137                         TimeGranularityEnum.getEumByCode(planItem.getTimeGranularity()).getValue());
7fd198 138                 break;
139             default:
140                 break;
141         }
142         return dateTime;
143     }
144
145     /**
146      * 获取粒度,s
147      *
148      * @param columnItem
149      * @return
150      * @throws Exception
151      */
152     protected Integer getGranularity(ColumnItem columnItem) {
91343d 153         // 默认60s
7fd198 154         Integer granularity = 60;
91343d 155         switch (ModelParamType.getEumByCode(columnItem.getParamType())) {
156             case DATAPOINT:
536b8e 157                 ApiPointDTO dataPoint = dataPointApi.getInfoById(columnItem.getParamId());
e78121 158                 granularity = DataPointFreqEnum.getEumByCode(dataPoint.getMinfreqid()).getValue();
7fd198 159                 break;
19a4fb 160             case NORMALITEM:
D 161                 granularity = mmPredictItemService.getItemByOutPutId(columnItem.getParamId()).getGranularity();
162                 break;
163             case MERGEITEM:
91343d 164                 granularity = mmPredictItemService.getItemById(columnItem.getParamId()).getGranularity();
7fd198 165                 break;
91343d 166             case IND:
167                 ApiIndItemDTO indItemDTO = indItemApi.getInfoById(columnItem.getParamId());
168                 granularity = TimeGranularitySecEnum.getEumByCode(indItemDTO.getTimeGranularity()).getValue();;
7fd198 169                 break;
91343d 170             case PLAN:
171                 ApiPlanItemDTO apiPlanItemDTO = planItemApi.getInfoById(columnItem.getParamId());
172                 granularity = TimeGranularitySecEnum.getEumByCode(apiPlanItemDTO.getTimeGranularity()).getValue();
7fd198 173                 break;
174             default:
175                 break;
176         }
177         return granularity;
178     }
179
180     /**
181      * 样本的列信息
182      *
183      * @param modelId
184      * @param predictTime
185      * @return
186      */
187     protected abstract List<ColumnItemPort> getColumnInfo(String modelId, Date predictTime);
188
189     /**
190      * 样本的采样周期
191      *
192      * @param modelId
193      * @return
194      */
195     protected abstract Integer getSampleCycle(String modelId);
196
197     /**
198      * 计算取值的时间
199      *
200      * @param originalTime
201      * @param backward
202      * @param dataLength
203      * @param granularity
204      * @return
205      */
206     public Date calculateTime(Date originalTime, Boolean backward, int dataLength, int granularity) {
207         int timeLength;
208         if (backward) {
209             timeLength = (-1) * dataLength;
210         } else {
211             timeLength = dataLength - 1;
212         }
213         Date desTime = originalTime;
214         Calendar calendar = Calendar.getInstance();
215         calendar.setTime(desTime);
216         calendar.set(Calendar.MILLISECOND, 0);
217         // 数据长度 * 粒度
218         calendar.add(Calendar.SECOND, timeLength * granularity);
219         return calendar.getTime();
220     }
e78121 221
222     public Date calculateDate(Date originalTime, Boolean backward, int dataLength, int field) {
223         int timeLength;
224         if (backward) {
225             timeLength = (-1) * dataLength;
226         } else {
227             timeLength = dataLength - 1;
228         }
229         Date desTime = originalTime;
230         Calendar calendar = Calendar.getInstance();
231         calendar.setTime(desTime);
232         calendar.set(Calendar.MILLISECOND, 0);
233         // 数据长度 * 粒度
234         calendar.add(Calendar.SECOND, timeLength);
235         return calendar.getTime();
236     }
7fd198 237 }