Search in sources :

Example 1 with DatapointPeriod

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);
                }
            }
        }
    }));
}
Also used : DatapointPeriod(org.openremote.model.datapoint.DatapointPeriod) AbstractReturningWork(org.hibernate.jdbc.AbstractReturningWork) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 AbstractReturningWork (org.hibernate.jdbc.AbstractReturningWork)1 DatapointPeriod (org.openremote.model.datapoint.DatapointPeriod)1