updated statistics format

This commit is contained in:
Derrick Johnson 2014-04-18 00:01:21 -04:00
parent 3dbb383e6f
commit f8f2748142
5 changed files with 40 additions and 1325 deletions

File diff suppressed because it is too large Load Diff

View File

@ -14,14 +14,11 @@ import java.util.Map;
public class Statistic {
private String name;
private Map<String, String> dimensions;
//private Statistics statistics;
private List<String> columns;
//private List<Statistics> values;
private List<List<Object>> values;
public Statistic() {
values = new ArrayList<>();
//columns = new ArrayList<>();
//columns.add("timestamp");
}
@Override
@ -38,16 +35,22 @@ public class Statistic {
return false;
} else if (!dimensions.equals(other.dimensions))
return false;
if(columns==null) {
if(other.columns!=null)
return false;
} else if(!columns.equals(other.columns))
return false;
if(values==null) {
if(other.values!=null)
return false;
} else if(!values.equals(other.values))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
/* if (statistics == null) {
if (other.statistics != null)
return false;
} else if (!statistics.equals(other.statistics))
return false;*/
return true;
}
@ -59,12 +62,7 @@ public class Statistic {
return name;
}
/*public Statistics getStatistics() {
return statistics;
} */
public List<String> getColumns() { return columns;}
//public List<Statistics> getValues() { return values;}
public List<List<Object>> getValues() { return values;}
@ -74,7 +72,8 @@ public class Statistic {
int result = 1;
result = prime * result + ((dimensions == null) ? 0 : dimensions.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
//result = prime * result + ((statistics == null) ? 0 : statistics.hashCode());
result = prime * result + ((values == null) ? 0 : values.hashCode());
result = prime * result + ((columns == null) ? 0 : columns.hashCode());
return result;
}
@ -87,7 +86,7 @@ public class Statistic {
}
public void setColumns(List<String> columns) { this.columns =columns;}
//public void setValues(List<Statistics> values) { this.values = values;}
public void setValues(List<List<Object>> values) { this.values = values;}
public void addValues(List<Object> value) {

View File

@ -1,195 +0,0 @@
package com.hpcloud.mon.domain.model.statistic;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.JsonSerializable;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.json4s.jackson.JsonMethods;
import java.util.ArrayList;
import java.util.List;
/**
* Encapsulate a statistics
*/
public class Statistics {
private Double average;
private Long count;
private Double min;
public Double max;
public Double getSum() {
return sum;
}
public void setSum(Double sum) {
this.sum = sum;
}
private Double sum;
private Long timestamp;
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public Double getAverage() {
return average;
}
public void setAverage(Double average) {
this.average = average;
}
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public Double getMin() {
return min;
}
public void setMin(Double min) {
this.min = min;
}
/* @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public Double getMax() {
return max;
}*/
// mapper.setVisibility(JsonMethod.ALL, Visibility.NONE);
//mapper.setVisibility(JsonMethod.FIELD, Visibility.ANY);
public void setMax(Double max) {
this.max = max;
}
public Long getTimestamp() {
return timestamp;
}
public void setTimestamp(Long timestamp) {
this.timestamp = timestamp/1000;
}
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public Long getCount() {
return count;
}
public void setCount(Long count) {
this.count = count;
}
/*public Statistics() {
rows = new ArrayList<Statistics>();
} */
/*
@JsonProperty("timestamp")
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public List<Long> getTimestamp() {
return timestamp;
}
public void setTimestamp(List<Long> timestamp) {
this.timestamp = timestamp;
}
private List<Double> average;
@JsonProperty("avg")
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public List<Double> getAverage() {
return average;
}
public void setAverage(List<Double> average) {
this.average = average;
}
private List<Double> min;
@JsonProperty("min")
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public List<Double> getMin() {
return min;
}
public void setMin(List<Double> min) {
this.min = min;
}
private List<Double> max;
@JsonProperty("max")
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public List<Double> getMax() {
return max;
}
public void setMax(List<Double> mas) {
this.max = mas;
}
private List<Long> count;
@JsonProperty("count")
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public List<Long> getCount() {
return count;
}
public void setCount(List<Long> count) {
this.count = count;
}
private List<Double> sum;
@JsonProperty("sum")
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public List<Double> getSum() {
return sum;
}
public void addSum(Double value) {
sum = lazyInstantiation(sum, value);
}
public void addAverage(Double value) {
average = lazyInstantiation(average, value);
}
public void addMin(Double value) {
min = lazyInstantiation(min, value);
}
public void addMax(Double value) {
max = lazyInstantiation(max, value);
}
public void addCount(Long value) {
count = lazyInstantiation(count, value);
}
public void addTimeStamp(Long value) {
//need to convert from Java Timestamp to Unix Timestamp
timestamp = lazyInstantiation(timestamp, value/1000);
}
public void setSum(List<Double> sum) {
this.sum = sum;
}
//lazy instantiation
private <T> List<T> lazyInstantiation(List<T> list, T value) {
if (list == null)
list = new ArrayList<T>();
list.add(value);
return list;
} */
}

View File

@ -1,66 +0,0 @@
package com.hpcloud.mon.domain.model.statistic;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.sql.Timestamp;
/**
* Created by johnderr on 4/2/14.
*/
public class StatisticsJoinRow {
private Double sum;
private Double count;
private Double max;
private Double min;
private Double average;
private Timestamp timeInterval;
@JsonProperty("timestamp")
public Timestamp getTime_interval() {
return timeInterval;
}
public void setTime_interval(Timestamp timeInterval) {
this.timeInterval = timeInterval;
}
public Double getSum() {
return sum;
}
public void setSum(Double sum) {
this.sum = sum;
}
public Double getCount() {
return count;
}
public void setCount(Double count) {
this.count = count;
}
public Double getMax() {
return max;
}
public void setMax(Double max) {
this.max = max;
}
public Double getMin() {
return min;
}
public void setMin(Double min) {
this.min = min;
}
public Double getAverage() {
return average;
}
public void setAverage(Double average) {
this.average = average;
}
}

View File

@ -1,6 +1,5 @@
package com.hpcloud.mon.infrastructure.persistence;
import java.nio.ByteBuffer;
import java.sql.Timestamp;
import java.util.*;
@ -10,7 +9,6 @@ import javax.inject.Named;
import com.hpcloud.mon.domain.model.statistic.Statistic;
import com.hpcloud.mon.domain.model.statistic.Statistics;
import org.joda.time.DateTime;
import org.skife.jdbi.v2.DBI;
@ -25,9 +23,6 @@ import org.skife.jdbi.v2.Query;
public class StatisticRepositoryImpl implements StatisticRepository {
private final DBI db;
/*private static final String FIND_BY_METRIC_DEF_SQL = "select distinct def.id, def.name, d.name as dname, d.value as dvalue "
+ "from MonMetrics.Definitions def, MonMetrics.Dimensions d%s "
+ "where def.tenant_id = :tenantId and d.definition_id = def.id%s order by def.id";*/
private static final String FIND_BY_METRIC_DEF_SQL = "select dd.id, def.name, d.name as dname, d.value as dvalue "
+ "from MonMetrics.Definitions def, MonMetrics.DefinitionDimensions dd "
+ "left outer join MonMetrics.Dimensions d on d.dimension_set_id = dd.dimension_set_id%s "
@ -45,6 +40,8 @@ public class StatisticRepositoryImpl implements StatisticRepository {
Handle h = db.open();
List<Statistic> listStats = new ArrayList<>();
List<String> copyStatistics = createColumns(statistics);
try {
Map<byte[], Statistic> byteMap = findDefIds(h, tenantId, name, dimensions, startTime, endTime);
@ -58,10 +55,8 @@ public class StatisticRepositoryImpl implements StatisticRepository {
// Execute
List<Map<String, Object>> rows = query.list();
//Statistics currentStatistics = new Statistics();
List<Object> statisticsRow = new ArrayList<Object>();
for (Map<String, Object> row : rows) {
Double sum = (Double)row.get("sum");
Double average = (Double) row.get("avg");
@ -71,37 +66,29 @@ public class StatisticRepositoryImpl implements StatisticRepository {
Timestamp time_stamp = (Timestamp)row.get("time_interval");
if(time_stamp != null) {
//currentStatistics.setTimestamp(time_stamp.getTime());
statisticsRow.add(time_stamp.getTime());
statisticsRow.add(time_stamp.getTime());
}
if(average != null) {
//currentStatistics.setAverage(average);
statisticsRow.add(average);
}
if(count != null) {
//currentStatistics.setCount(count);
statisticsRow.add(count);
}
if(max != null) {
//currentStatistics.setMax(max);
statisticsRow.add(max);
}
if(min != null) {
//currentStatistics.setMin(min);
statisticsRow.add(min);
}
if(sum != null) {
//currentStatistics.setSum(sum);
statisticsRow.add(sum);
}
byteMap.get(bufferId).addValues(statisticsRow);
statisticsRow = new ArrayList<>();
}//for
//Collections.sort(statistics);
statistics.add(0,"timestamp");
byteMap.get(bufferId).setColumns(statistics);
}
byteMap.get(bufferId).setColumns(copyStatistics);
listStats.add(byteMap.get(bufferId));
}
@ -114,20 +101,20 @@ public class StatisticRepositoryImpl implements StatisticRepository {
private Map<byte[], Statistic> findDefIds(Handle h, String tenantId, String name,
Map<String, String> dimensions, DateTime startTime, DateTime endTime) {
List<ByteBuffer> byteBufferArray = new ArrayList<>();
List<byte[]> bytes = new ArrayList<>();
// Build query
//StringBuilder sbFrom = new StringBuilder();
StringBuilder sbWhere = new StringBuilder();
// MetricQueries.buildClausesForDimensions(sbFrom, sbWhere, dimensions);
if (name != null)
sbWhere.append(" and def.name = :name");
if (endTime != null) {
sbWhere.append(" and m.time_stamp <= :endTime");
}
//String sql = String.format(FIND_BY_METRIC_DEF_SQL, sbFrom.toString(), sbWhere.toString());
String sql = String.format(FIND_BY_METRIC_DEF_SQL,
MetricQueries.buildJoinClauseFor(dimensions), sbWhere);
@ -184,6 +171,18 @@ public class StatisticRepositoryImpl implements StatisticRepository {
return byteIdMap;
}
List<String> createColumns(List<String> list) {
List<String> copy = new ArrayList<>();
for(String string:list) {
copy.add(string);
}
Collections.sort(copy);
copy.add(0, "timestamp");
return copy;
}
private String createQuery(int period, DateTime startTime, DateTime endTime, List<String> statistics) {
StringBuilder builder = new StringBuilder();
@ -217,6 +216,7 @@ public class StatisticRepositoryImpl implements StatisticRepository {
}
return clause;
}
private String createOffset(int period, DateTime startTime, DateTime endTime) {
StringBuilder offset = new StringBuilder();
@ -229,6 +229,7 @@ public class StatisticRepositoryImpl implements StatisticRepository {
return offset.toString();
}
private String getColumns(List<String> statistics) {
StringBuilder buildColumns = new StringBuilder();