Skip to content

Commit 3dce62d

Browse files
[b/390590500] Airflow dates params (#766)
* add date params to airflow connector, intermediate commit * add date range support to airflow connector * use data range parameters for one file in Airflow connector * fix typos * rename private method * move comment to java doc * update parameter description * update params description * update comment * remove lookback-days param
1 parent 9bfa583 commit 3dce62d

File tree

10 files changed

+245
-38
lines changed

10 files changed

+245
-38
lines changed

dumper/app/src/main/java/com/google/edwmigration/dumper/application/dumper/ConnectorArguments.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ public class ConnectorArguments extends DefaultArguments {
9191
public static final int OPT_PORT_ORDER = 200;
9292
public static final String OPT_USER = "user";
9393
public static final String OPT_PASSWORD = "password";
94+
public static final String OPT_START_DATE = "start-date";
95+
public static final String OPT_END_DATE = "end-date";
9496

9597
public static final String OPT_CLUSTER = "cluster";
9698
public static final String OPT_ROLE = "role";
@@ -228,6 +230,26 @@ public class ConnectorArguments extends DefaultArguments {
228230
.withOptionalArg()
229231
.describedAs("sekr1t");
230232

233+
private final OptionSpec<ZonedDateTime> optionStartDate =
234+
parser
235+
.accepts(
236+
OPT_START_DATE,
237+
"Inclusive start date for data to export, value will be truncated to hour")
238+
.withOptionalArg()
239+
.ofType(Date.class)
240+
.withValuesConvertedBy(ZonedParser.withDefaultPattern(DayOffset.START_OF_DAY))
241+
.describedAs("2001-01-15[ 00:00:00.[000]]");
242+
243+
private final OptionSpec<ZonedDateTime> optionEndDate =
244+
parser
245+
.accepts(
246+
OPT_END_DATE,
247+
"Exclusive end date for data to export, value will be truncated to hour")
248+
.withOptionalArg()
249+
.ofType(Date.class)
250+
.withValuesConvertedBy(ZonedParser.withDefaultPattern(DayOffset.START_OF_DAY))
251+
.describedAs("2001-01-15[ 00:00:00.[000]]");
252+
231253
private final OptionSpec<String> optionCluster =
232254
parser
233255
.accepts(OPT_CLUSTER, "Cluster name to dump metadata")
@@ -309,7 +331,7 @@ public class ConnectorArguments extends DefaultArguments {
309331
.withOptionalArg()
310332
.ofType(Date.class)
311333
.withValuesConvertedBy(ZonedParser.withDefaultPattern(DayOffset.START_OF_DAY))
312-
.describedAs("2001-01-01[ 00:00:00.[000]]");
334+
.describedAs("2001-01-15[ 00:00:00.[000]]");
313335
private final OptionSpec<ZonedDateTime> optionQueryLogEnd =
314336
parser
315337
.accepts(
@@ -318,7 +340,7 @@ public class ConnectorArguments extends DefaultArguments {
318340
.withOptionalArg()
319341
.ofType(Date.class)
320342
.withValuesConvertedBy(ZonedParser.withDefaultPattern(DayOffset.END_OF_DAY))
321-
.describedAs("2001-01-01[ 00:00:00.[000]]");
343+
.describedAs("2001-01-15[ 00:00:00.[000]]");
322344

323345
// This is intentionally NOT provided as a default value to the optionQueryLogEnd OptionSpec,
324346
// because some callers
@@ -889,6 +911,20 @@ public Integer getQueryLogDays() {
889911
return getOptions().valueOf(optionQueryLogDays);
890912
}
891913

914+
@CheckForNull
915+
public ZonedDateTime getStartDate() {
916+
return getOptions().valueOf(optionStartDate);
917+
}
918+
919+
public ZonedDateTime getStartDate(ZonedDateTime defaultTime) {
920+
return firstNonNull(getStartDate(), defaultTime);
921+
}
922+
923+
@CheckForNull
924+
public ZonedDateTime getEndDate() {
925+
return getOptions().valueOf(optionEndDate);
926+
}
927+
892928
public Duration getQueryLogRotationFrequency() {
893929
return RotationFrequencyConverter.convert(
894930
getOptions().valueOf(optionQueryLogRotationFrequency));

dumper/app/src/main/java/com/google/edwmigration/dumper/application/dumper/annotations/RespectsArgumentQueryLogDays.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@
3535
description = RespectsArgumentQueryLogDays.DESCRIPTION)
3636
public @interface RespectsArgumentQueryLogDays {
3737

38-
public static final String DESCRIPTION = "The number of days of query history to dump.";
38+
String DESCRIPTION = "The number of days of query history to dump.";
3939
}

dumper/app/src/main/java/com/google/edwmigration/dumper/application/dumper/annotations/RespectsArgumentQueryLogEnd.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@
3535
description = RespectsArgumentQueryLogEnd.DESCRIPTION)
3636
public @interface RespectsArgumentQueryLogEnd {
3737

38-
public static final String DESCRIPTION = "End date for query history to dump";
38+
String DESCRIPTION = "End date for query history to dump";
3939
}

dumper/app/src/main/java/com/google/edwmigration/dumper/application/dumper/annotations/RespectsArgumentQueryLogStart.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@
3535
description = RespectsArgumentQueryLogStart.DESCRIPTION)
3636
public @interface RespectsArgumentQueryLogStart {
3737

38-
public static final String DESCRIPTION = "Start date for query history to dump";
38+
String DESCRIPTION = "Start date for query history to dump";
3939
}

dumper/app/src/main/java/com/google/edwmigration/dumper/application/dumper/connector/AbstractJdbcConnector.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
import java.security.PrivilegedExceptionAction;
2929
import java.sql.Driver;
3030
import java.sql.SQLException;
31+
import java.time.ZoneOffset;
32+
import java.time.ZonedDateTime;
33+
import java.time.format.DateTimeFormatter;
3134
import java.util.ArrayList;
3235
import java.util.Arrays;
3336
import java.util.List;
@@ -39,14 +42,18 @@
3942

4043
/** @author shevek */
4144
public abstract class AbstractJdbcConnector extends AbstractConnector {
42-
43-
@SuppressWarnings("UnusedVariable")
4445
private static final Logger logger = LoggerFactory.getLogger(AbstractJdbcConnector.class);
46+
private static final DateTimeFormatter SQL_FORMAT =
47+
DateTimeFormatter.ISO_OFFSET_DATE_TIME.withZone(ZoneOffset.UTC);
4548

4649
public AbstractJdbcConnector(@Nonnull String name) {
4750
super(name);
4851
}
4952

53+
protected static String dateToSqlFormat(ZonedDateTime dateTime) {
54+
return SQL_FORMAT.format(dateTime);
55+
}
56+
5057
@Nonnull
5158
private static ClassLoader newDriverParentClassLoader() throws PrivilegedActionException {
5259
return AccessController.doPrivileged(

dumper/app/src/main/java/com/google/edwmigration/dumper/application/dumper/connector/ZonedIntervalIterable.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,25 @@
2323
import java.util.Iterator;
2424
import javax.annotation.Nonnull;
2525
import org.apache.commons.lang3.time.DurationFormatUtils;
26-
import org.slf4j.Logger;
27-
import org.slf4j.LoggerFactory;
2826

2927
/** @author shevek */
3028
public class ZonedIntervalIterable implements Iterable<ZonedInterval> {
31-
32-
@SuppressWarnings("UnusedVariable")
33-
private static final Logger logger = LoggerFactory.getLogger(ZonedIntervalIterable.class);
34-
3529
private final ZonedDateTime start;
3630
private final ZonedDateTime end;
37-
private final Duration duration;
31+
private final Duration intervalDuration;
3832

39-
/* pp */ ZonedIntervalIterable(
33+
ZonedIntervalIterable(
4034
@Nonnull ZonedDateTime start,
4135
@Nonnull ZonedDateTime end,
42-
@Nonnull Duration duration,
36+
@Nonnull Duration intervalDuration,
4337
IntervalExpander expander) {
44-
this.duration = Preconditions.checkNotNull(duration, "Duration was null.");
38+
this.intervalDuration =
39+
Preconditions.checkNotNull(intervalDuration, "Interval duration was null.");
4540
Preconditions.checkNotNull(start, "Start was null.");
4641
Preconditions.checkNotNull(end, "End was null.");
4742

4843
Preconditions.checkState(
49-
start.isBefore(end), "Start date %s must precede end date %s", start, end);
44+
start.isBefore(end), "Start date [%s] must precede end date [%s]", start, end);
5045

5146
ZonedInterval expandedInterval = expander.apply(new ZonedInterval(start, end));
5247

@@ -65,23 +60,23 @@ public ZonedDateTime getEnd() {
6560
}
6661

6762
@Nonnull
68-
public Duration getDuration() {
69-
return duration;
63+
public Duration getIntervalDuration() {
64+
return intervalDuration;
7065
}
7166

72-
private class Itr extends AbstractIterator<ZonedInterval> {
67+
private class DatesIterator extends AbstractIterator<ZonedInterval> {
7368

7469
private ZonedDateTime current;
7570

76-
public Itr() {
71+
public DatesIterator() {
7772
this.current = start;
7873
}
7974

8075
@Override
8176
protected ZonedInterval computeNext() {
8277
if (current.isEqual(end) || current.isAfter(end)) return endOfData();
8378

84-
ZonedDateTime next = current.plus(duration);
79+
ZonedDateTime next = current.plus(intervalDuration);
8580
if (next.isAfter(end)) return endOfData();
8681

8782
ZonedInterval result = new ZonedInterval(current, next);
@@ -93,13 +88,15 @@ protected ZonedInterval computeNext() {
9388
@Nonnull
9489
@Override
9590
public Iterator<ZonedInterval> iterator() {
96-
return new Itr();
91+
return new DatesIterator();
9792
}
9893

9994
@Override
10095
public String toString() {
10196
return String.format(
102-
"from %s to %s every %s",
103-
start, end, DurationFormatUtils.formatDurationWords(duration.toMillis(), true, true));
97+
"from [%s] to [%s] every [%s]ms",
98+
start,
99+
end,
100+
DurationFormatUtils.formatDurationWords(intervalDuration.toMillis(), true, true));
104101
}
105102
}

dumper/app/src/main/java/com/google/edwmigration/dumper/application/dumper/connector/ZonedIntervalIterableGenerator.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ public class ZonedIntervalIterableGenerator {
5656
return forTimeUnitsUntil(ZonedDateTime.now(ZoneOffset.UTC), unitCount, duration, expander);
5757
}
5858

59+
@Nonnull
60+
public static ZonedIntervalIterable forDateRangeWithIntervalDuration(
61+
ZonedDateTime startDate, ZonedDateTime endDate, Duration intervalDuration)
62+
throws MetadataDumperUsageException {
63+
return new ZonedIntervalIterable(
64+
startDate,
65+
endDate,
66+
intervalDuration,
67+
IntervalExpander.createBasedOnDuration(intervalDuration));
68+
}
69+
5970
/**
6071
* Builds a ZonedIntervalIterable from connector arguments, the intervals will all be one hour
6172
* long ({@link ChronoUnit#HOURS}) and have an inclusive starting datetime and exclusive ending

0 commit comments

Comments
 (0)