Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement method for finding coarsest ZonedTimeGrain #230

Merged
merged 6 commits into from
Apr 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ pull request if there was one.
Current
-------
### Added:
- [Method for finding coarsest ZonedTimeGrain](https://github.com/yahoo/fili/pull/230)
* Added utility method for returning coarsest `ZonedTimeGrain` from a collection of `ZonedTimeGrain`s. This is
useful to construct composite tables that requires the coarsest `ZonedTimeGrain` among a set of tables.

- [Should also setConnectTimeout when using setReadTimeout](https://github.com/yahoo/fili/pull/231)
* Setting connectTimeout on DefaultAsyncHttpClientConfig when building AsyncDruidWebServiceImpl

- [CompositePhysicalTable Core Components Refactor](https://github.com/yahoo/fili/pull/179)
* Added `ConcretePhysicalTable` and `ConcreteAvailability` to model table in druid datasource and its availabillity in the new table availability structure
* Added class variable for `DataSourceMetadataService` and `ConfigurationLoader` into `AbstractBinderFactory` for application to access
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

import com.yahoo.bard.webservice.config.SystemConfig;
import com.yahoo.bard.webservice.config.SystemConfigProvider;
import com.yahoo.bard.webservice.data.time.ZonedTimeGrain;
import com.yahoo.bard.webservice.druid.model.query.Granularity;
import com.yahoo.bard.webservice.table.PhysicalTable;
import com.yahoo.bard.webservice.table.PhysicalTableSchema;
import com.yahoo.bard.webservice.table.resolver.GranularityComparator;

import org.joda.time.DateTime;
import org.joda.time.Duration;
Expand All @@ -30,6 +34,8 @@
*/
public class IntervalUtils {

private static final GranularityComparator COMPARATOR = GranularityComparator.getInstance();

public static final SystemConfig SYSTEM_CONFIG = SystemConfigProvider.getInstance();

public static final String ALIGNMENT_EPOCH_STRING = SYSTEM_CONFIG.getStringProperty(
Expand Down Expand Up @@ -209,4 +215,19 @@ public static Optional<DateTime> firstMoment(Collection<? extends Collection<Int
.map(Interval::getStart)
.reduce(Utils::getMinValue);
}

/**
* Returns the coarsest ZonedTimeGrain among a set of PhysicalTables.
*
* @param physicalTables A set of PhysicalTables among which the coarsest ZonedTimeGrain is to be found.
*
* @return the coarsest ZonedTimeGrain among a set of PhysicalTables
*/
public static Optional<ZonedTimeGrain> getCoarsestTimeGrain(Collection<PhysicalTable> physicalTables) {
return physicalTables.stream()
.sorted(COMPARATOR)
.findFirst()
.map(PhysicalTable::getSchema)
.map(PhysicalTableSchema::getTimeGrain);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
package com.yahoo.bard.webservice.util

import static com.yahoo.bard.webservice.data.time.DefaultTimeGrain.DAY
import static com.yahoo.bard.webservice.data.time.DefaultTimeGrain.HOUR
import static com.yahoo.bard.webservice.data.time.DefaultTimeGrain.MINUTE
import static com.yahoo.bard.webservice.data.time.DefaultTimeGrain.MONTH
import static com.yahoo.bard.webservice.data.time.DefaultTimeGrain.QUARTER
import static com.yahoo.bard.webservice.data.time.DefaultTimeGrain.WEEK
import static com.yahoo.bard.webservice.data.time.DefaultTimeGrain.YEAR

import com.yahoo.bard.webservice.data.time.ZonedTimeGrain
import com.yahoo.bard.webservice.druid.model.query.AllGranularity
import com.yahoo.bard.webservice.druid.model.query.Granularity
import com.yahoo.bard.webservice.table.PhysicalTable
import com.yahoo.bard.webservice.table.PhysicalTableSchema

import org.joda.time.DateTime
import org.joda.time.DateTimeZone
Expand Down Expand Up @@ -326,6 +332,51 @@ class IntervalUtilsSpec extends Specification {
DAY | ["2012-02-03/2017-04-04"] | ["2012-02-03/2012-05-04", "2017-02-03/2017-04-04"]
}

@Unroll
def "getCoarsestTimeGrain returns coarsest grain in the case of #description"() {
given:
PhysicalTableSchema schema1 = Mock(PhysicalTableSchema)
PhysicalTableSchema schema2 = Mock(PhysicalTableSchema)
PhysicalTableSchema schema3 = Mock(PhysicalTableSchema)

schema1.getTimeGrain() >> new ZonedTimeGrain(timeGrain1, DateTimeZone.forID(timeZone1))
schema2.getTimeGrain() >> new ZonedTimeGrain(timeGrain2, DateTimeZone.forID(timeZone2))
schema3.getTimeGrain() >> new ZonedTimeGrain(timeGrain3, DateTimeZone.forID(timeZone3))

PhysicalTable physicalTable1 = Mock(PhysicalTable)
PhysicalTable physicalTable2 = Mock(PhysicalTable)
PhysicalTable physicalTable3 = Mock(PhysicalTable)

physicalTable1.getSchema() >> schema1
physicalTable2.getSchema() >> schema2
physicalTable3.getSchema() >> schema3

expect:
IntervalUtils.getCoarsestTimeGrain(
[
physicalTable1,
physicalTable2,
physicalTable3
]
).get() == new ZonedTimeGrain(expectedTimeGrain, DateTimeZone.forID(expectedTimeZone))

where:
timeGrain1 | timeGrain2 | timeGrain3 | timeZone1 | timeZone2 | timeZone3 | expectedTimeGrain | expectedTimeZone | description
DAY | DAY | DAY | 'America/Chicago' | 'America/Los_Angeles' | 'America/Phoenix' | DAY | 'America/Chicago' | 'same grain but different time zones'
MINUTE | HOUR | DAY | 'America/Phoenix' | 'America/Phoenix' | 'America/Phoenix' | DAY | 'America/Phoenix' | 'same time zone but different grans'
MINUTE | HOUR | DAY | 'America/Chicago' | 'America/Los_Angeles' | 'America/Phoenix' | DAY | 'America/Phoenix' | 'different grains, with DAY as the coarsest grain, and different time zones'
HOUR | DAY | WEEK | 'America/Chicago' | 'America/Los_Angeles' | 'America/Phoenix' | WEEK | 'America/Phoenix' | 'different grains, with WEEK as the coarsest grain, and different time zones'
DAY | WEEK | MONTH | 'America/Chicago' | 'America/Los_Angeles' | 'America/Phoenix' | MONTH | 'America/Phoenix' | 'different grains, with MONTH as the coarsest grain, and different time zones'
WEEK | MONTH | QUARTER | 'America/Chicago' | 'America/Los_Angeles' | 'America/Phoenix' | QUARTER | 'America/Phoenix' | 'different grains, with QUARTER as the coarsest grain, and different time zones'
MONTH | QUARTER | YEAR | 'America/Chicago' | 'America/Los_Angeles' | 'America/Phoenix' | YEAR | 'America/Phoenix' | 'different grains, with YEAR as the coarsest grain, and different time zones'

}

def "getCoarsestTimeGrain returns empty on empty input physical table collections"() {
expect:
IntervalUtils.getCoarsestTimeGrain([]) == Optional.empty()
}

/**
* Returns the instant at which this year began.
*
Expand Down