Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
* KeenQueryClient provides all of the functionality required to execute the basic queries
* supported by the Data Analysis API.
*
This includes Count, Count Unique, Sum, Average, Maximum, Minimum, Median, Percentile,
* Select Unique, Funnel and Multi-Analysis. It does not include Extractions, Saved Queries or
* Query Caching.
*
* @author claireyoung, baumatron, masojus
* @since 1.0.0
*/
public class KeenQueryClient {
private static final String ENCODING = "UTF-8";
private final KeenJsonHandler jsonHandler;
private final RequestUrlBuilder requestUrlBuilder;
private final KeenProject project;
private final HttpHandler httpHandler;
/**
* Gets the default project that this {@link KeenQueryClient} is using.
*
* @return The {@link KeenProject}.
*/
public KeenProject getProject() {
return project;
}
/**
* Count query with only the required arguments.
* Query API info here: https://keen.io/docs/api/#count
*
* @param eventCollection The name of the event collection you are analyzing.
* @param timeframe The {@link RelativeTimeframe} or {@link AbsoluteTimeframe}.
* @return the count query response.
* @throws IOException If there was an error communicating with the server or
* an error message received from the server.
*/
public long count(String eventCollection, Timeframe timeframe) throws IOException {
Query queryParams = new Query.Builder(QueryType.COUNT)
.withEventCollection(eventCollection)
.withTimeframe(timeframe)
.build();
QueryResult result = execute(queryParams);
return queryResultToLong(result);
}
/**
* Count Unique query with only the required arguments.
* Query API info here: https://keen.io/docs/api/#count-unique
*
* @param eventCollection The name of the event collection you are analyzing.
* @param targetProperty The name of the property you are analyzing.
* @param timeframe The {@link RelativeTimeframe} or {@link AbsoluteTimeframe}.
* @return The count unique query response.
* @throws IOException If there was an error communicating with the server or
* an error message received from the server.
*/
public long countUnique(String eventCollection, String targetProperty, Timeframe timeframe) throws IOException {
Query queryParams = new Query.Builder(QueryType.COUNT_UNIQUE)
.withEventCollection(eventCollection)
.withTargetProperty(targetProperty)
.withTimeframe(timeframe)
.build();
QueryResult result = execute(queryParams);
return queryResultToLong(result);
}
/**
* Minimum query with only the required arguments.
* Query API info here: https://keen.io/docs/api/reference/#minimum-resource
*
* @param eventCollection The name of the event collection you are analyzing.
* @param targetProperty The name of the property you are analyzing.
* @param timeframe The {@link RelativeTimeframe} or {@link AbsoluteTimeframe}.
* @return The minimum query response.
* @throws IOException If there was an error communicating with the server or
* an error message received from the server.
*/
public double minimum(String eventCollection, String targetProperty, Timeframe timeframe) throws IOException {
Query queryParams = new Query.Builder(QueryType.MINIMUM)
.withEventCollection(eventCollection)
.withTargetProperty(targetProperty)
.withTimeframe(timeframe)
.build();
QueryResult result = execute(queryParams);
return queryResultToDouble(result);
}
/**
* Maximum query with only the required arguments.
* Query API info here: https://keen.io/docs/api/reference/#maximum-resource
*
* @param eventCollection The name of the event collection you are analyzing.
* @param targetProperty The name of the property you are analyzing.
* @param timeframe The {@link RelativeTimeframe} or {@link AbsoluteTimeframe}.
* @return The response from the server in the "result" map.
* @throws IOException If there was an error communicating with the server or
* an error message received from the server.
*/
public double maximum(String eventCollection, String targetProperty, Timeframe timeframe) throws IOException {
Query queryParams = new Query.Builder(QueryType.MAXIMUM)
.withEventCollection(eventCollection)
.withTargetProperty(targetProperty)
.withTimeframe(timeframe)
.build();
QueryResult result = execute(queryParams);
return queryResultToDouble(result);
}
/**
* Average query with only the required arguments.
* Query API info here: https://keen.io/docs/api/reference/#average-resource
*
* @param eventCollection The name of the event collection you are analyzing.
* @param targetProperty The name of the property you are analyzing.
* @param timeframe The {@link RelativeTimeframe} or {@link AbsoluteTimeframe}.
* @return The average query response.
* @throws IOException If there was an error communicating with the server or
* an error message received from the server.
*/
public double average(String eventCollection, String targetProperty, Timeframe timeframe) throws IOException {
Query queryParams = new Query.Builder(QueryType.AVERAGE)
.withEventCollection(eventCollection)
.withTargetProperty(targetProperty)
.withTimeframe(timeframe)
.build();
QueryResult result = execute(queryParams);
return queryResultToDouble(result);
}
/**
* Median query with only the required arguments.
* Query API info here: https://keen.io/docs/api/reference/#median-resource
*
* @param eventCollection The name of the event collection you are analyzing.
* @param targetProperty The name of the property you are analyzing.
* @param timeframe The {@link RelativeTimeframe} or {@link AbsoluteTimeframe}.
* @return The median query response.
* @throws IOException If there was an error communicating with the server or
* an error message received from the server.
*/
public double median(String eventCollection, String targetProperty, Timeframe timeframe) throws IOException {
Query queryParams = new Query.Builder(QueryType.MEDIAN)
.withEventCollection(eventCollection)
.withTargetProperty(targetProperty)
.withTimeframe(timeframe)
.build();
QueryResult result = execute(queryParams);
return queryResultToDouble(result);
}
/**
* Percentile query with only the required arguments.
* Query API info here: https://keen.io/docs/api/reference/#percentile-resource
*
* @param eventCollection The name of the event collection you are analyzing.
* @param targetProperty The name of the property you are analyzing.
* @param percentile The percentile.
* @param timeframe The {@link RelativeTimeframe} or {@link AbsoluteTimeframe}.
* @return The percentile query response.
* @throws IOException If there was an error communicating with the server or
* an error message received from the server.
*/
public double percentile(String eventCollection, String targetProperty, Double percentile, Timeframe timeframe) throws IOException {
Query queryParams = new Query.Builder(QueryType.PERCENTILE)
.withEventCollection(eventCollection)
.withTargetProperty(targetProperty)
.withPercentile(percentile)
.withTimeframe(timeframe)
.build();
QueryResult result = execute(queryParams);
return queryResultToDouble(result);
}
/**
* Sum query with only the required arguments.
* Query API info here: https://keen.io/docs/api/reference/#sum-resource
*
* @param eventCollection The name of the event collection you are analyzing.
* @param targetProperty The name of the property you are analyzing.
* @param timeframe The {@link RelativeTimeframe} or {@link AbsoluteTimeframe}.
* @return The sum response.
* @throws IOException If there was an error communicating with the server or
* an error message received from the server.
*/
public double sum(String eventCollection, String targetProperty, Timeframe timeframe) throws IOException {
Query queryParams = new Query.Builder(QueryType.SUM)
.withEventCollection(eventCollection)
.withTargetProperty(targetProperty)
.withTimeframe(timeframe)
.build();
QueryResult result = execute(queryParams);
return queryResultToDouble(result);
}
/**
* Select Unique query with only the required arguments.
* Query API info here: https://keen.io/docs/api/reference/#select-unique-resource
*
* @param eventCollection The name of the event collection you are analyzing.
* @param targetProperty The name of the property you are analyzing.
* @param timeframe The {@link RelativeTimeframe} or {@link AbsoluteTimeframe}.
* @return The select unique query response.
* @throws IOException If there was an error communicating with the server or
* an error message received from the server.
*/
public QueryResult selectUnique(String eventCollection, String targetProperty, Timeframe timeframe) throws IOException {
Query queryParams = new Query.Builder(QueryType.SELECT_UNIQUE)
.withEventCollection(eventCollection)
.withTargetProperty(targetProperty)
.withTimeframe(timeframe)
.build();
QueryResult result = execute(queryParams);
return result;
}
/**
* This is the most flexible way to run a query. Use {@link Builder} to
* build all the query arguments to run the query.
*
* @param request The {@link KeenQueryRequest} to be executed.
* @return The {@link QueryResult} result.
* @throws IOException If there was an error communicating with the server or
* an error message received from the server.
*/
public QueryResult execute(KeenQueryRequest request) throws IOException {
Map response = getMapResponse(request);
return rawMapResponseToQueryResult(request, response);
}
/**
* Provides an implementation of {@link SavedQueries} for performing operations against
* the Saved/Cached Query API endpoints.
*
* @return The SavedQueries implementation.
*/
public SavedQueries getSavedQueriesInterface() {
return new SavedQueriesImpl(this);
}
Map getMapResponse(KeenQueryRequest request) throws IOException {
@SuppressWarnings("unchecked")
Map response = getResponse(Map.class, request);
return response;
}
List