Search in sources :

Example 41 with TimeSeries

use of com.google.api.ads.admanager.axis.v202111.TimeSeries in project googleads-java-lib by googleads.

the class GetTrafficData method runExample.

/**
 * Runs the example.
 *
 * @param adManagerServices the services factory.
 * @param session the session.
 * @param advertiserId the ID of the advertiser (company) to forecast for. Setting an advertiser
 *     will cause the forecast to apply the appropriate unified blocking rules.
 * @throws ApiException if the API request failed with one or more service errors.
 * @throws RemoteException if the API request failed due to other errors.
 */
public static void runExample(AdManagerServices adManagerServices, AdManagerSession session) throws RemoteException {
    // Get the ForecastService.
    ForecastServiceInterface forecastService = adManagerServices.get(session, ForecastServiceInterface.class);
    // Get the NetworkService.
    NetworkServiceInterface networkService = adManagerServices.get(session, NetworkServiceInterface.class);
    // Get the root ad unit ID used to target the whole site.
    String rootAdUnitId = networkService.getCurrentNetwork().getEffectiveRootAdUnitId();
    // Create inventory targeting.
    InventoryTargeting inventoryTargeting = new InventoryTargeting();
    // Create ad unit targeting for the root ad unit.
    AdUnitTargeting adUnitTargeting = new AdUnitTargeting();
    adUnitTargeting.setAdUnitId(rootAdUnitId);
    adUnitTargeting.setIncludeDescendants(true);
    inventoryTargeting.setTargetedAdUnits(new AdUnitTargeting[] { adUnitTargeting });
    // Create targeting.
    Targeting targeting = new Targeting();
    targeting.setInventoryTargeting(inventoryTargeting);
    // Create the date range. Include the previous and next 7 days.
    Interval interval = new Interval(Instant.now().plus(Duration.standardDays(-7)), Instant.now().plus(Duration.standardDays(7)));
    DateRange dateRange = new DateRange();
    dateRange.setStartDate(DateTimes.toDateTime(interval.getStart()).getDate());
    dateRange.setEndDate(DateTimes.toDateTime(interval.getEnd()).getDate());
    // Request the traffic data.
    TrafficDataRequest trafficDataRequest = new TrafficDataRequest();
    trafficDataRequest.setRequestedDateRange(dateRange);
    trafficDataRequest.setTargeting(targeting);
    TrafficDataResponse trafficData = forecastService.getTrafficData(trafficDataRequest);
    // Read the historical traffic data.
    TimeSeries historicalTimeSeries = trafficData.getHistoricalTimeSeries();
    if (historicalTimeSeries != null) {
        Date historicalStartDate = historicalTimeSeries.getTimeSeriesDateRange().getStartDate();
        DateTime historicalStart = new DateTime(historicalStartDate.getYear(), historicalStartDate.getMonth(), historicalStartDate.getDay(), 0, 0, 0);
        for (int i = 0; i < historicalTimeSeries.getValues().length; i++) {
            System.out.printf("%s: %d historical ad opportunities%n", historicalStart.plus(Duration.standardDays(i)).toString(DateTimeFormat.longDate()), historicalTimeSeries.getValues()[i]);
        }
    }
    // Read the forecasted traffic data.
    TimeSeries forecastedTimeSeries = trafficData.getForecastedTimeSeries();
    if (forecastedTimeSeries != null) {
        Date forecastedStartDate = forecastedTimeSeries.getTimeSeriesDateRange().getStartDate();
        DateTime forecastedStart = new DateTime(forecastedStartDate.getYear(), forecastedStartDate.getMonth(), forecastedStartDate.getDay(), 0, 0, 0);
        for (int i = 0; i < forecastedTimeSeries.getValues().length; i++) {
            System.out.printf("%s: %d forecasted ad opportunities%n", forecastedStart.plus(Duration.standardDays(i)).toString(DateTimeFormat.longDate()), forecastedTimeSeries.getValues()[i]);
        }
    }
}
Also used : TrafficDataResponse(com.google.api.ads.admanager.axis.v202111.TrafficDataResponse) NetworkServiceInterface(com.google.api.ads.admanager.axis.v202111.NetworkServiceInterface) TimeSeries(com.google.api.ads.admanager.axis.v202111.TimeSeries) Targeting(com.google.api.ads.admanager.axis.v202111.Targeting) InventoryTargeting(com.google.api.ads.admanager.axis.v202111.InventoryTargeting) AdUnitTargeting(com.google.api.ads.admanager.axis.v202111.AdUnitTargeting) InventoryTargeting(com.google.api.ads.admanager.axis.v202111.InventoryTargeting) Date(com.google.api.ads.admanager.axis.v202111.Date) DateTime(org.joda.time.DateTime) ForecastServiceInterface(com.google.api.ads.admanager.axis.v202111.ForecastServiceInterface) DateRange(com.google.api.ads.admanager.axis.v202111.DateRange) AdUnitTargeting(com.google.api.ads.admanager.axis.v202111.AdUnitTargeting) TrafficDataRequest(com.google.api.ads.admanager.axis.v202111.TrafficDataRequest) Interval(org.joda.time.Interval)

Example 42 with TimeSeries

use of com.google.api.ads.admanager.axis.v202111.TimeSeries in project googleads-java-lib by googleads.

the class GetTrafficData method runExample.

/**
 * Runs the example.
 *
 * @param adManagerServices the services factory.
 * @param session the session.
 * @param advertiserId the ID of the advertiser (company) to forecast for. Setting an advertiser
 *     will cause the forecast to apply the appropriate unified blocking rules.
 * @throws ApiException if the API request failed with one or more service errors.
 * @throws RemoteException if the API request failed due to other errors.
 */
public static void runExample(AdManagerServices adManagerServices, AdManagerSession session) throws RemoteException {
    // Get the ForecastService.
    ForecastServiceInterface forecastService = adManagerServices.get(session, ForecastServiceInterface.class);
    // Get the NetworkService.
    NetworkServiceInterface networkService = adManagerServices.get(session, NetworkServiceInterface.class);
    // Get the root ad unit ID used to target the whole site.
    String rootAdUnitId = networkService.getCurrentNetwork().getEffectiveRootAdUnitId();
    // Create inventory targeting.
    InventoryTargeting inventoryTargeting = new InventoryTargeting();
    // Create ad unit targeting for the root ad unit.
    AdUnitTargeting adUnitTargeting = new AdUnitTargeting();
    adUnitTargeting.setAdUnitId(rootAdUnitId);
    adUnitTargeting.setIncludeDescendants(true);
    inventoryTargeting.setTargetedAdUnits(new AdUnitTargeting[] { adUnitTargeting });
    // Create targeting.
    Targeting targeting = new Targeting();
    targeting.setInventoryTargeting(inventoryTargeting);
    // Create the date range. Include the previous and next 7 days.
    Interval interval = new Interval(Instant.now().plus(Duration.standardDays(-7)), Instant.now().plus(Duration.standardDays(7)));
    DateRange dateRange = new DateRange();
    dateRange.setStartDate(DateTimes.toDateTime(interval.getStart()).getDate());
    dateRange.setEndDate(DateTimes.toDateTime(interval.getEnd()).getDate());
    // Request the traffic data.
    TrafficDataRequest trafficDataRequest = new TrafficDataRequest();
    trafficDataRequest.setRequestedDateRange(dateRange);
    trafficDataRequest.setTargeting(targeting);
    TrafficDataResponse trafficData = forecastService.getTrafficData(trafficDataRequest);
    // Read the historical traffic data.
    TimeSeries historicalTimeSeries = trafficData.getHistoricalTimeSeries();
    if (historicalTimeSeries != null) {
        Date historicalStartDate = historicalTimeSeries.getTimeSeriesDateRange().getStartDate();
        DateTime historicalStart = new DateTime(historicalStartDate.getYear(), historicalStartDate.getMonth(), historicalStartDate.getDay(), 0, 0, 0);
        for (int i = 0; i < historicalTimeSeries.getValues().length; i++) {
            System.out.printf("%s: %d historical ad opportunities%n", historicalStart.plus(Duration.standardDays(i)).toString(DateTimeFormat.longDate()), historicalTimeSeries.getValues()[i]);
        }
    }
    // Read the forecasted traffic data.
    TimeSeries forecastedTimeSeries = trafficData.getForecastedTimeSeries();
    if (forecastedTimeSeries != null) {
        Date forecastedStartDate = forecastedTimeSeries.getTimeSeriesDateRange().getStartDate();
        DateTime forecastedStart = new DateTime(forecastedStartDate.getYear(), forecastedStartDate.getMonth(), forecastedStartDate.getDay(), 0, 0, 0);
        for (int i = 0; i < forecastedTimeSeries.getValues().length; i++) {
            System.out.printf("%s: %d forecasted ad opportunities%n", forecastedStart.plus(Duration.standardDays(i)).toString(DateTimeFormat.longDate()), forecastedTimeSeries.getValues()[i]);
        }
    }
}
Also used : TrafficDataResponse(com.google.api.ads.admanager.axis.v202205.TrafficDataResponse) NetworkServiceInterface(com.google.api.ads.admanager.axis.v202205.NetworkServiceInterface) TimeSeries(com.google.api.ads.admanager.axis.v202205.TimeSeries) AdUnitTargeting(com.google.api.ads.admanager.axis.v202205.AdUnitTargeting) Targeting(com.google.api.ads.admanager.axis.v202205.Targeting) InventoryTargeting(com.google.api.ads.admanager.axis.v202205.InventoryTargeting) InventoryTargeting(com.google.api.ads.admanager.axis.v202205.InventoryTargeting) Date(com.google.api.ads.admanager.axis.v202205.Date) DateTime(org.joda.time.DateTime) ForecastServiceInterface(com.google.api.ads.admanager.axis.v202205.ForecastServiceInterface) DateRange(com.google.api.ads.admanager.axis.v202205.DateRange) AdUnitTargeting(com.google.api.ads.admanager.axis.v202205.AdUnitTargeting) TrafficDataRequest(com.google.api.ads.admanager.axis.v202205.TrafficDataRequest) Interval(org.joda.time.Interval)

Example 43 with TimeSeries

use of com.google.api.ads.admanager.axis.v202111.TimeSeries in project java-monitoring by googleapis.

the class MetricServiceClientTest method createServiceTimeSeriesTest2.

@Test
public void createServiceTimeSeriesTest2() throws Exception {
    Empty expectedResponse = Empty.newBuilder().build();
    mockMetricService.addResponse(expectedResponse);
    String name = "name3373707";
    List<TimeSeries> timeSeries = new ArrayList<>();
    client.createServiceTimeSeries(name, timeSeries);
    List<AbstractMessage> actualRequests = mockMetricService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    CreateTimeSeriesRequest actualRequest = ((CreateTimeSeriesRequest) actualRequests.get(0));
    Assert.assertEquals(name, actualRequest.getName());
    Assert.assertEquals(timeSeries, actualRequest.getTimeSeriesList());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : Empty(com.google.protobuf.Empty) TimeSeries(com.google.monitoring.v3.TimeSeries) AbstractMessage(com.google.protobuf.AbstractMessage) CreateTimeSeriesRequest(com.google.monitoring.v3.CreateTimeSeriesRequest) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 44 with TimeSeries

use of com.google.api.ads.admanager.axis.v202111.TimeSeries in project java-monitoring by googleapis.

the class MetricServiceClientTest method createTimeSeriesTest.

@Test
public void createTimeSeriesTest() throws Exception {
    Empty expectedResponse = Empty.newBuilder().build();
    mockMetricService.addResponse(expectedResponse);
    ProjectName name = ProjectName.of("[PROJECT]");
    List<TimeSeries> timeSeries = new ArrayList<>();
    client.createTimeSeries(name, timeSeries);
    List<AbstractMessage> actualRequests = mockMetricService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    CreateTimeSeriesRequest actualRequest = ((CreateTimeSeriesRequest) actualRequests.get(0));
    Assert.assertEquals(name.toString(), actualRequest.getName());
    Assert.assertEquals(timeSeries, actualRequest.getTimeSeriesList());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : Empty(com.google.protobuf.Empty) TimeSeries(com.google.monitoring.v3.TimeSeries) AbstractMessage(com.google.protobuf.AbstractMessage) ProjectName(com.google.monitoring.v3.ProjectName) CreateTimeSeriesRequest(com.google.monitoring.v3.CreateTimeSeriesRequest) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 45 with TimeSeries

use of com.google.api.ads.admanager.axis.v202111.TimeSeries in project java-monitoring by googleapis.

the class MetricServiceClientTest method listTimeSeriesTest3.

@Test
public void listTimeSeriesTest3() throws Exception {
    TimeSeries responsesElement = TimeSeries.newBuilder().build();
    ListTimeSeriesResponse expectedResponse = ListTimeSeriesResponse.newBuilder().setNextPageToken("").addAllTimeSeries(Arrays.asList(responsesElement)).build();
    mockMetricService.addResponse(expectedResponse);
    ProjectName name = ProjectName.of("[PROJECT]");
    String filter = "filter-1274492040";
    TimeInterval interval = TimeInterval.newBuilder().build();
    ListTimeSeriesRequest.TimeSeriesView view = ListTimeSeriesRequest.TimeSeriesView.forNumber(0);
    ListTimeSeriesPagedResponse pagedListResponse = client.listTimeSeries(name, filter, interval, view);
    List<TimeSeries> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getTimeSeriesList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockMetricService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListTimeSeriesRequest actualRequest = ((ListTimeSeriesRequest) actualRequests.get(0));
    Assert.assertEquals(name.toString(), actualRequest.getName());
    Assert.assertEquals(filter, actualRequest.getFilter());
    Assert.assertEquals(interval, actualRequest.getInterval());
    Assert.assertEquals(view, actualRequest.getView());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : TimeSeries(com.google.monitoring.v3.TimeSeries) AbstractMessage(com.google.protobuf.AbstractMessage) TimeInterval(com.google.monitoring.v3.TimeInterval) ProjectName(com.google.monitoring.v3.ProjectName) ListTimeSeriesResponse(com.google.monitoring.v3.ListTimeSeriesResponse) ListTimeSeriesPagedResponse(com.google.cloud.monitoring.v3.MetricServiceClient.ListTimeSeriesPagedResponse) ListTimeSeriesRequest(com.google.monitoring.v3.ListTimeSeriesRequest) Test(org.junit.Test)

Aggregations

TimeSeries (com.google.monitoring.v3.TimeSeries)36 Test (org.junit.Test)21 ArrayList (java.util.ArrayList)18 ProjectName (com.google.monitoring.v3.ProjectName)17 TimeInterval (com.google.monitoring.v3.TimeInterval)14 ListTimeSeriesPagedResponse (com.google.cloud.monitoring.v3.MetricServiceClient.ListTimeSeriesPagedResponse)11 CreateTimeSeriesRequest (com.google.monitoring.v3.CreateTimeSeriesRequest)11 TimeSeries (org.spf4j.tsdb2.TimeSeries)11 ListTimeSeriesRequest (com.google.monitoring.v3.ListTimeSeriesRequest)10 MetricServiceClient (com.google.cloud.monitoring.v3.MetricServiceClient)8 ListTimeSeriesResponse (com.google.monitoring.v3.ListTimeSeriesResponse)8 AbstractMessage (com.google.protobuf.AbstractMessage)8 Point (com.google.monitoring.v3.Point)6 MonitoredResource (com.google.api.MonitoredResource)5 Empty (com.google.protobuf.Empty)5 StatusRuntimeException (io.grpc.StatusRuntimeException)5 HashMap (java.util.HashMap)5 Metric (com.google.api.Metric)4 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)4 DateTime (org.joda.time.DateTime)4