modules/enterprise/server/server-metrics/src/main/java/org/rhq/server/metrics/ArithmeticMeanCalculator.java | 44 ++
modules/enterprise/server/server-metrics/src/main/java/org/rhq/server/metrics/Buckets.java | 7
modules/enterprise/server/server-metrics/src/main/java/org/rhq/server/metrics/MetricBaselineCalculator.java | 28 +
modules/enterprise/server/server-metrics/src/main/java/org/rhq/server/metrics/MetricsDAO.java | 151 +++++-----
modules/enterprise/server/server-metrics/src/main/java/org/rhq/server/metrics/MetricsServer.java | 21 -
modules/enterprise/server/server-metrics/src/test/java/org/rhq/server/metrics/MetricsServerTest.java | 7
6 files changed, 157 insertions(+), 101 deletions(-)
New commits:
commit 3ecbfcab546406684dbbe49a840a6e9bc26a0a57
Author: Stefan Negrea <snegrea(a)redhat.com>
Date: Fri Dec 21 17:39:45 2012 -0600
Update existing average calculations to use the rolling algorithm to prevent overflow. Move the initial implementation to the test class to check for rolling algorithm correctness.
diff --git a/modules/enterprise/server/server-metrics/src/main/java/org/rhq/server/metrics/Buckets.java b/modules/enterprise/server/server-metrics/src/main/java/org/rhq/server/metrics/Buckets.java
index bcac61a..db96b80 100644
--- a/modules/enterprise/server/server-metrics/src/main/java/org/rhq/server/metrics/Buckets.java
+++ b/modules/enterprise/server/server-metrics/src/main/java/org/rhq/server/metrics/Buckets.java
@@ -38,7 +38,7 @@ public class Buckets {
// end time is exclusive
private long endTime;
- private double sum;
+ private ArithmeticMeanCalculator mean;
private double max;
private double min;
private int count;
@@ -46,6 +46,7 @@ public class Buckets {
public Bucket(long startTime, long endTime) {
this.startTime = startTime;
this.endTime = endTime;
+ this.mean = new ArithmeticMeanCalculator();
}
public long getStartTime() {
@@ -57,7 +58,7 @@ public class Buckets {
}
public Bucket insert(double value) {
- sum += value;
+ mean.add(value);
if (count == 0) {
min = value;
max = value;
@@ -74,7 +75,7 @@ public class Buckets {
if (count == 0) {
return Double.NaN;
}
- return MetricsServer.divide(sum, count);
+ return mean.getArithmeticMean();
}
public double getMax() {
diff --git a/modules/enterprise/server/server-metrics/src/main/java/org/rhq/server/metrics/MetricsServer.java b/modules/enterprise/server/server-metrics/src/main/java/org/rhq/server/metrics/MetricsServer.java
index 33c6a13..0c5f329 100644
--- a/modules/enterprise/server/server-metrics/src/main/java/org/rhq/server/metrics/MetricsServer.java
+++ b/modules/enterprise/server/server-metrics/src/main/java/org/rhq/server/metrics/MetricsServer.java
@@ -27,8 +27,6 @@ package org.rhq.server.metrics;
import static org.rhq.core.domain.util.PageOrdering.DESC;
-import java.math.BigDecimal;
-import java.math.MathContext;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -265,8 +263,8 @@ public class MetricsServer {
private AggregatedNumericMetric calculateAggregatedRaw(List<RawNumericMetric> rawMetrics, long timestamp) {
double min = Double.NaN;
double max = min;
- double sum = 0;
int count = 0;
+ ArithmeticMeanCalculator mean = new ArithmeticMeanCalculator();
double value;
for (RawNumericMetric metric : rawMetrics) {
@@ -280,14 +278,13 @@ public class MetricsServer {
} else if (value > max) {
max = value;
}
- sum += value;
+ mean.add(value);
++count;
}
- double avg = divide(sum, count);
// We let the caller handle setting the schedule id because in some cases we do
// not care about it.
- return new AggregatedNumericMetric(0, avg, min, max, timestamp);
+ return new AggregatedNumericMetric(0, mean.getArithmeticMean(), min, max, timestamp);
}
private List<AggregatedNumericMetric> calculateAggregates(MetricsTable fromColumnFamily,
@@ -322,8 +319,8 @@ public class MetricsServer {
private AggregatedNumericMetric calculateAggregate(List<AggregatedNumericMetric> metrics, long timestamp) {
double min = Double.NaN;
double max = min;
- double sum = 0;
int count = 0;
+ ArithmeticMeanCalculator mean = new ArithmeticMeanCalculator();
for (AggregatedNumericMetric metric : metrics) {
if (count == 0) {
@@ -335,14 +332,13 @@ public class MetricsServer {
} else if (metric.getMax() > max) {
max = metric.getMax();
}
- sum += metric.getAvg();
+ mean.add(metric.getAvg());
++count;
}
- double avg = divide(sum, count);
// We let the caller handle setting the schedule id because in some cases we do
// not care about it.
- return new AggregatedNumericMetric(0, avg, min, max, timestamp);
+ return new AggregatedNumericMetric(0, mean.getArithmeticMean(), min, max, timestamp);
}
private MetricsTable getTable(DateTime begin) {
@@ -386,11 +382,6 @@ public class MetricsServer {
// public void addCallTimeData(Set<CallTimeData> callTimeDatas) {
// }
- static double divide(double dividend, int divisor) {
- return new BigDecimal(Double.toString(dividend)).divide(new BigDecimal(Integer.toString(divisor)),
- MathContext.DECIMAL64).doubleValue();
- }
-
protected DateTime getCurrentHour() {
DateTime now = new DateTime();
return now.hourOfDay().roundFloorCopy();
diff --git a/modules/enterprise/server/server-metrics/src/test/java/org/rhq/server/metrics/MetricsServerTest.java b/modules/enterprise/server/server-metrics/src/test/java/org/rhq/server/metrics/MetricsServerTest.java
index ca6dd95..2c76e45 100644
--- a/modules/enterprise/server/server-metrics/src/test/java/org/rhq/server/metrics/MetricsServerTest.java
+++ b/modules/enterprise/server/server-metrics/src/test/java/org/rhq/server/metrics/MetricsServerTest.java
@@ -27,12 +27,13 @@ package org.rhq.server.metrics;
import static java.util.Arrays.asList;
import static org.joda.time.DateTime.now;
-import static org.rhq.server.metrics.MetricsServer.divide;
import static org.rhq.test.AssertUtils.assertCollectionMatchesNoOrder;
import static org.rhq.test.AssertUtils.assertPropertiesMatch;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
+import java.math.BigDecimal;
+import java.math.MathContext;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -757,4 +758,8 @@ public class MetricsServerTest extends CassandraIntegrationTest {
assertEquals(index.size(), 0, "Expected metrics index for " + table + " to be empty but found " + index);
}
+ static double divide(double dividend, int divisor) {
+ return new BigDecimal(Double.toString(dividend)).divide(new BigDecimal(Integer.toString(divisor)),
+ MathContext.DECIMAL64).doubleValue();
+ }
}
commit b0134d8673127e0023e37d06bb2c443e9e701d66
Author: Stefan Negrea <snegrea(a)redhat.com>
Date: Fri Dec 21 17:23:09 2012 -0600
Improve baseline calculation performance by quering for raw min and max. Also updated the average algorithm to use rolling calculations to prevent overflows (due to addition).
diff --git a/modules/enterprise/server/server-metrics/src/main/java/org/rhq/server/metrics/ArithmeticMeanCalculator.java b/modules/enterprise/server/server-metrics/src/main/java/org/rhq/server/metrics/ArithmeticMeanCalculator.java
new file mode 100644
index 0000000..dfba260
--- /dev/null
+++ b/modules/enterprise/server/server-metrics/src/main/java/org/rhq/server/metrics/ArithmeticMeanCalculator.java
@@ -0,0 +1,44 @@
+/*
+ * RHQ Management Platform
+ * Copyright 2011, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+package org.rhq.server.metrics;
+
+/**
+ * @author Stefan Negrea
+ *
+ */
+public class ArithmeticMeanCalculator {
+ private int iterations = 0;
+ private double arithmeticMean = 0;
+
+ public void add(double value) {
+ iterations++;
+ arithmeticMean = arithmeticMean + (value - arithmeticMean) / iterations;
+ }
+
+ public double getArithmeticMean() {
+ return arithmeticMean;
+ }
+
+ public void reset() {
+ iterations = 0;
+ arithmeticMean = 0;
+ }
+}
diff --git a/modules/enterprise/server/server-metrics/src/main/java/org/rhq/server/metrics/MetricBaselineCalculator.java b/modules/enterprise/server/server-metrics/src/main/java/org/rhq/server/metrics/MetricBaselineCalculator.java
index b8a97c2..c3322e8 100644
--- a/modules/enterprise/server/server-metrics/src/main/java/org/rhq/server/metrics/MetricBaselineCalculator.java
+++ b/modules/enterprise/server/server-metrics/src/main/java/org/rhq/server/metrics/MetricBaselineCalculator.java
@@ -30,6 +30,7 @@ import java.util.List;
import com.datastax.driver.core.Session;
import org.rhq.core.domain.measurement.MeasurementBaseline;
+import org.rhq.core.domain.util.PageOrdering;
/**
* @author Stefan Negrea
@@ -57,30 +58,33 @@ public class MetricBaselineCalculator {
}
private MeasurementBaseline calculateBaseline(Integer scheduleId, long startTime, long endTime) {
-
List<AggregatedNumericMetric> metrics = this.metricsDAO.findAggregateMetrics(MetricsTable.ONE_HOUR, scheduleId, startTime, endTime);
if (metrics.size() != 0) {
- double min = metrics.get(0).getMin();
- double max = metrics.get(0).getMax();
- double average = 0;
+ ArithmeticMeanCalculator mean = new ArithmeticMeanCalculator();
for (AggregatedNumericMetric entry : metrics) {
- if (entry.getMax() > max) {
- max = entry.getMax();
- } else if (entry.getMin() < min) {
- min = entry.getMin();
- }
+ mean.add(entry.getAvg());
+ }
- average += entry.getAvg();
+ double min = Double.MIN_VALUE;
+ List<Double> results = this.metricsDAO.findAggregateSimpleMetric(MetricsTable.ONE_HOUR, AggregateType.MIN,
+ scheduleId, startTime, endTime, PageOrdering.ASC, 1);
+ if(results.size() != 0){
+ min = results.get(0);
}
- average = average / (double) metrics.size();
+ double max = Double.MAX_VALUE;
+ results = this.metricsDAO.findAggregateSimpleMetric(MetricsTable.ONE_HOUR, AggregateType.MAX, scheduleId,
+ startTime, endTime, PageOrdering.DESC, 1);
+ if (results.size() != 0) {
+ max = results.get(0);
+ }
MeasurementBaseline baseline = new MeasurementBaseline();
baseline.setMax(max);
baseline.setMin(min);
- baseline.setMean(average);
+ baseline.setMean(mean.getArithmeticMean());
baseline.setScheduleId(scheduleId);
return baseline;
commit 2ed670d98a66143923a9af78db5e41b823cbfc25
Author: Stefan Negrea <snegrea(a)redhat.com>
Date: Fri Dec 21 17:17:43 2012 -0600
Update metrics dao to use prepared statements as much as possible with the current driver.
diff --git a/modules/enterprise/server/server-metrics/src/main/java/org/rhq/server/metrics/MetricsDAO.java b/modules/enterprise/server/server-metrics/src/main/java/org/rhq/server/metrics/MetricsDAO.java
index dfdfd14..8d4855b 100644
--- a/modules/enterprise/server/server-metrics/src/main/java/org/rhq/server/metrics/MetricsDAO.java
+++ b/modules/enterprise/server/server-metrics/src/main/java/org/rhq/server/metrics/MetricsDAO.java
@@ -25,10 +25,6 @@
package org.rhq.server.metrics;
-import static com.datastax.driver.core.utils.querybuilder.Clause.eq;
-import static com.datastax.driver.core.utils.querybuilder.Clause.gte;
-import static com.datastax.driver.core.utils.querybuilder.Clause.lt;
-import static com.datastax.driver.core.utils.querybuilder.QueryBuilder.select;
import static org.rhq.core.util.StringUtil.listToString;
import java.util.ArrayList;
@@ -59,10 +55,20 @@ public class MetricsDAO {
//
// jsanda
+ private static final String RAW_METRICS_SIMPLE_QUERY =
+ "SELECT schedule_id, time, value " +
+ "FROM " + MetricsTable.RAW + " " +
+ "WHERE schedule_id = ? ORDER by time";
+
private static final String RAW_METRICS_QUERY =
"SELECT schedule_id, time, value " +
"FROM " + MetricsTable.RAW + " " +
- "WHERE schedule_id = ? AND time >= ? AND time < ?";
+ "WHERE schedule_id = ? AND time >= ? AND time < ? ORDER BY time";
+
+ private static final String RAW_METRICS_SCHEDULE_LIST_QUERY =
+ "SELECT schedule_id, time, value " +
+ "FROM " + MetricsTable.RAW + " " +
+ "WHERE schedule_id IN (?) AND time >= ? AND time < ? ORDER BY time";
private static final String RAW_METRICS_WITH_METADATA_QUERY =
"SELECT schedule_id, time, value, ttl(value), writetime(value) " +
@@ -88,22 +94,19 @@ public class MetricsDAO {
this.session = session;
}
- public void setSession(Session session) {
- this.session = session;
- }
-
public Set<MeasurementDataNumeric> insertRawMetrics(Set<MeasurementDataNumeric> dataSet, int ttl) {
- Set<MeasurementDataNumeric> insertedMetrics = new HashSet<MeasurementDataNumeric>();
- String sql = "INSERT INTO raw_metrics (schedule_id, time, value) VALUES (?, ?, ?) " +
- "USING TTL " + ttl;
try {
- PreparedStatement statement = session.prepare(sql);
+ String cql = "INSERT INTO raw_metrics (schedule_id, time, value) VALUES (?, ?, ?) " + "USING TTL " + ttl;
+ PreparedStatement statement = session.prepare(cql);
+
+ Set<MeasurementDataNumeric> insertedMetrics = new HashSet<MeasurementDataNumeric>();
for (MeasurementDataNumeric data : dataSet) {
BoundStatement boundStatement = statement.bind(data.getScheduleId(), new Date(data.getTimestamp()),
data.getValue());
session.execute(boundStatement);
insertedMetrics.add(data);
}
+
return insertedMetrics;
} catch (NoHostAvailableException e) {
throw new CQLException(e);
@@ -133,6 +136,7 @@ public class MetricsDAO {
updates.add(metric);
}
+
return updates;
} catch (NoHostAvailableException e) {
throw new CQLException(e);
@@ -141,17 +145,16 @@ public class MetricsDAO {
public List<RawNumericMetric> findRawMetrics(int scheduleId, long startTime, long endTime) {
try {
- List<RawNumericMetric> metrics = new ArrayList<RawNumericMetric>();
-
- String cql = "SELECT schedule_id, time, value FROM " + MetricsTable.RAW + " WHERE schedule_id = "
- + scheduleId + " AND time >= " + startTime + " AND time < " + endTime
- + " ORDER BY time";
+ PreparedStatement statement = session.prepare(RAW_METRICS_QUERY);
+ BoundStatement boundStatement = statement.bind(scheduleId, new Date(startTime), new Date(endTime));
+ ResultSet resultSet = session.execute(boundStatement);
- ResultSet resultSet = session.execute(cql);
+ List<RawNumericMetric> metrics = new ArrayList<RawNumericMetric>();
ResultSetMapper<RawNumericMetric> resultSetMapper = new RawNumericMetricMapper();
for (Row row : resultSet) {
metrics.add(resultSetMapper.map(row));
}
+
return metrics;
} catch (NoHostAvailableException e) {
throw new CQLException(e);
@@ -160,16 +163,15 @@ public class MetricsDAO {
public List<RawNumericMetric> findRawMetrics(int scheduleId, PageOrdering ordering, int limit) {
try {
- List<RawNumericMetric> metrics = new ArrayList<RawNumericMetric>();
-
- String cql = "SELECT schedule_id, time, value FROM " + MetricsTable.RAW + " WHERE schedule_id = " +
- scheduleId + " ORDER BY time " + ordering;
-
+ String cql = RAW_METRICS_SIMPLE_QUERY + " " + ordering;
if (limit > 0) {
cql += " LIMIT " + limit;
}
+ PreparedStatement statement = session.prepare(cql);
+ BoundStatement boundStatement = statement.bind(scheduleId);
+ ResultSet resultSet = session.execute(boundStatement);
- ResultSet resultSet = session.execute(cql);
+ List<RawNumericMetric> metrics = new ArrayList<RawNumericMetric>();
ResultSetMapper<RawNumericMetric> resultSetMapper = new RawNumericMetricMapper();
for (Row row : resultSet) {
metrics.add(resultSetMapper.map(row));
@@ -188,15 +190,10 @@ public class MetricsDAO {
}
try {
- ResultSet resultSet = session.execute(
- select("schedule_id", "time", "value", "ttl(value), writetime(value)")
- .from(MetricsTable.RAW.toString())
- .where(
- eq("schedule_id", scheduleId),
- gte("time", new Date(startTime)),
- lt("time", new Date(endTime)))
- .getQueryString()
- );
+ PreparedStatement statement = session.prepare(RAW_METRICS_WITH_METADATA_QUERY);
+ BoundStatement boundStatement = statement.bind(scheduleId, new Date(startTime), new Date(endTime));
+ ResultSet resultSet = session.execute(boundStatement);
+
List<RawNumericMetric> metrics = new ArrayList<RawNumericMetric>();
ResultSetMapper<RawNumericMetric> resultSetMapper = new RawNumericMetricMapper(true);
for (Row row : resultSet) {
@@ -214,25 +211,14 @@ public class MetricsDAO {
// I was not able to get the below query working by directly binding the List
// object. From a quick glance at the driver code, it looks like it might not
// yet be properly supported in which case we need to report a bug.
- //
// jsanda
-// String cql = "SELECT schedule_id, time, value FROM " + RAW_METRICS_TABLE +
-// " WHERE schedule_id IN (?) AND time >= ? AND time < ? ORDER BY time";
-// PreparedStatement statement = session.prepare(cql);
-// BoundStatement boundStatement = statement.bind(scheduleIds, startTime.toDate(), endTime.toDate());
-// ResultSet resultSet = session.execute(boundStatement);
-// String cql =
-// select("schedule_id", "time", "value")
-// .from(RAW_METRICS_TABLE)
-// .where(
-// in("schedule_id", listToString(scheduleIds)),
-// gte("time", startTime.toDate()),
-// lt("time", endTime.toDate()))
-// .getQueryString();
-
- String cql = "SELECT schedule_id, time, value FROM " + MetricsTable.RAW + " WHERE schedule_id IN (" +
- listToString(scheduleIds) + ") AND time >= " + startTime + " AND time <= " + endTime;
+ //PreparedStatement statement = session.prepare(RAW_METRICS_SCHEDULE_LIST_QUERY);
+ //BoundStatement boundStatement = statement.bind(scheduleIds, startTime, endTime);
+ //ResultSet resultSet = session.execute(boundStatement);
+
+ String cql = "SELECT schedule_id, time, value FROM " + MetricsTable.RAW + " WHERE schedule_id IN ("
+ + listToString(scheduleIds) + ") AND time >= " + startTime + " AND time <= " + endTime;
ResultSet resultSet = session.execute(cql);
List<RawNumericMetric> metrics = new ArrayList<RawNumericMetric>();
@@ -240,6 +226,7 @@ public class MetricsDAO {
for (Row row : resultSet) {
metrics.add(resultSetMapper.map(row));
}
+
return metrics;
} catch (NoHostAvailableException e) {
throw new CQLException(e);
@@ -251,12 +238,14 @@ public class MetricsDAO {
String cql =
"SELECT schedule_id, time, type, value " +
"FROM " + table + " " +
- "WHERE schedule_id = " + scheduleId + " " +
+ "WHERE schedule_id = ? " +
"ORDER BY time, type";
+ PreparedStatement statement = session.prepare(cql);
+ BoundStatement boundStatement = statement.bind(scheduleId);
+ ResultSet resultSet = session.execute(boundStatement);
+
List<AggregatedNumericMetric> metrics = new ArrayList<AggregatedNumericMetric>();
ResultSetMapper<AggregatedNumericMetric> resultSetMapper = new AggregateMetricMapper();
- ResultSet resultSet = session.execute(cql);
-
while (!resultSet.isExhausted()) {
metrics.add(resultSetMapper.map(resultSet.fetchOne(), resultSet.fetchOne(), resultSet.fetchOne()));
}
@@ -274,12 +263,13 @@ public class MetricsDAO {
String cql =
"SELECT schedule_id, time, type, value " +
"FROM " + table + " " +
- "WHERE schedule_id = "
- + scheduleId + " AND time >= " + startTime + " AND time < " + endTime;
+ "WHERE schedule_id = ? AND time >= ? AND time < ?";
+ PreparedStatement statement = session.prepare(cql);
+ BoundStatement boundStatement = statement.bind(scheduleId, new Date(startTime), new Date(endTime));
+ ResultSet resultSet = session.execute(boundStatement);
+
List<AggregatedNumericMetric> metrics = new ArrayList<AggregatedNumericMetric>();
ResultSetMapper<AggregatedNumericMetric> resultSetMapper = new AggregateMetricMapper();
- ResultSet resultSet = session.execute(cql);
-
while (!resultSet.isExhausted()) {
metrics.add(resultSetMapper.map(resultSet.fetchOne(), resultSet.fetchOne(), resultSet.fetchOne()));
}
@@ -290,18 +280,38 @@ public class MetricsDAO {
}
}
+ public List<Double> findAggregateSimpleMetric(MetricsTable table, AggregateType type, int scheduleId,
+ long startTime, long endTime, PageOrdering ordering, int limit) {
+ try {
+ String cql = "SELECT schedule_id, time, type, value " + "FROM " + table + " "
+ + "WHERE schedule_id = ? AND time >= ? AND time < ? AND type = ? "
+ + "ORDER BY value " + ordering + " LIMIT " + limit;
+ PreparedStatement statement = session.prepare(cql);
+ BoundStatement boundStatement = statement.bind(scheduleId, new Date(startTime), new Date(endTime),
+ type.ordinal());
+ ResultSet resultSet = session.execute(boundStatement);
+
+ List<Double> metrics = new ArrayList<Double>();
+ while (!resultSet.isExhausted()) {
+ metrics.add(resultSet.fetchOne().getDouble(3));
+ }
+
+ return metrics;
+ } catch (NoHostAvailableException e) {
+ throw new CQLException(e);
+ }
+ }
+
public List<AggregatedNumericMetric> findAggregateMetrics(MetricsTable table, List<Integer> scheduleIds,
long startTime, long endTime) {
try {
String cql =
- "SELECT schedule_id, time, type, value " +
- "FROM " + table + " " +
- "WHERE schedule_id IN ("
- + listToString(scheduleIds) + ") AND time >= " + startTime + " AND time < " + endTime;
- List<AggregatedNumericMetric> metrics = new ArrayList<AggregatedNumericMetric>();
- ResultSetMapper<AggregatedNumericMetric> resultSetMapper = new AggregateMetricMapper();
+ "SELECT schedule_id, time, type, value FROM " + table + " " +
+ "WHERE schedule_id IN (" + listToString(scheduleIds) + ") AND time >= " + startTime + " AND time < " + endTime;
ResultSet resultSet = session.execute(cql);
+ List<AggregatedNumericMetric> metrics = new ArrayList<AggregatedNumericMetric>();
+ ResultSetMapper<AggregatedNumericMetric> resultSetMapper = new AggregateMetricMapper();
while (!resultSet.isExhausted()) {
metrics.add(resultSetMapper.map(resultSet.fetchOne(), resultSet.fetchOne(), resultSet.fetchOne()));
}
@@ -312,19 +322,20 @@ public class MetricsDAO {
}
}
- List<AggregatedNumericMetric> findAggregateMetricsWithMetadata(MetricsTable table, int scheduleId,
+ public List<AggregatedNumericMetric> findAggregateMetricsWithMetadata(MetricsTable table, int scheduleId,
long startTime, long endTime) {
try {
String cql =
"SELECT schedule_id, time, type, value, ttl(value), writetime(value) " +
"FROM " + table + " " +
- "WHERE schedule_id = " + scheduleId + " AND time >= " + startTime +
- " AND time < " + endTime;
+ "WHERE schedule_id = ? AND time >= ? AND time < ?";
+ PreparedStatement statement = session.prepare(cql);
+ BoundStatement boundStatement = statement.bind(scheduleId, new Date(startTime), new Date(endTime));
+ ResultSet resultSet = session.execute(boundStatement);
+
List<AggregatedNumericMetric> metrics = new ArrayList<AggregatedNumericMetric>();
ResultSetMapper<AggregatedNumericMetric> resultSetMapper = new AggregateMetricMapper(true);
- ResultSet resultSet = session.execute(cql);
-
while (!resultSet.isExhausted()) {
metrics.add(resultSetMapper.map(resultSet.fetchOne(), resultSet.fetchOne(), resultSet.fetchOne()));
}
@@ -340,9 +351,9 @@ public class MetricsDAO {
PreparedStatement statement = session.prepare(METRICS_INDEX_QUERY);
BoundStatement boundStatement = statement.bind(table.toString());
ResultSet resultSet = session.execute(boundStatement);
+
List<MetricsIndexEntry> indexEntries = new ArrayList<MetricsIndexEntry>();
ResultSetMapper<MetricsIndexEntry> resultSetMapper = new MetricsIndexResultSetMapper(table);
-
for (Row row : resultSet) {
indexEntries.add(resultSetMapper.map(row));
}