Search in sources :

Example 1 with GetMetricsRequest

use of com.google.recaptchaenterprise.v1.GetMetricsRequest in project java-recaptchaenterprise by googleapis.

the class GetMetrics method getMetrics.

/**
 * Get metrics specific to a recaptcha site key. E.g: score bucket count for a key or number of
 * times the checkbox key failed/ passed etc.,
 *
 * @param projectId: Google Cloud Project Id.
 * @param recaptchaSiteKey: Specify the site key to get metrics.
 */
public static void getMetrics(String projectId, String recaptchaSiteKey) throws IOException {
    // clean up any remaining background resources.
    try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {
        GetMetricsRequest getMetricsRequest = GetMetricsRequest.newBuilder().setName(MetricsName.of(projectId, recaptchaSiteKey).toString()).build();
        Metrics response = client.getMetrics(getMetricsRequest);
        // response.getScoreMetricsList()
        for (ScoreMetrics scoreMetrics : response.getScoreMetricsList()) {
            // Each ScoreMetrics is in the granularity of one day.
            int scoreBucketCount = scoreMetrics.getOverallMetrics().getScoreBucketsCount();
            System.out.println(scoreBucketCount);
        }
        System.out.printf("Retrieved the bucket count for score based key: %s", recaptchaSiteKey);
    }
}
Also used : Metrics(com.google.recaptchaenterprise.v1.Metrics) ScoreMetrics(com.google.recaptchaenterprise.v1.ScoreMetrics) ScoreMetrics(com.google.recaptchaenterprise.v1.ScoreMetrics) GetMetricsRequest(com.google.recaptchaenterprise.v1.GetMetricsRequest) RecaptchaEnterpriseServiceClient(com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient)

Example 2 with GetMetricsRequest

use of com.google.recaptchaenterprise.v1.GetMetricsRequest in project java-recaptchaenterprise by googleapis.

the class RecaptchaEnterpriseServiceClientTest method getMetricsTest2.

@Test
public void getMetricsTest2() throws Exception {
    Metrics expectedResponse = Metrics.newBuilder().setName(MetricsName.of("[PROJECT]", "[KEY]").toString()).setStartTime(Timestamp.newBuilder().build()).addAllScoreMetrics(new ArrayList<ScoreMetrics>()).addAllChallengeMetrics(new ArrayList<ChallengeMetrics>()).build();
    mockRecaptchaEnterpriseService.addResponse(expectedResponse);
    String name = "name3373707";
    Metrics actualResponse = client.getMetrics(name);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockRecaptchaEnterpriseService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    GetMetricsRequest actualRequest = ((GetMetricsRequest) actualRequests.get(0));
    Assert.assertEquals(name, actualRequest.getName());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : ChallengeMetrics(com.google.recaptchaenterprise.v1.ChallengeMetrics) ScoreMetrics(com.google.recaptchaenterprise.v1.ScoreMetrics) Metrics(com.google.recaptchaenterprise.v1.Metrics) ScoreMetrics(com.google.recaptchaenterprise.v1.ScoreMetrics) AbstractMessage(com.google.protobuf.AbstractMessage) ArrayList(java.util.ArrayList) GetMetricsRequest(com.google.recaptchaenterprise.v1.GetMetricsRequest) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 3 with GetMetricsRequest

use of com.google.recaptchaenterprise.v1.GetMetricsRequest in project java-recaptchaenterprise by googleapis.

the class RecaptchaEnterpriseServiceClientTest method getMetricsTest.

@Test
public void getMetricsTest() throws Exception {
    Metrics expectedResponse = Metrics.newBuilder().setName(MetricsName.of("[PROJECT]", "[KEY]").toString()).setStartTime(Timestamp.newBuilder().build()).addAllScoreMetrics(new ArrayList<ScoreMetrics>()).addAllChallengeMetrics(new ArrayList<ChallengeMetrics>()).build();
    mockRecaptchaEnterpriseService.addResponse(expectedResponse);
    MetricsName name = MetricsName.of("[PROJECT]", "[KEY]");
    Metrics actualResponse = client.getMetrics(name);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockRecaptchaEnterpriseService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    GetMetricsRequest actualRequest = ((GetMetricsRequest) actualRequests.get(0));
    Assert.assertEquals(name.toString(), actualRequest.getName());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : ChallengeMetrics(com.google.recaptchaenterprise.v1.ChallengeMetrics) ScoreMetrics(com.google.recaptchaenterprise.v1.ScoreMetrics) Metrics(com.google.recaptchaenterprise.v1.Metrics) ScoreMetrics(com.google.recaptchaenterprise.v1.ScoreMetrics) AbstractMessage(com.google.protobuf.AbstractMessage) ArrayList(java.util.ArrayList) GetMetricsRequest(com.google.recaptchaenterprise.v1.GetMetricsRequest) MetricsName(com.google.recaptchaenterprise.v1.MetricsName) Test(org.junit.Test)

Aggregations

GetMetricsRequest (com.google.recaptchaenterprise.v1.GetMetricsRequest)3 Metrics (com.google.recaptchaenterprise.v1.Metrics)3 ScoreMetrics (com.google.recaptchaenterprise.v1.ScoreMetrics)3 AbstractMessage (com.google.protobuf.AbstractMessage)2 ChallengeMetrics (com.google.recaptchaenterprise.v1.ChallengeMetrics)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 RecaptchaEnterpriseServiceClient (com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient)1 ByteString (com.google.protobuf.ByteString)1 MetricsName (com.google.recaptchaenterprise.v1.MetricsName)1