Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding "Executive Summary" to report #8

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ out/
### Secrets ###
src/main/resources/secrets.properties

### Outputs ###
output.pdf
5 changes: 5 additions & 0 deletions run.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@echo off
:: Changes directory to current directory
cd /D %~dp0
:: Executes app using bash.exe
bash.exe -c "./weather alerts"
52 changes: 0 additions & 52 deletions src/main/java/org/kcrha/weather/AlertService.java

This file was deleted.

6 changes: 3 additions & 3 deletions src/main/java/org/kcrha/weather/BaseFileReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import org.apache.commons.io.IOUtils;
import org.kcrha.weather.models.cli.MissingFileException;

import java.io.*;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;

abstract public class BaseFileReader {
protected static String readFile(String filePath) {
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/org/kcrha/weather/EmailAlertService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.kcrha.weather;

import org.apache.commons.cli.CommandLine;
import org.kcrha.weather.aggregators.BasicAggregateForecastService;
import org.kcrha.weather.collectors.HttpService;
import org.kcrha.weather.models.cli.Location;
import org.kcrha.weather.models.cli.Region;
import org.kcrha.weather.models.forecast.DailyAggregateForecast;
import org.kcrha.weather.notifications.AlertHtmlReportFormatter;
import org.kcrha.weather.notifications.EmailNotification;
import org.kcrha.weather.notifications.builders.EmailReportBuilder;
import org.simplejavamail.api.email.Email;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class EmailAlertService implements CommandLineService {

private final EmailNotification notification;
private final EmailReportBuilder builder;
private final HttpService httpService;

public EmailAlertService() {
this.notification = new EmailNotification();
this.builder = new EmailReportBuilder(new AlertHtmlReportFormatter());
this.httpService = new HttpService();
}

public void run(CommandLine taskCommand) {
List<Region> regions = RegionFileReader.getRegions();
BasicAggregateForecastService forecastAggregateService = new BasicAggregateForecastService(httpService);

Map<Region, Map<Location, List<DailyAggregateForecast>>> regionalForecasts = new HashMap<>();

for (Region region : regions) {
if (regionalForecasts.containsKey(region)) {
System.out.println("WARNING: Regional forecasts already contains key for " + region.region() + "; overwriting");
}
regionalForecasts.put(region, new HashMap<>());
for (Location location : region.locations()) {
if (regionalForecasts.get(region).containsKey(location)) {
System.out.println("WARNING: Regional forecasts already contains key for " + region.region() + " & " + location.location() + "; overwriting");
}
regionalForecasts.get(region).put(location, forecastAggregateService.getForecasts(7, location.lat(), location.lon()));
}
}

builder.setRegionalForecasts(regionalForecasts);
Report report = builder.getReport();

Email email = notification.prepare(report);
notification.send(email);
}
}
30 changes: 14 additions & 16 deletions src/main/java/org/kcrha/weather/ForecastService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,41 @@
import org.kcrha.weather.collectors.HttpService;
import org.kcrha.weather.models.cli.Location;
import org.kcrha.weather.models.cli.Region;
import org.kcrha.weather.models.forecast.BasicAggregateForecast;
import org.kcrha.weather.notifications.HtmlForecastNotificationFormatter;
import org.kcrha.weather.models.forecast.DailyAggregateForecast;
import org.kcrha.weather.notifications.ConsoleNotification;
import org.kcrha.weather.notifications.ForecastHtmlReportFormatter;
import org.kcrha.weather.notifications.Notification;
import org.kcrha.weather.notifications.NotificationFormatter;
import org.kcrha.weather.notifications.ReportFormatter;

import java.util.List;
import java.util.Set;

public class ForecastService implements CommandLineService {
private final Notification notification;
private final NotificationFormatter formatter;
private final ConsoleNotification notification;
private final ReportFormatter formatter;
private final HttpService httpService;

public ForecastService(Notification notification) {
this(notification, null, null);
}

public ForecastService(Notification notification, NotificationFormatter formatter, HttpService httpService) {
this.notification = notification;
this.formatter = formatter != null ? formatter : new HtmlForecastNotificationFormatter();
this.httpService = httpService != null ? httpService : new HttpService();
public ForecastService() {
this.notification = new ConsoleNotification();
this.formatter = new ForecastHtmlReportFormatter();
this.httpService = new HttpService();
}

public void run(CommandLine taskCommand) {
List<Region> regions = RegionFileReader.getRegions();
StringBuilder output = new StringBuilder(formatter.formatHeader());
StringBuilder output = new StringBuilder(formatter.formatReportHeader());
BasicAggregateForecastService forecastAggregateService = new BasicAggregateForecastService(httpService);

for (Region region : regions) {
for (Location location : region.locations()) {
List<BasicAggregateForecast> aggregatedForecasts = forecastAggregateService.getForecasts(7, location.lat(), location.lon());
List<DailyAggregateForecast> aggregatedForecasts = forecastAggregateService.getForecasts(7, location.lat(), location.lon());

output.append(formatter.formatForecastTableHeader(String.format("%s (%s)", location.location(), region.region())));
output.append(formatter.formatForecastTable(aggregatedForecasts));
output.append(formatter.formatForecastTableFooter(String.format("<a target=\"_blank\" href=\"https://maps.google.com/?q=%s,%s\"><i class=\"fa fa-external-link\"></i></a>\n", location.lat(), location.lon())));
}
}
output.append(formatter.formatFooter());
output.append(formatter.formatReportFooter());
notification.send(output.toString());
}
}
3 changes: 0 additions & 3 deletions src/main/java/org/kcrha/weather/GridPointCacheFileReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.kcrha.weather.models.cli.GridPoint;
import org.kcrha.weather.models.cli.MissingFileException;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.lang.reflect.Type;
import java.util.Map;

Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/kcrha/weather/Report.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.kcrha.weather;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class Report {
String summary;
String fullReport;
}
10 changes: 2 additions & 8 deletions src/main/java/org/kcrha/weather/WeatherApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
import org.kcrha.weather.notifications.ConsoleNotification;
import org.kcrha.weather.notifications.EmailNotification;

import java.io.IOException;

import static org.kcrha.weather.models.cli.PropertyReader.getSecretProperty;

public class WeatherApplication {

final static Options OPTIONS = getOptions();
Expand Down Expand Up @@ -63,12 +59,10 @@ private static void run(CommandLine line, String[] args) {

switch (task) {
case ALERTS -> {
EmailNotification notification = new EmailNotification();
new AlertService(notification).run(taskCommand);
new EmailAlertService().run(taskCommand);
}
case FORECASTS -> {
ConsoleNotification notification = new ConsoleNotification();
new ForecastService(notification).run(taskCommand);
new ForecastService().run(taskCommand);
}
default -> throw new RuntimeException(String.format("Unknown TaskType: %s", task));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import org.kcrha.weather.collectors.AirQualityForecastCollector;
import org.kcrha.weather.collectors.HeatRiskForecastCollector;
import org.kcrha.weather.collectors.HttpService;
import org.kcrha.weather.collectors.TemperatureForecastCollector;
import org.kcrha.weather.models.forecast.BasicAggregateForecast;
import org.kcrha.weather.collectors.WeatherForecastCollector;
import org.kcrha.weather.models.forecast.DailyAggregateForecast;
import org.kcrha.weather.models.forecast.DailyAirQualityForecast;
import org.kcrha.weather.models.forecast.DailyHeatRiskForecast;
import org.kcrha.weather.models.forecast.DailyTemperatureForecast;
import org.kcrha.weather.models.forecast.DailyWeatherForecast;
import org.kcrha.weather.models.forecast.metrics.AirQualityIndex;
import org.kcrha.weather.models.forecast.metrics.HeatRiskIndex;

Expand All @@ -16,36 +16,38 @@
import java.util.function.Function;
import java.util.stream.Collectors;

public class BasicAggregateForecastService implements AggregateService<BasicAggregateForecast> {
public class BasicAggregateForecastService implements AggregateService<DailyAggregateForecast> {

AirQualityForecastCollector airQualityForecastCollector;
HeatRiskForecastCollector heatRiskForecastCollector;
TemperatureForecastCollector temperatureForecastCollector;
WeatherForecastCollector weatherForecastCollector;

public BasicAggregateForecastService(HttpService httpService) {
airQualityForecastCollector = new AirQualityForecastCollector(httpService);
heatRiskForecastCollector = new HeatRiskForecastCollector(httpService);
temperatureForecastCollector = new TemperatureForecastCollector(httpService);
weatherForecastCollector = new WeatherForecastCollector(httpService);
}

public List<BasicAggregateForecast> getForecasts(Integer days, Float latitude, Float longitude) {
Map<LocalDate, DailyTemperatureForecast> temperatureForecasts = temperatureForecastCollector.retrieveDailyForecasts(days, latitude, longitude)
.stream().collect(Collectors.toMap(DailyTemperatureForecast::getDay, Function.identity()));
public List<DailyAggregateForecast> getForecasts(Integer days, Float latitude, Float longitude) {
Map<LocalDate, DailyWeatherForecast> temperatureForecasts = weatherForecastCollector.retrieveDailyForecasts(days, latitude, longitude)
.stream().collect(Collectors.toMap(DailyWeatherForecast::getDay, Function.identity()));
List<DailyAirQualityForecast> airQualityForecasts = airQualityForecastCollector.retrieveDailyForecasts(days, latitude, longitude);
Map<LocalDate, DailyAirQualityForecast> airQualityForecastMap = airQualityForecasts.stream().filter(Objects::nonNull).collect(Collectors.toMap(DailyAirQualityForecast::getDay, Function.identity()));
Map<LocalDate, DailyHeatRiskForecast> heatRiskForecasts = getHeatRiskForecasts(days, latitude, longitude);
java.util.List<BasicAggregateForecast> aggregatedForecasts = new ArrayList<>();
for (Map.Entry<LocalDate, DailyTemperatureForecast> entry : temperatureForecasts.entrySet()) {
DailyTemperatureForecast dailyTemperatureForecast = entry.getValue();
java.util.List<DailyAggregateForecast> aggregatedForecasts = new ArrayList<>();
for (Map.Entry<LocalDate, DailyWeatherForecast> entry : temperatureForecasts.entrySet()) {
DailyWeatherForecast dailyWeatherForecast = entry.getValue();
AirQualityIndex aqi = airQualityForecastMap.getOrDefault(entry.getKey(), DailyAirQualityForecast.builder().build()).getAirQualityIndex();
HeatRiskIndex hri = heatRiskForecasts.getOrDefault(entry.getKey(), DailyHeatRiskForecast.builder().build()).getHeatRiskIndex();
aggregatedForecasts.add(BasicAggregateForecast.builder()
.day(dailyTemperatureForecast.getDay())
aggregatedForecasts.add(DailyAggregateForecast.builder()
.day(dailyWeatherForecast.getDay())
.airQualityIndex(aqi)
.temperatureLow(dailyTemperatureForecast.getTemperatureLow())
.temperatureAverage(dailyTemperatureForecast.getTemperatureAverage())
.temperatureHigh(dailyTemperatureForecast.getTemperatureHigh())
.temperatureLow(dailyWeatherForecast.getTemperatureLow())
.temperatureAverage(dailyWeatherForecast.getTemperatureAverage())
.temperatureHigh(dailyWeatherForecast.getTemperatureHigh())
.heatRiskIndex(hri)
.rainfallAccumulation(dailyWeatherForecast.getRainfallAccumulation())
.snowIceAccumulation(dailyWeatherForecast.getSnowIceAccumulation())
.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.net.http.HttpResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializer;
import lombok.AllArgsConstructor;
import org.kcrha.weather.collectors.api.nws.HeatRisk;
import org.kcrha.weather.collectors.api.nws.hourly_forecast.HeatRisk;
import org.kcrha.weather.models.forecast.DailyHeatRiskForecast;
import org.kcrha.weather.models.forecast.metrics.HeatRiskIndex;

Expand Down
Loading