1
@RequestMapping("/api/v1")
public class RemoteActionHistoryController {

    public Mono<ResponseEntityDTO<List<RemoteActionHistoryDTO>>> excaliburData(@PathVariable(name = "deviceId", required = true) 
String deviceId) {
        return service.getRemoteActionHistory(deviceId);
    }
}

public class RemoteActionHistoryServiceImpl {

    @Autowired
    RemoteActionHistoryRepository arrowFlightRepository;
    
    @Override
    public Mono<ResponseEntityDTO<List<RemoteActionHistoryDTO>>> getRemoteActionHistory(String deviceId) throws Exception {
        return (Mono<ResponseEntityDTO<List<RemoteActionHistoryDTO>>>) arrowFlightRepository.getRemoteActionDetailsByDeviceId(deviceId).collectList().flatMap(mapper);
    }
    
    Function<List<RemoteActionHistory>, Mono<ResponseEntityDTO<RemoteActionHistoryDTO>>> mapper = remoteActions -> {
        var remoteActionDTO = new RemoteActionHistoryDTO();
        if (CollectionUtils.isEmpty(remoteActions)) {
            return Mono.justOrEmpty(new ResponseEntityDTO<>(RemoteActionHistoryConstants.CODE_RET_000, null, remoteActionDTO));
        }
        System.out.println("Printing remoteActions"+remoteActions);
        for (RemoteActionHistory excaliburData : remoteActions) {
            remoteActionDTO.setStartDateTime(excaliburData.getStartDateTime());
            remoteActionDTO.setEndDateTime(excaliburData.getEndDateTime());
            remoteActionDTO.setRemoteActionType(excaliburData.getCommand());
            remoteActionDTO.setTaskStatus(excaliburData.getStatus());
        }
        return Mono.justOrEmpty(new ResponseEntityDTO<>(RemoteActionHistoryConstants.CODE_RET_000, null, remoteActionDTO));
    };
}

public class RemoteActionHistoryServiceImpl {

    @Autowired
    RemoteActionHistoryRepository arrowFlightRepository;

    @Override
    public Mono<ResponseEntityDTO<List<RemoteActionHistoryDTO>>> getRemoteActionHistory(String deviceId) throws Exception {

        return (Mono<ResponseEntityDTO<List<RemoteActionHistoryDTO>>>) arrowFlightRepository.getRemoteActionDetailsByDeviceId(deviceId).collectList().flatMap(mapper);
    }

    Function<List<RemoteActionHistory>, Mono<ResponseEntityDTO<RemoteActionHistoryDTO>>> mapper = remoteActions -> {
        var remoteActionDTO = new RemoteActionHistoryDTO();
        if (CollectionUtils.isEmpty(remoteActions)) {
            return Mono.justOrEmpty(new ResponseEntityDTO<>(RemoteActionHistoryConstants.CODE_RET_000, null, remoteActionDTO));
        }
        System.out.println("Printing remoteActions"+remoteActions);
        for (RemoteActionHistory excaliburData : remoteActions) {
            remoteActionDTO.setStartDateTime(excaliburData.getStartDateTime());
            remoteActionDTO.setEndDateTime(excaliburData.getEndDateTime());
            remoteActionDTO.setRemoteActionType(excaliburData.getCommand());
            remoteActionDTO.setTaskStatus(excaliburData.getStatus());

        }
        return Mono.justOrEmpty(new ResponseEntityDTO<>(RemoteActionHistoryConstants.CODE_RET_000, null, remoteActionDTO));
    };
}

public class RemoteActionHistoryRepositoryImpl {

    @Override
    public Flux<RemoteActionHistory> getRemoteActionDetailsByDeviceId(String deviceId) throws Exception {
        return Flux.fromIterable(getExcaliburData(String.format(query, tableName, deviceId), true));
    }

    private List<RemoteActionHistory> getExcaliburData(String query, boolean retry) throws Exception {
        List<RemoteActionHistory> remoteActionData = new ArrayList<>();
        try (final FlightStream flightStream = adhocFlightClientConfig.createAdhocFlightClient().runQuery(query);) {    
            while (flightStream.next()) {

                if (!flightStream.hasRoot()) {
                    break;
                }
                VectorSchemaRoot vectorSchemaRoot = flightStream.getRoot();

                TimeStampMilliVector createdDateVector = (TimeStampMilliVector) vectorSchemaRoot
                        .getVector(RemoteActionHistoryConstants.CREATED_DATE_TIME);
                TimeStampMilliVector modifiedDateVector = (TimeStampMilliVector) vectorSchemaRoot
                        .getVector(RemoteActionHistoryConstants.MODIFIED_DATE_TIME);

                RemoteActionHistory remoteAction = null;
                for (int i = 0; i < vectorSchemaRoot.getRowCount(); i++) {
                    remoteAction = new RemoteActionHistory();
                    remoteAction.setStartDateTime(createdDateVector.isNull(i)? null : new Timestamp(createdDateVector.get(i)));
                    remoteAction.setEndDateTime(modifiedDateVector.isNull(i)? null : new Timestamp(modifiedDateVector.get(i)));

                    remoteActionData.add(remoteAction);
                }

        } catch (Exception ex) {
            log.error(String.format("Exception string : %s with retryEnabled is %s", ex.toString(),retry));
            if (retry && StringUtils.containsIgnoreCase(ex.toString(), RemoteActionHistoryConstants.UNAUTHENTICATED_ERROR)) {
                adhocFlightClientConfig.removeToken();
                getExcaliburData(query, false);
            }
            throw ex;
        }
        return remoteActionData;
    }
}

I need to fetch the records like this

Currently getting single record

Getting error on Mono code

I have attached the result which I'm currently getting single record. when i debugged the code in the object i have multiple records but it shows only one record in postman

Roar S.
  • 8,103
  • 1
  • 15
  • 37
  • Hi. Your problem and what you want to achieve, is kind of unclear. You should start your question with some text describing the problem in more details. BR – Roar S. Jun 27 '22 at 08:12
  • Hi, i am getting output as{ "errorCode": "RET-000", "errorMessage": null, "data": { "startDateTime": "2021-08-10T00:18:35.813+00:00", "endDateTime": "2021-08-10T00:18:34.834+00:00", "remoteActionType": "DriverInstall", "taskStatus": "DeviceTwinUpdated" } } i am expecting output should be of multiple records as i have attached the image. – Nagma Firdose Jun 27 '22 at 09:10
  • Hi again. Just a friendly advice in order to get your problem solved, add additional info to your question, not as comments. BR – Roar S. Jun 27 '22 at 09:26

0 Answers0