Skip to content

Commit

Permalink
feat/report retention time (#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thermalight authored Jan 27, 2025
1 parent 1c391ae commit 17ec74d
Showing 1 changed file with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2022-2024 WeAreFrank!
Copyright 2022-2025 WeAreFrank!
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -87,9 +87,11 @@ public class DatabaseStorage implements Storage {
protected @Setter String table;
protected @Setter @Inject @Resource(name="metadataNames") List<String> metadataNames; // Used as column names in this storage
protected @Setter String storageIdColumn;
protected @Setter String endTimeColumn;
protected @Setter List<String> bigValueColumns; // Columns for which to limit the number of retrieved characters to 100
protected @Setter Boolean storeReportXml;
protected @Setter Long maxStorageSize;
protected @Setter Long maxStorageDays;
protected @Setter @Getter @Inject @Autowired JdbcTemplate ladybugJdbcTemplate;
protected @Setter @Getter @Inject @Autowired DbmsSupport dbmsSupport;
protected @Setter @Getter @Inject @Autowired MetadataExtractor metadataExtractor;
Expand All @@ -106,6 +108,14 @@ public String getTable() {
}
}

public String getEndTimeColum() {
if (endTimeColumn == null) {
return "ENDTIME";
} else {
return endTimeColumn;
}
}

public List<String> getMetadataNames() {
if (getMetadataExtractor().getExtraMetadataFieldExtractors() != null) {
List<String> metadataNames = new ArrayList<String>(this.metadataNames);
Expand Down Expand Up @@ -154,6 +164,14 @@ public long getMaxStorageSize() {
}
}

public long getMaxStorageDays() {
if (maxStorageDays == null) {
return -1;
} else {
return maxStorageDays;
}
}

@PostConstruct
public void init() throws StorageException {
if (!(getMetadataNames() != null && getMetadataNames().contains(getStorageIdColumn()))) {
Expand Down Expand Up @@ -240,6 +258,13 @@ public void setValues(PreparedStatement ps) throws SQLException {
+ " <= ((select max(" + getStorageIdColumn() + ") from " + getTable() + ") - ?)";
delete(deleteQuery, maxNrOfReports);
}
if (getMaxStorageDays() > -1) {
String deleteQuery = "delete from " + getTable() + " where " + getEndTimeColum()
+ " < now() - interval '" + getMaxStorageDays() + " days'";
int nrOfDeletedReports = ladybugJdbcTemplate.update(deleteQuery);
log.debug("Checked for reports older than " + getMaxStorageDays() + " days: "
+ nrOfDeletedReports + " reports deleted.");
}
}

@Override
Expand Down

0 comments on commit 17ec74d

Please sign in to comment.