use of com.ibm.watson.discovery.v1.model.GetMetricsQueryOptions in project java-sdk by watson-developer-cloud.
the class DiscoveryTest method testGetMetricsQueryWOptions.
// Test the getMetricsQuery operation with a valid options model parameter
@Test
public void testGetMetricsQueryWOptions() throws Throwable {
// Register a mock response
String mockResponseBody = "{\"aggregations\": [{\"interval\": \"interval\", \"event_type\": \"eventType\", \"results\": [{\"key_as_string\": \"2019-01-01T12:00:00.000Z\", \"key\": 3, \"matching_results\": 15, \"event_rate\": 9}]}]}";
String getMetricsQueryPath = "/v1/metrics/number_of_queries";
server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
// Construct an instance of the GetMetricsQueryOptions model
GetMetricsQueryOptions getMetricsQueryOptionsModel = new GetMetricsQueryOptions.Builder().startTime(DateUtils.parseAsDateTime("2019-01-01T12:00:00.000Z")).endTime(DateUtils.parseAsDateTime("2019-01-01T12:00:00.000Z")).resultType("document").build();
// Invoke getMetricsQuery() with a valid options model and verify the result
Response<MetricResponse> response = discoveryService.getMetricsQuery(getMetricsQueryOptionsModel).execute();
assertNotNull(response);
MetricResponse responseObj = response.getResult();
assertNotNull(responseObj);
// Verify the contents of the request sent to the mock server
RecordedRequest request = server.takeRequest();
assertNotNull(request);
assertEquals(request.getMethod(), "GET");
// Verify request path
String parsedPath = TestUtilities.parseReqPath(request);
assertEquals(parsedPath, getMetricsQueryPath);
// Verify query params
Map<String, String> query = TestUtilities.parseQueryString(request);
assertNotNull(query);
assertEquals(query.get("version"), "testString");
assertEquals(query.get("result_type"), "document");
}
use of com.ibm.watson.discovery.v1.model.GetMetricsQueryOptions in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceTest method getMetricsQueryIsSuccessful.
/**
* Gets the metrics query is successful.
*
* @throws InterruptedException the interrupted exception
*/
@Test
public void getMetricsQueryIsSuccessful() throws InterruptedException {
server.enqueue(jsonResponse(metricResp));
String interval = "1d";
Long key = 1533513600000L;
Double eventRate = 0.0;
GetMetricsQueryOptions options = new GetMetricsQueryOptions.Builder().startTime(date).endTime(date).resultType(GetMetricsQueryOptions.ResultType.DOCUMENT).build();
MetricResponse response = discoveryService.getMetricsQuery(options).execute().getResult();
RecordedRequest request = server.takeRequest();
assertEquals(GET, request.getMethod());
assertTrue(!response.getAggregations().isEmpty());
assertEquals(interval, response.getAggregations().get(0).getInterval());
assertEquals(CreateEventOptions.Type.CLICK, response.getAggregations().get(0).getEventType());
assertTrue(!response.getAggregations().get(0).getResults().isEmpty());
assertEquals(key, response.getAggregations().get(0).getResults().get(0).getKey());
assertNotNull(response.getAggregations().get(0).getResults().get(0).getKeyAsString());
assertEquals(eventRate, response.getAggregations().get(0).getResults().get(0).getEventRate());
}
use of com.ibm.watson.discovery.v1.model.GetMetricsQueryOptions in project java-sdk by watson-developer-cloud.
the class Discovery method getMetricsQuery.
/**
* Number of queries over time.
*
* <p>Total number of queries using the **natural_language_query** parameter over a specific time
* window.
*
* @param getMetricsQueryOptions the {@link GetMetricsQueryOptions} containing the options for the
* call
* @return a {@link ServiceCall} with a result of type {@link MetricResponse}
*/
public ServiceCall<MetricResponse> getMetricsQuery(GetMetricsQueryOptions getMetricsQueryOptions) {
if (getMetricsQueryOptions == null) {
getMetricsQueryOptions = new GetMetricsQueryOptions.Builder().build();
}
RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/metrics/number_of_queries"));
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v1", "getMetricsQuery");
for (Entry<String, String> header : sdkHeaders.entrySet()) {
builder.header(header.getKey(), header.getValue());
}
builder.header("Accept", "application/json");
builder.query("version", String.valueOf(this.version));
if (getMetricsQueryOptions.startTime() != null) {
builder.query("start_time", DateUtils.formatAsDateTime(getMetricsQueryOptions.startTime()));
}
if (getMetricsQueryOptions.endTime() != null) {
builder.query("end_time", DateUtils.formatAsDateTime(getMetricsQueryOptions.endTime()));
}
if (getMetricsQueryOptions.resultType() != null) {
builder.query("result_type", String.valueOf(getMetricsQueryOptions.resultType()));
}
ResponseConverter<MetricResponse> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<MetricResponse>() {
}.getType());
return createServiceCall(builder.build(), responseConverter);
}
Aggregations