Skip to content

Commit

Permalink
Merge pull request #2 from vczoika/springtracker
Browse files Browse the repository at this point in the history
added a simple tracker made with Spring
  • Loading branch information
janmeshjs committed Oct 4, 2023
2 parents b5a7c20 + 292bf62 commit f6e31af
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Java/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Simple COVID-19 Tracker
#### Simple project made using Spring to track the total and new cases of COVID around the world.

This project works with the help of a CSV data extracted from https://github.com/CSSEGISandData/COVID-19.

<img src="https://i.imgur.com/eLX4mTP.png">

## How to run the program:

> Clone the repository
> Use an IDE that you prefer, I use IntelliJ
> Run CovidTrackerApplication.java
> Enter your favorite browser, and access http://localhost:8080

#### This tracker refreshes for new cases every hour.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.covidtracker;

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

@SpringBootApplication
@EnableScheduling
public class CovidTrackerApplication {

public static void main(String[] args) {
SpringApplication.run(CovidTrackerApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.github.covidtracker.controllers;

import com.github.covidtracker.models.LocationStats;
import com.github.covidtracker.services.CovidDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@Controller
public class HomeController {

@Autowired
CovidDataService covidDataService;

@GetMapping("/")
public String home(Model model) {
List<LocationStats> allStats = covidDataService.getAllStats();
int totalReportedCases = allStats.stream().mapToInt(LocationStats::getLatestTotalCases).sum();
int dailyCases = allStats.stream().mapToInt(LocationStats::getNewDailyCases).sum();
int newCases = totalReportedCases - dailyCases;
model.addAttribute("locationStats", allStats);
model.addAttribute("totalReportedCases", totalReportedCases);
model.addAttribute("newCases", newCases);
return "home";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.github.covidtracker.models;

public class LocationStats {

private String state;
private String country;
private int latestTotalCases;
private int newDailyCases;

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

public int getLatestTotalCases() {
return latestTotalCases;
}

public void setLatestTotalCases(int latestTotalCases) {
this.latestTotalCases = latestTotalCases;
}

public int getNewDailyCases() {
return newDailyCases;
}

public void setNewDailyCases(int newDailyCases) {
this.newDailyCases = newDailyCases;
}

public int dailyNew() {
return latestTotalCases - newDailyCases;
}
@Override
public String toString() {
return "LocationStats{" +
"state='" + state + '\'' +
", country='" + country + '\'' +
", latestTotalCases=" + latestTotalCases +
", newDailyCases=" + dailyNew() +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.github.covidtracker.services;
import com.github.covidtracker.models.LocationStats;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.apache.tomcat.util.http.parser.HttpParser;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.StringReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.List;

@Service
public class CovidDataService {

private static String VIRUS_DATA_URL = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv";

private List<LocationStats> allStats = new ArrayList<>();

public List<LocationStats> getAllStats() {
return allStats;
}

@PostConstruct
@Scheduled(cron = "* * 1 * * *")
public void fetchData() throws IOException, InterruptedException {
List<LocationStats> newStats = new ArrayList<>();
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(VIRUS_DATA_URL))
.build();
HttpResponse<String> httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString());
StringReader csvBodyReader = new StringReader(httpResponse.body());
Iterable<CSVRecord> records = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(csvBodyReader);
for (CSVRecord record : records) {
LocationStats locationStat = new LocationStats();
locationStat.setState(record.get("Province/State"));
locationStat.setCountry(record.get("Country/Region"));
locationStat.setLatestTotalCases(Integer.parseInt(record.get(record.size() - 1)));
locationStat.setNewDailyCases(Integer.parseInt(record.get(record.size() - 2)));
newStats.add(locationStat);
}
this.allStats = newStats;
}

}
1 change: 1 addition & 0 deletions Java/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

51 changes: 51 additions & 0 deletions Java/src/main/resources/templates/home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<!DOCTYPE html>

<html xmlns:th="http://www.thymeleaf.org" lang="en-US">

<head>
<title>COVID Tracker</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" media="all"
href="../../css/gtvg.css" th:href="@{/css/gtvg.css}" />
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">
</head>

<body>

<div class="container">
<hr class="my-4">
<h1>Simple COVID-19 Tracker</h1>
<hr class="my-4">
<p class="lead">This application shows current number of cases from all over Za Warudo!</p>
<p>Made with Spring <img alt="Spring icon." src="https://spring.io/images/projects/spring-edf462fec682b9d48cf628eaf9e19521.svg" width="23" height="23"></p>

<div class="p-5 mb-4 bg-light rounded-3">
<p class="lead">Total cases reported as of today:</p>
<h1 class="display-4" th:text="${totalReportedCases}"></h1>
<hr class="my-4">
<p>
<span>New cases reported since previous day:</span><br>
<span th:text="${newCases}"></span>
</p>
</div>

<div class="p-5 mb-4">
<table class="table table-bordered">
<tr>
<th>Country/Region</th>
<th>Province/State</th>
<th>Total Cases Reported</th>
<th>Daily New Cases</th>
</tr>
<tr th:each="locationStat : ${locationStats}">
<td th:text="${locationStat.country}">N/A</td>
<td th:text="${locationStat.state}">N/A</td>
<td th:text="${locationStat.latestTotalCases}">0</td>
<td th:text="${locationStat.dailyNew()}">0</td>
</tr>
</table>
</div>
</div>
</body>

</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.github.covidtracker;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class CovidTrackerApplicationTests {

@Test
void contextLoads() {
}

}

0 comments on commit f6e31af

Please sign in to comment.