Search in sources :

Example 1 with GOOGLE_CLOUD_SQL_RSANEK_POSTGRES_USERNAME

use of com.robertsanek.util.SecretType.GOOGLE_CLOUD_SQL_RSANEK_POSTGRES_USERNAME 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));
    }
}
Also used : Arrays(java.util.Arrays) Connection(java.sql.Connection) ZonedDateTime(java.time.ZonedDateTime) Inject(com.google.inject.Inject) Reflections(org.reflections.Reflections) Unchecked(com.robertsanek.util.Unchecked) Log(com.robertsanek.util.Log) SQLException(java.sql.SQLException) Table(javax.persistence.Table) Pair(org.apache.commons.lang3.tuple.Pair) ResultSet(java.sql.ResultSet) SecretProvider(com.robertsanek.util.SecretProvider) GOOGLE_CLOUD_SQL_RSANEK_POSTGRES_USERNAME(com.robertsanek.util.SecretType.GOOGLE_CLOUD_SQL_RSANEK_POSTGRES_USERNAME) Period(java.time.Period) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) NotificationSender(com.robertsanek.util.NotificationSender) Set(java.util.Set) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Logs(com.robertsanek.util.Logs) GOOGLE_CLOUD_SQL_RSANEK_POSTGRES_PASSWORD(com.robertsanek.util.SecretType.GOOGLE_CLOUD_SQL_RSANEK_POSTGRES_PASSWORD) Column(javax.persistence.Column) LocalDate(java.time.LocalDate) Statement(java.sql.Statement) Optional(java.util.Optional) DriverManager(java.sql.DriverManager) Table(javax.persistence.Table) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) LocalDate(java.time.LocalDate) ZonedDateTime(java.time.ZonedDateTime) Column(javax.persistence.Column) ResultSet(java.sql.ResultSet) Reflections(org.reflections.Reflections)

Aggregations

ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Sets (com.google.common.collect.Sets)1 Inject (com.google.inject.Inject)1 Log (com.robertsanek.util.Log)1 Logs (com.robertsanek.util.Logs)1 NotificationSender (com.robertsanek.util.NotificationSender)1 SecretProvider (com.robertsanek.util.SecretProvider)1 GOOGLE_CLOUD_SQL_RSANEK_POSTGRES_PASSWORD (com.robertsanek.util.SecretType.GOOGLE_CLOUD_SQL_RSANEK_POSTGRES_PASSWORD)1 GOOGLE_CLOUD_SQL_RSANEK_POSTGRES_USERNAME (com.robertsanek.util.SecretType.GOOGLE_CLOUD_SQL_RSANEK_POSTGRES_USERNAME)1 Unchecked (com.robertsanek.util.Unchecked)1 Connection (java.sql.Connection)1 DriverManager (java.sql.DriverManager)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 LocalDate (java.time.LocalDate)1 Period (java.time.Period)1 ZonedDateTime (java.time.ZonedDateTime)1 Arrays (java.util.Arrays)1