Skip to content

Commit

Permalink
Merge branch 'dev' into qa_tests/regression_test_authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
Am0du authored Aug 25, 2024
2 parents 2c1ffe6 + 3b404ef commit 9b85a3a
Show file tree
Hide file tree
Showing 15 changed files with 277 additions and 282 deletions.
2 changes: 2 additions & 0 deletions src/main/java/hng_java_boilerplate/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class Program {

public static void main(String[] args) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package hng_java_boilerplate.statusPage.controller;

import hng_java_boilerplate.statusPage.entity.StatusPage;
import hng_java_boilerplate.statusPage.service.StatusPageService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/api-status")
public class StatusPageController {

private final StatusPageService statusPageService;

@GetMapping
public List<StatusPage> getAllApiStatuses() {
return statusPageService.getAllApiStatuses();
}

@PostMapping
public void updateApiStatus() {
statusPageService.updateApiStatus();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@Getter
@Setter
@Table(name = "api_status")
public class ApiStatus {
public class StatusPage {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package hng_java_boilerplate.statusPage.repository;

import hng_java_boilerplate.statusPage.entity.StatusPage;
import org.springframework.data.jpa.repository.JpaRepository;

public interface StatusPageRepository extends JpaRepository<StatusPage, Long> {
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package hng_java_boilerplate.statusPage.service;

import com.fasterxml.jackson.databind.JsonNode;
import hng_java_boilerplate.statusPage.entity.StatusPage;

import java.util.List;

public interface StatusPageService {
void updateApiStatus();
void processJsonData(JsonNode jsonNode);
List<StatusPage> getAllApiStatuses();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package hng_java_boilerplate.statusPage.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import hng_java_boilerplate.statusPage.entity.StatusPage;
import hng_java_boilerplate.statusPage.entity.StatusPage.Status;
import hng_java_boilerplate.statusPage.repository.StatusPageRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class StatusPageServiceImpl implements StatusPageService {

private final StatusPageRepository statusPageRepository;
private final ObjectMapper objectMapper;
private static final String RESULT_JSON_PATH = "/home/${{ secrets.USERNAME }}/hng_boilerplate_java_web/result.json";

@Override
@Scheduled(fixedRate = 900000)
public void updateApiStatus() {
try {
File jsonFile = new File(RESULT_JSON_PATH);
JsonNode jsonNode = objectMapper.readTree(jsonFile);
processJsonData(jsonNode);
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void processJsonData(JsonNode jsonNode) {
JsonNode executions = jsonNode.get("run").get("executions");

for (JsonNode execution : executions) {
String apiGroup = execution.get("item").get("name").asText();
int responseTime = execution.get("response").get("responseTime").asInt();
int statusCode = execution.get("response").get("code").asInt();
String statusMessage = execution.get("response").get("status").asText();

StatusPage statusPage = new StatusPage();
statusPage.setApiGroup(apiGroup);
statusPage.setResponseTime(responseTime);
statusPage.setLastChecked(LocalDateTime.now());

if (statusCode >= 200 && statusCode < 300) {
statusPage.setStatus(Status.OPERATIONAL);
} else if (statusCode >= 500) {
statusPage.setStatus(Status.DEGRADED);
} else {
statusPage.setStatus(Status.DOWN);
}

statusPage.setDetails(statusMessage);

statusPageRepository.save(statusPage);
}
}

@Override
public List<StatusPage> getAllApiStatuses() {
return statusPageRepository.findAll();
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 9b85a3a

Please sign in to comment.