Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Updated DB schema so asset data point and syslog tables now use timestamp data type

Rich Turner7c51f2c311db
diff --git a/agent/src/main/java/org/openremote/agent/protocol/http/PaginationFilter.java b/agent/src/main/java/org/openremote/agent/protocol/http/PaginationFilter.java
index 485f1ffc5..4d68fafe0 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/http/PaginationFilter.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/http/PaginationFilter.java
@@ -26,14 +26,14 @@ import java.net.URI;
public class PaginationFilter implements ClientRequestFilter {
- protected String nextUrl;
+ protected URI nextUrl;
- public PaginationFilter(String nextUrl) {
+ public PaginationFilter(URI nextUrl) {
this.nextUrl = nextUrl;
}
@Override
public void filter(ClientRequestContext requestContext) throws IOException {
- requestContext.setUri(URI.create(nextUrl));
+ requestContext.setUri(nextUrl);
}
}
diff --git a/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointService.java b/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointService.java
index 42783e038..127e5e9f7 100644
--- a/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointService.java
+++ b/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointService.java
@@ -29,10 +29,7 @@ import org.postgresql.util.PGInterval;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
+import java.sql.*;
import java.time.Duration;
import java.time.Instant;
import java.time.Period;
@@ -231,7 +228,7 @@ public class AssetDatapointService implements ContainerService, AssetUpdateProce
" ) TS " +
" left join ( " +
" select " +
- " date_trunc(?, to_timestamp(TIMESTAMP / 1000))::timestamp as TS, ");
+ " date_trunc(?, TIMESTAMP)::timestamp as TS, ");
if (attributeValueType == ValueType.NUMBER) {
query.append(" AVG(VALUE::text::numeric) as AVG_VALUE ");
@@ -241,9 +238,9 @@ public class AssetDatapointService implements ContainerService, AssetUpdateProce
query.append(" from ASSET_DATAPOINT " +
" where " +
- " to_timestamp(TIMESTAMP / 1000) >= to_timestamp(?) - ? " +
+ " TIMESTAMP >= to_timestamp(?) - ? " +
" and " +
- " to_timestamp(TIMESTAMP / 1000) <= to_timestamp(?) " +
+ " TIMESTAMP <= to_timestamp(?) " +
" and " +
" ENTITY_ID = ? and ATTRIBUTE_NAME = ? " +
" group by TS " +
@@ -251,11 +248,11 @@ public class AssetDatapointService implements ContainerService, AssetUpdateProce
" order by TS asc "
);
} else {
- query.append("select distinct to_timestamp(TIMESTAMP / 1000) AS X, value AS Y from ASSET_DATAPOINT " +
+ query.append("select distinct TIMESTAMP AS X, value AS Y from ASSET_DATAPOINT " +
"where " +
- "to_timestamp(TIMESTAMP / 1000) >= to_timestamp(?) - ? " +
+ "TIMESTAMP >= to_timestamp(?) - ? " +
"and " +
- "to_timestamp(TIMESTAMP / 1000) <= to_timestamp(?) " +
+ "TIMESTAMP <= to_timestamp(?) " +
"and " +
"ENTITY_ID = ? and ATTRIBUTE_NAME = ? "
);
@@ -325,7 +322,7 @@ public class AssetDatapointService implements ContainerService, AssetUpdateProce
persistenceService.doTransaction(em -> em.createQuery(
"delete from AssetDatapoint dp " +
"where dp.timestamp < :dt" + buildWhereClause(attributes, true)
- ).setParameter("dt", 1000L * timerService.getNow().truncatedTo(DAYS).minus(maxDatapointAgeDays, DAYS).getEpochSecond()).executeUpdate());
+ ).setParameter("dt", Date.from(timerService.getNow().truncatedTo(DAYS).minus(maxDatapointAgeDays, DAYS))).executeUpdate());
if (!attributes.isEmpty()) {
// Purge data points that have specific age constraints
@@ -344,7 +341,7 @@ public class AssetDatapointService implements ContainerService, AssetUpdateProce
persistenceService.doTransaction(em -> em.createQuery(
"delete from AssetDatapoint dp " +
"where dp.timestamp < :dt" + buildWhereClause(attrs, false)
- ).setParameter("dt", 1000L * timerService.getNow().truncatedTo(DAYS).minus(age, DAYS).getEpochSecond()).executeUpdate());
+ ).setParameter("dt", Date.from(timerService.getNow().truncatedTo(DAYS).minus(age, DAYS))).executeUpdate());
} catch (Exception e) {
LOG.log(Level.SEVERE, "An error occurred whilst deleting data points, this should not happen", e);
}
diff --git a/manager/src/main/resources/org/openremote/manager/setup/database/V20191202_01__Schema.sql b/manager/src/main/resources/org/openremote/manager/setup/database/V20191202_01__Schema.sql
index d376a67f6..da37fc98a 100644
--- a/manager/src/main/resources/org/openremote/manager/setup/database/V20191202_01__Schema.sql
+++ b/manager/src/main/resources/org/openremote/manager/setup/database/V20191202_01__Schema.sql
@@ -25,16 +25,15 @@ create table ASSET (
ASSET_TYPE varchar(255) not null,
ACCESS_PUBLIC_READ boolean not null,
OBJ_VERSION int8 not null,
- LOCATION geometry,
primary key (ID),
check (ID != PARENT_ID)
);
create table ASSET_DATAPOINT (
- TIMESTAMP int8 not null,
- ENTITY_ID varchar(36) not null,
- ATTRIBUTE_NAME varchar(255) not null,
- VALUE jsonb not null,
+ TIMESTAMP timestamp with time zone not null,
+ ENTITY_ID varchar(36) not null,
+ ATTRIBUTE_NAME varchar(255) not null,
+ VALUE jsonb not null,
primary key (TIMESTAMP, ENTITY_ID, ATTRIBUTE_NAME)
);
@@ -114,7 +113,7 @@ create table NOTIFICATION (
create table SYSLOG_EVENT (
ID int8 not null,
- TIMESTAMP int8 not null,
+ TIMESTAMP timestamp with time zone not null,
CATEGORY varchar(255) not null,
LEVEL int4 not null,
MESSAGE varchar(131072),
diff --git a/model/src/main/java/org/openremote/model/attribute/MetaItem.java b/model/src/main/java/org/openremote/model/attribute/MetaItem.java
index f6916e1dc..9c66f1ffe 100644
--- a/model/src/main/java/org/openremote/model/attribute/MetaItem.java
+++ b/model/src/main/java/org/openremote/model/attribute/MetaItem.java
@@ -21,7 +21,6 @@ package org.openremote.model.attribute;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
-import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.gwt.regexp.shared.RegExp;
import org.openremote.model.AbstractValueHolder;
diff --git a/model/src/main/java/org/openremote/model/datapoint/Datapoint.java b/model/src/main/java/org/openremote/model/datapoint/Datapoint.java
index 5bb8a2375..ba9b46215 100644
--- a/model/src/main/java/org/openremote/model/datapoint/Datapoint.java
+++ b/model/src/main/java/org/openremote/model/datapoint/Datapoint.java
@@ -24,11 +24,10 @@ import org.openremote.model.attribute.AttributeRef;
import org.openremote.model.attribute.AttributeState;
import org.openremote.model.value.Value;
-import javax.persistence.Column;
-import javax.persistence.Id;
-import javax.persistence.IdClass;
-import javax.persistence.MappedSuperclass;
+import javax.persistence.*;
import java.io.Serializable;
+import java.util.Date;
+import java.util.Objects;
import static org.openremote.model.Constants.PERSISTENCE_JSON_VALUE_TYPE;
@@ -50,8 +49,8 @@ public abstract class Datapoint implements Serializable {
protected String attributeName;
@Id
- @Column(name = "TIMESTAMP", nullable = false)
- protected long timestamp;
+ @Column(name = "TIMESTAMP", updatable = false, nullable = false, columnDefinition = "TIMESTAMP WITH TIME ZONE")
+ protected Date timestamp;
@Column(name = "VALUE", columnDefinition = "jsonb", nullable = false)
@org.hibernate.annotations.Type(type = PERSISTENCE_JSON_VALUE_TYPE)
@@ -75,7 +74,7 @@ public abstract class Datapoint implements Serializable {
public Datapoint(String entityId, String attributeName, Value value, long timestamp) {
this.entityId = entityId;
this.attributeName = attributeName;
- this.timestamp = timestamp;
+ this.timestamp = new Date(timestamp);
this.value = value;
}
@@ -96,11 +95,11 @@ public abstract class Datapoint implements Serializable {
}
public long getTimestamp() {
- return timestamp;
+ return timestamp != null ? timestamp.getTime() : 0L;
}
public void setTimestamp(long timestamp) {
- this.timestamp = timestamp;
+ this.timestamp = new Date(timestamp);
}
public Value getValue() {
@@ -126,11 +125,7 @@ public abstract class Datapoint implements Serializable {
@Override
public int hashCode() {
- int result = entityId.hashCode();
- result = 31 * result + attributeName.hashCode();
- result = 31 * result + (int) (timestamp ^ (timestamp >>> 32));
- result = 31 * result + value.hashCode();
- return result;
+ return Objects.hash(entityId, attributeName, timestamp, value);
}
@Override
diff --git a/model/src/main/java/org/openremote/model/event/Event.java b/model/src/main/java/org/openremote/model/event/Event.java
index 9aad229be..a88450766 100644
--- a/model/src/main/java/org/openremote/model/event/Event.java
+++ b/model/src/main/java/org/openremote/model/event/Event.java
@@ -23,6 +23,9 @@ import org.openremote.model.util.TextUtil;
import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
+import javax.persistence.Temporal;
+import javax.persistence.TemporalType;
+import java.util.Date;
import java.util.logging.Logger;
/**
@@ -37,11 +40,12 @@ public abstract class Event {
private static final Logger LOG = Logger.getLogger(Event.class.getName());
- @Column(name = "TIMESTAMP", nullable = false)
- public long timestamp;
+ @Temporal(TemporalType.TIMESTAMP)
+ @Column(name = "TIMESTAMP", updatable = false, nullable = false, columnDefinition = "TIMESTAMP WITH TIME ZONE")
+ public Date timestamp;
protected Event(long timestamp) {
- this.timestamp = timestamp;
+ this.timestamp = new Date(timestamp);
}
protected Event() {
@@ -63,11 +67,11 @@ public abstract class Event {
}
public long getTimestamp() {
- return timestamp;
+ return timestamp != null ? timestamp.getTime() : 0L;
}
public void setTimestamp(long timestamp) {
- this.timestamp = timestamp;
+ this.timestamp = new Date(timestamp);
}
@Override
diff --git a/model/src/main/java/org/openremote/model/event/shared/SharedEvent.java b/model/src/main/java/org/openremote/model/event/shared/SharedEvent.java
index 821c08cae..da04b3129 100644
--- a/model/src/main/java/org/openremote/model/event/shared/SharedEvent.java
+++ b/model/src/main/java/org/openremote/model/event/shared/SharedEvent.java
@@ -19,7 +19,6 @@
*/
package org.openremote.model.event.shared;
-import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.openremote.model.asset.AssetEvent;