-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
helps people starting with spring
- Loading branch information
Showing
8 changed files
with
234 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
14 changes: 14 additions & 0 deletions
14
Java/Spring/covidTracker/src/main/java/com/github/covidtracker/CovidTrackerApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...Spring/covidTracker/src/main/java/com/github/covidtracker/controllers/HomeController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
Java/Spring/covidTracker/src/main/java/com/github/covidtracker/models/LocationStats.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() + | ||
'}'; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
.../Spring/covidTracker/src/main/java/com/github/covidtracker/services/CovidDataService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
1
Java/Spring/covidTracker/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
51 changes: 51 additions & 0 deletions
51
Java/Spring/covidTracker/src/main/resources/templates/home.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/[email protected]/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> |
13 changes: 13 additions & 0 deletions
13
...ring/covidTracker/src/test/java/com/github/covidtracker/CovidTrackerApplicationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
} | ||
|
||
} |