use of com.google.bigtable.repackaged.com.google.monitoring.v3.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');
}
}
}
use of com.google.bigtable.repackaged.com.google.monitoring.v3.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');
}
}
}
}
use of com.google.bigtable.repackaged.com.google.monitoring.v3.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()][]));
}
}
use of com.google.bigtable.repackaged.com.google.monitoring.v3.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);
}
use of com.google.bigtable.repackaged.com.google.monitoring.v3.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]);
}
}
}
Aggregations