潘志宝
2024-12-25 e2b151e9bed2b6798cfc1ed499e98bcb0665d6ec
提交 | 用户 | 时间
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         //样本的列信息
50084d 52         return getColumnInfo(modelId, predictTime);
7fd198 53     }
54
55     /**
56      * 返回样本矩阵的列数
57      *
58      * @param modelId
59      * @return
60      */
61     protected abstract Integer getSampleColumn(String modelId);
45520a 62
D 63
64     /**
65      * 样本的列信息
66      *
67      * @param modelId
68      * @param predictTime
69      * @return
70      */
50084d 71     protected abstract SampleInfo getColumnInfo(String modelId, Date predictTime);
45520a 72
D 73     /**
74      * 样本的采样周期
75      *
76      * @param modelId
77      * @return
78      */
79     protected abstract Integer getSampleCycle(String modelId);
7fd198 80
81     /**
82      * 获取开始时间
83      *
84      * @param columnItem
85      * @param originalTime
86      * @return
87      * @throws Exception
88      */
8bf553 89     protected Date getStartTime(ColumnItem columnItem, Date originalTime, Map<String, ApiPointDTO> pointMap, Map<String, ApiPlanItemDTO> planMap) {
7fd198 90         Date dateTime = new Date();
91         Calendar calendar = Calendar.getInstance();
92         calendar.setTime(originalTime);
e78121 93         switch (ModelParamType.getEumByCode(columnItem.getParamType())) {
94             case DATAPOINT:
07890e 95                 ApiPointDTO dataPoint = pointMap.get(columnItem.getParamId());
7fd198 96                 if (dataPoint == null) {
97                     return null;
98                 }
e78121 99                 dateTime = calculateTime(originalTime, true, columnItem.getDataLength(), DataPointFreqEnum.getEumByCode(dataPoint.getMinfreqid()).getValue());
7fd198 100                 break;
19a4fb 101             case NORMALITEM:
D 102             case MERGEITEM:
3ddbb6 103                 dateTime = calendar.getTime();
7fd198 104                 break;
e78121 105             case IND:
7fd198 106                 dateTime = calculateTime(originalTime, true, columnItem.getDataLength(), 60);
107                 break;
e78121 108             case PLAN:
8bf553 109                 ApiPlanItemDTO apiPlanItemDTO = planMap.get(columnItem.getParamId());
D 110                 if (apiPlanItemDTO == null) {
111                     return null;
112                 }
113                 dateTime = calculateTime(originalTime, true, columnItem.getDataLength(), TimeGranularitySecEnum.getEumByCode(apiPlanItemDTO.getTimeGranularity()).getValue());
7fd198 114                 break;
115             default:
116                 break;
117         }
118         return dateTime;
119     }
120
121     /**
122      * 获取结束时间
123      *
124      * @param columnItem
125      * @param originalTime
126      * @return
127      * @throws Exception
128      */
8bf553 129     protected Date getEndTime(ColumnItem columnItem, Date originalTime,Map<String, ApiPointDTO> pointMap, Map<String, ApiPlanItemDTO> planMap) {
7fd198 130         Date dateTime = new Date();
131         Calendar calendar = Calendar.getInstance();
132         calendar.setTime(originalTime);
e78121 133         switch (ModelParamType.getEumByCode(columnItem.getParamType())) {
134             case DATAPOINT:
b2aca2 135                 dateTime = calendar.getTime();
7fd198 136                 break;
19a4fb 137             case NORMALITEM:
D 138             case MERGEITEM:
3ddbb6 139                 dateTime = calculateTime(originalTime, false, columnItem.getDataLength(), 60);
7fd198 140                 break;
e78121 141             case IND:
7fd198 142                 dateTime = calendar.getTime();
143                 break;
e78121 144             case PLAN:
3ddbb6 145                 dateTime = calendar.getTime();
7fd198 146                 break;
147             default:
148                 break;
149         }
150         return dateTime;
151     }
152
153     /**
154      * 获取粒度,s
155      *
156      * @param columnItem
157      * @return
158      * @throws Exception
159      */
8bf553 160     protected Integer getGranularity(ColumnItem columnItem,Map<String, ApiPointDTO> pointMap, Map<String, ApiPlanItemDTO> planMap) {
91343d 161         // 默认60s
7fd198 162         Integer granularity = 60;
91343d 163         switch (ModelParamType.getEumByCode(columnItem.getParamType())) {
164             case DATAPOINT:
8bf553 165                 ApiPointDTO dataPoint = pointMap.get(columnItem.getParamId());
e78121 166                 granularity = DataPointFreqEnum.getEumByCode(dataPoint.getMinfreqid()).getValue();
7fd198 167                 break;
19a4fb 168             case NORMALITEM:
D 169             case MERGEITEM:
50084d 170                 granularity = mmPredictItemService.getItemByOutPutId(columnItem.getParamId()).getGranularity();
7fd198 171                 break;
91343d 172             case IND:
173                 ApiIndItemDTO indItemDTO = indItemApi.getInfoById(columnItem.getParamId());
174                 granularity = TimeGranularitySecEnum.getEumByCode(indItemDTO.getTimeGranularity()).getValue();;
7fd198 175                 break;
91343d 176             case PLAN:
8bf553 177                 ApiPlanItemDTO apiPlanItemDTO = planMap.get(columnItem.getParamId());
91343d 178                 granularity = TimeGranularitySecEnum.getEumByCode(apiPlanItemDTO.getTimeGranularity()).getValue();
7fd198 179                 break;
180             default:
181                 break;
182         }
183         return granularity;
184     }
185
186     /**
187      * 计算取值的时间
188      *
189      * @param originalTime
190      * @param backward
191      * @param dataLength
192      * @param granularity
193      * @return
194      */
195     public Date calculateTime(Date originalTime, Boolean backward, int dataLength, int granularity) {
196         int timeLength;
197         if (backward) {
198             timeLength = (-1) * dataLength;
199         } else {
200             timeLength = dataLength - 1;
201         }
202         Date desTime = originalTime;
203         Calendar calendar = Calendar.getInstance();
204         calendar.setTime(desTime);
205         calendar.set(Calendar.MILLISECOND, 0);
206         // 数据长度 * 粒度
207         calendar.add(Calendar.SECOND, timeLength * granularity);
e78121 208         return calendar.getTime();
209     }
7fd198 210 }