use of com.robertsanek.util.SecretType.GOOGLE_CLOUD_SQL_RSANEK_POSTGRES_PASSWORD in project core by z1lc.
the class EnsureAllTablesHaveRecentData method ensure.
public void ensure() {
Set<String> violations = Sets.newHashSet();
String jdbcUrl = "jdbc:postgresql://google/postgres?socketFactory=com.google.cloud.sql.postgres.SocketFactory" + "&cloudSqlInstance=arctic-rite-143002:us-west1:rsanek-db";
String username = secretProvider.getSecret(GOOGLE_CLOUD_SQL_RSANEK_POSTGRES_USERNAME);
String password = secretProvider.getSecret(GOOGLE_CLOUD_SQL_RSANEK_POSTGRES_PASSWORD);
Unchecked.run(() -> Class.forName("org.postgresql.Driver"));
try (Connection connection = DriverManager.getConnection(jdbcUrl, username, password)) {
try (Statement statement = connection.createStatement()) {
new Reflections("com.robertsanek").getTypesAnnotatedWith(Table.class).forEach(clazz -> {
String tableName = clazz.getAnnotation(Table.class).name();
if (infiniteStalenessTable.contains(tableName)) {
return;
}
Set<String> dateColumns = Arrays.stream(clazz.getDeclaredFields()).filter(field -> field.getType().equals(ZonedDateTime.class)).map(zdtField -> Optional.ofNullable(zdtField.getAnnotation(Column.class)).map(Column::name).orElse(zdtField.getName().toLowerCase())).collect(Collectors.toSet());
dateColumns.stream().filter(dateColumn -> !infiniteStalenessColumn.contains(Pair.of(tableName, dateColumn))).forEach(dateColumn -> {
try {
ResultSet resultSet = statement.executeQuery(String.format("SELECT MAX(%s) FROM %s;", dateColumn, tableName));
resultSet.next();
Optional<LocalDate> maxDate = Optional.ofNullable(resultSet.getString(1)).map(dateString -> LocalDate.parse(dateString.substring(0, 10)));
if (maxDate.map(date -> date.isBefore(LocalDate.now().minus(customMaxStaleness.getOrDefault(Pair.of(tableName, dateColumn), DEFAULT_MAX_STALENESS)))).orElse(true)) {
violations.add(String.format("Column '%s' in table '%s' potentially has stale data: latest date is %s.", dateColumn, tableName, maxDate.orElse(null)));
}
} catch (SQLException e) {
log.error(e);
}
});
});
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
if (violations.size() > 0) {
notificationSender.sendEmailDefault("Potentially stale data!", String.join("<br>", violations));
}
}
Aggregations