Skip to content

Commit

Permalink
Merge pull request #19 from Bahmni-Covid19/IST-Time
Browse files Browse the repository at this point in the history
Ist time
  • Loading branch information
SanoferSameera authored Nov 18, 2021
2 parents f821b5d + a13e94e commit 31af760
Show file tree
Hide file tree
Showing 22 changed files with 93 additions and 62 deletions.
10 changes: 6 additions & 4 deletions src/main/java/in/org/projecteka/hiu/common/CustomScheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import reactor.core.publisher.Mono;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.function.Consumer;
import java.util.function.Function;

import static in.org.projecteka.hiu.common.Constants.IST;
import static reactor.core.publisher.Mono.error;

@AllArgsConstructor
Expand Down Expand Up @@ -47,15 +49,15 @@ public static <T> CustomScheduler<T> scheduleThis(Mono<T> producer) {
public static Function<Flux<Long>, Publisher<?>> exponentialBackOff(Duration minimum,
Duration maximum,
Duration timeout) {
Instant finish = Instant.now().plus(timeout);
LocalDateTime finish = LocalDateTime.now(ZoneId.of(IST)).plus(timeout);
return iterations -> getDelay(minimum, maximum, finish, iterations);
}

private static Flux<?> getDelay(Duration minimum, Duration maximum, Instant finish, Flux<Long> iterations) {
private static Flux<?> getDelay(Duration minimum, Duration maximum, LocalDateTime finish, Flux<Long> iterations) {
return iterations
.map(iteration -> calculateDuration(minimum, maximum, iteration))
.concatMap(delay -> {
if (Instant.now().isAfter(finish)) {
if (LocalDateTime.now(ZoneId.of(IST)).isAfter(finish)) {
return error(new DelayTimeoutException());
}
return Mono.delay(delay).doOnSubscribe(logDelay(delay));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import reactor.util.retry.Retry;

import java.time.Duration;
import java.time.ZoneId;

import static in.org.projecteka.hiu.common.Constants.IST;
import static java.lang.String.format;
import static java.time.Duration.ofMinutes;
import static java.time.LocalDateTime.now;
Expand Down Expand Up @@ -50,7 +52,7 @@ private <U> Mono<U> retryable(Mono<U> producer) {
.doOnError(error -> logger.error(error.getMessage(), error))
.retryWhen(Retry
.backoff(retry, Duration.ofMillis(100)).jitter(0d)
.doAfterRetry(rs -> logger.error(RETRIED_AT, now()))
.doAfterRetry(rs -> logger.error(RETRIED_AT, now(ZoneId.of(IST))))
.onRetryExhaustedThrow((spec, rs) -> rs.failure()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.time.ZoneId;
import java.util.concurrent.TimeoutException;

import static in.org.projecteka.hiu.Error.of;
import static in.org.projecteka.hiu.common.Constants.IST;
import static in.org.projecteka.hiu.common.heartbeat.model.Status.DOWN;
import static in.org.projecteka.hiu.common.heartbeat.model.Status.UP;
import static java.lang.String.format;
import static java.time.LocalDateTime.now;
import static java.time.ZoneOffset.UTC;
import static reactor.core.publisher.Mono.just;


Expand All @@ -35,12 +36,12 @@ public class Heartbeat {
public Mono<HeartbeatResponse> getStatus() {
try {
if (cacheHealth.isUp() && isRabbitMQUp() && isPostgresUp()) {
return just(HeartbeatResponse.builder().timeStamp(now(UTC)).status(UP).build());
return just(HeartbeatResponse.builder().timeStamp(now(ZoneId.of(IST))).status(UP).build());
}
return just(HeartbeatResponse.builder().timeStamp(now(UTC)).status(DOWN).error(of(SERVICE_DOWN)).build());
return just(HeartbeatResponse.builder().timeStamp(now(ZoneId.of(IST))).status(DOWN).error(of(SERVICE_DOWN)).build());
} catch (IOException | TimeoutException e) {
logger.error(format("Heartbeat is not healthy with failure: %s", e.getMessage()), e);
return just(HeartbeatResponse.builder().timeStamp(now(UTC)).status(DOWN).error(of(SERVICE_DOWN)).build());
return just(HeartbeatResponse.builder().timeStamp(now(ZoneId.of(IST))).status(DOWN).error(of(SERVICE_DOWN)).build());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.time.ZoneId;

import static in.org.projecteka.hiu.common.Constants.IST;
import static in.org.projecteka.hiu.common.heartbeat.model.Status.UP;
import static java.time.LocalDateTime.now;
import static java.time.ZoneOffset.UTC;
import static org.springframework.http.HttpStatus.OK;
import static org.springframework.http.HttpStatus.SERVICE_UNAVAILABLE;

Expand All @@ -33,7 +35,7 @@ public Mono<ResponseEntity<HeartbeatResponse>> getReadiness() {
public Mono<ResponseEntity<HeartbeatResponse>> getLiveliness() {
HeartbeatResponse heartbeatResponse = HeartbeatResponse.builder()
.status(UP)
.timeStamp(now(UTC))
.timeStamp(now(ZoneId.of(IST)))
.build();
return Mono.just(new ResponseEntity<>(heartbeatResponse, OK));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.HashMap;
import java.util.Map;

import static in.org.projecteka.hiu.ClientError.consentArtefactNotFound;
import static in.org.projecteka.hiu.ClientError.consentRequestNotFound;
import static in.org.projecteka.hiu.ClientError.dbOperationFailure;
import static in.org.projecteka.hiu.common.Constants.IST;
import static in.org.projecteka.hiu.common.Constants.STATUS;
import static in.org.projecteka.hiu.common.Serializer.from;
import static in.org.projecteka.hiu.common.Serializer.to;
Expand Down Expand Up @@ -153,7 +155,7 @@ public Mono<Void> insertConsentArtefact(ConsentArtefact consentArtefact,
new JsonObject(from(consentArtefact)),
consentArtefact.getConsentId(),
status.toString(),
LocalDateTime.now()),
LocalDateTime.now(ZoneId.of(IST))),
handler -> {
if (handler.failed()) {
logger.error(handler.cause().getMessage(), handler.cause());
Expand Down Expand Up @@ -316,7 +318,7 @@ public Mono<Void> updateConsentRequestStatus(String gatewayRequestId,
String consentRequestId) {
return Mono.create(monoSink ->
readWriteClient.preparedQuery(UPDATE_GATEWAY_CONSENT_REQUEST_STATUS)
.execute(Tuple.of(consentRequestId, status.toString(), LocalDateTime.now(), gatewayRequestId),
.execute(Tuple.of(consentRequestId, status.toString(), LocalDateTime.now(ZoneId.of(IST)), gatewayRequestId),
handler -> {
if (handler.failed()) {
logger.error(handler.cause().getMessage(), handler.cause());
Expand Down Expand Up @@ -355,7 +357,7 @@ public Flux<Map<String, Object>> requestsOf(String requesterId) {
public Mono<Void> updateConsentRequestStatus(ConsentStatus status, String consentRequestId) {
return Mono.create(monoSink ->
readWriteClient.preparedQuery(UPDATE_CONSENT_REQUEST_STATUS)
.execute(Tuple.of(consentRequestId, status.toString(), LocalDateTime.now()),
.execute(Tuple.of(consentRequestId, status.toString(), LocalDateTime.now(ZoneId.of(IST))),
handler -> {
if (handler.failed()) {
logger.error(handler.cause().getMessage(), handler.cause());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import static in.org.projecteka.hiu.ClientError.consentArtefactNotFound;
import static in.org.projecteka.hiu.common.Constants.IST;
import static in.org.projecteka.hiu.common.Constants.getCmSuffix;
import static in.org.projecteka.hiu.consent.model.ConsentStatus.EXPIRED;
import static in.org.projecteka.hiu.consent.model.consentmanager.ConsentAcknowledgementStatus.OK;
Expand Down Expand Up @@ -59,7 +60,7 @@ private ConsentOnNotifyRequest buildConsentOnNotifyRequest(List<ConsentArtefact>
var requestId = UUID.randomUUID();
var consentArtefactRequest = ConsentOnNotifyRequest
.builder()
.timestamp(LocalDateTime.now(ZoneOffset.UTC))
.timestamp(LocalDateTime.now(ZoneId.of(IST)))
.requestId(requestId);
var acknowledgements = new ArrayList<ConsentAcknowledgement>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZoneId;
import java.util.UUID;

import static in.org.projecteka.hiu.common.Constants.IST;
import static in.org.projecteka.hiu.common.Constants.getCmSuffix;
import static in.org.projecteka.hiu.consent.model.ConsentStatus.GRANTED;
import static reactor.core.publisher.Flux.fromIterable;
Expand All @@ -40,7 +41,7 @@ private Mono<Void> perform(ConsentArtefactReference reference, String consentReq
var consentArtefactRequest = ConsentArtefactRequest
.builder()
.consentId(reference.getId())
.timestamp(LocalDateTime.now(ZoneOffset.UTC))
.timestamp(LocalDateTime.now(ZoneId.of(IST)))
.requestId(requestId)
.build();
return gatewayClient.requestConsentArtefact(consentArtefactRequest, cmSuffix);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@
import java.util.stream.Collectors;

import static in.org.projecteka.hiu.ErrorCode.INVALID_PURPOSE_OF_USE;
import static in.org.projecteka.hiu.common.Constants.IST;
import static in.org.projecteka.hiu.common.Constants.EMPTY_STRING;
import static in.org.projecteka.hiu.common.Constants.PATIENT_REQUESTED_PURPOSE_CODE;
import static in.org.projecteka.hiu.common.Constants.getCmSuffix;
import static java.time.LocalDateTime.now;
import static java.time.ZoneOffset.UTC;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static reactor.core.publisher.Mono.defer;
import static reactor.core.publisher.Mono.just;
Expand Down Expand Up @@ -101,7 +101,7 @@ public Mono<Map<String, String>> handlePatientConsentRequest(String requesterId,
}))
.flatMapMany(Flux::fromIterable)
.flatMap(hipId -> buildConsentRequest(requesterId, hipId,
now(UTC).minusYears(consentServiceProperties.getConsentRequestFromYears())))
now(ZoneId.of(IST)).minusYears(consentServiceProperties.getConsentRequestFromYears())))
.flatMap(consentRequestData -> generateConsentRequestForSelf(consentRequestData)
.map(dataReqId -> Map.entry(consentRequestData.getConsent().getHipId(), dataReqId)))
.collectList()
Expand All @@ -115,7 +115,7 @@ public Mono<Map<String, String>> handlePatientConsentRequest(String requesterId,
private List<PatientDataRequestDetail> filterRequestAfterThreshold(List<PatientDataRequestDetail> dataRequestDetails) {
return dataRequestDetails.stream()
.filter(dataRequestDetail -> !dataRequestDetail.getPatientDataRequestedAt()
.isAfter(now(UTC).minusMinutes(consentServiceProperties.getConsentRequestDelay())))
.isAfter(now(ZoneId.of(IST)).minusMinutes(consentServiceProperties.getConsentRequestDelay())))
.collect(Collectors.toList());

}
Expand All @@ -125,7 +125,7 @@ private List<String> filterEmptyAndNullValues(List<String> ids) {
}

private Mono<ConsentRequestData> handleForReloadConsent(String patientId, String hipId) {
LocalDateTime now = now(UTC);
LocalDateTime now = now(ZoneId.of(IST));
logger.warn("handleForReloadConsent");
logger.warn("now(IST) " + now(ZoneId.of("Asia/Calcutta")));
logger.warn("now() " + now());
Expand Down Expand Up @@ -156,11 +156,11 @@ private Mono<ConsentRequestData> buildConsentRequest(String requesterId, String
return just(ConsentRequestData.builder().consent(Consent.builder()
.hiTypes(getAllApplicableHITypes())
.patient(Patient.builder().id(requesterId).build())
.permission(Permission.builder().dataEraseAt(now(UTC)
.permission(Permission.builder().dataEraseAt(now(ZoneId.of(IST))
.plusMonths(consentServiceProperties.getConsentExpiryInMonths()))
.dateRange(DateRange.builder()
.from(dateFrom)
.to(now(UTC)).build())
.to(now(ZoneId.of(IST))).build())
.build())
.purpose(new Purpose(PATIENT_REQUESTED_PURPOSE_CODE))
.hipId(hipId)
Expand Down Expand Up @@ -212,7 +212,7 @@ private Mono<Void> sendConsentRequestToGateway(
var patientId = hiRequest.getConsent().getPatient().getId();
var consentRequest = ConsentRequest.builder()
.requestId(gatewayRequestId)
.timestamp(now(UTC))
.timestamp(now(ZoneId.of(IST)))
.consent(reqInfo)
.build();
var hiuConsentRequest = hiRequest.getConsent().toConsentRequest(gatewayRequestId.toString(), requesterId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import static in.org.projecteka.hiu.ClientError.consentArtefactNotFound;
import static in.org.projecteka.hiu.common.Constants.IST;
import static in.org.projecteka.hiu.common.Constants.getCmSuffix;
import static in.org.projecteka.hiu.consent.model.ConsentStatus.EXPIRED;
import static in.org.projecteka.hiu.consent.model.ConsentStatus.REVOKED;
Expand Down Expand Up @@ -60,7 +61,7 @@ private ConsentOnNotifyRequest buildConsentOnNotifyRequest(List<ConsentArtefact>
var requestId = UUID.randomUUID();
var consentArtefactRequest = ConsentOnNotifyRequest
.builder()
.timestamp(LocalDateTime.now(ZoneOffset.UTC))
.timestamp(LocalDateTime.now(ZoneId.of(IST)))
.requestId(requestId);
var acknowledgements = new ArrayList<ConsentAcknowledgement>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

import static in.org.projecteka.hiu.common.Constants.IST;
import static in.org.projecteka.hiu.consent.model.consentmanager.Frequency.ONE_HOUR;

@JsonIgnoreProperties(ignoreUnknown = true)
Expand Down Expand Up @@ -63,7 +65,7 @@ public ConsentRequest toConsentRequest(String id, String requesterId) {
.hiTypes(getHiTypes())
.permission(getPermission())
.status(ConsentStatus.REQUESTED)
.createdDate(LocalDateTime.now())
.createdDate(LocalDateTime.now(ZoneId.of(IST)))
.hip(hip)
.careContexts(getCareContexts())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZoneId;
import java.util.Optional;
import java.util.UUID;

import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS;
import static in.org.projecteka.hiu.ClientError.queueNotFound;
import static in.org.projecteka.hiu.common.Constants.CORRELATION_ID;
import static in.org.projecteka.hiu.common.Constants.IST;
import static in.org.projecteka.hiu.common.Serializer.to;
import static reactor.core.publisher.Mono.defer;

Expand Down Expand Up @@ -113,7 +114,7 @@ public void subscribe() {

private GatewayDataFlowRequest getDataFlowRequest(DataFlowRequest dataFlowRequest) {
var requestId = UUID.randomUUID();
var timestamp = LocalDateTime.now(ZoneOffset.UTC);
var timestamp = LocalDateTime.now(ZoneId.of(IST));
return new GatewayDataFlowRequest(requestId, timestamp, dataFlowRequest);
}

Expand Down Expand Up @@ -147,7 +148,7 @@ private KeyMaterial keyMaterial(DataFlowRequestKeyMaterial dataFlowKeyMaterial)
}

private LocalDateTime getExpiryDate() {
return LocalDateTime.now().plusDays(dataFlowProperties.getOffsetInDays());
return LocalDateTime.now(ZoneId.of(IST)).plusDays(dataFlowProperties.getOffsetInDays());
}

@SneakyThrows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static in.org.projecteka.hiu.common.Constants.IST;
import static reactor.core.publisher.Mono.defer;

@AllArgsConstructor
Expand Down Expand Up @@ -127,7 +129,7 @@ private Mono<String> validateAndRetrieveRequestedConsent(String transactionId) {
}

private boolean hasConsentArtefactExpired(LocalDateTime dataEraseAt) {
return dataEraseAt != null && dataEraseAt.isBefore(LocalDateTime.now());
return dataEraseAt != null && dataEraseAt.isBefore(LocalDateTime.now(ZoneId.of(IST)));
}

private boolean hasContent(Entry entry) {
Expand Down
Loading

0 comments on commit 31af760

Please sign in to comment.