|
12 | 12 | import org.apache.kafka.streams.kstream.KTable; |
13 | 13 | import org.apache.log4j.Logger; |
14 | 14 |
|
| 15 | +import java.lang.reflect.InvocationTargetException; |
| 16 | +import java.lang.reflect.Method; |
15 | 17 | import java.util.ArrayList; |
16 | 18 | import java.util.Arrays; |
17 | 19 | import java.util.List; |
@@ -247,4 +249,99 @@ public static KStream<String, LogRecordTopicsFlattened> flatLogs(KStream<String, |
247 | 249 |
|
248 | 250 | } |
249 | 251 |
|
| 252 | + public static KStream<String, FlatEventBlockRecord> flatEventBlockRecord(KStream<String, EventBlockRecord> eventBlockStream) { |
| 253 | + |
| 254 | + return eventBlockStream.mapValues( eventBlock -> { |
| 255 | + |
| 256 | + FlatEventBlockRecord flatEventBlockRecord = new FlatEventBlockRecord(); |
| 257 | + |
| 258 | + flatEventBlockRecord.setAddress(eventBlock.getEvent().getAddress()); |
| 259 | + flatEventBlockRecord.setBlockHash(eventBlock.getEvent().getBlockHash()); |
| 260 | + flatEventBlockRecord.setBlockNumber(eventBlock.getEvent().getBlockNumber()); |
| 261 | + flatEventBlockRecord.setContractName(eventBlock.getEvent().getContractName()); |
| 262 | + flatEventBlockRecord.setEventSpecificationSignature(eventBlock.getEvent().getEventSpecificationSignature()); |
| 263 | + flatEventBlockRecord.setFilterId(eventBlock.getEvent().getFilterId()); |
| 264 | + flatEventBlockRecord.setId(eventBlock.getEvent().getId()); |
| 265 | + flatEventBlockRecord.setLogIndex(eventBlock.getEvent().getLogIndex()); |
| 266 | + flatEventBlockRecord.setName(eventBlock.getEvent().getName()); |
| 267 | + flatEventBlockRecord.setNetworkName(eventBlock.getEvent().getNetworkName()); |
| 268 | + flatEventBlockRecord.setNodeName(eventBlock.getEvent().getNodeName()); |
| 269 | + flatEventBlockRecord.setRetries(eventBlock.getEvent().getRetries()); |
| 270 | + flatEventBlockRecord.setStatus(getEventStatus(eventBlock.getEvent().getStatus())); |
| 271 | + flatEventBlockRecord.setTimestamp(eventBlock.getBlock().getTimestamp()); |
| 272 | + |
| 273 | + List<Object> indexedParameteres = eventBlock.getEvent().getIndexedParameters(); |
| 274 | + List<Object> nonIndexedParameteres = eventBlock.getEvent().getNonIndexedParameters(); |
| 275 | + |
| 276 | + setParametersOfFlatEvent(flatEventBlockRecord, indexedParameteres, true); |
| 277 | + setParametersOfFlatEvent(flatEventBlockRecord, nonIndexedParameteres, false); |
| 278 | + |
| 279 | + return flatEventBlockRecord; |
| 280 | + |
| 281 | + }); |
| 282 | + |
| 283 | + } |
| 284 | + |
| 285 | + private static void setParametersOfFlatEvent(FlatEventBlockRecord flatEvent, List<Object> parameters, Boolean indexed) { |
| 286 | + |
| 287 | + |
| 288 | + Integer maxIndexParameters = 4; |
| 289 | + Integer maxNonIndexParameters = 5; |
| 290 | + |
| 291 | + Integer max = indexed?maxIndexParameters:maxNonIndexParameters; |
| 292 | + |
| 293 | + String nameMethod = indexed?"setIndexedParamName":"setNonIndexedParamName"; |
| 294 | + String typeMethod = indexed?"setIndexedParamType":"setNonIndexedParamType"; |
| 295 | + String valueMethod = indexed?"setIndexedParamValue":"setNonIndexedParamValue"; |
| 296 | + |
| 297 | + Method setNameMethod; |
| 298 | + Method setTypeMethod; |
| 299 | + Method setValueMethod; |
| 300 | + |
| 301 | + String paramName = ""; |
| 302 | + String paramType = ""; |
| 303 | + String paramValue = ""; |
| 304 | + |
| 305 | + for (int i=0; i< parameters.size() && i< max; i++){ |
| 306 | + |
| 307 | + try { |
| 308 | + setNameMethod = FlatEventBlockRecord.class.getMethod(nameMethod + i, String.class); |
| 309 | + setTypeMethod = FlatEventBlockRecord.class.getMethod(typeMethod + i, String.class); |
| 310 | + setValueMethod = FlatEventBlockRecord.class.getMethod(valueMethod + i, String.class); |
| 311 | + |
| 312 | + if (parameters.get(i) instanceof NumberParameter) { |
| 313 | + NumberParameter nParam = (NumberParameter) parameters.get(i); |
| 314 | + paramName = nParam.getName(); |
| 315 | + paramType = nParam.getType(); |
| 316 | + paramValue = nParam.getValue(); |
| 317 | + } else { |
| 318 | + StringParameter sParam = (StringParameter) parameters.get(i); |
| 319 | + paramName = sParam.getName(); |
| 320 | + paramType = sParam.getType(); |
| 321 | + paramValue = sParam.getValue(); |
| 322 | + } |
| 323 | + |
| 324 | + setNameMethod.invoke(flatEvent, paramName); |
| 325 | + setTypeMethod.invoke(flatEvent, paramType); |
| 326 | + setValueMethod.invoke(flatEvent, paramValue); |
| 327 | + |
| 328 | + }catch (Exception e){ |
| 329 | + log.error("Error setting parameter " + e.getMessage()); |
| 330 | + continue; |
| 331 | + } |
| 332 | + |
| 333 | + } |
| 334 | + |
| 335 | + } |
| 336 | + |
| 337 | + private static FlatContractEventBlockStatus getEventStatus(ContractEventStatus status){ |
| 338 | + switch(status){ |
| 339 | + case CONFIRMED: return FlatContractEventBlockStatus.CONFIRMED; |
| 340 | + case UNCONFIRMED: return FlatContractEventBlockStatus.UNCONFIRMED; |
| 341 | + case INVALIDATED: return FlatContractEventBlockStatus.INVALIDATED; |
| 342 | + } |
| 343 | + |
| 344 | + return FlatContractEventBlockStatus.UNCONFIRMED; |
| 345 | + } |
| 346 | + |
250 | 347 | } |
0 commit comments