Search in sources :

Example 16 with TimeSeries

use of com.google.api.ads.admanager.axis.v202205.TimeSeries in project spf4j by zolyfarkas.

the class TimeSeriesDatabase method writeCsvTables.

public void writeCsvTables(final List<String> tableNames, final File output) throws IOException {
    DateTimeFormatter formatter = DateTimeFormats.TS_FORMAT;
    try (Writer writer = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(output.toPath()), StandardCharsets.UTF_8))) {
        String firstTable = tableNames.get(0);
        TSTable table = getTSTable(firstTable);
        Csv.writeCsvElement("table", writer);
        writer.append(',');
        Csv.writeCsvElement("timestamp", writer);
        for (String colName : table.getColumnNames()) {
            writer.append(',');
            Csv.writeCsvElement(colName, writer);
        }
        writer.write('\n');
        for (String tableName : tableNames) {
            TimeSeries data = readAll(tableName);
            long[] timestamps = data.getTimeStamps();
            long[][] values = data.getValues();
            for (int i = 0; i < timestamps.length; i++) {
                Csv.writeCsvElement(tableName, writer);
                writer.append(',');
                Csv.writeCsvElement(formatter.format(Instant.ofEpochMilli(timestamps[i])), writer);
                for (long val : values[i]) {
                    writer.append(',');
                    Csv.writeCsvElement(Long.toString(val), writer);
                }
                writer.write('\n');
            }
        }
    }
}
Also used : TimeSeries(org.spf4j.tsdb2.TimeSeries) OutputStreamWriter(java.io.OutputStreamWriter) DateTimeFormatter(java.time.format.DateTimeFormatter) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) BufferedWriter(java.io.BufferedWriter)

Example 17 with TimeSeries

use of com.google.api.ads.admanager.axis.v202205.TimeSeries in project spf4j by zolyfarkas.

the class TimeSeriesDatabase method writeCsvTable.

public void writeCsvTable(final String tableName, final File output) throws IOException {
    TSTable table = getTSTable(tableName);
    TimeSeries data = readAll(tableName);
    DateTimeFormatter formatter = DateTimeFormats.TS_FORMAT;
    try (Writer writer = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(output.toPath()), StandardCharsets.UTF_8))) {
        Csv.writeCsvElement("timestamp", writer);
        for (String colName : table.getColumnNames()) {
            writer.append(',');
            Csv.writeCsvElement(colName, writer);
        }
        writer.write('\n');
        long[] timestamps = data.getTimeStamps();
        long[][] values = data.getValues();
        for (int i = 0; i < timestamps.length; i++) {
            Csv.writeCsvElement(formatter.format(Instant.ofEpochMilli(timestamps[i])), writer);
            for (long val : values[i]) {
                writer.append(',');
                Csv.writeCsvElement(Long.toString(val), writer);
            }
            writer.write('\n');
        }
    }
}
Also used : TimeSeries(org.spf4j.tsdb2.TimeSeries) OutputStreamWriter(java.io.OutputStreamWriter) DateTimeFormatter(java.time.format.DateTimeFormatter) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) BufferedWriter(java.io.BufferedWriter)

Example 18 with TimeSeries

use of com.google.api.ads.admanager.axis.v202205.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.v202202.TrafficDataResponse) NetworkServiceInterface(com.google.api.ads.admanager.axis.v202202.NetworkServiceInterface) TimeSeries(com.google.api.ads.admanager.axis.v202202.TimeSeries) AdUnitTargeting(com.google.api.ads.admanager.axis.v202202.AdUnitTargeting) InventoryTargeting(com.google.api.ads.admanager.axis.v202202.InventoryTargeting) Targeting(com.google.api.ads.admanager.axis.v202202.Targeting) InventoryTargeting(com.google.api.ads.admanager.axis.v202202.InventoryTargeting) Date(com.google.api.ads.admanager.axis.v202202.Date) DateTime(org.joda.time.DateTime) ForecastServiceInterface(com.google.api.ads.admanager.axis.v202202.ForecastServiceInterface) DateRange(com.google.api.ads.admanager.axis.v202202.DateRange) AdUnitTargeting(com.google.api.ads.admanager.axis.v202202.AdUnitTargeting) TrafficDataRequest(com.google.api.ads.admanager.axis.v202202.TrafficDataRequest) Interval(org.joda.time.Interval)

Example 19 with TimeSeries

use of com.google.api.ads.admanager.axis.v202205.TimeSeries in project java-docs-samples by GoogleCloudPlatform.

the class TimeSeriesSummary method getMostRecentPoint.

Point getMostRecentPoint(TimeSeries timeSeries) {
    Point max = Collections.max(timeSeries.getPointsList(), Comparator.comparingLong(p -> p.getInterval().getEndTime().getSeconds()));
    mostRecentRunTime = max.getInterval().getEndTime();
    return max;
}
Also used : List(java.util.List) Lists(com.google.common.collect.Lists) TimeSeries(com.google.monitoring.v3.TimeSeries) Collections2(com.google.common.collect.Collections2) Point(com.google.monitoring.v3.Point) Comparator(java.util.Comparator) Timestamp(com.google.protobuf.Timestamp) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) Point(com.google.monitoring.v3.Point)

Example 20 with TimeSeries

use of com.google.api.ads.admanager.axis.v202205.TimeSeries in project java-docs-samples by GoogleCloudPlatform.

the class WorkloadGeneratorTest method testPipeline.

@Test
public void testPipeline() throws IOException, InterruptedException {
    String workloadJobName = "bigtable-workload-generator-test-" + new Date().getTime();
    final int WORKLOAD_DURATION = 10;
    final int WAIT_DURATION = WORKLOAD_DURATION * 60 * 1000;
    int rate = 1000;
    BigtableWorkloadOptions options = PipelineOptionsFactory.create().as(BigtableWorkloadOptions.class);
    options.setBigtableInstanceId(instanceId);
    options.setBigtableTableId(TABLE_ID);
    options.setWorkloadRate(rate);
    options.setRegion(REGION_ID);
    options.setWorkloadDurationMinutes(WORKLOAD_DURATION);
    options.setRunner(DataflowRunner.class);
    options.setJobName(workloadJobName);
    final PipelineResult pipelineResult = WorkloadGenerator.generateWorkload(options);
    MetricServiceClient metricServiceClient = MetricServiceClient.create();
    ProjectName name = ProjectName.of(projectId);
    // Wait X minutes and then get metrics for the X minute period.
    Thread.sleep(WAIT_DURATION);
    long startMillis = System.currentTimeMillis() - WAIT_DURATION;
    TimeInterval interval = TimeInterval.newBuilder().setStartTime(Timestamps.fromMillis(startMillis)).setEndTime(Timestamps.fromMillis(System.currentTimeMillis())).build();
    ListTimeSeriesRequest request = ListTimeSeriesRequest.newBuilder().setName(name.toString()).setFilter("metric.type=\"bigtable.googleapis.com/server/request_count\"").setInterval(interval).build();
    ListTimeSeriesPagedResponse response = metricServiceClient.listTimeSeries(request);
    long startRequestCount = 0;
    long endRequestCount = 0;
    for (TimeSeries ts : response.iterateAll()) {
        startRequestCount = ts.getPoints(0).getValue().getInt64Value();
        endRequestCount = ts.getPoints(ts.getPointsCount() - 1).getValue().getInt64Value();
    }
    assertThat(endRequestCount - startRequestCount > rate);
    // Stop the running job.
    String jobId = ((DataflowPipelineJob) pipelineResult).getJobId();
    DataflowClient client = DataflowClient.create(options);
    Job job = client.getJob(jobId);
    assertThat(job.getCurrentState().equals("JOB_STATE_CANCELLED"));
}
Also used : TimeSeries(com.google.bigtable.repackaged.com.google.monitoring.v3.TimeSeries) MetricServiceClient(com.google.bigtable.repackaged.com.google.cloud.monitoring.v3.MetricServiceClient) TimeInterval(com.google.bigtable.repackaged.com.google.monitoring.v3.TimeInterval) ProjectName(com.google.bigtable.repackaged.com.google.monitoring.v3.ProjectName) PipelineResult(org.apache.beam.sdk.PipelineResult) ListTimeSeriesPagedResponse(com.google.bigtable.repackaged.com.google.cloud.monitoring.v3.MetricServiceClient.ListTimeSeriesPagedResponse) Date(java.util.Date) ListTimeSeriesRequest(com.google.bigtable.repackaged.com.google.monitoring.v3.ListTimeSeriesRequest) BigtableWorkloadOptions(bigtable.WorkloadGenerator.BigtableWorkloadOptions) DataflowPipelineJob(org.apache.beam.runners.dataflow.DataflowPipelineJob) DataflowClient(org.apache.beam.runners.dataflow.DataflowClient) Job(com.google.api.services.dataflow.model.Job) DataflowPipelineJob(org.apache.beam.runners.dataflow.DataflowPipelineJob) 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