Search in sources :

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

Example 12 with TimeSeries

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

the class TSDBTailerTest method main.

public static void main(final String[] parameters) throws IOException {
    final MutableHolder<Integer> counter = new MutableHolder<>(0);
    TimeSeriesDatabase tsdb = new TimeSeriesDatabase(new File(parameters[0]));
    tsdb.tail(10, 0, new TSDataHandler() {

        private int count = 0;

        @Override
        public void newTable(final String tableName, final String[] columnNames) {
            LOG.info("New Table: {} columns: {}", tableName, columnNames);
        }

        @Override
        public void newData(final String tableName, final TimeSeries data) {
            LOG.debug("Table {} - {}", tableName, data);
            counter.setValue(counter.getValue() + data.getTimeStamps().length);
        }

        @Override
        public boolean finish() {
            return count++ > 400;
        }
    });
    System.exit(counter.getValue());
}
Also used : MutableHolder(org.spf4j.base.MutableHolder) TimeSeries(org.spf4j.tsdb2.TimeSeries) File(java.io.File)

Example 13 with TimeSeries

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

the class TimeSeriesDatabaseTest method testWriteTSDB.

/**
 * Test of close method, of class TimeSeriesDatabase.
 */
@Test
public void testWriteTSDB() throws Exception {
    if (new File(FILE_NAME).delete()) {
        LOG.debug("existing tsdb {} file deleted", FILE_NAME);
    }
    try (TimeSeriesDatabase instance = new TimeSeriesDatabase(new File(FILE_NAME), new byte[] {})) {
        instance.addTSTable("gr1", new byte[] {}, 5, new String[] { "a", "b" }, new byte[][] {});
        instance.write(System.currentTimeMillis(), "gr1", new long[] { 0, 1 });
        Thread.sleep(5);
        instance.write(System.currentTimeMillis(), "gr1", new long[] { 1, 2 });
        Thread.sleep(5);
        instance.write(System.currentTimeMillis(), "gr1", new long[] { 3, 4 });
        Thread.sleep(5);
        instance.addTSTable("gr2", new byte[] {}, 5, new String[] { "a", "b" }, new byte[][] {});
        instance.write(System.currentTimeMillis(), "gr2", new long[] { 7, 8 });
        instance.flush();
        instance.addTSTable("gr3", new byte[] {}, 5, new String[] { "a", "b" }, new byte[][] {});
        instance.write(System.currentTimeMillis(), "gr3", new long[] { 7, 8 });
        Thread.sleep(5);
        instance.write(System.currentTimeMillis(), "gr3", new long[] { 9, 10 });
        instance.flush();
        LOG.debug("TSTables = {}", instance.getTSTables());
        TimeSeries readAll = instance.readAll("gr1");
        Assert.assertEquals(2, readAll.getValues()[1][1]);
        LOG.debug("TimeSeries = {}", readAll);
        readAll = instance.readAll("gr2");
        Assert.assertEquals(7, readAll.getValues()[0][0]);
        LOG.debug("TimeSeries = {}", readAll);
        readAll = instance.readAll("gr3");
        Assert.assertEquals(10, readAll.getValues()[1][1]);
        LOG.debug("TimeSeries = {}", readAll);
    }
    TimeSeriesDatabase instanceRead = new TimeSeriesDatabase(new File(FILE_NAME), null);
    Collection<TSTable> tsTables = instanceRead.getTSTables();
    LOG.debug("Tables = {}", tsTables);
    Assert.assertEquals(3, tsTables.size());
    instanceRead.writeCsvTable("gr1", File.createTempFile("test", ".csv"));
    instanceRead.writeCsvTables(Arrays.asList("gr1", "gr2", "gr3"), File.createTempFile("testAll", ".csv"));
}
Also used : TimeSeries(org.spf4j.tsdb2.TimeSeries) File(java.io.File) Test(org.junit.Test)

Example 14 with TimeSeries

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

the class TimeSeriesDatabase method createJFreeCharts.

public List<JFreeChart> createJFreeCharts(final String tableName, final long startTime, final long endTime) throws IOException {
    TSTable info = this.getTSTable(tableName);
    TimeSeries data = this.read(tableName, startTime, endTime);
    return createJFreeCharts(data, info);
}
Also used : TimeSeries(org.spf4j.tsdb2.TimeSeries)

Example 15 with TimeSeries

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

the class TimeSeriesDatabase method read.

/**
 * Read measurements from table.
 *
 * @param tableName
 * @param startTime start time including
 * @param endTime end time including
 * @return
 * @throws IOException
 */
private TimeSeries read(final long startTime, final long endTime, final long startAtFragment, final long endAtFragment, final boolean skipFirst) throws IOException {
    synchronized (path) {
        TLongArrayList timeStamps = new TLongArrayList();
        List<long[]> data = new ArrayList<>();
        if (startAtFragment > 0) {
            FileLock lock = ch.lock(0, Long.MAX_VALUE, true);
            try {
                DataFragment frag;
                long nextFragmentLocation = startAtFragment;
                boolean last = false;
                boolean psFirst = skipFirst;
                do {
                    if (nextFragmentLocation == endAtFragment) {
                        last = true;
                    }
                    file.seek(nextFragmentLocation);
                    frag = new DataFragment(file);
                    if (psFirst) {
                        psFirst = false;
                    } else {
                        long fragStartTime = frag.getStartTimeMillis();
                        if (fragStartTime >= startTime) {
                            TIntArrayList fragTimestamps = frag.getTimestamps();
                            int nr = 0;
                            for (int i = 0; i < fragTimestamps.size(); i++) {
                                long ts = fragStartTime + fragTimestamps.get(i);
                                if (ts <= endTime) {
                                    timeStamps.add(ts);
                                    nr++;
                                } else {
                                    break;
                                }
                            }
                            int i = 0;
                            for (long[] d : frag.getData()) {
                                if (i < nr) {
                                    data.add(d);
                                } else {
                                    break;
                                }
                                nr++;
                            }
                            if (fragTimestamps.size() > nr) {
                                break;
                            }
                        }
                    }
                    nextFragmentLocation = frag.getNextDataFragment();
                } while (nextFragmentLocation > 0 && !last);
            } catch (IOException | RuntimeException e) {
                try {
                    lock.release();
                    throw e;
                } catch (IOException ex) {
                    ex.addSuppressed(e);
                    throw ex;
                }
            }
            lock.release();
        }
        return new TimeSeries(timeStamps.toArray(), data.toArray(new long[data.size()][]));
    }
}
Also used : TimeSeries(org.spf4j.tsdb2.TimeSeries) TLongArrayList(gnu.trove.list.array.TLongArrayList) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) TLongArrayList(gnu.trove.list.array.TLongArrayList) IOException(java.io.IOException) TIntArrayList(gnu.trove.list.array.TIntArrayList) FileLock(java.nio.channels.FileLock)

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