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