Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.streampipes.rest.impl.datalake;
import com.google.gson.Gson;
import okhttp3.OkHttpClient;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;
import org.apache.streampipes.config.backend.BackendConfig;
import org.apache.streampipes.model.datalake.DataLakeMeasure;
import org.apache.streampipes.rest.impl.datalake.model.DataResult;
import org.apache.streampipes.rest.impl.datalake.model.GroupedDataResult;
import org.apache.streampipes.rest.impl.datalake.model.PageResult;
import org.apache.streampipes.storage.management.StorageDispatcher;
import java.io.IOException;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.StreamingOutput;
public class DataLakeManagementV3 {
private static final double NUM_OF_AUTO_AGGREGATION_VALUES = 2000;
private SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
private SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
public List getInfos() {
List indices = StorageDispatcher.INSTANCE.getNoSqlStore().getDataLakeStorage()
.getAllDataLakeMeasures();
return indices;
}
public DataResult getEvents(String index, long startDate, long endDate, String aggregationUnit, int aggregationValue) {
InfluxDB influxDB = getInfluxDBClient();
Query query = new Query("SELECT mean(*) FROM " + index + " WHERE time > " + startDate * 1000000 + " AND time < " + endDate * 1000000
+ " GROUP BY time(" + aggregationValue + aggregationUnit + ") fill(none) ORDER BY time",
BackendConfig.INSTANCE.getInfluxDatabaseName());
QueryResult result = influxDB.query(query);
DataResult dataResult = convertResult(result);
influxDB.close();
return dataResult;
}
public GroupedDataResult getEvents(String index, long startDate, long endDate, String aggregationUnit, int aggregationValue,
String groupingTag) {
InfluxDB influxDB = getInfluxDBClient();
Query query = new Query("SELECT mean(*) FROM " + index + " WHERE time > " + startDate * 1000000 + " AND time < " + endDate * 1000000
+ " GROUP BY " + groupingTag + ",time(" + aggregationValue + aggregationUnit + ") fill(none) ORDER BY time",
BackendConfig.INSTANCE.getInfluxDatabaseName());
QueryResult result = influxDB.query(query);
GroupedDataResult groupedDataResult = convertMultiResult(result);
influxDB.close();
return groupedDataResult;
}
public DataResult getEvents(String index, long startDate, long endDate) {
InfluxDB influxDB = getInfluxDBClient();
Query query = new Query("SELECT * FROM " + index
+ " WHERE time > " + startDate * 1000000 + " AND time < " + endDate * 1000000
+ " ORDER BY time",
BackendConfig.INSTANCE.getInfluxDatabaseName());
QueryResult result = influxDB.query(query);
DataResult dataResult = convertResult(result);
influxDB.close();
return dataResult;
}
public GroupedDataResult getEvents(String index, long startDate, long endDate, String groupingTag) {
InfluxDB influxDB = getInfluxDBClient();
Query query = new Query("SELECT * FROM " + index
+ " WHERE time > " + startDate * 1000000 + " AND time < " + endDate * 1000000
+ " GROUP BY " + groupingTag
+ " ORDER BY time",
BackendConfig.INSTANCE.getInfluxDatabaseName());
QueryResult result = influxDB.query(query);
GroupedDataResult groupedDataResult = convertMultiResult(result);
influxDB.close();
return groupedDataResult;
}
public DataResult getEventsAutoAggregation(String index, long startDate, long endDate)
throws ParseException {
InfluxDB influxDB = getInfluxDBClient();
double numberOfRecords = getNumOfRecordsOfTable(index, influxDB, startDate, endDate);
influxDB.close();
if (numberOfRecords == 0) {
influxDB.close();
return new DataResult();
} else if (numberOfRecords <= NUM_OF_AUTO_AGGREGATION_VALUES) {
influxDB.close();
return getEvents(index, startDate, endDate);
} else {
int aggregatinValue = getAggregationValue(index, influxDB);
influxDB.close();
return getEvents(index, startDate, endDate, "ms", aggregatinValue);
}
}
public GroupedDataResult getEventsAutoAggregation(String index, long startDate, long endDate, String groupingTag)
throws ParseException {
InfluxDB influxDB = getInfluxDBClient();
double numberOfRecords = getNumOfRecordsOfTable(index, influxDB, startDate, endDate);
influxDB.close();
if (numberOfRecords == 0) {
influxDB.close();
return new GroupedDataResult(0, new HashMap<>());
} else if (numberOfRecords <= NUM_OF_AUTO_AGGREGATION_VALUES) {
influxDB.close();
return getEvents(index, startDate, endDate, groupingTag);
} else {
int aggregatinValue = getAggregationValue(index, influxDB);
influxDB.close();
return getEvents(index, startDate, endDate, "ms", aggregatinValue, groupingTag);
}
}
public DataResult getEventsFromNow(String index, String timeunit, int value,
String aggregationUnit, int aggregationValue)
throws ParseException {
InfluxDB influxDB = getInfluxDBClient();
Query query = new Query("SELECT mean(*) FROM " + index + " WHERE time > now() -" + value + timeunit
+ " GROUP BY time(" + aggregationValue + aggregationUnit + ") fill(none) ORDER BY time",
BackendConfig.INSTANCE.getInfluxDatabaseName());
QueryResult result = influxDB.query(query);
DataResult dataResult = convertResult(result);
return dataResult;
}
public DataResult getEventsFromNow(String index, String timeunit, int value) {
InfluxDB influxDB = getInfluxDBClient();
Query query = new Query("SELECT * FROM "
+ index
+ " WHERE time > now() -"
+ value
+ timeunit
+ " ORDER BY time",
BackendConfig.INSTANCE.getInfluxDatabaseName());
QueryResult result = influxDB.query(query);
DataResult dataResult = convertResult(result);
influxDB.close();
return dataResult;
}
public DataResult getEventsFromNowAutoAggregation(String index, String timeunit, int value)
throws ParseException {
InfluxDB influxDB = getInfluxDBClient();
double numberOfRecords = getNumOfRecordsOfTableFromNow(index, influxDB, timeunit, value);
if (numberOfRecords == 0) {
influxDB.close();
return new DataResult();
} else if (numberOfRecords <= NUM_OF_AUTO_AGGREGATION_VALUES) {
influxDB.close();
return getEventsFromNow(index, timeunit, value);
} else {
int aggregationValue = getAggregationValue(index, influxDB);
influxDB.close();
return getEventsFromNow(index, timeunit, value, "ms", aggregationValue);
}
}
public PageResult getEvents(String index, int itemsPerPage, int page) {
InfluxDB influxDB = getInfluxDBClient();
Query query = new Query("SELECT * FROM "
+ index
+ " ORDER BY time LIMIT "
+ itemsPerPage
+ " OFFSET "
+ page * itemsPerPage,
BackendConfig.INSTANCE.getInfluxDatabaseName());
QueryResult result = influxDB.query(query);
DataResult dataResult = convertResult(result);
influxDB.close();
int pageSum = getMaxPage(index, itemsPerPage);
return new PageResult(dataResult.getTotal(), dataResult.getHeaders(), dataResult.getRows(), page, pageSum);
}
public PageResult getEvents(String index, int itemsPerPage) throws IOException {
int page = getMaxPage(index, itemsPerPage);
return getEvents(index, itemsPerPage, page);
}
public StreamingOutput getAllEvents(String index, String outputFormat) {
return getAllEvents(index, outputFormat, null, null);
}
public StreamingOutput getAllEvents(String index, String outputFormat, @Nullable Long startDate,
@Nullable Long endDate) {
return new StreamingOutput() {
@Override
public void write(OutputStream outputStream) throws IOException, WebApplicationException {
InfluxDB influxDB = getInfluxDBClient();
int itemsPerRequest = 10000;
DataResult dataResult;
//JSON
if (outputFormat.equals("json")) {
Gson gson = new Gson();
int i = 0;
boolean isFirstDataObject = true;
outputStream.write(toBytes("["));
do {
Query query = getRawDataQueryWithPage(i, itemsPerRequest, index, startDate, endDate);
QueryResult result = influxDB.query(query, TimeUnit.MILLISECONDS);
dataResult = new DataResult();
if ((result.getResults().get(0).getSeries() != null)) {
dataResult = convertResult(result.getResults().get(0).getSeries().get(0));
}
if (dataResult.getTotal() > 0 ) {
for (List