Skip to content

Commit

Permalink
Added tests for parsingService + updated logging
Browse files Browse the repository at this point in the history
  • Loading branch information
This-Is-Ko committed Oct 8, 2023
1 parent 27a3def commit 7a66b76
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 31 deletions.
11 changes: 0 additions & 11 deletions logback-spring.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ public PlayerMatchPerformanceStats parsePlayerMatchData(Player player, Document
for (Element resultRow : resultRows) {
// For games not played, appears as unused_sub class
if (!resultRow.getElementsByClass("unused_sub").isEmpty()) {
log.atInfo().setMessage(player.getName() + " " + "Skip latest due to unused sub").addKeyValue("player", player.getName()).log();
log.atInfo().setMessage("Skip latest due to unused sub").addKeyValue("player", player.getName()).log();
continue;
} else if (!resultRow.getElementsByClass("partial_table").isEmpty()) {
log.atInfo().setMessage(player.getName() + " " + "Spacer row skipped").addKeyValue("player", player.getName()).log();
log.atInfo().setMessage("Spacer row skipped").addKeyValue("player", player.getName()).log();
continue;
}
String latestMatchUrl = resultRow.select("th[data-stat=date] > a").attr("href");
Expand All @@ -81,17 +81,17 @@ public PlayerMatchPerformanceStats parsePlayerMatchData(Player player, Document
if (!resultRow.select("th[data-stat=date] > a").text().isEmpty()) {
selectedMatchDate = dateFormat.parse(resultRow.select("th[data-stat=date] > a").text());
} else {
log.atInfo().setMessage(player.getName() + " - Unable to get date from match row").addKeyValue("player", player.getName()).log();
log.atInfo().setMessage("Unable to get date from match row").addKeyValue("player", player.getName()).log();
return null;
}

if (player.getCheckedStatus() != null) {
if (player.getCheckedStatus().getLatestCheckedMatchDate() != null && !(selectedMatchDate.compareTo(player.getCheckedStatus().getLatestCheckedMatchDate()) > 0)) {
log.atInfo().setMessage(player.getName() + " - Selected match is not newer than last checked").addKeyValue("player", player.getName()).log();
log.atInfo().setMessage("Selected match is not newer than last checked").addKeyValue("player", player.getName()).log();
return null;
} else if (player.getCheckedStatus().getLatestCheckedMatchUrl() != null && player.getCheckedStatus().getLatestCheckedMatchUrl().equals(latestMatchUrl)) {
// No new updates
log.atInfo().setMessage(player.getName() + " - latestMatchUrl matches last checked").addKeyValue("player", player.getName()).log();
log.atInfo().setMessage("latestMatchUrl matches last checked").addKeyValue("player", player.getName()).log();
return null;
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public PlayerMatchPerformanceStats parsePlayerMatchData(Player player, Document
// Assume first row is the latest match
Elements latestMatchRow = document.selectXpath("//main/div[2]/div[1]/div[4]/section/div/article/table/tbody/tr[1]/td[2]/a");
if (latestMatchRow.isEmpty()) {
log.info("Cannot find any match results on player page");
log.atInfo().setMessage("Cannot find any match results on player page").addKeyValue("player", player.getName()).log();
return null;
}

Expand Down Expand Up @@ -82,7 +82,7 @@ public PlayerMatchPerformanceStats parsePlayerMatchData(Player player, Document
// Check whether this match is newer than last checked
Date selectedMatchDate = dateFormat.parse(jsonNode.get("general").get("matchTimeUTCDate").textValue());
if (player.getCheckedStatus().getLatestCheckedMatchDate() != null && !(selectedMatchDate.compareTo(player.getCheckedStatus().getLatestCheckedMatchDate()) > 0)) {
log.info(player.getName() + " - Selected match is not newer than last checked");
log.atInfo().setMessage("Selected match is not newer than last checked").addKeyValue("player", player.getName()).log();
return null;
}

Expand Down Expand Up @@ -125,9 +125,9 @@ public PlayerMatchPerformanceStats parsePlayerMatchData(Player player, Document
}
}
}
log.atInfo().setMessage(player.getName() + " " + "Unable to update player, parsed all players in the match").addKeyValue("player", player.getName()).log();
log.atInfo().setMessage("Unable to update player, parsed all players in the match").addKeyValue("player", player.getName()).log();
} catch (Exception ex) {
log.warn("Error while trying to update player: " + player.getName(), ex);
log.atWarn().setMessage("Error while trying to update player").setCause(ex).addKeyValue("player", player.getName()).log();
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ public CheckedStatus() {
public CheckedStatus(DataSourceSiteName siteName) {
this.siteName = siteName;
}

public CheckedStatus(Integer id, DataSourceSiteName siteName) {
this.id = id;
this.siteName = siteName;
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/ko/footballupdater/models/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,14 @@ public Player(String name, Date dob) {
this.name = name;
this.dob = dob;
}

public Player(Integer id, String name, Team team, Date dob, Set<Image> images, Set<DataSource> dataSources, CheckedStatus checkedStatus) {
this.id = id;
this.name = name;
this.team = team;
this.dob = dob;
this.images = images;
this.dataSources = dataSources;
this.checkedStatus = checkedStatus;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public class PlayerMatchPerformanceStats {
private Integer gkPenaltiesScoredAgainst;
private Integer gkPenaltiesSaved;

// For tests
public PlayerMatchPerformanceStats(DataSourceSiteName dataSourceSiteName) {
this.dataSourceSiteName = dataSourceSiteName;
}

public PlayerMatchPerformanceStats(DataSourceSiteName dataSourceSiteName, Match match, Integer minutesPlayed, Integer goals, Integer assists, Integer shots, Integer shotsBlocked, Integer fouls, Integer fouled, Integer offsides, String crossingAccuracyAll, Integer dispossessed, Integer touches, String tacklingSuccessAll, Integer defensiveActions, Integer recoveries, Integer duelsWon, Integer duelsLost, Integer groundDuelsWon, Integer aerialDuelsWon, Integer chancesCreatedAll, String passingAccuracyAll, Integer passesIntoFinalThird, String carriesSuccessAll) {
this.dataSourceSiteName = dataSourceSiteName;
this.match = match;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@ public void uploadtoS3(InstagramPost post) {
.withCannedAcl(CannedAccessControlList.PublicRead);
s3Client.putObject(request);
String imageUrl = s3Client.getUrl(amazonS3Properties.getBucketName(), imageFileName).toString();
log.atInfo().setMessage(post.getPlayer().getName() + " - Successfully uploaded image " + imageFileName + " to S3 @ " + imageUrl).log();
log.atInfo().setMessage("Successfully uploaded image " + imageFileName + " to S3 @ " + imageUrl).addKeyValue("player", post.getPlayer().getName()).log();
post.getImagesS3Urls().add(imageUrl);

cleanUpFile(file);
}
} catch (AmazonServiceException ex) {
// The call was transmitted successfully, but Amazon S3 couldn't process
// it, so it returned an error response.
log.warn(post.getPlayer().getName() + " - Error attempting to upload", ex);
log.atWarn().setMessage("Error attempting to upload").setCause(ex).addKeyValue("player", post.getPlayer().getName()).log();
} catch (SdkClientException ex) {
// Amazon S3 couldn't be contacted for a response, or the client
// couldn't parse the response from Amazon S3.
log.warn(post.getPlayer().getName() + " - Error attempting to upload", ex);
log.atWarn().setMessage("Error attempting to upload").setCause(ex).addKeyValue("player", post.getPlayer().getName()).log();
}
} else {
log.atInfo().setMessage(post.getPlayer().getName() + " - No images to upload").log();
Expand All @@ -64,9 +64,9 @@ public void uploadtoS3(InstagramPost post) {
// Delete files uploaded to S3
private void cleanUpFile(File file) {
if (file.delete()) {
log.info("Deleted file: " + file.getAbsolutePath());
log.atInfo().setMessage("Deleted file: " + file.getAbsolutePath()).log();
} else {
log.warn("Unable to delete file: " + file.getAbsolutePath());
log.atWarn().setMessage("Unable to delete file: " + file.getAbsolutePath()).log();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void generatePlayerStatImage(InstagramPost post) throws Exception {
createdImageCounter++;
saveImage(post, image, createdImageCounter);
} catch (Exception ex) {
log.warn(post.getPlayer().getName() + " - Error while generating stat image ", ex);
log.atWarn().setMessage("Error while generating stat image").setCause(ex).addKeyValue("player", post.getPlayer().getName()).log();
throw new Exception(post.getPlayer().getName() + " - Error while generating stat image ", ex);
}
}
Expand Down Expand Up @@ -138,7 +138,7 @@ private void saveImage(InstagramPost post, BufferedImage image, int createdImage
String outputImageFilePath = imageGeneratorProperies.getOutputPath() + fileName;
ImageIO.write(image, "jpg", new File(outputImageFilePath));
post.getImagesFileNames().add(fileName);
log.atInfo().setMessage(post.getPlayer().getName() + " - Generated stat image " + createdImageCounter).addKeyValue("player", post.getPlayer().getName()).log();
log.atInfo().setMessage("Generated stat image " + createdImageCounter).addKeyValue("player", post.getPlayer().getName()).log();
}

private static List<String> getZeroValueFilter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,20 @@ public PlayerMatchPerformanceStats parsePlayerMatchData(Player player) {
if (dataSources.stream().anyMatch(o -> o.getSiteName().equals(source))) {
DataSource dataSource = dataSources.stream().filter(o -> o.getSiteName().equals(source)).findFirst().get();

log.info(player.getName() + " - Last checked was " + player.getCheckedStatus().getSiteName() + "; dataSource is " + dataSource.getSiteName());
log.atInfo().setMessage("Attempting dataSource " + dataSource.getSiteName()).addKeyValue("player", player.getName()).log();

for (DataSourceParser dataSourceParser : dataSourceParsers) {
if (dataSourceParser.getDataSourceSiteName().equals(dataSource.getSiteName())) {
try {
Document doc = Jsoup.connect(dataSource.getUrl()).get();
PlayerMatchPerformanceStats playerMatchPerformanceStats = dataSourceParser.parsePlayerMatchData(player, doc);
if (playerMatchPerformanceStats != null) {
log.atInfo().setMessage(player.getName() + " - " + dataSource.getSiteName() + " - Successfully parse player data").addKeyValue("player", player.getName()).log();
log.atInfo().setMessage(dataSource.getSiteName() + " - Successfully parse player data").addKeyValue("player", player.getName()).log();
player.getCheckedStatus().setSiteName(dataSource.getSiteName());
return playerMatchPerformanceStats;
}
} catch (IOException e) {
log.warn("Unable to retrieve page at " + dataSource.getUrl() + '\n' + e);
} catch (IOException ex) {
log.atWarn().setMessage("Unable to retrieve page at " + dataSource.getUrl()).setCause(ex).addKeyValue("player", player.getName()).log();
return null;
}
break;
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<configuration scan="true">

<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd'T'HH:mm:ss.SSSXXX}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr(${LOGGED_APPLICATION_NAME:-}[%15.15t]){faint} %clr(${LOG_CORRELATION_PATTERN:-}){faint}%clr(%-40.40logger{39}){cyan} %clr(:){faint}%replace( %kvp - ){' - ', ' '}%m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>

<include resource="org/springframework/boot/logging/logback/base.xml"/>

</configuration>

0 comments on commit 7a66b76

Please sign in to comment.