Skip to content

Commit 63485f3

Browse files
Merge pull request #117 from keenlabs/pbuchman-cached-datasets
Support for cached datasets
2 parents dc9755e + 6f6a218 commit 63485f3

29 files changed

+1749
-57
lines changed

README.md

Lines changed: 90 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ repositories {
2020
mavenCentral()
2121
}
2222
dependencies {
23-
compile 'io.keen:keen-client-api-java:5.3.0'
23+
compile 'io.keen:keen-client-api-java:5.4.0'
2424
}
2525
```
2626

2727
For Android, use:
2828

2929
```groovy
30-
compile 'io.keen:keen-client-api-android:5.3.0@aar'
30+
compile 'io.keen:keen-client-api-android:5.4.0@aar'
3131
```
3232

3333
### Maven
@@ -38,7 +38,7 @@ Paste the following snippet into your pom.xml:
3838
<dependency>
3939
<groupId>io.keen</groupId>
4040
<artifactId>keen-client-api-java</artifactId>
41-
<version>5.3.0</version>
41+
<version>5.4.0</version>
4242
</dependency>
4343
```
4444

@@ -271,7 +271,7 @@ like to use the query client then you will need to ensure that you also have the
271271

272272
```groovy
273273
dependencies {
274-
compile 'io.keen:keen-client-api-query:5.3.0'
274+
compile 'io.keen:keen-client-api-query:5.4.0'
275275
}
276276
```
277277

@@ -283,7 +283,7 @@ Paste the following snippet into your pom.xml:
283283
<dependency>
284284
<groupId>io.keen</groupId>
285285
<artifactId>keen-client-api-query</artifactId>
286-
<version>5.3.0</version>
286+
<version>5.4.0</version>
287287
</dependency>
288288
```
289289

@@ -610,6 +610,81 @@ updateEventCollection.put("query", queryUpdates);
610610
updateResponse = savedQueryApi.updateQuery("cached-query-name", updateEventCollection);
611611
```
612612

613+
### Cached Datasets
614+
615+
Cached Datasets are a powerful way for you to build applications with charts and tables that load instantly,
616+
even as your Streams volume grows. Conceptually similar to Cached Queries, a Cached Dataset additionally allows
617+
you to retrieve results indexed by properties like customer, cohort, article, campaign and more.
618+
619+
To work with [Cached Datasets](https://keen.io/docs/api/#cached-datasets), create a `KeenQueryClient` as normal,
620+
then use it to create a `CachedDatasets` implementation:
621+
622+
```java
623+
KeenQueryClient queryClient = ...;
624+
CachedDatasets cachedDatasets = queryClient.getCachedDatasetsClient();
625+
```
626+
627+
Please notice, that Cached Datasets are [Early Release](https://intercom.help/keen/setup-help/what-does-early-release-mean)
628+
and there still some major improvements pending (example: server side error handling).
629+
630+
#### Creating a Cached Dataset
631+
632+
Depending on your needs you can adjust the query. Please see [Cached Dataset API Reference](https://keen.io/docs/api/#cached-datasets/) for details.
633+
This example shows the most complicated usage pattern: multi analysis with multiple indexes.
634+
635+
```java
636+
String datasetName = "new-project-members";
637+
String displayName = "New Project Members";
638+
Collection<String> indexBy = asList("organization.id", "project.is_external");
639+
640+
DatasetQuery datasetQuery = DatasetQueryBuilder
641+
.aDatasetQuery()
642+
.withAnalysisType("multi_analysis")
643+
.withAnalyses(asList(new SubAnalysis("total count", QueryType.COUNT), new SubAnalysis("unique users", QueryType.SELECT_UNIQUE, "user.email")))
644+
.withEventCollection("new_project_member")
645+
.withFilters(singletonList(new Filter("organization.tag", FilterOperator.EQUAL_TO, "opensource")))
646+
.withTimeframe("this_6_months")
647+
.withTimezone("UTC")
648+
.withInterval("monthly")
649+
.build();
650+
651+
DatasetDefinition datasetDefinition = cachedDatasets.create(datasetName, displayName, datasetQuery, indexBy);
652+
```
653+
654+
#### Getting a Dataset Definition
655+
656+
```java
657+
DatasetDefinition datasetDefinition = cachedDatasets.getDefinition("new-project-members");
658+
```
659+
660+
#### Retrieving results from a Cached Dataset
661+
662+
```java
663+
DatasetDefinition datasetDefinition = ...
664+
665+
List<IntervalResultValue> results = cachedDatasets.getResults(
666+
datasetDefinition,
667+
new HashMap<String, Object>() {{
668+
put("organization.id", "the-penguins");
669+
put("project.is_external", true);
670+
}},
671+
new RelativeTimeframe("this_3_months")
672+
);
673+
```
674+
675+
#### Listing Cached Dataset Definitions for Project
676+
677+
```java
678+
List<DatasetDefinition> definitions = cachedDatasets.getDefinitions(30, "the-name-of-last-skipped-dataset");
679+
```
680+
681+
#### Deleting a Cached Dataset
682+
683+
```java
684+
boolean success = cachedDatasets.delete("new-project-members");
685+
```
686+
687+
613688
### Utility Methods
614689

615690
There are also some utility methods to add filters and timeframes to a `Query`:
@@ -765,6 +840,8 @@ If you are not trying to build the Android client, you can remove `android` from
765840

766841
sdk.dir=<Android SDK path>
767842

843+
If you use Intellij IDE, the `local.properties` file should be created in a root directory of the project.
844+
768845
#### "RuntimeException: Stub!" error in JUnit tests
769846

770847
This is usually caused by the Android SDK being before JUnit in your classpath. (Android includes a stubbed version of JUnit.) To fix this, move JUnit ahead of the Android SDK so it gets picked up first.
@@ -793,6 +870,14 @@ client.addEvent("collection-name", event, keenProperties);
793870

794871
## Changelog
795872

873+
##### 5.4.0
874+
875+
+ Added support for Cached Datasets
876+
+ Added toString, equals and hashCode for objects representing definitions and results of queries/datasets
877+
+ Replaced generation of KeenVersion.java with validation whether committed file got updated manually (faster development in Intellij)
878+
+ Added detailed, optional debug messages for both requests and responses.
879+
+ Minor, backward compatible refactoring
880+
796881
##### 5.3.0
797882

798883
+ Added withReadTimeout and withConnectTimeout methods to both KeenQueryClient.Builder and KeenClient.Builder

core/build.gradle

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,17 @@ tasks.withType(JavaCompile) {
1212
options.compilerArgs << "-source" << "1.6" << "-target" << "1.6"
1313
}
1414

15-
// Build up a KeenVersion.java akin to the Android BuildConfig.java, but available in all projects.
16-
// We set Implementation-Version on the Manifest, but reading Package.getImplementationVersion()
17-
// on the core Package seems return null in the Android client, so instead generate this. Also, the
18-
// Manifest info isn't set for unit tests.
19-
task generateVersionFile {
20-
def outputDir = file("$buildDir/generated/source")
21-
outputs.dir outputDir
15+
task verifyVersionFile {
16+
def versionFile = file("$projectDir/src/main/java/io/keen/client/java/KeenVersion.java")
2217

2318
doFirst {
24-
def srcFile = new File(outputDir, "io/keen/client/java/KeenVersion.java")
25-
srcFile.parentFile.mkdirs()
26-
srcFile.write("""
27-
package io.keen.client.java;
28-
public class KeenVersion {
29-
private static final String SDK_VERSION = "$project.version";
30-
31-
private KeenVersion() {}
32-
public static String getSdkVersion() { return SDK_VERSION; }
33-
}
34-
""")
19+
if (!versionFile.text.contains("SDK_VERSION = \"$project.version\"")) {
20+
throw new IllegalStateException("Project version set in KeenVersion.java does not match version set in gradle.properties")
21+
}
3522
}
3623
}
3724

38-
compileJava.dependsOn generateVersionFile
39-
compileJava.source generateVersionFile.outputs.files, sourceSets.main.java
25+
compileJava.dependsOn verifyVersionFile
4026

4127
dependencies {
4228
testCompile 'com.fasterxml.jackson.core:jackson-databind:2.3.0'
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.keen.client.java;
2+
3+
public class KeenVersion {
4+
5+
private static final String SDK_VERSION = "5.4.0";
6+
7+
private KeenVersion() {
8+
}
9+
10+
public static String getSdkVersion() {
11+
return SDK_VERSION;
12+
}
13+
14+
}

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
VERSION_NAME=5.3.0
2-
VERSION_CODE=15
1+
VERSION_NAME=5.4.0
2+
VERSION_CODE=16
33
GROUP=io.keen
44

55
POM_URL=www.keen.io

query/build.gradle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ tasks.withType(JavaCompile) {
77
options.compilerArgs << "-source" << "1.6" << "-target" << "1.6"
88
}
99

10+
project.evaluationDependsOn(":core")
11+
1012
dependencies {
1113
compile project(':core')
1214
compile project(':java')
15+
compile 'org.apache.commons:commons-lang3:3.8.1'
1316
testCompile 'com.fasterxml.jackson.core:jackson-databind:2.3.0'
14-
testCompile 'commons-io:commons-io:2.4'
1517
testCompile 'junit:junit:4.11'
1618
testCompile 'org.hamcrest:hamcrest-library:1.3'
1719
testCompile 'org.mockito:mockito-core:1.9.5'
20+
testCompile 'pl.pragmatists:JUnitParams:1.1.1'
1821
testCompile project(':core').sourceSets.test.output
1922
}
2023

query/src/main/java/io/keen/client/java/AbsoluteTimeframe.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package io.keen.client.java;
22

3+
import org.apache.commons.lang3.builder.EqualsBuilder;
4+
import org.apache.commons.lang3.builder.HashCodeBuilder;
5+
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
6+
import org.apache.commons.lang3.builder.ToStringStyle;
7+
38
import java.util.HashMap;
49
import java.util.Map;
510

@@ -62,4 +67,19 @@ public Map<String, Object> constructTimeframeArgs() {
6267

6368
return timeframe;
6469
}
70+
71+
@Override
72+
public String toString() {
73+
return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
74+
}
75+
76+
@Override
77+
public boolean equals(Object obj) {
78+
return EqualsBuilder.reflectionEquals(this, obj);
79+
}
80+
81+
@Override
82+
public int hashCode() {
83+
return HashCodeBuilder.reflectionHashCode(this);
84+
}
6585
}

0 commit comments

Comments
 (0)