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