Isolate customizable code in activity reporting

Modularize the code for DNS count reporting to allow it to be customized for
more flexible systems.
Tested:
  Uploaded to alpha with hacks to allow admin initiating and logging from the
  DnsCountQueryCoordinatorModule, verified that the provider function is invoked and
  that the action runs successfully.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=225225587
This commit is contained in:
mmuller 2018-12-12 11:48:08 -08:00 committed by Michael Muller
parent 6966151bed
commit c396957d15
13 changed files with 208 additions and 12 deletions

View file

@ -29,9 +29,7 @@ import org.joda.time.YearMonth;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
/**
* Utility class that produces SQL queries used to generate activity reports from Bigquery.
*/
/** Utility class that produces SQL queries used to generate activity reports from Bigquery. */
public final class ActivityReportingQueryBuilder implements QueryBuilder {
// Names for intermediary tables for overall activity reporting query.
@ -42,20 +40,23 @@ public final class ActivityReportingQueryBuilder implements QueryBuilder {
static final String WHOIS_COUNTS = "whois_counts";
static final String ACTIVITY_REPORT_AGGREGATION = "activity_report_aggregation";
@Inject @Config("projectId") String projectId;
@Inject
@Config("projectId")
String projectId;
@Inject YearMonth yearMonth;
@Inject ActivityReportingQueryBuilder() {}
@Inject DnsCountQueryCoordinator dnsCountQueryCoordinator;
@Inject
ActivityReportingQueryBuilder() {}
/** Returns the aggregate query which generates the activity report from the saved view. */
@Override
public String getReportQuery() {
return String.format(
"#standardSQL\nSELECT * FROM `%s.%s.%s`",
projectId,
ICANN_REPORTING_DATA_SET,
getTableName(ACTIVITY_REPORT_AGGREGATION));
projectId, ICANN_REPORTING_DATA_SET, getTableName(ACTIVITY_REPORT_AGGREGATION));
}
/** Sets the month we're doing activity reporting for, and returns the view query map. */
@ -67,6 +68,10 @@ public final class ActivityReportingQueryBuilder implements QueryBuilder {
return createQueryMap(firstDayOfMonth, lastDayOfMonth);
}
public void prepareForQuery() throws Exception {
dnsCountQueryCoordinator.prepareForQuery();
}
/** Returns a map from view name to its associated SQL query. */
private ImmutableMap<String, String> createQueryMap(
LocalDate firstDayOfMonth, LocalDate lastDayOfMonth) {
@ -80,8 +85,7 @@ public final class ActivityReportingQueryBuilder implements QueryBuilder {
.build();
queriesBuilder.put(getTableName(REGISTRAR_OPERATING_STATUS), operationalRegistrarsQuery);
String dnsCountsQuery =
SqlTemplate.create(getQueryFromFile("dns_counts.sql")).build();
String dnsCountsQuery = dnsCountQueryCoordinator.createQuery();
queriesBuilder.put(getTableName(DNS_COUNTS), dnsCountsQuery);
// Convert reportingMonth into YYYYMMDD format for Bigquery table partition pattern-matching.
@ -133,7 +137,6 @@ public final class ActivityReportingQueryBuilder implements QueryBuilder {
return queriesBuilder.build();
}
/** Returns the table name of the query, suffixed with the yearMonth in _yyyyMM format. */
private String getTableName(String queryName) {
return String.format("%s_%s", queryName, DateTimeFormat.forPattern("yyyyMM").print(yearMonth));