use of org.openremote.model.datapoint.DatapointPeriod in project openremote by openremote.
the class AbstractDatapointService method getDatapointPeriod.
public DatapointPeriod getDatapointPeriod(String assetId, String attributeName) {
return persistenceService.doReturningTransaction(em -> em.unwrap(Session.class).doReturningWork(new AbstractReturningWork<DatapointPeriod>() {
@Override
public DatapointPeriod execute(Connection connection) throws SQLException {
String tableName = getDatapointTableName();
String query = "SELECT DISTINCT periods.* FROM " + "(SELECT entity_id, attribute_name, " + "MIN(timestamp) AS oldestTimestamp, MAX(timestamp) AS latestTimestamp " + "FROM " + tableName + " GROUP BY entity_id, attribute_name) AS periods " + "INNER JOIN " + tableName + " ON " + tableName + ".entity_id = periods.entity_id AND " + tableName + ".attribute_name = periods.attribute_name " + "WHERE " + tableName + ".entity_id = ? " + "AND " + tableName + ".attribute_name = ? ";
try (PreparedStatement st = connection.prepareStatement(query)) {
st.setString(1, assetId);
st.setString(2, attributeName);
try (ResultSet rs = st.executeQuery()) {
if (rs.next()) {
return new DatapointPeriod(rs.getString(1), rs.getString(2), rs.getTimestamp(3).getTime(), rs.getTimestamp(4).getTime());
}
return new DatapointPeriod(assetId, attributeName, null, null);
}
}
}
}));
}
Aggregations