Skip to content

Commit

Permalink
feat: added Health Endpoint returning sync status. (#16)
Browse files Browse the repository at this point in the history
* feat: added Health Endpoint returning sync status.

* feat: added sync status dto. Sync status will be true if the syncjob won't start and will signalize it with a different status message.

* feat: added sync status dto. Sync status will be true if the syncjob won't start and will signalize it with a different status message.
  • Loading branch information
Kammerlo authored May 27, 2024
1 parent f8ca08c commit c94e91e
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.cardanofoundation.tokenmetadata.registry.api.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.cardanofoundation.tokenmetadata.registry.api.model.rest.HealthResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Validated
@Tag(name = "health", description = "Health Endpoint for the cardano token metadata registry")
public interface HealthApi {

@Operation(operationId = "getHealthStatus", summary = "Returns health status of service including if the initial sync is done", responses = {
@ApiResponse(responseCode = "200", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HealthResponse.class)))
})
@RequestMapping(method = RequestMethod.GET, value = "/health", produces = {"application/json;charset=utf-8"})
ResponseEntity<HealthResponse> getHealthStatus();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.cardanofoundation.tokenmetadata.registry.api.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.cardanofoundation.tokenmetadata.registry.api.model.rest.HealthResponse;
import org.cardanofoundation.tokenmetadata.registry.service.SyncStatus;
import org.cardanofoundation.tokenmetadata.registry.service.TokenMetadataSyncService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@CrossOrigin
@RequestMapping("${openapi.metadataServer.base-path:}")
@Slf4j
@RequiredArgsConstructor
public class HealthApiController implements HealthApi {

private final TokenMetadataSyncService tokenMetadataSyncService;

@Override
public ResponseEntity<HealthResponse> getHealthStatus() {
SyncStatus syncStatus = tokenMetadataSyncService.getSyncStatus();
return new ResponseEntity<>(HealthResponse.builder()
.synced(syncStatus.isInitialSyncDone())
.syncStatus(syncStatus.getSyncStatus().toString())
.build(), HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.cardanofoundation.tokenmetadata.registry.api.model.rest;

import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.cardanofoundation.tokenmetadata.registry.model.enums.SyncStatusEnum;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class HealthResponse {

@JsonProperty("synced")
@Valid
private boolean synced;

@JsonProperty("syncStatus")
@Valid
private String syncStatus;

}
20 changes: 20 additions & 0 deletions api/src/main/resources/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ x-tagGroups:
- v1 queries
- v2 queries
paths:
/health:
get:
summary: "Get Health status"
tags:
- v1 queries
operationId: "health"
responses:
200:
description: "Health Response including if initial sync is done and current syncing status."
content:
application/json;charset=utf-8:
schema:
$ref: '#/components/schemas/HealthResponse'
/metadata/{subject}:
get:
summary: "Query All Properties"
Expand Down Expand Up @@ -545,6 +558,13 @@ components:
type: array
items:
$ref: '#/components/schemas/Property'
HealthResponse:
type: object
properties:
synced:
type: boolean
syncStatus:
type: string
BatchRequest:
required:
- subjects
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.cardanofoundation.tokenmetadata.registry.model.enums;

import org.cardanofoundation.tokenmetadata.registry.util.Constants;

public enum SyncStatusEnum {

SYNC_NOT_STARTED(Constants.SYNC_NOT_STARTED),
SYNC_IN_PROGRESS(Constants.SYNC_IN_PROGRESS),
SYNC_DONE(Constants.SYNC_DONE),
SYNC_ERROR(Constants.SYNC_ERROR),
SYNC_IN_EXTRA_JOB(Constants.SYNC_IN_EXTRA_JOB);

private final String text;

SyncStatusEnum(final String text) {
this.text = text;
}

@Override
public String toString() {
return text;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.cardanofoundation.tokenmetadata.registry.service;

import lombok.Builder;
import lombok.Data;
import org.cardanofoundation.tokenmetadata.registry.model.enums.SyncStatusEnum;

@Builder
@Data
public class SyncStatus {

private boolean isInitialSyncDone;
private SyncStatusEnum syncStatus;

}
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package org.cardanofoundation.tokenmetadata.registry.service;

import lombok.AllArgsConstructor;
import jakarta.annotation.PostConstruct;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.cardanofoundation.tokenmetadata.registry.model.Mapping;
import org.cardanofoundation.tokenmetadata.registry.model.MappingDetails;
import org.cardanofoundation.tokenmetadata.registry.model.MappingUpdateDetails;
import org.cardanofoundation.tokenmetadata.registry.model.enums.SyncStatusEnum;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.groupingBy;

@Service
@Slf4j
@AllArgsConstructor
@RequiredArgsConstructor
public class TokenMetadataSyncService {

private final GitService gitService;
Expand All @@ -29,8 +29,25 @@ public class TokenMetadataSyncService {

private final TokenMappingService tokenMappingService;

@Value("${token.metadata.job.enabled}")
private boolean isMetadataJobEnabled;

@Getter
private SyncStatus syncStatus;

@PostConstruct
private void initSyncStatus() {
if(isMetadataJobEnabled) {
syncStatus = new SyncStatus(false, SyncStatusEnum.SYNC_NOT_STARTED);
} else {
syncStatus = new SyncStatus(true, SyncStatusEnum.SYNC_IN_EXTRA_JOB);
}
}

public void synchronizeDatabase() {

syncStatus.setSyncStatus(SyncStatusEnum.SYNC_IN_PROGRESS);

Optional<Path> repoPathOpt = gitService.cloneCardanoTokenRegistryGitRepository();

if (repoPathOpt.isPresent()) {
Expand Down Expand Up @@ -64,10 +81,15 @@ public void synchronizeDatabase() {

});

syncStatus.setSyncStatus(SyncStatusEnum.SYNC_DONE);
syncStatus.setInitialSyncDone(true);

} else {
log.warn("cardano-token-registry could not be cloned");
syncStatus.setSyncStatus(SyncStatusEnum.SYNC_ERROR);
}

}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.cardanofoundation.tokenmetadata.registry.util;

public class Constants {

private Constants() {}

public static String SYNC_NOT_STARTED = "Sync not started";
public static String SYNC_IN_PROGRESS = "Sync in progress";
public static String SYNC_DONE = "Sync done";
public static String SYNC_ERROR = "Error while syncing";
public static String SYNC_IN_EXTRA_JOB = "Sync will be done in a different job. Status unkown";

}

0 comments on commit c94e91e

Please sign in to comment.