Search in sources :

Example 11 with MetricResponse

use of com.ibm.watson.discovery.v1.model.MetricResponse in project java-sdk by watson-developer-cloud.

the class Discovery method getMetricsEventRate.

/**
 * Percentage of queries with an associated event.
 *
 * <p>The percentage of queries using the **natural_language_query** parameter that have a
 * corresponding "click" event over a specified time window. This metric requires having
 * integrated event tracking in your application using the **Events** API.
 *
 * @param getMetricsEventRateOptions the {@link GetMetricsEventRateOptions} containing the options
 *     for the call
 * @return a {@link ServiceCall} with a result of type {@link MetricResponse}
 */
public ServiceCall<MetricResponse> getMetricsEventRate(GetMetricsEventRateOptions getMetricsEventRateOptions) {
    if (getMetricsEventRateOptions == null) {
        getMetricsEventRateOptions = new GetMetricsEventRateOptions.Builder().build();
    }
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/metrics/event_rate"));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v1", "getMetricsEventRate");
    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 (getMetricsEventRateOptions.startTime() != null) {
        builder.query("start_time", DateUtils.formatAsDateTime(getMetricsEventRateOptions.startTime()));
    }
    if (getMetricsEventRateOptions.endTime() != null) {
        builder.query("end_time", DateUtils.formatAsDateTime(getMetricsEventRateOptions.endTime()));
    }
    if (getMetricsEventRateOptions.resultType() != null) {
        builder.query("result_type", String.valueOf(getMetricsEventRateOptions.resultType()));
    }
    ResponseConverter<MetricResponse> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<MetricResponse>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) MetricResponse(com.ibm.watson.discovery.v1.model.MetricResponse) RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder)

Example 12 with MetricResponse

use of com.ibm.watson.discovery.v1.model.MetricResponse in project java-sdk by watson-developer-cloud.

the class DiscoveryServiceTest method getMetricsEventRateIsSuccessful.

/**
 * Gets the metrics event rate is successful.
 *
 * @throws InterruptedException the interrupted exception
 */
@Test
public void getMetricsEventRateIsSuccessful() throws InterruptedException {
    server.enqueue(jsonResponse(metricResp));
    String interval = "1d";
    Long key = 1533513600000L;
    Double eventRate = 0.0;
    GetMetricsEventRateOptions options = new GetMetricsEventRateOptions.Builder().startTime(date).endTime(date).resultType(GetMetricsEventRateOptions.ResultType.DOCUMENT).build();
    MetricResponse response = discoveryService.getMetricsEventRate(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());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) GetMetricsEventRateOptions(com.ibm.watson.discovery.v1.model.GetMetricsEventRateOptions) MetricResponse(com.ibm.watson.discovery.v1.model.MetricResponse) WatsonServiceUnitTest(com.ibm.watson.common.WatsonServiceUnitTest)

Example 13 with MetricResponse

use of com.ibm.watson.discovery.v1.model.MetricResponse in project java-sdk by watson-developer-cloud.

the class DiscoveryServiceTest method getMetricsQueryNoArgsIsSuccessful.

/**
 * Gets the metrics query no args is successful.
 *
 * @throws InterruptedException the interrupted exception
 */
@Test
public void getMetricsQueryNoArgsIsSuccessful() throws InterruptedException {
    server.enqueue(jsonResponse(metricResp));
    String interval = "1d";
    Long key = 1533513600000L;
    Double eventRate = 0.0;
    MetricResponse response = discoveryService.getMetricsQuery().execute().getResult();
    RecordedRequest request = server.takeRequest();
    assertEquals(GET_METRICS_QUERY_PATH, request.getPath());
    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());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MetricResponse(com.ibm.watson.discovery.v1.model.MetricResponse) WatsonServiceUnitTest(com.ibm.watson.common.WatsonServiceUnitTest)

Example 14 with MetricResponse

use of com.ibm.watson.discovery.v1.model.MetricResponse in project java-sdk by watson-developer-cloud.

the class DiscoveryTest method testGetMetricsEventRateWOptions.

// Test the getMetricsEventRate operation with a valid options model parameter
@Test
public void testGetMetricsEventRateWOptions() 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 getMetricsEventRatePath = "/v1/metrics/event_rate";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    // Construct an instance of the GetMetricsEventRateOptions model
    GetMetricsEventRateOptions getMetricsEventRateOptionsModel = new GetMetricsEventRateOptions.Builder().startTime(DateUtils.parseAsDateTime("2019-01-01T12:00:00.000Z")).endTime(DateUtils.parseAsDateTime("2019-01-01T12:00:00.000Z")).resultType("document").build();
    // Invoke getMetricsEventRate() with a valid options model and verify the result
    Response<MetricResponse> response = discoveryService.getMetricsEventRate(getMetricsEventRateOptionsModel).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, getMetricsEventRatePath);
    // Verify query params
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNotNull(query);
    assertEquals(query.get("version"), "testString");
    assertEquals(query.get("result_type"), "document");
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) GetMetricsEventRateOptions(com.ibm.watson.discovery.v1.model.GetMetricsEventRateOptions) MetricResponse(com.ibm.watson.discovery.v1.model.MetricResponse) Test(org.testng.annotations.Test)

Example 15 with MetricResponse

use of com.ibm.watson.discovery.v1.model.MetricResponse in project java-sdk by watson-developer-cloud.

the class Discovery method getMetricsQueryEvent.

/**
 * Number of queries with an event over time.
 *
 * <p>Total number of queries using the **natural_language_query** parameter that have a
 * corresponding "click" event over a specified time window. This metric requires having
 * integrated event tracking in your application using the **Events** API.
 *
 * @param getMetricsQueryEventOptions the {@link GetMetricsQueryEventOptions} containing the
 *     options for the call
 * @return a {@link ServiceCall} with a result of type {@link MetricResponse}
 */
public ServiceCall<MetricResponse> getMetricsQueryEvent(GetMetricsQueryEventOptions getMetricsQueryEventOptions) {
    if (getMetricsQueryEventOptions == null) {
        getMetricsQueryEventOptions = new GetMetricsQueryEventOptions.Builder().build();
    }
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/metrics/number_of_queries_with_event"));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v1", "getMetricsQueryEvent");
    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 (getMetricsQueryEventOptions.startTime() != null) {
        builder.query("start_time", DateUtils.formatAsDateTime(getMetricsQueryEventOptions.startTime()));
    }
    if (getMetricsQueryEventOptions.endTime() != null) {
        builder.query("end_time", DateUtils.formatAsDateTime(getMetricsQueryEventOptions.endTime()));
    }
    if (getMetricsQueryEventOptions.resultType() != null) {
        builder.query("result_type", String.valueOf(getMetricsQueryEventOptions.resultType()));
    }
    ResponseConverter<MetricResponse> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<MetricResponse>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) MetricResponse(com.ibm.watson.discovery.v1.model.MetricResponse) RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder)

Aggregations

MetricResponse (com.ibm.watson.discovery.v1.model.MetricResponse)16 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)12 WatsonServiceUnitTest (com.ibm.watson.common.WatsonServiceUnitTest)8 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)4 MockResponse (okhttp3.mockwebserver.MockResponse)4 Test (org.testng.annotations.Test)4 GetMetricsEventRateOptions (com.ibm.watson.discovery.v1.model.GetMetricsEventRateOptions)2 GetMetricsQueryEventOptions (com.ibm.watson.discovery.v1.model.GetMetricsQueryEventOptions)2 GetMetricsQueryNoResultsOptions (com.ibm.watson.discovery.v1.model.GetMetricsQueryNoResultsOptions)2 GetMetricsQueryOptions (com.ibm.watson.discovery.v1.model.GetMetricsQueryOptions)2