100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Feature: Datapoints improvement for charting with use of algorithms/calculations (#932)
Martin Peetersb5906d95b1cc
diff --git a/.github/workflows/ci_cd.yml b/.github/workflows/ci_cd.ymlindex 57d26ca4c..311123286 100644--- a/.github/workflows/ci_cd.yml+++ b/.github/workflows/ci_cd.yml@@ -656,6 +656,10 @@ jobs:composeProfile="openremote/$composeProfile"fi+ # Make temp dir and set mask to 777 as docker seems to run as root+ mkdir tmp+ chmod 777 tmp+# Define cleanup commandecho "cleanup=docker-compose -f $composeProfile down" >> $GITHUB_OUTPUTdiff --git a/container/src/main/java/org/openremote/container/persistence/PersistenceService.java b/container/src/main/java/org/openremote/container/persistence/PersistenceService.javaindex 22b3a9204..1a0e77311 100644--- a/container/src/main/java/org/openremote/container/persistence/PersistenceService.java+++ b/container/src/main/java/org/openremote/container/persistence/PersistenceService.java@@ -19,13 +19,12 @@*/package org.openremote.container.persistence;-import org.apache.camel.ExchangePattern;import org.apache.camel.FluentProducerTemplate;import org.apache.camel.Predicate;-import org.apache.camel.ProducerTemplate;import org.flywaydb.core.Flyway;import org.flywaydb.core.api.MigrationInfo;import org.flywaydb.core.api.output.MigrateResult;+import org.flywaydb.core.internal.sqlscript.FlywaySqlScriptException;import org.hibernate.Session;import org.hibernate.annotations.Formula;import org.hibernate.cfg.AvailableSettings;@@ -33,7 +32,10 @@ import org.hibernate.engine.spi.SharedSessionContractImplementor;import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl;import org.hibernate.jpa.boot.internal.PersistenceUnitInfoDescriptor;import org.openremote.container.message.MessageBrokerService;-import org.openremote.model.*;+import org.openremote.model.Container;+import org.openremote.model.ContainerService;+import org.openremote.model.EntityClassProvider;+import org.openremote.model.PersistenceEvent;import org.openremote.model.apps.ConsoleAppConfig;import org.openremote.model.asset.Asset;import org.openremote.model.asset.AssetDescriptor;@@ -41,9 +43,9 @@ import org.openremote.model.asset.UserAssetLink;import org.openremote.model.asset.impl.UnknownAsset;import org.openremote.model.dashboard.Dashboard;import org.openremote.model.datapoint.AssetDatapoint;+import org.openremote.model.datapoint.AssetPredictedDatapoint;import org.openremote.model.gateway.GatewayConnection;import org.openremote.model.notification.SentNotification;-import org.openremote.model.datapoint.AssetPredictedDatapoint;import org.openremote.model.provisioning.ProvisioningConfig;import org.openremote.model.provisioning.X509ProvisioningConfig;import org.openremote.model.rules.AssetRuleset;@@ -486,16 +488,32 @@ public class PersistenceService implements ContainerService, Consumer<PersistencappendSchemas(schemas);appendSchemaLocations(locations);+ // Adding timescaledb extension to make sure it has been initialized. Flyway clean caused issues with relations to timescaledb tables.+ // Excluding the extension(s) in the cleanup process will not be added soon https://github.com/flyway/flyway/issues/2271+ // Now applied it here (so it is excluded for the migration process), to prevent that flyway drops the extension during cleanup.+ StringBuilder initSql = new StringBuilder();+ initSql.append("CREATE EXTENSION IF NOT EXISTS timescaledb SCHEMA public cascade;");+ initSql.append("CREATE EXTENSION IF NOT EXISTS timescaledb_toolkit SCHEMA public cascade;");+flyway = Flyway.configure().cleanDisabled(false).validateMigrationNaming(true).dataSource(connectionUrl, databaseUsername, databasePassword).schemas(schemas.toArray(new String[0])).locations(locations.toArray(new String[0]))+ .initSql(initSql.toString()).baselineOnMigrate(true).load();- MigrationInfo currentMigration = flyway.info().current();+ MigrationInfo currentMigration;+ try {+ currentMigration = flyway.info().current();+ } catch (FlywaySqlScriptException fssex) {+ if(fssex.getStatement().contains("CREATE EXTENSION IF NOT EXISTS timescaledb")) { // ... SCHEMA public cascade;+ LOG.severe("Timescale DB extension not found; please ensure you are using a postgres image with timescale DB extension included.");+ }+ throw fssex;+ }if (currentMigration == null && !forceClean) {LOG.warning("DB is empty so changing forceClean to true");diff --git a/gradle.properties b/gradle.propertiesindex 934ab57ab..51a30710a 100644--- a/gradle.properties+++ b/gradle.properties@@ -34,7 +34,7 @@ jSerialCommVersion = 2.7.0saxonHEVersion = 11.3jmxtermVersion = 1.0.0nettyVersion = 4.1.79.Final-flywaydbVersion = 9.0.1+flywaydbVersion = 9.15.1ical4jVersion = 3.2.6friendlyIdVersion = 1.0.1geotoolsVersion = 19.1diff --git a/manager/build.gradle b/manager/build.gradleindex e486628f9..7163b2fb6 100644--- a/manager/build.gradle+++ b/manager/build.gradle@@ -57,6 +57,11 @@ jar {archivesBaseName = "openremote-${project.name}"}+afterEvaluate {+ // Change the working directory for the run task+ tasks.run.workingDir = isCustomProject() ? project(":openremote").projectDir : rootProject.projectDir+}+distributions {main {contents {diff --git a/manager/src/main/java/org/openremote/manager/datapoint/AbstractDatapointService.java b/manager/src/main/java/org/openremote/manager/datapoint/AbstractDatapointService.javaindex d69e4fb09..c01216249 100644--- a/manager/src/main/java/org/openremote/manager/datapoint/AbstractDatapointService.java+++ b/manager/src/main/java/org/openremote/manager/datapoint/AbstractDatapointService.java@@ -1,3 +1,22 @@+/*+ * Copyright 2023, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.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 Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * 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 Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <http://www.gnu.org/licenses/>.+ */package org.openremote.manager.datapoint;import com.fasterxml.jackson.databind.JsonNode;@@ -12,12 +31,11 @@ import org.openremote.model.asset.Asset;import org.openremote.model.attribute.Attribute;import org.openremote.model.attribute.AttributeRef;import org.openremote.model.datapoint.Datapoint;-import org.openremote.model.datapoint.DatapointInterval;import org.openremote.model.datapoint.DatapointPeriod;import org.openremote.model.datapoint.ValueDatapoint;+import org.openremote.model.datapoint.query.AssetDatapointQuery;import org.openremote.model.util.Pair;import org.openremote.model.util.ValueUtil;-import org.postgresql.util.PGInterval;import org.postgresql.util.PGobject;import javax.persistence.TypedQuery;@@ -31,6 +49,7 @@ import java.time.temporal.ChronoUnit;import java.util.ArrayList;import java.util.Date;import java.util.List;+import java.util.Map;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.ScheduledFuture;import java.util.logging.Level;@@ -150,168 +169,44 @@ public abstract class AbstractDatapointService<T extends Datapoint> implements C});}- public ValueDatapoint<?>[] getValueDatapoints(AttributeRef attributeRef,- DatapointInterval datapointInterval,- Integer stepSize,- LocalDateTime fromTimestamp,- LocalDateTime toTimestamp) {-- Asset<?> asset = assetStorageService.find(attributeRef.getId());- if (asset == null) {- throw new IllegalStateException("Asset not found: " + attributeRef.getId());+ public ValueDatapoint<?>[] queryDatapoints(String assetId, String attributeName, AssetDatapointQuery datapointQuery) {+ Asset<?> asset = assetStorageService.find(assetId, true);+ if(asset == null) {+ throw new IllegalStateException("Asset not found: " + assetId);}+ Attribute<?> assetAttribute = asset.getAttribute(attributeName)+ .orElseThrow(() -> new IllegalStateException("Attribute not found: " + attributeName));- Attribute<?> assetAttribute = asset.getAttribute(attributeRef.getName())- .orElseThrow(() -> new IllegalStateException("Attribute not found: " + attributeRef.getName()));-- return getValueDatapoints(asset.getId(), assetAttribute, datapointInterval, stepSize, fromTimestamp, toTimestamp);+ return this.queryDatapoints(asset.getId(), assetAttribute, datapointQuery);}- public ValueDatapoint<?>[] getValueDatapoints(String assetId,- Attribute<?> attribute,- DatapointInterval datapointInterval,- final Integer stepSize,- LocalDateTime fromTimestamp,- LocalDateTime toTimestamp) {+ public ValueDatapoint<?>[] queryDatapoints(String assetId, Attribute<?> attribute, AssetDatapointQuery datapointQuery) {AttributeRef attributeRef = new AttributeRef(assetId, attribute.getName());+ Map<Integer, Object> parameters = datapointQuery.getSQLParameters(attributeRef);- getLogger().finest("Getting datapoints for: " + attributeRef);+ getLogger().finest("Querying datapoints for: " + attributeRef);return persistenceService.doReturningTransaction(entityManager ->+ entityManager.unwrap(Session.class).doReturningWork(new AbstractReturningWork<>() {- entityManager.unwrap(Session.class).doReturningWork(new AbstractReturningWork<ValueDatapoint<?>[]>() {@Overridepublic ValueDatapoint<?>[] execute(Connection connection) throws SQLException {Class<?> attributeType = attribute.getType().getType();boolean isNumber = Number.class.isAssignableFrom(attributeType);boolean isBoolean = Boolean.class.isAssignableFrom(attributeType);- StringBuilder query = new StringBuilder();- boolean downsample = isNumber || isBoolean;-- String truncate = null;- String part = null;- String interval = null;- String stepStr = null;- String partQuery = "date_part(?, ?)::int";- String partQuery2 = "date_part(?, TIMESTAMP)::int";- int step = 1;-- if (downsample) {-- switch (datapointInterval) {-- case MINUTE:- // This works with minutes of the day so not constrained to step size < 60- step = stepSize == null ? 1 : Math.max(1, Math.min(1440, stepSize));- truncate = "day";- part = "min";- interval = "min";- partQuery = "(date_part('hour', ?)::int * 60 + date_part(?, ?)::int)";- partQuery2 = "(date_part('hour', TIMESTAMP)::int * 60 + date_part(?, TIMESTAMP)::int)";- break;- case HOUR:- step = stepSize == null ? 1 : Math.max(1, Math.min(24, stepSize));- truncate = "day";- part = "hour";- interval = "hour";- break;- case DAY:- step = stepSize == null ? 1 : Math.max(1, Math.min(365, stepSize));- truncate = "year";- part = "doy";- interval = "day";- break;- case WEEK:- step = stepSize == null ? 1 : Math.max(1, Math.min(53, stepSize));- truncate = "year";- part = "week";- interval = "week";- break;- case MONTH:- step = stepSize == null ? 1 : Math.max(1, Math.min(12, stepSize));- truncate = "year";- part = "month";- interval = "month";- break;- case YEAR:- step = stepSize == null ? 1 : Math.max(1, stepSize);- truncate = "decade";- part = "year";- interval = "year";- break;- default:- throw new UnsupportedOperationException("Can't handle interval: " + datapointInterval);- }- stepStr = step + " " + interval;-- // TODO: Change this to use something like this max min decimation algorithm https://knowledge.ni.com/KnowledgeArticleDetails?id=kA00Z0000019YLKSA2&l=en-GB)- query.append("select PERIOD as X, AVG_VALUE as Y " +- "from generate_series(date_trunc(?, ?) + " + partQuery + " / ? * ?, date_trunc(?, ?) + " + partQuery + " / ? * ?, ?) PERIOD left join ( " +- "select (date_trunc(?, TIMESTAMP) + " + partQuery2 + " / ? * ?)::timestamp as TS, ");-- if (isNumber) {- query.append(" AVG(VALUE::text::numeric) as AVG_VALUE ");- } else {- query.append(" AVG(case when VALUE::text::boolean is true then 1 else 0 end) as AVG_VALUE ");- }-- query.append("from " + getDatapointTableName() +- " where TIMESTAMP >= date_trunc(?, ?) and TIMESTAMP < (date_trunc(?, ?) + ?) and ENTITY_ID = ? and ATTRIBUTE_NAME = ? group by TS) DP on DP.TS = PERIOD order by PERIOD asc");-- } else {- query.append("select distinct TIMESTAMP AS X, value AS Y from " + getDatapointTableName() +- " where " +- "TIMESTAMP >= ?" +- "and " +- "TIMESTAMP <= ? " +- "and " +- "ENTITY_ID = ? and ATTRIBUTE_NAME = ?"- );- }- try (PreparedStatement st = connection.prepareStatement(query.toString())) {+ try (PreparedStatement st = connection.prepareStatement(datapointQuery.getSQLQuery(getDatapointTableName(), attributeType))) {- if (downsample) {- int counter = 1;- boolean isMinute = datapointInterval == DatapointInterval.MINUTE;-- st.setString(counter++, truncate);- st.setObject(counter++, fromTimestamp);- if (isMinute) {- st.setObject(counter++, fromTimestamp);- }- st.setString(counter++, part);- st.setObject(counter++, fromTimestamp);- st.setInt(counter++, step);- st.setObject(counter++, new PGInterval(stepStr));- st.setString(counter++, truncate);- st.setObject(counter++, toTimestamp);- if (isMinute) {- st.setObject(counter++, toTimestamp);+ if(parameters.size() > 0) {+ for(Map.Entry<Integer, Object> param : parameters.entrySet()) {+ if(param.getValue() instanceof String) {+ st.setString(param.getKey(), param.getValue().toString());+ } else {+ st.setObject(param.getKey(), param.getValue());+ }}- st.setString(counter++, part);- st.setObject(counter++, toTimestamp);- st.setInt(counter++, step);- st.setObject(counter++, new PGInterval(stepStr));- st.setObject(counter++, new PGInterval(stepStr));- st.setString(counter++, truncate);- st.setString(counter++, part);- st.setInt(counter++, step);- st.setObject(counter++, new PGInterval(stepStr));- st.setString(counter++, interval);- st.setObject(counter++, fromTimestamp);- st.setString(counter++, interval);- st.setObject(counter++, toTimestamp);- st.setObject(counter++, new PGInterval(stepStr));- st.setString(counter++, attributeRef.getId());- st.setString(counter++, attributeRef.getName());- } else {- st.setObject(1, fromTimestamp);- st.setObject(2, toTimestamp);- st.setString(3, attributeRef.getId());- st.setString(4, attributeRef.getName());}try (ResultSet rs = st.executeQuery()) {@@ -319,7 +214,7 @@ public abstract class AbstractDatapointService<T extends Datapoint> implements Cwhile (rs.next()) {Object value = null;if (rs.getObject(2) != null) {- if (downsample) {+ if(isNumber || isBoolean) {value = ValueUtil.getValueCoerced(rs.getObject(2), Double.class).orElse(null);} else {if (rs.getObject(2) instanceof PGobject) {diff --git a/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointResourceImpl.java b/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointResourceImpl.javaindex 51955a44f..48227184d 100644--- a/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointResourceImpl.java+++ b/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointResourceImpl.java@@ -28,8 +28,8 @@ import org.openremote.manager.web.ManagerWebResource;import org.openremote.model.asset.Asset;import org.openremote.model.attribute.Attribute;import org.openremote.model.attribute.AttributeRef;+import org.openremote.model.datapoint.query.AssetDatapointQuery;import org.openremote.model.datapoint.AssetDatapointResource;-import org.openremote.model.datapoint.DatapointInterval;import org.openremote.model.datapoint.DatapointPeriod;import org.openremote.model.datapoint.ValueDatapoint;import org.openremote.model.http.RequestParams;@@ -44,9 +44,6 @@ import javax.ws.rs.container.ConnectionCallback;import javax.ws.rs.core.HttpHeaders;import javax.ws.rs.core.Response;import java.io.*;-import java.time.Instant;-import java.time.LocalDateTime;-import java.time.ZoneId;import java.util.concurrent.ScheduledFuture;import java.util.logging.Level;import java.util.logging.Logger;@@ -77,10 +74,7 @@ public class AssetDatapointResourceImpl extends ManagerWebResource implements Aspublic ValueDatapoint<?>[] getDatapoints(@BeanParam RequestParams requestParams,String assetId,String attributeName,- DatapointInterval interval,- Integer stepSize,- long fromTimestamp,- long toTimestamp) {+ AssetDatapointQuery query) {try {if (isRestrictedUser() && !assetStorageService.isUserAsset(getUserId(), assetId)) {@@ -94,7 +88,7 @@ public class AssetDatapointResourceImpl extends ManagerWebResource implements As}if (!isRealmActiveAndAccessible(asset.getRealm())) {- LOG.info("Forbidden access for user '" + getUsername() + "': " + asset);+ LOG.info("Forbidden access for user '" + getUsername() + "': " + asset.getRealm());throw new WebApplicationException(Response.Status.FORBIDDEN);}@@ -102,12 +96,8 @@ public class AssetDatapointResourceImpl extends ManagerWebResource implements Asnew WebApplicationException(Response.Status.NOT_FOUND));- return assetDatapointService.getValueDatapoints(assetId,- attribute,- interval,- stepSize,- LocalDateTime.ofInstant(Instant.ofEpochMilli(fromTimestamp), ZoneId.systemDefault()),- LocalDateTime.ofInstant(Instant.ofEpochMilli(toTimestamp), ZoneId.systemDefault()));+ return assetDatapointService.queryDatapoints(assetId, attribute, query);+} catch (IllegalStateException ex) {throw new BadRequestException(ex);} catch (UnsupportedOperationException ex) {diff --git a/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointService.java b/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointService.javaindex 24be39209..82ae6fd6b 100644--- a/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointService.java+++ b/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointService.java@@ -57,7 +57,7 @@ public class AssetDatapointService extends AbstractDatapointService<AssetDatapoipublic static final String OR_DATA_POINTS_MAX_AGE_DAYS = "OR_DATA_POINTS_MAX_AGE_DAYS";public static final int OR_DATA_POINTS_MAX_AGE_DAYS_DEFAULT = 31;public static final String OR_DATA_POINTS_EXPORT_DIR = "OR_DATA_POINTS_EXPORT_DIR";- public static final String OR_DATA_POINTS_EXPORT_DIR_DEFAULT = "/tmp";+ public static final String OR_DATA_POINTS_EXPORT_DIR_DEFAULT = "tmp";private static final Logger LOG = Logger.getLogger(AssetDatapointService.class.getName());protected int maxDatapointAgeDays;protected Path exportPath;@@ -207,7 +207,7 @@ public class AssetDatapointService extends AbstractDatapointService<AssetDatapoisb.append(Arrays.stream(attributeRefs).map(attributeRef -> String.format("(ad.entity_id = '%s' and ad.attribute_name = '%s')", attributeRef.getId(), attributeRef.getName())).collect(Collectors.joining(" or ")));- sb.append(String.format(")) to '/tmp/%s' delimiter ',' CSV HEADER;", fileName));+ sb.append(String.format(")) to '" + exportPath.toString() + "/%s' delimiter ',' CSV HEADER;", fileName));persistenceService.doTransaction(em -> em.createNativeQuery(sb.toString()).executeUpdate());diff --git a/manager/src/main/java/org/openremote/manager/datapoint/AssetPredictedDatapointResourceImpl.java b/manager/src/main/java/org/openremote/manager/datapoint/AssetPredictedDatapointResourceImpl.javaindex c47a31562..546fa5cb9 100644--- a/manager/src/main/java/org/openremote/manager/datapoint/AssetPredictedDatapointResourceImpl.java+++ b/manager/src/main/java/org/openremote/manager/datapoint/AssetPredictedDatapointResourceImpl.java@@ -25,17 +25,14 @@ import org.openremote.manager.security.ManagerIdentityService;import org.openremote.manager.web.ManagerWebResource;import org.openremote.model.asset.Asset;import org.openremote.model.attribute.Attribute;+import org.openremote.model.datapoint.query.AssetDatapointQuery;import org.openremote.model.datapoint.AssetPredictedDatapointResource;-import org.openremote.model.datapoint.DatapointInterval;import org.openremote.model.datapoint.ValueDatapoint;import org.openremote.model.http.RequestParams;import javax.ws.rs.BeanParam;import javax.ws.rs.WebApplicationException;import javax.ws.rs.core.Response;-import java.time.Instant;-import java.time.LocalDateTime;-import java.time.ZoneId;import java.util.logging.Logger;public class AssetPredictedDatapointResourceImpl extends ManagerWebResource implements AssetPredictedDatapointResource {@@ -58,10 +55,7 @@ public class AssetPredictedDatapointResourceImpl extends ManagerWebResource implpublic ValueDatapoint<?>[] getPredictedDatapoints(@BeanParam RequestParams requestParams,String assetId,String attributeName,- DatapointInterval interval,- Integer stepSize,- long fromTimestamp,- long toTimestamp) {+ AssetDatapointQuery query) {try {if (isRestrictedUser() && !assetStorageService.isUserAsset(getUserId(), assetId)) {@@ -75,7 +69,7 @@ public class AssetPredictedDatapointResourceImpl extends ManagerWebResource impl}if (!isRealmActiveAndAccessible(asset.getRealm())) {- LOG.info("Forbidden access for user '" + getUsername() + "': " + asset);+ LOG.info("Forbidden access for user '" + getUsername() + "': " + asset.getRealm());throw new WebApplicationException(Response.Status.FORBIDDEN);}@@ -83,12 +77,7 @@ public class AssetPredictedDatapointResourceImpl extends ManagerWebResource implnew WebApplicationException(Response.Status.NOT_FOUND));- return assetPredictedDatapointService.getValueDatapoints(assetId,- attribute,- interval,- stepSize,- LocalDateTime.ofInstant(Instant.ofEpochMilli(fromTimestamp), ZoneId.systemDefault()),- LocalDateTime.ofInstant(Instant.ofEpochMilli(toTimestamp), ZoneId.systemDefault()));+ return assetPredictedDatapointService.queryDatapoints(assetId, attribute, query);} catch (IllegalStateException ex) {throw new WebApplicationException(ex, Response.Status.BAD_REQUEST);}diff --git a/manager/src/main/java/org/openremote/manager/energy/EnergyOptimisationService.java b/manager/src/main/java/org/openremote/manager/energy/EnergyOptimisationService.javaindex 384b56fb3..b5b6f06b9 100644--- a/manager/src/main/java/org/openremote/manager/energy/EnergyOptimisationService.java+++ b/manager/src/main/java/org/openremote/manager/energy/EnergyOptimisationService.java@@ -37,8 +37,8 @@ import org.openremote.model.attribute.Attribute;import org.openremote.model.attribute.AttributeEvent;import org.openremote.model.attribute.AttributeExecuteStatus;import org.openremote.model.attribute.AttributeRef;-import org.openremote.model.datapoint.DatapointInterval;import org.openremote.model.datapoint.ValueDatapoint;+import org.openremote.model.datapoint.query.AssetDatapointIntervalQuery;import org.openremote.model.query.AssetQuery;import org.openremote.model.query.LogicGroup;import org.openremote.model.query.filter.AttributePredicate;@@ -799,12 +799,16 @@ public class EnergyOptimisationService extends RouteBuilder implements Containerif (attribute.hasMeta(MetaItemType.HAS_PREDICTED_DATA_POINTS)) {LocalDateTime timestamp = LocalDateTime.ofInstant(optimisationTime, ZoneId.systemDefault());- ValueDatapoint<?>[] predictedData = assetPredictedDatapointService.getValueDatapoints(- ref,- DatapointInterval.MINUTE,- (int)(intervalSize * 60),- timestamp,- timestamp.plus(24, HOURS).minus((long)(intervalSize * 60), ChronoUnit.MINUTES)+ ValueDatapoint<?>[] predictedData = assetPredictedDatapointService.queryDatapoints(+ ref.getId(),+ ref.getName(),+ new AssetDatapointIntervalQuery(+ timestamp,+ timestamp.plus(24, HOURS).minus((long)(intervalSize * 60), ChronoUnit.MINUTES),+ (intervalSize * 60) + " minutes",+ AssetDatapointIntervalQuery.Formula.AVG,+ true+ ));if (predictedData.length != values.length) {LOG.warning("Returned predicted data point count does not match interval count: Ref=" + ref + ", expected=" + values.length + ", actual=" + predictedData.length);@@ -820,8 +824,8 @@ public class EnergyOptimisationService extends RouteBuilder implements ContainerDouble next = null;int j = i-1;while (previous == null && j >= 0) {- previous = (Double) predictedData[j].getValue();- j--;+ previous = (Double) predictedData[j].getValue();+ j--;}j = i+1;while (next == null && j < predictedData.length) {diff --git a/manager/src/main/java/org/openremote/manager/rules/facade/HistoricFacade.java b/manager/src/main/java/org/openremote/manager/rules/facade/HistoricFacade.javaindex a55c9d092..c107ce7fc 100644--- a/manager/src/main/java/org/openremote/manager/rules/facade/HistoricFacade.java+++ b/manager/src/main/java/org/openremote/manager/rules/facade/HistoricFacade.java@@ -22,12 +22,11 @@ package org.openremote.manager.rules.facade;import org.openremote.manager.datapoint.AssetDatapointService;import org.openremote.manager.rules.RulesEngineId;import org.openremote.model.attribute.AttributeRef;-import org.openremote.model.datapoint.DatapointInterval;import org.openremote.model.datapoint.ValueDatapoint;+import org.openremote.model.datapoint.query.AssetDatapointQuery;import org.openremote.model.rules.HistoricDatapoints;import org.openremote.model.rules.Ruleset;-import java.time.LocalDateTime;import java.util.logging.Logger;public class HistoricFacade<T extends Ruleset> extends HistoricDatapoints {@@ -42,12 +41,7 @@ public class HistoricFacade<T extends Ruleset> extends HistoricDatapoints {}@Override- public ValueDatapoint<?>[] getValueDatapoints(AttributeRef attributeRef, DatapointInterval interval, LocalDateTime fromTimestamp, LocalDateTime toTimestamp) {- return assetDatapointService.getValueDatapoints(attributeRef, interval, null, fromTimestamp, toTimestamp);- }-- @Override- public ValueDatapoint<?>[] getValueDatapoints(AttributeRef attributeRef, DatapointInterval interval, int stepSize, LocalDateTime fromTimestamp, LocalDateTime toTimestamp) {- return assetDatapointService.getValueDatapoints(attributeRef, interval, stepSize, fromTimestamp, toTimestamp);+ public ValueDatapoint<?>[] getValueDatapoints(AttributeRef attributeRef, AssetDatapointQuery query) {+ return assetDatapointService.queryDatapoints(attributeRef.getId(), attributeRef.getName(), query);}}diff --git a/manager/src/main/java/org/openremote/manager/rules/facade/PredictedFacade.java b/manager/src/main/java/org/openremote/manager/rules/facade/PredictedFacade.javaindex 78f2640cd..ba75018f1 100644--- a/manager/src/main/java/org/openremote/manager/rules/facade/PredictedFacade.java+++ b/manager/src/main/java/org/openremote/manager/rules/facade/PredictedFacade.java@@ -22,8 +22,8 @@ package org.openremote.manager.rules.facade;import org.openremote.manager.datapoint.AssetPredictedDatapointService;import org.openremote.manager.rules.RulesEngineId;import org.openremote.model.attribute.AttributeRef;-import org.openremote.model.datapoint.DatapointInterval;import org.openremote.model.datapoint.ValueDatapoint;+import org.openremote.model.datapoint.query.AssetDatapointQuery;import org.openremote.model.rules.PredictedDatapoints;import org.openremote.model.rules.Ruleset;@@ -41,13 +41,8 @@ public class PredictedFacade<T extends Ruleset> extends PredictedDatapoints {@Override- public ValueDatapoint<?>[] getValueDatapoints(AttributeRef attributeRef, DatapointInterval interval, LocalDateTime fromTimestamp, LocalDateTime toTimestamp) {- return assetPredictedDatapointService.getValueDatapoints(attributeRef, interval, null, fromTimestamp, toTimestamp);- }-- @Override- public ValueDatapoint<?>[] getValueDatapoints(AttributeRef attributeRef, DatapointInterval interval, int stepSize, LocalDateTime fromTimestamp, LocalDateTime toTimestamp) {- return assetPredictedDatapointService.getValueDatapoints(attributeRef, interval, stepSize, fromTimestamp, toTimestamp);+ public ValueDatapoint<?>[] getValueDatapoints(AttributeRef attributeRef, AssetDatapointQuery query) {+ return assetPredictedDatapointService.queryDatapoints(attributeRef.getId(), attributeRef.getName(), query);}@Overridediff --git a/manager/src/main/resources/org/openremote/manager/setup/database/V20230120_01__Timescale.sql b/manager/src/main/resources/org/openremote/manager/setup/database/V20230120_01__Timescale.sqlnew file mode 100644index 000000000..3f069ca01--- /dev/null+++ b/manager/src/main/resources/org/openremote/manager/setup/database/V20230120_01__Timescale.sql@@ -0,0 +1,2 @@+SELECT public.create_hypertable('asset_datapoint', 'timestamp', if_not_exists := true, migrate_data := true);+SELECT public.create_hypertable('asset_predicted_datapoint', 'timestamp', if_not_exists := true, migrate_data := true);diff --git a/manager/src/main/resources/org/openremote/manager/setup/database/beforeClean.sql b/manager/src/main/resources/org/openremote/manager/setup/database/beforeClean.sqldeleted file mode 100644index 3d8325aa2..000000000--- a/manager/src/main/resources/org/openremote/manager/setup/database/beforeClean.sql+++ /dev/null@@ -1,2 +0,0 @@-drop extension if exists POSTGIS cascade;-drop extension if exists CITEXT cascade;\ No newline at end of filediff --git a/manager/src/web/shared/locales/en/or.json b/manager/src/web/shared/locales/en/or.jsonindex de276e844..ef41b06e8 100644--- a/manager/src/web/shared/locales/en/or.json+++ b/manager/src/web/shared/locales/en/or.json@@ -127,13 +127,32 @@"location": "Location","longPressSetLoc": "Long press to set location","hour": "Hour",+ "lastHour": "Last hour",+ "last24Hours": "Last 24 hours",+ "thisHour": "This hour","day": "Day",+ "thisDay": "This day",+ "thisDayLastWeek": "This day last week",+ "yesterday": "Yesterday",+ "last7Days": "Last 7 days",+ "last30Days": "Last 30 days",+ "last90Days": "Last 90 days","week": "Week",+ "thisWeek": "This week",+ "previousWeek": "Previous week","month": "Month",+ "thisMonth": "This month",+ "previousMonth": "Previous month",+ "last6Months": "Last 6 months","year": "Year",+ "lastYear": "Last year",+ "thisYear": "This year",+ "previousYear": "Previous year","period": "Period","ending": "Ending","timeframe": "Timeframe",+ "timeframeDefault": "Default timeframe",+ "algorithm": "Algorithm","addAttribute": "Add attribute","selectAttributes": "Select attributes","selectAttribute": "Select attribute",@@ -337,6 +356,8 @@"addAsset": "Add asset","parent": "Parent","none": "None",+ "xAxis": "X-axis",+ "yAxis": "Y-axis","properties": "Properties","pressed": "Pressed","released": "Released",@@ -563,6 +584,7 @@"widgets": "Widgets","widget-kpi": "KPI","widget-linechart": "Line Chart",+ "chartConfig": "Chart Configuration","presets.wrap_to_single_column": "Wrap to single column","presets.keep_layout": "Keep layout","presets.block_device": "Block this device",@@ -593,7 +615,7 @@"deletePermanentWarning": "This will delete {{dashboard}} permanently.","deleteWidgetWarning": "This will remove the widget from your board, and be permanently deleted after saving.","widgetTooSmall": "Widget is too small.",- "showTimestampControls": "Show timestamp controls",+ "allowTimerangeSelect": "Allow time range selection","showLegend": "Show legend","showValueAs": "Show value as","deleteWidget": "Delete widget",diff --git a/manager/src/web/shared/locales/nl/or.json b/manager/src/web/shared/locales/nl/or.jsonindex a8728a2bb..0824fe2aa 100644--- a/manager/src/web/shared/locales/nl/or.json+++ b/manager/src/web/shared/locales/nl/or.json@@ -127,13 +127,32 @@"location": "Locatie","longPressSetLoc": "Druk lang op de kaart om locatie in te stellen","hour": "Uur",+ "lastHour": "Afgelopen uur",+ "last24Hours": "Afgelopen 24 uur",+ "thisHour": "Dit uur","day": "Dag",+ "thisDay": "Vandaag",+ "thisDayLastWeek": "Vandaag een week geleden",+ "yesterday": "Gisteren",+ "last7Days": "Afgelopen 7 dagen",+ "last30Days": "Afgelopen 30 dagen",+ "last90Days": "Afgelopen 90 dagen","week": "Week",+ "thisWeek": "Deze week",+ "previousWeek": "Vorige week","month": "Maand",+ "thisMonth": "Deze maand",+ "previousMonth": "Vorige maand",+ "last6Months": "Afgelopen 6 maanden","year": "Jaar",+ "lastYear": "Afgelopen jaar",+ "thisYear": "Dit jaar",+ "previousYear": "Vorig jaar","period": "Periode","ending": "Einde","timeframe": "Tijdvak",+ "timeframeDefault": "Standaard tijdvak",+ "algorithm": "Algoritme","addAttribute": "Attribuut toevoegen","selectAttributes": "Kies attributen","selectAttribute": "Kies attribuut",@@ -337,6 +356,8 @@"addAsset": "Asset toevoegen","parent": "Parent","none": "Geen",+ "xAxis": "X-as",+ "yAxis": "Y-as","properties": "Eigenschappen","pressed": "Ingedrukt","released": "Losgelaten",@@ -563,10 +584,11 @@"widgets": "Widgets","widget-kpi": "KPI","widget-linechart": "Lijngrafiek",- "presets.wrap_to_single_column": "Krimpen naar één kolom",- "presets.keep_layout": "Indeling behouden",- "presets.block_device": "Geblokkeerd worden",- "presets.redirect": "Een ander dashboard openen..",+ "chartConfig": "Grafiek configuratie",+ "presets.wrap_to_single_column": "Widgets onder elkaar zetten",+ "presets.keep_layout": "Behoud indeling",+ "presets.block_device": "Blokkeer dit apparaat",+ "presets.redirect": "Open een ander dashboard..","size.fullscreen": "Volledig scherm","size.large": "Groot","size.desktop": "Desktop",@@ -593,7 +615,7 @@"deletePermanentWarning": "Dit zal {{dashboard}} permanent verwijderen.","deleteWidgetWarning": "De widget zal worden weggehaald van het bord, en zal permanent worden verwijderd als u opslaat.","widgetTooSmall": "Widget is te klein.",- "showTimestampControls": "Toon tijdsopties",+ "allowTimerangeSelect": "Sta tijdselectie toe","showLegend": "Toon legenda","showValueAs": "Laat waarde zien als","deleteWidget": "Verwijder widget",diff --git a/model/src/main/java/org/openremote/model/datapoint/AssetDatapointResource.java b/model/src/main/java/org/openremote/model/datapoint/AssetDatapointResource.javaindex 6ef042c4d..d84db0717 100644--- a/model/src/main/java/org/openremote/model/datapoint/AssetDatapointResource.java+++ b/model/src/main/java/org/openremote/model/datapoint/AssetDatapointResource.java@@ -21,6 +21,7 @@ package org.openremote.model.datapoint;import io.swagger.v3.oas.annotations.tags.Tag;import org.openremote.model.Constants;+import org.openremote.model.datapoint.query.AssetDatapointQuery;import org.openremote.model.http.RequestParams;import javax.annotation.security.RolesAllowed;import javax.ws.rs.*;@@ -40,17 +41,15 @@ public interface AssetDatapointResource {* restricted and the asset is not linked to the user. A 400 status is returned if the asset attribute does* not have datapoint storage enabled.*/- @GET+ @POST@Path("{assetId}/attribute/{attributeName}")+ @Consumes(APPLICATION_JSON)@Produces(APPLICATION_JSON)@RolesAllowed({Constants.READ_ASSETS_ROLE})ValueDatapoint<?>[] getDatapoints(@BeanParam RequestParams requestParams,- @PathParam("assetId") String assetId,- @PathParam("attributeName") String attributeName,- @QueryParam("interval") DatapointInterval datapointInterval,- @QueryParam("step") Integer stepSize,- @QueryParam("fromTimestamp") long fromTimestamp,- @QueryParam("toTimestamp") long toTimestamp);+ @PathParam("assetId") String assetId,+ @PathParam("attributeName") String attributeName,+ AssetDatapointQuery query);@GET@Path("periods")@@ -68,4 +67,5 @@ public interface AssetDatapointResource {@QueryParam("attributeRefs") String attributeRefsString,@QueryParam("fromTimestamp") long fromTimestamp,@QueryParam("toTimestamp") long toTimestamp);+}diff --git a/model/src/main/java/org/openremote/model/datapoint/AssetPredictedDatapointResource.java b/model/src/main/java/org/openremote/model/datapoint/AssetPredictedDatapointResource.javaindex 6fed2a5e7..55acf2fdc 100644--- a/model/src/main/java/org/openremote/model/datapoint/AssetPredictedDatapointResource.java+++ b/model/src/main/java/org/openremote/model/datapoint/AssetPredictedDatapointResource.java@@ -21,6 +21,7 @@ package org.openremote.model.datapoint;import io.swagger.v3.oas.annotations.tags.Tag;import org.openremote.model.Constants;+import org.openremote.model.datapoint.query.AssetDatapointQuery;import org.openremote.model.http.RequestParams;import javax.annotation.security.RolesAllowed;import javax.ws.rs.*;@@ -37,16 +38,14 @@ public interface AssetPredictedDatapointResource {* restricted and the asset is not linked to the user. A 400 status is returned if the asset attribute does* not have datapoint storage enabled.*/- @GET+ @POST@Path("{assetId}/attribute/{attributeName}")+ @Consumes(APPLICATION_JSON)@Produces(APPLICATION_JSON)@RolesAllowed({Constants.READ_ASSETS_ROLE})ValueDatapoint<?>[] getPredictedDatapoints(@BeanParam RequestParams requestParams,@PathParam("assetId") String assetId,@PathParam("attributeName") String attributeName,- @QueryParam("interval") DatapointInterval datapointInterval,- @QueryParam("step") Integer stepSize,- @QueryParam("fromTimestamp") long fromTimestamp,- @QueryParam("toTimestamp") long toTimestamp);+ AssetDatapointQuery query);}diff --git a/model/src/main/java/org/openremote/model/datapoint/query/AssetDatapointAllQuery.java b/model/src/main/java/org/openremote/model/datapoint/query/AssetDatapointAllQuery.javanew file mode 100644index 000000000..d7921e8a8--- /dev/null+++ b/model/src/main/java/org/openremote/model/datapoint/query/AssetDatapointAllQuery.java@@ -0,0 +1,40 @@+package org.openremote.model.datapoint.query;++import org.openremote.model.attribute.AttributeRef;++import java.time.Instant;+import java.time.LocalDateTime;+import java.time.ZoneId;+import java.util.HashMap;++public class AssetDatapointAllQuery extends AssetDatapointQuery {++ public AssetDatapointAllQuery() {}+ public AssetDatapointAllQuery(long fromTimestamp, long toTimestamp) {+ this.fromTimestamp = fromTimestamp;+ this.toTimestamp = toTimestamp;+ }+ public AssetDatapointAllQuery(LocalDateTime fromTime, LocalDateTime toTime) {+ this.fromTime = fromTime;+ this.toTime = toTime;+ }++ public String getSQLQuery(String tableName, Class<?> attributeType) {+ boolean isNumber = Number.class.isAssignableFrom(attributeType);+ if (isNumber) {+ return "select timestamp, value::text::numeric from " + tableName + " where ENTITY_ID = ? and ATTRIBUTE_NAME = ? and TIMESTAMP >= ? and TIMESTAMP <= ? order by timestamp desc";+ } else {+ return "select timestamp, (case when VALUE::text::boolean is true then 1 else 0 end) from " + tableName + " where ENTITY_ID = ? and ATTRIBUTE_NAME = ? and TIMESTAMP >= ? and TIMESTAMP <= ? order by timestamp desc";+ }+ }+ public HashMap<Integer, Object> getSQLParameters(AttributeRef attributeRef) {+ HashMap<Integer, Object> parameters = new HashMap<>();+ LocalDateTime fromTimestamp = (this.fromTime != null) ? this.fromTime : LocalDateTime.ofInstant(Instant.ofEpochMilli(this.fromTimestamp), ZoneId.systemDefault());+ LocalDateTime toTimestamp = (this.toTime != null) ? this.toTime : LocalDateTime.ofInstant(Instant.ofEpochMilli(this.toTimestamp), ZoneId.systemDefault());+ parameters.put(1, attributeRef.getId());+ parameters.put(2, attributeRef.getName());+ parameters.put(3, fromTimestamp);+ parameters.put(4, toTimestamp);+ return parameters;+ }+}diff --git a/model/src/main/java/org/openremote/model/datapoint/query/AssetDatapointIntervalQuery.java b/model/src/main/java/org/openremote/model/datapoint/query/AssetDatapointIntervalQuery.javanew file mode 100644index 000000000..c67012716--- /dev/null+++ b/model/src/main/java/org/openremote/model/datapoint/query/AssetDatapointIntervalQuery.java@@ -0,0 +1,60 @@+package org.openremote.model.datapoint.query;++import org.openremote.model.attribute.AttributeRef;++import java.time.Instant;+import java.time.LocalDateTime;+import java.time.ZoneId;+import java.util.HashMap;++public class AssetDatapointIntervalQuery extends AssetDatapointQuery {++ public String interval;+ public boolean gapFill;+ public Formula formula;++ public enum Formula {+ MIN, AVG, MAX+ }++ public AssetDatapointIntervalQuery() {+ }+ public AssetDatapointIntervalQuery(long fromTimestamp, long toTimestamp, String interval, Formula formula, boolean gapFill) {+ this.fromTimestamp = fromTimestamp;+ this.toTimestamp = toTimestamp;+ this.interval = interval;+ this.gapFill = gapFill;+ this.formula = formula;+ }+ public AssetDatapointIntervalQuery(LocalDateTime fromTime, LocalDateTime toTime, String interval, Formula formula, boolean gapFill) {+ this.fromTime = fromTime;+ this.toTime = toTime;+ this.interval = interval;+ this.gapFill = gapFill;+ this.formula = formula;+ }++ @Override+ public String getSQLQuery(String tableName, Class<?> attributeType) {+ boolean isNumber = Number.class.isAssignableFrom(attributeType);+ String function = (gapFill ? "public.time_bucket_gapfill" : "public.time_bucket");+ if (isNumber) {+ return "select " + function + "(?::interval, timestamp) AS x, " + this.formula.toString().toLowerCase() + "(value::text::numeric) FROM " + tableName + " WHERE ENTITY_ID = ? and ATTRIBUTE_NAME = ? and TIMESTAMP >= ? and TIMESTAMP <= ? GROUP BY x;";+ } else {+ return "select " + function + "(?::interval, timestamp) AS x, " + this.formula.toString().toLowerCase() + "(case when VALUE::text::boolean is true then 1 else 0 end) FROM " + tableName + " WHERE ENTITY_ID = ? and ATTRIBUTE_NAME = ? and TIMESTAMP >= ? and TIMESTAMP <= ? GROUP BY x;";+ }+ }++ @Override+ public HashMap<Integer, Object> getSQLParameters(AttributeRef attributeRef) {+ HashMap<Integer, Object> parameters = new HashMap<>();+ LocalDateTime fromTimestamp = (this.fromTime != null) ? this.fromTime : LocalDateTime.ofInstant(Instant.ofEpochMilli(super.fromTimestamp), ZoneId.systemDefault());+ LocalDateTime toTimestamp = (this.toTime != null) ? this.toTime : LocalDateTime.ofInstant(Instant.ofEpochMilli(super.toTimestamp), ZoneId.systemDefault());+ parameters.put(1, this.interval);+ parameters.put(2, attributeRef.getId());+ parameters.put(3, attributeRef.getName());+ parameters.put(4, fromTimestamp);+ parameters.put(5, toTimestamp);+ return parameters;+ }+}diff --git a/model/src/main/java/org/openremote/model/datapoint/query/AssetDatapointLTTBQuery.java b/model/src/main/java/org/openremote/model/datapoint/query/AssetDatapointLTTBQuery.javanew file mode 100644index 000000000..f8b888338--- /dev/null+++ b/model/src/main/java/org/openremote/model/datapoint/query/AssetDatapointLTTBQuery.java@@ -0,0 +1,48 @@+package org.openremote.model.datapoint.query;++import org.openremote.model.attribute.AttributeRef;++import java.time.Instant;+import java.time.LocalDateTime;+import java.time.ZoneId;+import java.util.HashMap;++public final class AssetDatapointLTTBQuery extends AssetDatapointQuery {++ public int amountOfPoints;++ public AssetDatapointLTTBQuery() {}+ public AssetDatapointLTTBQuery(long fromTimestamp, long toTimestamp, int amountOfPoints) {+ this.fromTimestamp = fromTimestamp;+ this.toTimestamp = toTimestamp;+ this.amountOfPoints = amountOfPoints;+ }+ public AssetDatapointLTTBQuery(LocalDateTime fromTime, LocalDateTime toTime, int amountOfPoints) {+ this.fromTime = fromTime;+ this.toTime = toTime;+ this.amountOfPoints = amountOfPoints;+ }++ @Override+ public String getSQLQuery(String tableName, Class<?> attributeType) {+ boolean isNumber = Number.class.isAssignableFrom(attributeType);+ if (isNumber) {+ return "select * from public.unnest((select public.lttb(timestamp::timestamptz, value::double precision, ?) from " + tableName + " where ENTITY_ID = ? and ATTRIBUTE_NAME = ? and TIMESTAMP >= ? and TIMESTAMP <= ?))";+ } else {+ return "select * from public.unnest((select public.lttb(timestamp::timestamptz, (case when VALUE::text::boolean is true then 1 else 0 end), ?) from " + tableName + " where ENTITY_ID = ? and ATTRIBUTE_NAME = ? and TIMESTAMP >= ? and TIMESTAMP <= ?))";+ }+ }++ @Override+ public HashMap<Integer, Object> getSQLParameters(AttributeRef attributeRef) {+ LocalDateTime fromTimestamp = (this.fromTime != null) ? this.fromTime : LocalDateTime.ofInstant(Instant.ofEpochMilli(super.fromTimestamp), ZoneId.systemDefault());+ LocalDateTime toTimestamp = (this.toTime != null) ? this.toTime : LocalDateTime.ofInstant(Instant.ofEpochMilli(super.toTimestamp), ZoneId.systemDefault());+ HashMap<Integer, Object> parameters = new HashMap<>();+ parameters.put(1, this.amountOfPoints);+ parameters.put(2, attributeRef.getId());+ parameters.put(3, attributeRef.getName());+ parameters.put(4, fromTimestamp);+ parameters.put(5, toTimestamp);+ return parameters;+ }+}diff --git a/model/src/main/java/org/openremote/model/datapoint/query/AssetDatapointQuery.java b/model/src/main/java/org/openremote/model/datapoint/query/AssetDatapointQuery.javanew file mode 100644index 000000000..52af33935--- /dev/null+++ b/model/src/main/java/org/openremote/model/datapoint/query/AssetDatapointQuery.java@@ -0,0 +1,54 @@+/*+ * Copyright 2023, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.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 Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * 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 Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <http://www.gnu.org/licenses/>.+ */+package org.openremote.model.datapoint.query;++import com.fasterxml.jackson.annotation.JsonSubTypes;+import com.fasterxml.jackson.annotation.JsonTypeInfo;+import org.openremote.model.attribute.AttributeRef;++import java.io.Serializable;+import java.time.LocalDateTime;+import java.util.HashMap;++@JsonSubTypes({+ @JsonSubTypes.Type(value = AssetDatapointAllQuery.class, name = "all"),+ @JsonSubTypes.Type(value = AssetDatapointLTTBQuery.class, name = "lttb"),+ @JsonSubTypes.Type(value = AssetDatapointIntervalQuery.class, name = "interval")+})+@JsonTypeInfo(+ use = JsonTypeInfo.Id.NAME,+ property = "type",+ defaultImpl = AssetDatapointAllQuery.class+)+public abstract class AssetDatapointQuery implements Serializable {++ public long fromTimestamp;+ public long toTimestamp;+ public LocalDateTime fromTime;+ public LocalDateTime toTime;++ public String getSQLQuery(String tableName, Class<?> attributeType) {+ return null;+ }++ public HashMap<Integer, Object> getSQLParameters(AttributeRef attributeRef) {+ return null;+ }+}diff --git a/model/src/main/java/org/openremote/model/rules/HistoricDatapoints.java b/model/src/main/java/org/openremote/model/rules/HistoricDatapoints.javaindex 82f01bde1..6ab85ac1a 100644--- a/model/src/main/java/org/openremote/model/rules/HistoricDatapoints.java+++ b/model/src/main/java/org/openremote/model/rules/HistoricDatapoints.java@@ -20,26 +20,15 @@package org.openremote.model.rules;import org.openremote.model.attribute.AttributeRef;-import org.openremote.model.datapoint.DatapointInterval;import org.openremote.model.datapoint.ValueDatapoint;+import org.openremote.model.datapoint.query.AssetDatapointQuery;import org.openremote.model.util.TsIgnore;-import java.time.LocalDateTime;-/*** Facade for historic data points in rules*/@TsIgnorepublic abstract class HistoricDatapoints {- public abstract ValueDatapoint<?>[] getValueDatapoints(AttributeRef attributeRef,- DatapointInterval interval,- LocalDateTime fromTimestamp,- LocalDateTime toTimestamp);-- public abstract ValueDatapoint<?>[] getValueDatapoints(AttributeRef attributeRef,- DatapointInterval interval,- int stepSize,- LocalDateTime fromTimestamp,- LocalDateTime toTimestamp);+ public abstract ValueDatapoint<?>[] getValueDatapoints(AttributeRef attributeRef, AssetDatapointQuery query);}diff --git a/model/src/main/java/org/openremote/model/rules/PredictedDatapoints.java b/model/src/main/java/org/openremote/model/rules/PredictedDatapoints.javaindex 90a9c3169..39a3c5f0e 100644--- a/model/src/main/java/org/openremote/model/rules/PredictedDatapoints.java+++ b/model/src/main/java/org/openremote/model/rules/PredictedDatapoints.java@@ -20,11 +20,10 @@package org.openremote.model.rules;import org.openremote.model.attribute.AttributeRef;-import org.openremote.model.datapoint.DatapointInterval;import org.openremote.model.datapoint.ValueDatapoint;+import org.openremote.model.datapoint.query.AssetDatapointQuery;import org.openremote.model.util.TsIgnore;-import java.time.LocalDate;import java.time.LocalDateTime;/**@@ -33,16 +32,7 @@ import java.time.LocalDateTime;@TsIgnorepublic abstract class PredictedDatapoints {- public abstract ValueDatapoint<?>[] getValueDatapoints(AttributeRef attributeRef,- DatapointInterval interval,- LocalDateTime fromTimestamp,- LocalDateTime toTimestamp);-- public abstract ValueDatapoint<?>[] getValueDatapoints(AttributeRef attributeRef,- DatapointInterval interval,- int stepSize,- LocalDateTime fromTimestamp,- LocalDateTime toTimestamp);+ public abstract ValueDatapoint<?>[] getValueDatapoints(AttributeRef attributeRef, AssetDatapointQuery query);public abstract void updateValue(String assetId, String attributeName, Object value, LocalDateTime timestamp);public abstract void updateValue(AttributeRef attributeRef, Object value, LocalDateTime timestamp);diff --git a/profile/dev-proxy.yml b/profile/dev-proxy.ymlindex 3a57cd6d2..faf6cec45 100644--- a/profile/dev-proxy.yml+++ b/profile/dev-proxy.yml@@ -43,7 +43,7 @@ services:file: deploy.ymlservice: postgresqlvolumes:- - ./tmp:/tmp+ - ../tmp:/tmp# Access directly if needed on localhostports:- "5432:5432"diff --git a/profile/dev-testing.yml b/profile/dev-testing.ymlindex 8181d0e40..3e892a3b6 100644--- a/profile/dev-testing.yml+++ b/profile/dev-testing.yml@@ -31,7 +31,7 @@ services:file: deploy.ymlservice: postgresqlvolumes:- - ./tmp:/tmp+ - ../tmp:/tmp# Access directly if needed on localhostports:- "5432:5432"diff --git a/project.gradle b/project.gradleindex 5c72d392a..038e97b6b 100644--- a/project.gradle+++ b/project.gradle@@ -21,11 +21,11 @@ if (project == rootProject) {if (rootProject.hasProperty("gradleFileEncrypt")) {println("File encryption plugin config found, configuring git pre commit hook and decrypt task dependency")-- // Write git hook for encryption plugin checks before any commit- def path = Paths.get(rootProject.projectDir.path, ".git/hooks/pre-commit")- def f = path.toFile()- f.text = """#!/bin/sh+ try {+ // Write git hook for encryption plugin checks before any commit+ def path = Paths.get(rootProject.projectDir.path, ".git/hooks/pre-commit")+ def f = path.toFile()+ f.text = """#!/bin/shecho "***** Running gradle encryption plugin checkFilesGitIgnored task ******"@@ -35,7 +35,6 @@ status=\$?exit \$status"""- try {Set<PosixFilePermission> perms = Files.readAttributes(path, PosixFileAttributes.class).permissions()perms.add(PosixFilePermission.OWNER_WRITE)perms.add(PosixFilePermission.OWNER_READ)diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovyindex 76c8b910a..a9540e3a7 100644--- a/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovy+++ b/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovy@@ -6,10 +6,10 @@ import org.openremote.manager.asset.AssetProcessingServiceimport org.openremote.manager.asset.AssetStorageServiceimport org.openremote.manager.datapoint.AssetDatapointServiceimport org.openremote.manager.setup.SetupService-import org.openremote.setup.integration.ManagerTestSetupimport org.openremote.model.attribute.AttributeRef-import org.openremote.model.datapoint.DatapointInterval+import org.openremote.model.datapoint.query.AssetDatapointIntervalQueryimport org.openremote.model.util.ValueUtil+import org.openremote.setup.integration.ManagerTestSetupimport org.openremote.test.ManagerContainerTraitimport spock.lang.Specificationimport spock.util.concurrent.PollingConditions@@ -20,9 +20,7 @@ import java.time.ZoneIdimport java.time.temporal.ChronoUnitimport java.util.concurrent.TimeUnit-import static java.util.concurrent.TimeUnit.DAYS-import static java.util.concurrent.TimeUnit.MINUTES-import static java.util.concurrent.TimeUnit.SECONDS+import static java.util.concurrent.TimeUnit.*import static org.openremote.manager.datapoint.AssetDatapointService.OR_DATA_POINTS_MAX_AGE_DAYS_DEFAULTimport static org.openremote.setup.integration.ManagerTestSetup.thingLightToggleAttributeNameimport static spock.util.matcher.HamcrestMatchers.closeTo@@ -137,13 +135,16 @@ class AssetDatapointTest extends Specification implements ManagerContainerTraitand: "the aggregated datapoints should match"conditions.eventually {def thing = assetStorageService.find(managerTestSetup.thingId, true)- def aggregatedDatapoints = assetDatapointService.getValueDatapoints(- thing.getId(),- thing.getAttribute("light1PowerConsumption").orElseThrow({ new RuntimeException("Missing attribute") }),- DatapointInterval.MINUTE,- null,- LocalDateTime.ofInstant(Instant.ofEpochMilli(getClockTimeOf(container)), ZoneId.systemDefault()).minus(1, ChronoUnit.HOURS),- LocalDateTime.ofInstant(Instant.ofEpochMilli(getClockTimeOf(container)), ZoneId.systemDefault())+ def aggregatedDatapoints = assetDatapointService.queryDatapoints(+ thing.getId(),+ thing.getAttribute("light1PowerConsumption").orElseThrow({ new RuntimeException("Missing attribute") }),+ new AssetDatapointIntervalQuery(+ LocalDateTime.ofInstant(Instant.ofEpochMilli(getClockTimeOf(container)), ZoneId.systemDefault()).minus(1, ChronoUnit.HOURS),+ LocalDateTime.ofInstant(Instant.ofEpochMilli(getClockTimeOf(container)), ZoneId.systemDefault()),+ "1 minute",+ AssetDatapointIntervalQuery.Formula.AVG,+ true+ ))assert aggregatedDatapoints.size() == 61assert aggregatedDatapoints[54].value == 13.3@@ -158,13 +159,16 @@ class AssetDatapointTest extends Specification implements ManagerContainerTraitand: "when the step size is set on the datapoint retrieval then the datapoints should match"conditions.eventually {def thing = assetStorageService.find(managerTestSetup.thingId, true)- def aggregatedDatapoints = assetDatapointService.getValueDatapoints(+ def aggregatedDatapoints = assetDatapointService.queryDatapoints(thing.getId(),thing.getAttribute("light1PowerConsumption").orElseThrow({ new RuntimeException("Missing attribute") }),- DatapointInterval.MINUTE,- 5,- LocalDateTime.ofInstant(Instant.ofEpochMilli(getClockTimeOf(container)), ZoneId.systemDefault()).minus(1, ChronoUnit.HOURS),- LocalDateTime.ofInstant(Instant.ofEpochMilli(getClockTimeOf(container)), ZoneId.systemDefault())+ new AssetDatapointIntervalQuery(+ LocalDateTime.ofInstant(Instant.ofEpochMilli(getClockTimeOf(container)), ZoneId.systemDefault()).minus(1, ChronoUnit.HOURS),+ LocalDateTime.ofInstant(Instant.ofEpochMilli(getClockTimeOf(container)), ZoneId.systemDefault()),+ "5 minutes",+ AssetDatapointIntervalQuery.Formula.AVG,+ true+ ))assert aggregatedDatapoints.size() == 13assert aggregatedDatapoints[11].value, closeTo(13.36666, 0.0001)@@ -227,13 +231,16 @@ class AssetDatapointTest extends Specification implements ManagerContainerTraitand: "the aggregated datapoints should match"conditions.eventually {def thing = assetStorageService.find(managerTestSetup.thingId, true)- def aggregatedDatapoints = assetDatapointService.getValueDatapoints(- thing.getId(),- thing.getAttribute(thingLightToggleAttributeName).orElseThrow({ new RuntimeException("Missing attribute") }),- DatapointInterval.MINUTE,- null,- LocalDateTime.ofInstant(Instant.ofEpochMilli(getClockTimeOf(container)), ZoneId.systemDefault()).minus(1, ChronoUnit.HOURS),- LocalDateTime.ofInstant(Instant.ofEpochMilli(getClockTimeOf(container)), ZoneId.systemDefault())+ def aggregatedDatapoints = assetDatapointService.queryDatapoints(+ thing.getId(),+ thing.getAttribute(thingLightToggleAttributeName).orElseThrow({ new RuntimeException("Missing attribute") }),+ new AssetDatapointIntervalQuery(+ LocalDateTime.ofInstant(Instant.ofEpochMilli(getClockTimeOf(container)), ZoneId.systemDefault()).minus(1, ChronoUnit.HOURS),+ LocalDateTime.ofInstant(Instant.ofEpochMilli(getClockTimeOf(container)), ZoneId.systemDefault()),+ "1 minute",+ AssetDatapointIntervalQuery.Formula.AVG,+ true+ ))assert aggregatedDatapoints.size() == 61assert aggregatedDatapoints[58].value == 0diff --git a/test/src/test/groovy/org/openremote/test/energy/EnergyOptimisationAssetTest.groovy b/test/src/test/groovy/org/openremote/test/energy/EnergyOptimisationAssetTest.groovyindex 849e48487..b07feacb7 100644--- a/test/src/test/groovy/org/openremote/test/energy/EnergyOptimisationAssetTest.groovy+++ b/test/src/test/groovy/org/openremote/test/energy/EnergyOptimisationAssetTest.groovy@@ -12,6 +12,7 @@ import org.openremote.model.asset.impl.*import org.openremote.model.attribute.AttributeEventimport org.openremote.model.attribute.AttributeRefimport org.openremote.model.datapoint.DatapointInterval+import org.openremote.model.datapoint.query.AssetDatapointIntervalQueryimport org.openremote.model.util.ValueUtilimport org.openremote.test.ManagerContainerTraitimport org.openremote.setup.integration.ManagerTestSetup@@ -124,12 +125,16 @@ class EnergyOptimisationAssetTest extends Specification implements ManagerContaithen: "the setpoints of the storage asset for the next 24hrs should be correctly optimised"conditions.eventually {assert ((ElectricityStorageAsset)assetStorageService.find(managerTestSetup.electricityBatteryAssetId)).getPowerSetpoint().orElse(0d) == 0d- def setpoints = assetPredictedDatapointService.getValueDatapoints(- new AttributeRef(managerTestSetup.electricityBatteryAssetId, ElectricityAsset.POWER_SETPOINT.name),- DatapointInterval.MINUTE,- (int)(optimiser.intervalSize * 60),- LocalDateTime.ofInstant(optimisationTime, ZoneId.systemDefault()).plus((long)(optimiser.intervalSize * 60), ChronoUnit.MINUTES),- LocalDateTime.ofInstant(optimisationTime, ZoneId.systemDefault()).plus(1, ChronoUnit.DAYS).minus((long)(optimiser.intervalSize * 60), ChronoUnit.MINUTES)+ def setpoints = assetPredictedDatapointService.queryDatapoints(+ managerTestSetup.electricityBatteryAssetId,+ ElectricityAsset.POWER_SETPOINT.name,+ new AssetDatapointIntervalQuery(+ LocalDateTime.ofInstant(optimisationTime, ZoneId.systemDefault()).plus((long)(optimiser.intervalSize * 60), ChronoUnit.MINUTES),+ LocalDateTime.ofInstant(optimisationTime, ZoneId.systemDefault()).plus(1, ChronoUnit.DAYS).minus((long)(optimiser.intervalSize * 60), ChronoUnit.MINUTES),+ (optimiser.intervalSize * 60) + " minutes",+ AssetDatapointIntervalQuery.Formula.AVG,+ true+ ))assert setpoints.size() == 7@@ -152,12 +157,16 @@ class EnergyOptimisationAssetTest extends Specification implements ManagerContaithen: "the battery should not be used in any interval"conditions.eventually {assert ((ElectricityStorageAsset)assetStorageService.find(managerTestSetup.electricityBatteryAssetId)).getPowerSetpoint().orElse(0d) == 0d- def setpoints = assetPredictedDatapointService.getValueDatapoints(- new AttributeRef(managerTestSetup.electricityBatteryAssetId, ElectricityAsset.POWER_SETPOINT.name),- DatapointInterval.MINUTE,- (int)(optimiser.intervalSize * 60),- LocalDateTime.ofInstant(optimisationTime, ZoneId.systemDefault()).plus((long)(optimiser.intervalSize * 60), ChronoUnit.MINUTES),- LocalDateTime.ofInstant(optimisationTime, ZoneId.systemDefault()).plus(1, ChronoUnit.DAYS).minus((long)(optimiser.intervalSize * 60), ChronoUnit.MINUTES)+ def setpoints = assetPredictedDatapointService.queryDatapoints(+ managerTestSetup.electricityBatteryAssetId,+ ElectricityAsset.POWER_SETPOINT.name,+ new AssetDatapointIntervalQuery(+ LocalDateTime.ofInstant(optimisationTime, ZoneId.systemDefault()).plus((long)(optimiser.intervalSize * 60), ChronoUnit.MINUTES),+ LocalDateTime.ofInstant(optimisationTime, ZoneId.systemDefault()).plus(1, ChronoUnit.DAYS).minus((long)(optimiser.intervalSize * 60), ChronoUnit.MINUTES),+ (optimiser.intervalSize * 60) + " minutes",+ AssetDatapointIntervalQuery.Formula.AVG,+ true+ ))assert setpoints.size() == 7@@ -180,12 +189,16 @@ class EnergyOptimisationAssetTest extends Specification implements ManagerContaithen: "the battery should only be used in the correct intervals"conditions.eventually {assert ((ElectricityStorageAsset)assetStorageService.find(managerTestSetup.electricityBatteryAssetId)).getPowerSetpoint().orElse(0d) == 0d- def setpoints = assetPredictedDatapointService.getValueDatapoints(- new AttributeRef(managerTestSetup.electricityBatteryAssetId, ElectricityAsset.POWER_SETPOINT.name),- DatapointInterval.MINUTE,- (int)(optimiser.intervalSize * 60),- LocalDateTime.ofInstant(optimisationTime, ZoneId.systemDefault()).plus((long)(optimiser.intervalSize * 60), ChronoUnit.MINUTES),- LocalDateTime.ofInstant(optimisationTime, ZoneId.systemDefault()).plus(1, ChronoUnit.DAYS).minus((long)(optimiser.intervalSize * 60), ChronoUnit.MINUTES)+ def setpoints = assetPredictedDatapointService.queryDatapoints(+ managerTestSetup.electricityBatteryAssetId,+ ElectricityAsset.POWER_SETPOINT.name,+ new AssetDatapointIntervalQuery(+ LocalDateTime.ofInstant(optimisationTime, ZoneId.systemDefault()).plus((long)(optimiser.intervalSize * 60), ChronoUnit.MINUTES),+ LocalDateTime.ofInstant(optimisationTime, ZoneId.systemDefault()).plus(1, ChronoUnit.DAYS).minus((long)(optimiser.intervalSize * 60), ChronoUnit.MINUTES),+ (optimiser.intervalSize * 60) + " minutes",+ AssetDatapointIntervalQuery.Formula.AVG,+ true+ ))assert setpoints.size() == 7@@ -287,12 +300,16 @@ class EnergyOptimisationAssetTest extends Specification implements ManagerContaiconditions.eventually {assert ((ElectricityStorageAsset)assetStorageService.find(managerTestSetup.electricityBatteryAssetId)).getPowerSetpoint().orElse(0d) == 10d- def setpoints = assetPredictedDatapointService.getValueDatapoints(- new AttributeRef(managerTestSetup.electricityBatteryAssetId, ElectricityAsset.POWER_SETPOINT.name),- DatapointInterval.MINUTE,- (int)(optimiser.intervalSize * 60),- LocalDateTime.ofInstant(optimisationTime, ZoneId.systemDefault()).plus((long)(optimiser.intervalSize * 60), ChronoUnit.MINUTES),- LocalDateTime.ofInstant(optimisationTime, ZoneId.systemDefault()).plus(1, ChronoUnit.DAYS).minus((long)(optimiser.intervalSize * 60), ChronoUnit.MINUTES)+ def setpoints = assetPredictedDatapointService.queryDatapoints(+ managerTestSetup.electricityBatteryAssetId,+ ElectricityAsset.POWER_SETPOINT.name,+ new AssetDatapointIntervalQuery(+ LocalDateTime.ofInstant(optimisationTime, ZoneId.systemDefault()).plus((long)(optimiser.intervalSize * 60), ChronoUnit.MINUTES),+ LocalDateTime.ofInstant(optimisationTime, ZoneId.systemDefault()).plus(1, ChronoUnit.DAYS).minus((long)(optimiser.intervalSize * 60), ChronoUnit.MINUTES),+ (optimiser.intervalSize * 60) + " minutes",+ AssetDatapointIntervalQuery.Formula.AVG,+ true+ ))assert setpoints.size() == 7diff --git a/ui/component/or-attribute-card/src/index.ts b/ui/component/or-attribute-card/src/index.tsindex 70bb93e30..cac00f5e3 100644--- a/ui/component/or-attribute-card/src/index.ts+++ b/ui/component/or-attribute-card/src/index.ts@@ -11,7 +11,8 @@ import {DatapointInterval,ReadAttributeEvent,ValueDatapoint,- AssetModelUtil+ AssetModelUtil,+ AssetDatapointLTTBQuery} from "@openremote/model";import {DefaultColor3, DefaultColor4, manager, Util} from "@openremote/core";import {Chart, ScatterDataPoint, LineController, LineElement, PointElement, LinearScale, TimeSeriesScale, Title} from "chart.js";@@ -142,13 +143,19 @@ const style = css`}.chart-wrapper {- flex: 1;+ flex: 1 1 auto;width: 65%;height: 75%;}+ .controls-wrapper {+ height: 100%;+ display: flex;+ flex-direction: column;+ place-content: space-between;+ }.delta-wrapper {- flex: 0 0 50px;+ /*flex: 0 0 50px;*/text-align: right;/*position: absolute;*/@@ -162,9 +169,7 @@ const style = css`}.period-selector {- position: absolute;- right: -12px;- bottom: 0;+}.delta {@@ -392,7 +397,7 @@ export class OrAttributeCard extends LitElement {<div class="panel-content-wrapper"><div class="panel-title">${this.shouldShowTitle() ? html`<span class="panel-title-text">${this.assets[0].name + " - " + i18next.t(this.assetAttributes[0][1].name!)}</span>` : undefined}- ${this.shouldShowControls() ? getContentWithMenuTemplate(html`+ ${this.shouldShowTitle() ? getContentWithMenuTemplate(html`<or-mwc-input icon="dots-vertical" type="button"></or-mwc-input>`,[@@ -426,25 +431,26 @@ export class OrAttributeCard extends LitElement {<div class="chart-wrapper"><canvas id="chart"></canvas></div>- <div class="delta-wrapper">- <span class="delta">${this.delta ? this.deltaPlus + (this.delta.val !== undefined && this.delta.val !== null ? this.delta.val : "") + (this.delta.unit || "") : ""}</span>- </div>-- ${this.shouldShowControls() ? html`- <div class="period-selector-wrapper">- ${getContentWithMenuTemplate(- html`<or-mwc-input class="period-selector" .type="${InputType.BUTTON}" .label="${i18next.t(this.period ? this.period : "-")}"></or-mwc-input>`,- [{value: "hour", text: "hour"}, {value: "day", text: "day"}, {value: "week", text: "week"}, {value: "month", text: "month"}, {value: "year", text: "year"}].map((option) => {- option.text = i18next.t(option.value);- return option;- }),- this.period,- (value) => this._setPeriodOption(value)- )}+ <div class="controls-wrapper">+ <div class="delta-wrapper">+ <span class="delta">${this.delta ? this.deltaPlus + (this.delta.val !== undefined && this.delta.val !== null ? this.delta.val : "") + (this.delta.unit || "") : ""}</span></div>- ` : html`- <or-mwc-input class="period-selector" .type="${InputType.BUTTON}" disabled .label="${i18next.t(this.period ? this.period : "-")}"></or-mwc-input>- `}+ ${this.shouldShowControls() ? html`+ <div class="period-selector-wrapper">+ ${getContentWithMenuTemplate(+ html`<or-mwc-input class="period-selector" .type="${InputType.BUTTON}" .label="${i18next.t(this.period ? this.period : "-")}"></or-mwc-input>`,+ [{value: "hour", text: "hour"}, {value: "day", text: "day"}, {value: "week", text: "week"}, {value: "month", text: "month"}, {value: "year", text: "year"}].map((option) => {+ option.text = i18next.t(option.value);+ return option;+ }),+ this.period,+ (value) => this._setPeriodOption(value)+ )}+ </div>+ ` : html`+ <or-mwc-input class="period-selector" .type="${InputType.BUTTON}" disabled .label="${i18next.t(this.period ? this.period : "-")}"></or-mwc-input>+ `}+ </div></div></div></div>@@ -737,10 +743,11 @@ export class OrAttributeCard extends LitElement {assetId,attributeName,{- interval: interval,+ type: "lttb",fromTimestamp: startOfPeriod,- toTimestamp: endOfPeriod- }+ toTimestamp: endOfPeriod,+ amountOfPoints: 20,+ } as AssetDatapointLTTBQuery);this._loading = false;diff --git a/ui/component/or-attribute-history/src/index.ts b/ui/component/or-attribute-history/src/index.tsindex 31798d2d1..f7555be60 100644--- a/ui/component/or-attribute-history/src/index.ts+++ b/ui/component/or-attribute-history/src/index.ts@@ -709,10 +709,10 @@ export class OrAttributeHistory extends translate(i18next)(LitElement) {assetId,attributeName,{- interval: interval,+ type: "all",fromTimestamp: this._startOfPeriod,toTimestamp: this._endOfPeriod- }+ } // TODO: maybe optimize this?);this._loading = false;diff --git a/ui/component/or-chart/src/index.ts b/ui/component/or-chart/src/index.tsindex 0d2f71456..7d1165790 100644--- a/ui/component/or-chart/src/index.ts+++ b/ui/component/or-chart/src/index.ts@@ -1,45 +1,56 @@-import {- css,- html,- LitElement,- PropertyValues,- TemplateResult,- unsafeCSS-} from "lit";+import {css, html, LitElement, PropertyValues, TemplateResult, unsafeCSS} from "lit";import {customElement, property, query} from "lit/decorators.js";import i18next from "i18next";import {translate} from "@openremote/or-translate";-import {AssetModelUtil, Asset, Attribute, AttributeRef, DatapointInterval, WellknownMetaItems, ReadAssetEvent, AssetEvent, ValueDatapoint, AssetQuery} from "@openremote/model";-import manager, {- DefaultColor2,- DefaultColor3,- DefaultColor4,- DefaultColor5,- Util-} from "@openremote/core";+import {+ Asset,+ AssetDatapointQueryUnion,+ AssetEvent,+ AssetModelUtil,+ AssetQuery,+ Attribute,+ AttributeRef,+ DatapointInterval,+ ReadAssetEvent,+ ValueDatapoint,+ WellknownMetaItems+} from "@openremote/model";+import manager, {DefaultColor2, DefaultColor3, DefaultColor4, DefaultColor5, Util} from "@openremote/core";import "@openremote/or-asset-tree";import "@openremote/or-mwc-components/or-mwc-input";import "@openremote/or-components/or-panel";import "@openremote/or-translate";-import {Chart, ChartDataset, TimeUnit, ScatterDataPoint, ScatterController, LineController, LineElement, PointElement, LinearScale, TimeScale,+import {+ Chart,+ ChartConfiguration,+ ChartDataset,Filler,Legend,+ LinearScale,+ LineController,+ LineElement,+ PointElement,+ ScatterController,+ ScatterDataPoint,+ TimeScale,+ TimeScaleOptions,+ TimeUnit,Title,- Tooltip,- ChartConfiguration,- TimeScaleOptions} from "chart.js";+ Tooltip+} from "chart.js";import {InputType, OrInputChangedEvent} from "@openremote/or-mwc-components/or-mwc-input";import moment from "moment";import {OrAssetTreeSelectionEvent} from "@openremote/or-asset-tree";import {getAssetDescriptorIconTemplate} from "@openremote/or-icon";-import {getContentWithMenuTemplate} from "@openremote/or-mwc-components/or-mwc-menu";-import ChartAnnotation, { AnnotationOptions } from "chartjs-plugin-annotation";+import ChartAnnotation, {AnnotationOptions} from "chartjs-plugin-annotation";import "chartjs-adapter-moment";-import {GenericAxiosResponse } from "@openremote/rest";+import {GenericAxiosResponse} from "@openremote/rest";import {OrAttributePicker, OrAttributePickerPickedEvent} from "@openremote/or-attribute-picker";-import { showDialog } from "@openremote/or-mwc-components/or-mwc-dialog";-import { cache } from "lit/directives/cache.js";-import { throttle } from "lodash";+import {showDialog} from "@openremote/or-mwc-components/or-mwc-dialog";+import {cache} from "lit/directives/cache.js";+import {debounce, throttle} from "lodash";+import { getContentWithMenuTemplate } from "@openremote/or-mwc-components/or-mwc-menu";+import {ListItem} from "@openremote/or-mwc-components/or-mwc-list";Chart.register(LineController, ScatterController, LineElement, PointElement, LinearScale, TimeScale, Title, Filler, Legend, Tooltip, ChartAnnotation);@@ -59,10 +70,13 @@ export class OrChartEvent extends CustomEvent<OrChartEventDetail> {}}+export type TimePresetCallback = (date: Date) => [Date, Date];+export interface ChartViewConfig {attributeRefs?: AttributeRef[];- timestamp?: number;- compareOffset?: number;+ fromTimestamp?: number;+ toTimestamp?: number;+ /*compareOffset?: number;*/period?: moment.unitOfTime.Base;deltaFormat?: "absolute" | "percentage";decimals?: number;@@ -184,31 +198,26 @@ const style = css`#msg:not([hidden]) {display: flex;}- .interval-controls,.period-controls {display: flex;- flex-wrap: wrap;- flex-direction: row;- }-- .period-controls {- --or-icon-fill: var(--or-app-color3);+ min-width: 180px;+ align-items: center;}#controls {display: flex;flex-wrap: wrap;margin: var(--internal-or-chart-controls-margin);- min-width: 320px;- padding-left: 10px;+ width: 100%;flex-direction: column;margin: 0;}#attribute-list {- /*overflow: auto;*/+ overflow: hidden auto;+ min-height: 50px;flex: 1 1 0;- width: 95%;+ width: 100%;display: flex;flex-direction: column;}@@ -296,6 +305,11 @@ const style = css`/*min-height: 400px;max-height: 550px;*/}+ #chart-controls {+ display: flex;+ flex-direction: column;+ align-items: center;+ }canvas {width: 100% !important;height: 100%; !important;@@ -316,6 +330,7 @@ const style = css`}#container {+ background-color: black;flex-direction: column;}@@ -323,6 +338,13 @@ const style = css`min-width: 100%;padding-left: 0;}+ .interval-controls,+ .period-controls {+ flex-direction: row;+ justify-content: left;+ align-items: center;+ gap: 8px;+ }}`;@@ -354,18 +376,15 @@ export class OrChart extends translate(i18next)(LitElement) {@property({type: Array})public colors: string[] = ["#3869B1", "#DA7E30", "#3F9852", "#CC2428", "#6B4C9A", "#922427", "#958C3D", "#535055"];- @property({type: String})- public period: moment.unitOfTime.Base = "day";-- @property({type: Number})- public timestamp: Date = moment().set('minute', 0).toDate();-- @property({type: Number})- public compareTimestamp?: Date = moment().set('minute', 0).toDate();+ @property({type: Object})+ public datapointQuery!: AssetDatapointQueryUnion;@property({type: Object})public config?: OrChartConfig;+ @property({type: Object}) // options that will get merged with our default chartjs configuration.+ public chartOptions?: any+@property({type: String})public realm?: string;@@ -378,6 +397,12 @@ export class OrChart extends translate(i18next)(LitElement) {@property()public timestampControls: boolean = true;+ @property()+ public timePresetOptions?: Map<string, TimePresetCallback>;++ @property()+ public timePresetKey?: string;+@property()public showLegend: boolean = true;@@ -435,8 +460,8 @@ export class OrChart extends translate(i18next)(LitElement) {}}- const reloadData = changedProperties.has("period") || changedProperties.has("compareTimestamp")- || changedProperties.has("timestamp") || changedProperties.has("assetAttributes") || changedProperties.has("realm") || changedProperties.has("dataProvider");+ const reloadData = changedProperties.has("datapointQuery") || changedProperties.has("timePresetKey") ||+ changedProperties.has("assetAttributes") || changedProperties.has("realm") || changedProperties.has("dataProvider");if (reloadData) {this._data = undefined;@@ -537,7 +562,9 @@ export class OrChart extends translate(i18next)(LitElement) {}} as ChartConfiguration<"line", ScatterDataPoint[]>;- this._chart = new Chart<"line", ScatterDataPoint[]>(this._chartElem.getContext("2d")!, options);+ const mergedOptions = Util.mergeObjects(options, this.chartOptions, false);++ this._chart = new Chart<"line", ScatterDataPoint[]>(this._chartElem.getContext("2d")!, mergedOptions as ChartConfiguration<"line", ScatterDataPoint[]>);} else {if (changedProperties.has("_data")) {this._chart.options.scales!.x!.min = this._startOfPeriod;@@ -555,21 +582,32 @@ export class OrChart extends translate(i18next)(LitElement) {});}++ // Not the best implementation, but it changes the legend & controls to wrap under the chart.+ // Also sorts the attribute lists horizontally when it is below the chartapplyChartResponsiveness(): void {if(this.shadowRoot) {const container = this.shadowRoot.getElementById('container');if(container) {- const bottomLegenda = (container.clientWidth < 600);- container.style.flexDirection = bottomLegenda ? 'column' : 'row';+ const bottomLegend: boolean = (container.clientWidth < 600);+ container.style.flexDirection = bottomLegend ? 'column' : 'row';+ const periodControls = this.shadowRoot.querySelector('.period-controls') as HTMLElement;+ if(periodControls) {+ periodControls.style.justifyContent = bottomLegend ? 'center' : 'start';+ periodControls.style.paddingLeft = bottomLegend ? '' : '18px';+ }const attributeList = this.shadowRoot.getElementById('attribute-list');if(attributeList) {- attributeList.style.gap = bottomLegenda ? '0 12px' : '';- attributeList.style.maxHeight = bottomLegenda ? '90px' : '';- attributeList.style.flexDirection = bottomLegenda ? 'row' : 'column'+ attributeList.style.gap = bottomLegend ? '4px 12px' : '';+ attributeList.style.maxHeight = bottomLegend ? '90px' : '';+ attributeList.style.flexFlow = bottomLegend ? 'row wrap' : 'column nowrap';+ attributeList.style.padding = bottomLegend ? '0' : '12px 0';}this.shadowRoot.querySelectorAll('.attribute-list-item').forEach((item: Element) => {- (item as HTMLElement).style.minHeight = bottomLegenda ? '' : '44px';- (item.children[1] as HTMLElement).style.flexDirection = bottomLegenda ? 'row' : 'column';+ (item as HTMLElement).style.minHeight = bottomLegend ? '0px' : '44px';+ (item as HTMLElement).style.paddingLeft = bottomLegend ? '' : '16px';+ (item.children[1] as HTMLElement).style.flexDirection = bottomLegend ? 'row' : 'column';+ (item.children[1] as HTMLElement).style.gap = bottomLegend ? '4px' : '';});}}@@ -577,7 +615,6 @@ export class OrChart extends translate(i18next)(LitElement) {render() {const disabled = this._loading;- const endDateInputType = this.getInputType();return html`<div id="container"><div id="chart-container">@@ -585,52 +622,32 @@ export class OrChart extends translate(i18next)(LitElement) {</div>${(this.timestampControls || this.attributeControls || this.showLegend) ? html`- <div id="chart-controls" style="overflow: hidden auto;">- ${this.timestampControls || this.attributeControls ? html`- <div id="controls">- ${this.timestampControls ? html`- <div class="interval-controls" style="margin-right: 6px;">+ <div id="chart-controls">+ <div id="controls">+ <div class="period-controls">+ ${this.timePresetOptions && this.timePresetKey ? html`+ ${this.timestampControls ? html`${getContentWithMenuTemplate(- html`<or-mwc-input .type="${InputType.BUTTON}" .label="${i18next.t("timeframe")}: ${i18next.t(this.period ? this.period : "-")}"></or-mwc-input>`,- this._getPeriodOptions(),- this.period,- (value) => this.setPeriodOption(value)- )}- ${!!this.compareTimestamp ? html`- <or-mwc-input style="margin-left:auto;" .type="${InputType.BUTTON}" .label="${i18next.t("period")}" @or-mwc-input-changed="${() => this.setPeriodCompare(false)}" icon="minus"></or-mwc-input>- ` : html`- <or-mwc-input style="margin-left:auto;" .type="${InputType.BUTTON}" .label="${i18next.t("period")}" @or-mwc-input-changed="${() => this.setPeriodCompare(true)}" icon="plus"></or-mwc-input>- `}- </div>-- <div class="period-controls">- ${!!this.compareTimestamp ? html`- <span class="line-label solid"></span>- `: undefined}- <or-mwc-input id="ending-date" .checkAssetWrite="${false}" .type="${endDateInputType}" ?disabled="${disabled}" .value="${this.timestamp}"- @or-mwc-input-changed="${(evt: OrInputChangedEvent) => this._updateTimestamp(moment(evt.detail.value as string).toDate())}">- </or-mwc-input>- <or-icon class="button-icon" icon="chevron-left" @click="${() => this._updateTimestamp(this.timestamp!, false, undefined, 0)}"></or-icon>- <or-icon class="button-icon" icon="chevron-right" @click="${() =>this._updateTimestamp(this.timestamp!, true, undefined, 0)}"></or-icon>- </div>- ${!!this.compareTimestamp ? html `- <div class="period-controls">- <span class="line-label dashed"></span>- <or-mwc-input id="ending-date" .checkAssetWrite="${false}" .type="${endDateInputType}" ?disabled="${disabled}" .value="${this.compareTimestamp}"- @or-mwc-input-changed="${(evt: OrInputChangedEvent) => this._updateTimestamp(moment(evt.detail.value as string).toDate(), undefined, true)}">- </or-mwc-input>- <or-icon class="button-icon" icon="chevron-left" @click="${() => this._updateTimestamp(this.compareTimestamp!, false, true, 0)}"></or-icon>- <or-icon class="button-icon" icon="chevron-right" @click="${() => this._updateTimestamp(this.compareTimestamp!, true, true, 0)}"></or-icon>- </div>- ` : undefined}- ` : undefined}- ${this.attributeControls ? html`- <or-mwc-input class="button" .type="${InputType.BUTTON}" ?disabled="${disabled}" label="${i18next.t("selectAttributes")}" icon="plus" @or-mwc-input-changed="${() => this._openDialog()}"></or-mwc-input>+ html`<or-mwc-input .type="${InputType.BUTTON}" .label="${i18next.t(this.timePresetKey)}"></or-mwc-input>`,+ Array.from(this.timePresetOptions!.keys()).map((key) => ({ value: key } as ListItem)),+ this.timePresetKey,+ (value: string | string[]) => this.timePresetKey = value.toString(),+ undefined,+ undefined,+ undefined,+ true+ )}+ ` : html`+ <or-mwc-input .type="${InputType.BUTTON}" .label="${i18next.t(this.timePresetKey)}" disabled="true"></or-mwc-input>+ `}` : undefined}</div>- ` : undefined}+ ${this.attributeControls ? html`+ <or-mwc-input class="button" .type="${InputType.BUTTON}" ?disabled="${disabled}" label="${i18next.t("selectAttributes")}" icon="plus" @or-mwc-input-changed="${() => this._openDialog()}"></or-mwc-input>+ ` : undefined}+ </div>${cache(this.showLegend ? html`- <div id="attribute-list" class="${this.denseLegend ? 'attribute-list-dense' : undefined}" style="padding: ${this.denseLegend ? '6px' : '12px'};">+ <div id="attribute-list" class="${this.denseLegend ? 'attribute-list-dense' : undefined}">${this.assetAttributes == null || this.assetAttributes.length == 0 ? html`<div><span>${i18next.t('noAttributesConnected')}</span>@@ -681,28 +698,6 @@ export class OrChart extends translate(i18next)(LitElement) {}}- setPeriodOption(value:any) {- this.period = value;-- this.saveSettings();- this.requestUpdate();- }-- getInputType() {- switch (this.period) {- case "hour":- return InputType.DATETIME;- case "day":- return InputType.DATE;- case "week":- return InputType.WEEK;- case "month":- return InputType.MONTH;- case "year":- return InputType.MONTH;- }- }-removeDatasetHighlight(bgColor:string) {if(this._chart && this._chart.data && this._chart.data.datasets){this._chart.data.datasets.map((dataset, idx) => {@@ -735,20 +730,18 @@ export class OrChart extends translate(i18next)(LitElement) {if(this.assetAttributes == undefined || reset) {this.assetAttributes = [];}- if(this.period == undefined || reset) {- this.period = "day";- }- if(this.timestamp == undefined || reset) {- this.timestamp = moment().set('minute', 0).toDate();- }- if(this.compareTimestamp == undefined || reset) {- this.compareTimestamp = undefined;- }if (!this.realm) {this.realm = manager.getRealm();}+ if (!this.timePresetOptions) {+ this.timePresetOptions = this._getDefaultTimestampOptions();+ }+ if (!this.timePresetKey) {+ this.timePresetKey = this.timePresetOptions.keys().next().value.toString();+ }+if (!this.panelName) {return;}@@ -812,9 +805,6 @@ export class OrChart extends translate(i18next)(LitElement) {const asset = assetIndex >= 0 ? this.assets[assetIndex] : undefined;return asset && asset.attributes ? [assetIndex!, asset.attributes[attrRef.name!]] : undefined;}).filter((indexAndAttr) => !!indexAndAttr) as [number, Attribute<any>][];- this.period = view.period || "day";- this.timestamp = new Date();- this.compareTimestamp = view.compareOffset ? new Date(new Date().getTime() + view.compareOffset) : undefined;}}}@@ -850,8 +840,6 @@ export class OrChart extends translate(i18next)(LitElement) {const asset = this.assets[index];return !!asset ? {id: asset.id, name: attr.name} as AttributeRef : undefined;}).filter((attrRef) => !!attrRef) as AttributeRef[],- period: this.period,- compareOffset: this.timestamp && this.compareTimestamp ? this.compareTimestamp.getTime() - this.timestamp.getTime() : undefined};}@@ -967,77 +955,55 @@ export class OrChart extends translate(i18next)(LitElement) {}}- protected _getPeriodOptions() {- return [- {- text: "hour",- value: "hour"- },- {- text: "day",- value: "day"- },- {- text: "week",- value: "week"- },- {- text: "month",- value: "month"- },- {- text: "year",- value: "year"- }- ];- }-- setPeriodCompare(periodCompare:boolean) {- if (periodCompare) {- this.compareTimestamp = this.timestamp;+ protected _getDefaultTimestampOptions(): Map<string, TimePresetCallback> {+ return new Map<string, TimePresetCallback>([+ ["lastHour", (date) => [moment(date).subtract(1, 'hour').toDate(), date]],+ ["last24Hours", (date) => [moment(date).subtract(24, 'hours').toDate(), date]],+ ["last7Days", (date) => [moment(date).subtract(7, 'days').toDate(), date]],+ ["last30Days", (date) => [moment(date).subtract(30, 'days').toDate(), date]],+ ["last90Days", (date) => [moment(date).subtract(90, 'days').toDate(), date]],+ ["last6Months", (date) => [moment(date).subtract(6, 'months').toDate(), date]],+ ["lastYear", (date) => [moment(date).subtract(1, 'year').toDate(), date]]+ ]);+ }++ protected _getInterval(diffInHours: number): [number, DatapointInterval] {++ if(diffInHours <= 1) {+ return [5, DatapointInterval.MINUTE];+ } else if(diffInHours <= 3) {+ return [10, DatapointInterval.MINUTE];+ } else if(diffInHours <= 6) {+ return [30, DatapointInterval.MINUTE];+ } else if(diffInHours <= 24) { // one day+ return [1, DatapointInterval.HOUR];+ } else if(diffInHours <= 168) { // one week+ return [6, DatapointInterval.HOUR];+ } else if(diffInHours <= 720) { // one month+ return [1, DatapointInterval.DAY];} else {- this.compareTimestamp = undefined+ return [1, DatapointInterval.MONTH];}-- this.saveSettings();}protected async _loadData() {- if (this._loading || this._data || !this.assetAttributes || !this.assets || (this.assets.length === 0 && !this.dataProvider) || (this.assetAttributes.length === 0 && !this.dataProvider) || !this.period || !this.timestamp) {+ if (this._loading || this._data || !this.assetAttributes || !this.assets || (this.assets.length === 0 && !this.dataProvider) || (this.assetAttributes.length === 0 && !this.dataProvider) || !this.datapointQuery) {return;}this._loading = true;- let interval: DatapointInterval = DatapointInterval.HOUR;- let stepSize = 1;-- switch (this.period) {- case "hour":- interval = DatapointInterval.MINUTE;- stepSize = 5;- break;- case "day":- interval = DatapointInterval.HOUR;- stepSize = 1;- break;- case "week":- interval = DatapointInterval.HOUR;- stepSize = 6;- break;- case "month":- interval = DatapointInterval.DAY;- stepSize = 1;- break;- case "year":- interval = DatapointInterval.MONTH;- stepSize = 1;- break;- }+ const dates: [Date, Date] = this.timePresetOptions!.get(this.timePresetKey!)!(new Date());+ this._startOfPeriod = dates[0].getTime();+ this._endOfPeriod = dates[1].getTime();++ const diffInHours = (this._endOfPeriod - this._startOfPeriod) / 1000 / 60 / 60;+ const intervalArr = this._getInterval(diffInHours);++ const stepSize: number = intervalArr[0];+ const interval: DatapointInterval = intervalArr[1];const lowerCaseInterval = interval.toLowerCase();- this._startOfPeriod = moment(this.timestamp).startOf(this.period).startOf(lowerCaseInterval as moment.unitOfTime.StartOf).add(1, lowerCaseInterval as moment.unitOfTime.Base).toDate().getTime();- this._endOfPeriod = moment(this.timestamp).endOf(this.period).startOf(lowerCaseInterval as moment.unitOfTime.StartOf).add(1, lowerCaseInterval as moment.unitOfTime.Base).toDate().getTime();this._timeUnits = lowerCaseInterval as TimeUnit;this._stepSize = stepSize;const now = moment().toDate().getTime();@@ -1047,7 +1013,7 @@ export class OrChart extends translate(i18next)(LitElement) {let promises;if(this.dataProvider) {- await this.dataProvider(this._startOfPeriod, this._endOfPeriod, this._timeUnits, this._stepSize).then((dataset) => {+ await this.dataProvider(this._startOfPeriod, this._endOfPeriod, (interval.toString() as TimeUnit), stepSize).then((dataset) => {dataset.forEach((set) => { data.push(set); });});} else {@@ -1058,33 +1024,14 @@ export class OrChart extends translate(i18next)(LitElement) {const label = Util.getAttributeLabel(attribute, descriptors[0], asset.type, false);const unit = Util.resolveUnits(Util.getAttributeUnits(attribute, descriptors[0], asset.type));const colourIndex = index % this.colors.length;- let dataset = await this._loadAttributeData(asset, attribute, this.colors[colourIndex], interval, this._startOfPeriod!, this._endOfPeriod!, false, asset.name + " " + label);+ let dataset = await this._loadAttributeData(asset, attribute, this.colors[colourIndex], this._startOfPeriod!, this._endOfPeriod!, false, asset.name + " " + label);(dataset as any).assetId = asset.id;(dataset as any).attrName = attribute.name;(dataset as any).unit = unit;data.push(dataset);- dataset = await this._loadAttributeData(this.assets[assetIndex], attribute, this.colors[colourIndex], interval, predictedFromTimestamp, this._endOfPeriod!, true, asset.name + " " + label + " " + i18next.t("predicted"));+ dataset = await this._loadAttributeData(this.assets[assetIndex], attribute, this.colors[colourIndex], predictedFromTimestamp, this._endOfPeriod!, true, asset.name + " " + label + " " + i18next.t("predicted"));data.push(dataset);-- if (this.compareTimestamp) {- const startOfPeriod = moment(this.compareTimestamp).startOf(this.period!).startOf(lowerCaseInterval as moment.unitOfTime.StartOf).add(1, lowerCaseInterval as moment.unitOfTime.Base).toDate().getTime();- const endOfPeriod = moment(this.compareTimestamp).endOf(this.period!).startOf(lowerCaseInterval as moment.unitOfTime.StartOf).add(1, lowerCaseInterval as moment.unitOfTime.Base).toDate().getTime();- const offset = this._startOfPeriod! - startOfPeriod;-- dataset = await this._loadAttributeData(this.assets[assetIndex], attribute, this.colors[colourIndex], interval, startOfPeriod, endOfPeriod, false, asset.name + " " + label + " " + i18next.t("compare"));- dataset.data.forEach((dp) => dp.x += offset);- dataset.borderDash = [10, 10];- (dataset as any).isComparisonDataset = true;- data.push(dataset);-- predictedFromTimestamp = now < startOfPeriod ? startOfPeriod : now;- dataset = await this._loadAttributeData(this.assets[assetIndex], attribute, this.colors[colourIndex], interval, startOfPeriod, endOfPeriod, true, asset.name + " " + label + " " + i18next.t("compare") + " " + i18next.t("predicted"));- dataset.data.forEach((dp) => dp.x += offset);- dataset.borderDash = [6, 8];- (dataset as any).isComparisonDataset = true;- data.push(dataset);- }});}@@ -1095,34 +1042,8 @@ export class OrChart extends translate(i18next)(LitElement) {this._data = data;}- protected _timestampLabel(timestamp: Date | number | undefined) {- let newMoment = moment.utc(timestamp).local();-- if(this.compareTimestamp) {- const initialTimestamp = moment(this.timestamp);- switch (this.period) {- case "hour":- newMoment = moment.utc(timestamp).local();- break;- case "day":- newMoment = moment.utc(timestamp).local().set('day', initialTimestamp.day());- break;- case "week":- newMoment = moment.utc(timestamp).local().set('week', initialTimestamp.week());- break;- case "month":- newMoment = moment.utc(timestamp).local().set('month', initialTimestamp.month());- break;- case "year":- newMoment = moment.utc(timestamp).local().set('year', initialTimestamp.year());- break;- }- }- return newMoment.format();- }-- protected async _loadAttributeData(asset: Asset, attribute: Attribute<any>, color: string | undefined, interval: DatapointInterval, from: number, to: number, predicted: boolean, label: string | undefined): Promise<ChartDataset<"line", ScatterDataPoint[]>> {+ protected async _loadAttributeData(asset: Asset, attribute: Attribute<any>, color: string | undefined, from: number, to: number, predicted: boolean, label: string | undefined): Promise<ChartDataset<"line", ScatterDataPoint[]>> {const dataset: ChartDataset<"line", ScatterDataPoint[]> = {borderColor: color,@@ -1134,27 +1055,24 @@ export class OrChart extends translate(i18next)(LitElement) {borderDash: predicted ? [2, 4] : undefined};- if (asset.id && attribute.name) {- const queryParams = {- interval: interval,- fromTimestamp: from,- toTimestamp: to- };-+ if (asset.id && attribute.name && this.datapointQuery) {let response: GenericAxiosResponse<ValueDatapoint<any>[]>;+ const query = this.datapointQuery;+ query.fromTimestamp = this._startOfPeriod;+ query.toTimestamp = this._endOfPeriod;++ if(query.type == 'lttb') {+ query.amountOfPoints = Math.round(this._chartElem.clientWidth / 10); // set amount of datapoints based on current chart width.+ } else if(query.type == 'interval') {+ const diffInHours = (this.datapointQuery.toTimestamp! - this.datapointQuery.fromTimestamp!) / 1000 / 60 / 60;+ const intervalArr = this._getInterval(diffInHours);+ query.interval = (intervalArr[0].toString() + " " + intervalArr[1].toString()); // for example: "5 minute"+ }- if (!predicted) {- response = await manager.rest.api.AssetDatapointResource.getDatapoints(- asset.id,- attribute.name,- queryParams- );+ if(!predicted) {+ response = await manager.rest.api.AssetDatapointResource.getDatapoints(asset.id, attribute.name, query)} else {- response = await manager.rest.api.AssetPredictedDatapointResource.getPredictedDatapoints(- asset.id,- attribute.name,- queryParams- );+ response = await manager.rest.api.AssetPredictedDatapointResource.getPredictedDatapoints(asset.id, attribute.name, query)}if (response.status === 200) {@@ -1165,25 +1083,4 @@ export class OrChart extends translate(i18next)(LitElement) {return dataset;}- protected _updateTimestamp(timestamp: Date, forward?: boolean, compare= false, timeout= 1500) {-- if (this._updateTimestampTimer) {- window.clearTimeout(this._updateTimestampTimer);- this._updateTimestampTimer = null;- }- this._updateTimestampTimer = window.setTimeout(() => {- const newMoment = moment(timestamp);-- if (forward !== undefined) {- newMoment.add(forward ? 1 : -1, this.period);- }- if (compare) {- this.compareTimestamp = newMoment.toDate()- } else {- this.timestamp = newMoment.toDate()- }- this.saveSettings();- }, timeout);- }-}diff --git a/ui/component/or-dashboard-builder/package.json b/ui/component/or-dashboard-builder/package.jsonindex 3f1da1278..26ee10d47 100644--- a/ui/component/or-dashboard-builder/package.json+++ b/ui/component/or-dashboard-builder/package.json@@ -18,6 +18,7 @@"dependencies": {"@openremote/core": "workspace:*","@openremote/model": "workspace:*",+ "@openremote/or-chart": "^1.1.0","@openremote/rest": "workspace:*","gridstack": "^7.2.0","lit": "^2.0.2"diff --git a/ui/component/or-dashboard-builder/src/or-dashboard-engine.ts b/ui/component/or-dashboard-builder/src/or-dashboard-engine.tsnew file mode 100644index 000000000..30f551d4f--- /dev/null+++ b/ui/component/or-dashboard-builder/src/or-dashboard-engine.ts@@ -0,0 +1,17 @@+import { GridStackEngine, GridStackNode, GridStackMoveOpts } from 'gridstack';++export class OrDashboardEngine extends GridStackEngine {++ // Cancelling move when it collides with the same widget.+ // Apparently during some rerenders, the widget moved downwards after colliding with itself.+ // Now cancelling the movement when the IDs are the same.+ public moveNode(node: GridStackNode, o: GridStackMoveOpts): boolean {+ if(o.skip && o.skip.id == node.id) {+ o.x = o.skip.x;+ o.y = o.skip.y;+ o.w = o.skip.w;+ o.h = o.skip.h;+ }+ return super.moveNode(node, o);+ }+}diff --git a/ui/component/or-dashboard-builder/src/or-dashboard-preview.ts b/ui/component/or-dashboard-builder/src/or-dashboard-preview.tsindex f44b70a16..3f014eee1 100644--- a/ui/component/or-dashboard-builder/src/or-dashboard-preview.ts+++ b/ui/component/or-dashboard-builder/src/or-dashboard-preview.ts@@ -19,6 +19,7 @@ import {showSnackbar} from "@openremote/or-mwc-components/or-mwc-snackbar";import { i18next } from "@openremote/or-translate";import { when } from "lit/directives/when.js";import { cache } from "lit/directives/cache.js";+import {OrDashboardEngine} from "./or-dashboard-engine";// TODO: Add webpack/rollup to build so consumers aren't forced to use the same toolingconst gridcss = require('gridstack/dist/gridstack.min.css');@@ -79,7 +80,9 @@ const editorStyling = css`box-sizing: border-box;border: 1px solid var(--or-app-color5, ${unsafeCSS(DefaultColor5)});border-radius: 4px;- overflow: hidden !important;+ }+ .grid-stack > .grid-stack-item > .grid-stack-item-content {+ overflow: visible;}.grid-stack-item-content__active {border: 2px solid var(--or-app-color4, ${unsafeCSS(DefaultColor4)});@@ -217,6 +220,9 @@ export class OrDashboardPreview extends LitElement {}// Defaulting to a Phone viewif(!this.previewSize) { this.previewSize = this.availablePreviewSizes[3]; }++ // Register custom override functions for GridStack+ GridStack.registerEngine(OrDashboardEngine);}static get styles() {@@ -501,7 +507,7 @@ export class OrDashboardPreview extends LitElement {<span>${i18next.t('dashboard.renderingGrid')}</span></div>` : html`- <div id="container" style="justify-content: center; overflow: ${this.fullscreen ? undefined : 'hidden auto'}; position: relative;">+ <div id="container" style="justify-content: center; position: relative;">${this.activePreset?.scalingPreset == DashboardScalingPreset.BLOCK_DEVICE ? html`<div style="position: absolute; z-index: 3; height: 100%; display: flex; align-items: center;"><span>${i18next.t('dashboard.deviceNotSupported')}</span>@@ -518,7 +524,7 @@ export class OrDashboardPreview extends LitElement {return html`<div class="grid-stack-item" id="${widget.id}" gs-id="${widget.gridItem?.id}" gs-x="${widget.gridItem?.x}" gs-y="${widget.gridItem?.y}" gs-w="${widget.gridItem?.w}" gs-h="${widget.gridItem?.h}" @click="${() => { this.onGridItemClick(widget.gridItem); }}"><div class="grid-stack-item-content" style="display: flex;">- <or-dashboard-widget .widget="${widget}" .editMode="${this.editMode}" .realm="${this.realm}" style="width: 100%; height: auto;"></or-dashboard-widget>+ <or-dashboard-widget .widget="${widget}" .editMode="${this.editMode}" .realm="${this.realm}" style="width: 100%; height: auto; border-radius: 4px;"></or-dashboard-widget></div></div>`diff --git a/ui/component/or-dashboard-builder/src/or-dashboard-widget.ts b/ui/component/or-dashboard-builder/src/or-dashboard-widget.tsindex 9abdcc64c..29a00424d 100644--- a/ui/component/or-dashboard-builder/src/or-dashboard-widget.ts+++ b/ui/component/or-dashboard-builder/src/or-dashboard-widget.ts@@ -48,6 +48,16 @@ export class OrDashboardWidget extends LitElement {}shouldUpdate(changedProperties: Map<PropertyKey, unknown>): boolean {++ // Update config if some values in the spec are not set.+ // Useful for when migrations have taken place.+ if(this.widget) {+ const widgetType = widgetTypes.get(this.widget!.widgetTypeId!);+ if(widgetType) {+ this.widget.widgetConfig = widgetType.verifyConfigSpec(this.widget);+ }+ }+const changed = changedProperties;changed.delete('resizeObserver');return changed.size > 0;@@ -79,20 +89,17 @@ export class OrDashboardWidget extends LitElement {protected render() {return html`- <div id="widget-container" style="height: 100%; padding: 8px 16px 8px 16px; display: flex; flex-direction: column; overflow: auto;">- <div style="display: flex; justify-content: space-between; align-items: center; margin-right: -12px; height: 36px;">+ <div id="widget-container" style="height: calc(100% - 16px); padding: 8px 16px 8px 16px; display: flex; flex-direction: column;">+ <div style="flex: 0 0 36px; display: flex; justify-content: space-between; align-items: center;"><span class="panel-title" style="width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">${this.widget?.displayName?.toUpperCase()}</span>- <div>- <!--<or-mwc-input type="${InputType.BUTTON}" outlined label="Period"></or-mwc-input>-->- <!--<or-mwc-input type="${InputType.BUTTON}" label="Settings"></or-mwc-input>-->- <!--<or-mwc-input type="${InputType.BUTTON}" icon="refresh" @or-mwc-input-changed="${() => { this.requestUpdate(); }}"></or-mwc-input>-->- </div></div>- ${when((!this.error && !this.loading), () => html`- ${this.getWidgetContent(this.widget!)}- `, () => html`- ${this.error ? html`${this.error}` : html`${i18next.t('loading')}`}- `)}+ <div style="flex: 1; max-height: calc(100% - 36px);">+ ${when((!this.error && !this.loading), () => html`+ ${this.getWidgetContent(this.widget!)}+ `, () => html`+ ${this.error ? html`${this.error}` : html`${i18next.t('loading')}`}+ `)}+ </div></div>`}diff --git a/ui/component/or-dashboard-builder/src/widgets/or-base-widget.ts b/ui/component/or-dashboard-builder/src/widgets/or-base-widget.tsindex 7e5fcb52f..3e2520b59 100644--- a/ui/component/or-dashboard-builder/src/widgets/or-base-widget.ts+++ b/ui/component/or-dashboard-builder/src/widgets/or-base-widget.ts@@ -13,6 +13,7 @@ export interface OrWidgetEntity {MIN_PIXEL_HEIGHT: number;getDefaultConfig: (widget: DashboardWidget) => OrWidgetConfig;+ verifyConfigSpec: (widget: DashboardWidget) => OrWidgetConfig;getWidgetHTML: (widget: DashboardWidget, editMode: boolean, realm: string) => TemplateResult;getSettingsHTML: (widget: DashboardWidget, realm: string) => TemplateResult;diff --git a/ui/component/or-dashboard-builder/src/widgets/or-chart-widget.ts b/ui/component/or-dashboard-builder/src/widgets/or-chart-widget.tsindex 7fda6421b..868ee2ed9 100644--- a/ui/component/or-dashboard-builder/src/widgets/or-chart-widget.ts+++ b/ui/component/or-dashboard-builder/src/widgets/or-chart-widget.ts@@ -1,25 +1,47 @@-import {Asset, Attribute, AttributeRef, DashboardWidget} from "@openremote/model";-import { InputType, OrInputChangedEvent } from "@openremote/or-mwc-components/or-mwc-input";+import {Asset, AssetDatapointIntervalQuery, AssetDatapointIntervalQueryFormula, AssetDatapointLTTBQuery, AssetDatapointQueryUnion, Attribute, AttributeRef, DashboardWidget} from "@openremote/model";+import {InputType, OrInputChangedEvent} from "@openremote/or-mwc-components/or-mwc-input";import {i18next} from "@openremote/or-translate";import {html, LitElement, TemplateResult} from "lit";import {customElement, property, state} from "lit/decorators.js";+import {when} from "lit/directives/when.js";+import {choose} from 'lit/directives/choose.js';import {OrWidgetConfig, OrWidgetEntity} from "./or-base-widget";import {SettingsPanelType, widgetSettingsStyling} from "../or-dashboard-settingspanel";import {style} from "../style";-import manager from "@openremote/core";-import { showSnackbar } from "@openremote/or-mwc-components/or-mwc-snackbar";-+import manager, { Util } from "@openremote/core";+import {showSnackbar} from "@openremote/or-mwc-components/or-mwc-snackbar";+import moment from "moment";+import {TimePresetCallback} from "@openremote/or-chart";export interface ChartWidgetConfig extends OrWidgetConfig {displayName: string;attributeRefs: AttributeRef[];- period?: 'year' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second';- timestamp?: Date;- compareTimestamp?: Date;+ datapointQuery: AssetDatapointQueryUnion;+ chartOptions?: any; // ChartConfiguration<"line", ScatterDataPoint[]>showTimestampControls: boolean;+ defaultTimePresetKey: string;showLegend: boolean;}+const timePresetOptions = new Map<string, TimePresetCallback>([+ ["lastHour", (date: Date) => [moment(date).subtract(1, 'hour').toDate(), date]],+ ["last24Hours", (date: Date) => [moment(date).subtract(24, 'hours').toDate(), date]],+ ["last7Days", (date: Date) => [moment(date).subtract(7, 'days').toDate(), date]],+ ["last30Days", (date: Date) => [moment(date).subtract(30, 'days').toDate(), date]],+ ["last90Days", (date: Date) => [moment(date).subtract(90, 'days').toDate(), date]],+ ["last6Months", (date: Date) => [moment(date).subtract(6, 'months').toDate(), date]],+ ["lastYear", (date: Date) => [moment(date).subtract(1, 'year').toDate(), date]],+ ["thisHour", (date: Date) => [moment(date).startOf('hour').toDate(), moment(date).endOf('hour').toDate()]],+ ["thisDay", (date: Date) => [moment(date).startOf('day').toDate(), moment(date).endOf('day').toDate()]],+ ["thisWeek", (date: Date) => [moment(date).startOf('isoWeek').toDate(), moment(date).endOf('isoWeek').toDate()]],+ ["thisMonth", (date: Date) => [moment(date).startOf('month').toDate(), moment(date).endOf('month').toDate()]],+ ["thisYear", (date: Date) => [moment(date).startOf('year').toDate(), moment(date).endOf('year').toDate()]],+ ["yesterday", (date: Date) => [moment(date).subtract(24, 'hours').startOf('day').toDate(), moment(date).subtract(24, 'hours').endOf('day').toDate()]],+ ["thisDayLastWeek", (date: Date) => [moment(date).subtract(1, 'week').startOf('day').toDate(), moment(date).subtract(1, 'week').endOf('day').toDate()]],+ ["previousWeek", (date: Date) => [moment(date).subtract(1, 'week').startOf('isoWeek').toDate(), moment(date).subtract(1, 'week').endOf('isoWeek').toDate()]],+ ["previousMonth", (date: Date) => [moment(date).subtract(1, 'month').startOf('month').toDate(), moment(date).subtract(1, 'month').endOf('month').toDate()]],+ ["previousYear", (date: Date) => [moment(date).subtract(1, 'year').startOf('year').toDate(), moment(date).subtract(1, 'year').endOf('year').toDate()]]+])export class OrChartWidget implements OrWidgetEntity {@@ -31,18 +53,43 @@ export class OrChartWidget implements OrWidgetEntity {readonly MIN_PIXEL_HEIGHT: number = 0;getDefaultConfig(widget: DashboardWidget): ChartWidgetConfig {+ const preset = "lastHour"+ const dateFunc = timePresetOptions.get(preset);+ const dates = dateFunc!(new Date());return {displayName: widget?.displayName,attributeRefs: [],- period: "day",- timestamp: new Date(),+ datapointQuery: {+ type: "lttb",+ fromTimestamp: dates[0].getTime(),+ toTimestamp: dates[1].getTime(),+ amountOfPoints: 100+ },+ chartOptions: {+ options: {+ scales: {+ y: {+ min: undefined,+ max: undefined+ }+ }+ },+ },showTimestampControls: false,+ defaultTimePresetKey: preset,showLegend: true} as ChartWidgetConfig;}+ // Triggered every update to double check if the specification.+ // It will merge missing values, or you can add custom logic to process here.+ verifyConfigSpec(widget: DashboardWidget): ChartWidgetConfig {+ const defaultConfig = this.getDefaultConfig(widget);+ return Util.mergeObjects(defaultConfig, widget.widgetConfig, false) as ChartWidgetConfig;+ }+getWidgetHTML(widget: DashboardWidget, editMode: boolean, realm: string): TemplateResult {- return html`<or-chart-widget .widget="${widget}" .editMode="${editMode}" .realm="${realm}" style="overflow: auto; height: 100%;"></or-chart-widget>`;+ return html`<or-chart-widget .widget="${widget}" .editMode="${editMode}" .realm="${realm}"></or-chart-widget>`;}getSettingsHTML(widget: DashboardWidget, realm: string): TemplateResult {@@ -76,15 +123,36 @@ export class OrChartWidgetContent extends LitElement {render() {return html`- <or-chart .assets="${this.assets}" .assetAttributes="${this.assetAttributes}" .period="${this.widget?.widgetConfig?.period}" denseLegend="${true}"- .showLegend="${(this.widget?.widgetConfig?.showLegend != null) ? this.widget?.widgetConfig?.showLegend : true}" .realm="${this.realm}"- .attributeControls="${false}" .timestampControls="${this.widget?.widgetConfig?.showTimestampControls}"- .compareTimestamp="${false}" style="height: 100%"+ <or-chart .assets="${this.assets}" .assetAttributes="${this.assetAttributes}" .realm="${this.realm}"+ .showLegend="${(this.widget?.widgetConfig?.showLegend != null) ? this.widget?.widgetConfig?.showLegend : true}"+ .attributeControls="${false}" .timestampControls="${!this.editMode && this.widget?.widgetConfig?.showTimestampControls}" .algorithm="${this.widget?.widgetConfig?.algorithm}"+ .timePresetOptions="${timePresetOptions}" .timePresetKey="${this.widget?.widgetConfig?.defaultTimePresetKey}"+ .datapointQuery="${this.widget?.widgetConfig?.datapointQuery}" .chartOptions="${this.widget?.widgetConfig?.chartOptions}"+ style="height: 100%"></or-chart>`}+ willUpdate(changedProperties: Map<string, any>) {++ // Add datapointQuery if not set yet (due to migration)+ if(this.widget && this.widget.widgetConfig.datapointQuery == undefined) {+ this.widget.widgetConfig.datapointQuery = {+ type: "lttb",+ fromTimestamp: moment().set('minute', -60).toDate().getTime(),+ toTimestamp: moment().set('minute', 60).toDate().getTime(),+ amountOfPoints: 100+ };+ if(!changedProperties.has("widget")) {+ changedProperties.set("widget", this.widget);+ }+ }+ super.willUpdate(changedProperties);+ }+updated(changedProperties: Map<string, any>) {++ // Fetch assetsif(changedProperties.has("widget") || changedProperties.has("editMode")) {if(this.assetAttributes.length != this.widget!.widgetConfig.attributeRefs.length) {this.fetchAssets(this.widget?.widgetConfig).then((assets) => {@@ -184,13 +252,30 @@ class OrChartWidgetSettings extends LitElement {public widget?: DashboardWidget;// Default values- private expandedPanels: string[] = [i18next.t('attributes'), i18next.t('display')];+ private expandedPanels: string[] = [i18next.t('attributes'), i18next.t('algorithm'), i18next.t('display'), i18next.t('dashboard.chartConfig')];static get styles() {return [style, widgetSettingsStyling];}+ willUpdate(changedProperties: Map<string, any>) {++ // Add datapointQuery if not set yet (due to migration)+ if(this.widget && this.widget.widgetConfig.datapointQuery == undefined) {+ this.widget.widgetConfig.datapointQuery = {+ type: "lttb",+ fromTimestamp: moment().set('minute', -60).toDate().getTime(),+ toTimestamp: moment().set('minute', 60).toDate().getTime(),+ amountOfPoints: 100+ };+ if(!changedProperties.has("widget")) {+ changedProperties.set("widget", this.widget);+ }+ }+ super.willUpdate(changedProperties);+ }+// UI Renderingrender() {const config = JSON.parse(JSON.stringify(this.widget!.widgetConfig)) as ChartWidgetConfig; // duplicate to edit, to prevent parent updates. Please trigger updateConfig()@@ -210,21 +295,19 @@ class OrChartWidgetSettings extends LitElement {</div><div>${this.expandedPanels.includes(i18next.t('display')) ? html`- <div style="padding: 24px 24px 48px 24px;">+ <div style="padding: 12px 24px 48px 24px;"><div>- <or-mwc-input .type="${InputType.SELECT}" style="width: 100%;"- .options="${['year', 'month', 'week', 'day', 'hour', 'minute', 'second']}"- .value="${config.period}" label="${i18next.t('timeframe')}"+ <or-mwc-input .type="${InputType.SELECT}" label="${i18next.t('timeframeDefault')}" style="width: 100%;"+ .options="${Array.from(timePresetOptions.keys())}" value="${config.defaultTimePresetKey}"@or-mwc-input-changed="${(event: OrInputChangedEvent) => {- config.period = event.detail.value;+ config.defaultTimePresetKey = event.detail.value.toString();this.updateConfig(this.widget!, config);}}"></or-mwc-input></div>- <div class="switchMwcInputContainer" style="margin-top: 18px;">- <span>${i18next.t('dashboard.showTimestampControls')}</span>- <or-mwc-input .type="${InputType.SWITCH}" style="width: 70px;"- .value="${config.showTimestampControls}"+ <div class="switchMwcInputContainer" style="margin-top: 16px;">+ <span>${i18next.t('dashboard.allowTimerangeSelect')}</span>+ <or-mwc-input .type="${InputType.SWITCH}" style="margin: 0 -10px;" .value="${config.showTimestampControls}"@or-mwc-input-changed="${(event: OrInputChangedEvent) => {config.showTimestampControls = event.detail.value;this.updateConfig(this.widget!, config);@@ -233,8 +316,7 @@ class OrChartWidgetSettings extends LitElement {</div><div class="switchMwcInputContainer"><span>${i18next.t('dashboard.showLegend')}</span>- <or-mwc-input .type="${InputType.SWITCH}" style="width: 70px;"- .value="${config.showLegend}"+ <or-mwc-input .type="${InputType.SWITCH}" style="margin: 0 -10px;" .value="${config.showLegend}"@or-mwc-input-changed="${(event: OrInputChangedEvent) => {config.showLegend = event.detail.value;this.updateConfig(this.widget!, config);@@ -244,11 +326,117 @@ class OrChartWidgetSettings extends LitElement {</div>` : null}</div>+ <div>+ ${this.generateExpandableHeader(i18next.t('dashboard.chartConfig'))}+ </div>+ <div>+ ${when(this.expandedPanels.includes(i18next.t('dashboard.chartConfig')), () => {+ const min = config.chartOptions.options?.scales?.y?.min;+ const max = config.chartOptions.options?.scales?.y?.max;+ return html`+ <div style="padding: 12px 24px 48px 24px; display: flex; flex-direction: column; gap: 16px;">+ <div>+ <div style="display: flex;">+ ${max != undefined ? html`+ <or-mwc-input .type="${InputType.NUMBER}" label="${i18next.t('yAxis') + ' ' + i18next.t('max')}" .value="${max}" style="width: 100%;"+ @or-mwc-input-changed="${(event: OrInputChangedEvent) => {+ config.chartOptions.options.scales.y.max = event.detail.value;+ this.updateConfig(this.widget!, config);+ }}"+ ></or-mwc-input>+ ` : html`+ <or-mwc-input .type="${InputType.TEXT}" label="${i18next.t('yAxis') + ' ' + i18next.t('max')}" disabled="true" value="auto" style="width: 100%;"></or-mwc-input>+ `}+ <or-mwc-input .type="${InputType.SWITCH}" style="margin: 0 -10px 0 0;" .value="${max != undefined}"+ @or-mwc-input-changed="${() => {+ config.chartOptions.options!.scales!.y!.max = (max == undefined) ? 100 : undefined;+ this.updateConfig(this.widget!, config);+ }}"+ ></or-mwc-input>+ </div>+ <div style="display: flex; margin-top: 12px;">+ ${min != undefined ? html`+ <or-mwc-input .type="${InputType.NUMBER}" label="${i18next.t('yAxis') + ' ' + i18next.t('min')}" .value="${min}" style="width: 100%;"+ @or-mwc-input-changed="${(event: OrInputChangedEvent) => {+ config.chartOptions.options.scales.y.min = event.detail.value;+ this.updateConfig(this.widget!, config);+ }}"+ ></or-mwc-input>+ ` : html`+ <or-mwc-input .type="${InputType.TEXT}" label="${i18next.t('yAxis') + ' ' + i18next.t('min')}" disabled="true" value="auto" style="width: 100%;"></or-mwc-input>+ `}+ <or-mwc-input .type="${InputType.SWITCH}" style="margin: 0 -10px 0 0;" .value="${min != undefined}"+ @or-mwc-input-changed="${() => {+ config.chartOptions.options!.scales!.y!.min = (min == undefined) ? 0 : undefined;+ this.updateConfig(this.widget!, config);+ }}"+ ></or-mwc-input>+ </div>+ </div>+ </div>+ `+ })}+ </div>+ <div>+ ${this.generateExpandableHeader(i18next.t('algorithm'))}+ </div>+ <div>+ ${this.expandedPanels.includes('algorithm') ? html`+ ${when(config.datapointQuery, () => {+ const typeOptions = new Map<string, string>([["Default", 'lttb'], ["With interval", 'interval']]);+ const typeValue = Array.from(typeOptions.entries()).find((entry => entry[1] == config.datapointQuery.type))![0]+ return html`+ <div style="padding: 12px 24px 48px 24px;">+ <or-mwc-input .type="${InputType.SELECT}" style="width: 100%" .options="${Array.from(typeOptions.keys())}"+ .value="${typeValue}" label="${i18next.t('algorithm')}" @or-mwc-input-changed="${(event: OrInputChangedEvent) => {+ config.datapointQuery.type = typeOptions.get(event.detail.value)! as any;+ this.updateConfig(this.widget!, config, true);+ }}"+ ></or-mwc-input>+ ${choose(config.datapointQuery.type, [+ ['interval', () => {+ const intervalQuery = config.datapointQuery as AssetDatapointIntervalQuery;+ const formulaOptions = [AssetDatapointIntervalQueryFormula.MIN, AssetDatapointIntervalQueryFormula.AVG, AssetDatapointIntervalQueryFormula.MAX];+ return html`+ <or-mwc-input .type="${InputType.SELECT}" style="width: 100%; margin-top: 18px;" .options="${formulaOptions}"+ .value="${intervalQuery.formula}" label="${i18next.t('formula')}" @or-mwc-input-changed="${(event: OrInputChangedEvent) => {+ intervalQuery.formula = event.detail.value;+ this.updateConfig(this.widget!, config, true);+ }}"+ ></or-mwc-input>+ `;+ }]+ ])}+ </div>+ `+ }, () => {+ console.error(config);+ return html`${i18next.t('errorOccurred')}`;+ })}+ ` : null}+ </div>`}updateConfig(widget: DashboardWidget, config: OrWidgetConfig | any, force: boolean = false) {const oldWidget = JSON.parse(JSON.stringify(widget)) as DashboardWidget;++ if(config.datapointQuery) {+ switch (config.datapointQuery.type) {+ case 'lttb': {+ const query = config.datapointQuery as AssetDatapointLTTBQuery;+ if(!query.amountOfPoints) { query.amountOfPoints = 100; }+ break;+ }+ case 'interval': {+ const query = config.datapointQuery as AssetDatapointIntervalQuery;+ if(!query.interval) { query.interval = "1 hour"; }+ if(!query.gapFill) { query.gapFill = false; }+ if(!query.formula) { query.formula = AssetDatapointIntervalQueryFormula.AVG; }+ }+ }+ }+widget.widgetConfig = config;this.requestUpdate("widget", oldWidget);this.forceParentUpdate(new Map<string, any>([["widget", widget]]), force);diff --git a/ui/component/or-dashboard-builder/src/widgets/or-gauge-widget.ts b/ui/component/or-dashboard-builder/src/widgets/or-gauge-widget.tsindex 7bb70176f..d70dd73d2 100644--- a/ui/component/or-dashboard-builder/src/widgets/or-gauge-widget.ts+++ b/ui/component/or-dashboard-builder/src/widgets/or-gauge-widget.ts@@ -6,7 +6,7 @@ import { html, LitElement, TemplateResult } from "lit";import { customElement, property, state } from "lit/decorators.js";import "@openremote/or-gauge";import { i18next } from "@openremote/or-translate";-import manager from "@openremote/core";+import manager, { Util } from "@openremote/core";import { showSnackbar } from "@openremote/or-mwc-components/or-mwc-snackbar";import {InputType} from "@openremote/or-mwc-components/or-mwc-input";import { when } from "lit/directives/when.js";@@ -40,8 +40,15 @@ export class OrGaugeWidget implements OrWidgetEntity {valueType: 'number',} as GaugeWidgetConfig;}++ // Triggered every update to double check if the specification.+ // It will merge missing values, or you can add custom logic to process here.+ verifyConfigSpec(widget: DashboardWidget): GaugeWidgetConfig {+ return Util.mergeObjects(this.getDefaultConfig(widget), widget.widgetConfig, false) as GaugeWidgetConfig;+ }+getWidgetHTML(widget: DashboardWidget, editMode: boolean, realm: string) {- return html`<or-gauge-widget .widget="${widget}" .editMode="${editMode}" realm="${realm}" style="height: 100%; overflow: hidden;"></or-gauge-widget>`;+ return html`<or-gauge-widget .widget="${widget}" .editMode="${editMode}" realm="${realm}"></or-gauge-widget>`;}getSettingsHTML(widget: DashboardWidget, realm: string) {@@ -75,7 +82,7 @@ export class OrGaugeWidgetContent extends LitElement {return html`<or-gauge .asset="${this.assets[0]}" .assetAttribute="${this.assetAttributes[0]}" .thresholds="${this.widget?.widgetConfig.thresholds}".decimals="${this.widget?.widgetConfig.decimals}" .min="${this.widget?.widgetConfig.min}" .max="${this.widget?.widgetConfig.max}"- style="height: 100%;"></or-gauge>+ style="height: 100%; overflow: hidden;"></or-gauge>`;}, () => {return html`diff --git a/ui/component/or-dashboard-builder/src/widgets/or-kpi-widget.ts b/ui/component/or-dashboard-builder/src/widgets/or-kpi-widget.tsindex 1c1b89931..bbc1d7ec0 100644--- a/ui/component/or-dashboard-builder/src/widgets/or-kpi-widget.ts+++ b/ui/component/or-dashboard-builder/src/widgets/or-kpi-widget.ts@@ -1,4 +1,4 @@-import manager from "@openremote/core";+import manager, { Util } from "@openremote/core";import {Asset, Attribute, AttributeRef, DashboardWidget } from "@openremote/model";import { showSnackbar } from "@openremote/or-mwc-components/or-mwc-snackbar";import { i18next } from "@openremote/or-translate";@@ -12,9 +12,10 @@ import {InputType, OrInputChangedEvent } from "@openremote/or-mwc-components/or-export interface KpiWidgetConfig extends OrWidgetConfig {displayName: string;attributeRefs: AttributeRef[];- period?: 'year' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second';+ period?: 'year' | 'month' | 'week' | 'day' | 'hour';decimals: number;deltaFormat: "absolute" | "percentage";+ showTimestampControls: boolean;}export class OrKpiWidget implements OrWidgetEntity {@@ -36,6 +37,13 @@ export class OrKpiWidget implements OrWidgetEntity {} as KpiWidgetConfig;}+ // Triggered every update to double check if the specification.+ // It will merge missing values, or you can add custom logic to process here.+ verifyConfigSpec(widget: DashboardWidget): KpiWidgetConfig {+ return Util.mergeObjects(this.getDefaultConfig(widget), widget.widgetConfig, false) as KpiWidgetConfig;+ }++getSettingsHTML(widget: DashboardWidget, realm: string) {return html`<or-kpi-widgetsettings .widget="${widget}" realm="${realm}"></or-kpi-widgetsettings>`;}@@ -68,7 +76,7 @@ export class OrKpiWidgetContent extends LitElement {return html`<or-attribute-card .assets="${this.loadedAssets}" .assetAttributes="${this.assetAttributes}" .period="${this.widget?.widgetConfig.period}".deltaFormat="${this.widget?.widgetConfig.deltaFormat}" .mainValueDecimals="${this.widget?.widgetConfig.decimals}"- showControls="${false}" showTitle="${false}" realm="${this.realm}" style="height: 100%;">+ showControls="${!this.editMode && this.widget?.widgetConfig?.showTimestampControls}" showTitle="${false}" realm="${this.realm}" style="height: 100%;"></or-attribute-card>`}@@ -151,7 +159,7 @@ export class OrKpiWidgetSettings extends LitElement {<div style="padding: 24px 24px 48px 24px;"><div><or-mwc-input .type="${InputType.SELECT}" style="width: 100%;"- .options="${['year', 'month', 'week', 'day', 'hour', 'minute', 'second']}"+ .options="${['year', 'month', 'week', 'day', 'hour']}".value="${config.period}" label="${i18next.t('timeframe')}"@or-mwc-input-changed="${(event: OrInputChangedEvent) => {config.period = event.detail.value;@@ -159,6 +167,15 @@ export class OrKpiWidgetSettings extends LitElement {}}"></or-mwc-input></div>+ <div class="switchMwcInputContainer" style="margin-top: 16px;">+ <span>${i18next.t('dashboard.allowTimerangeSelect')}</span>+ <or-mwc-input .type="${InputType.SWITCH}" style="margin: 0 -10px;" .value="${config.showTimestampControls}"+ @or-mwc-input-changed="${(event: OrInputChangedEvent) => {+ config.showTimestampControls = event.detail.value;+ this.updateConfig(this.widget!, config);+ }}"+ ></or-mwc-input>+ </div></div>` : null}</div>diff --git a/ui/component/or-dashboard-builder/src/widgets/or-map-widget.ts b/ui/component/or-dashboard-builder/src/widgets/or-map-widget.tsindex 2adbf66e7..2a63cc70f 100644--- a/ui/component/or-dashboard-builder/src/widgets/or-map-widget.ts+++ b/ui/component/or-dashboard-builder/src/widgets/or-map-widget.ts@@ -83,10 +83,15 @@ export class OrMapWidget implements OrWidgetEntity {} as MapWidgetConfig;}+ // Triggered every update to double check if the specification.+ // It will merge missing values, or you can add custom logic to process here.+ verifyConfigSpec(widget: DashboardWidget): MapWidgetConfig {+ return Util.mergeObjects(this.getDefaultConfig(widget), widget.widgetConfig, false) as MapWidgetConfig;+ }+getWidgetHTML(widget: DashboardWidget, editMode: boolean, realm: string): TemplateResult {return html`- <or-map-widget .widget="${widget}" .editMode="${editMode}" .realm="${realm}"- style="overflow: auto; height: 100%;"></or-map-widget>`;+ <or-map-widget .widget="${widget}" .editMode="${editMode}" .realm="${realm}" style="overflow: hidden;"></or-map-widget>`;}getSettingsHTML(widget: DashboardWidget, realm: string): TemplateResult {@@ -122,55 +127,57 @@ export class OrMapWidgetContent extends LitElement {render() {this.markers = {};return html`- <or-map id="miniMap" class="or-map" .zoom="${this.widget?.widgetConfig?.zoom}"- .center="${this.widget?.widgetConfig?.center}"- style="height: 100%">- ${(this.assets) ?- this.assets.filter((asset: Asset) => {- if (!asset.attributes) {- return false;- }- const attr = asset.attributes[WellknownAttributes.LOCATION] as Attribute<GeoJSONPoint>;- return !attr.meta || !attr.meta.hasOwnProperty(WellknownMetaItems.SHOWONDASHBOARD) || !!Util.getMetaValue(WellknownMetaItems.SHOWONDASHBOARD, attr);- }).map(asset => {- if (this.markers) {- // Configure map marker asset settings- this.markers[asset.type!] = {attributeName: this.widget!.widgetConfig.attributeName};- this.markers[asset.type!].showUnits = this.widget!.widgetConfig.showUnits;- this.markers[asset.type!].showLabel = this.widget!.widgetConfig.showLabels;- if (this.widget!.widgetConfig.valueType == 'boolean') {- this.widget!.widgetConfig.boolColors.true = this.widget!.widgetConfig.boolColors.true.replace("#", "");- this.widget!.widgetConfig.boolColors.false = this.widget!.widgetConfig.boolColors.false.replace("#", "");- this.markers[asset.type!].colours = this.widget!.widgetConfig.boolColors;- } else if (this.widget!.widgetConfig.valueType == 'text') {- var colors: AttributeMarkerColours = {type: 'string',};- (this.widget!.widgetConfig.textColors as [string, string][]).map((threshold) => {- colors[threshold[0] as string] = (threshold[1] as string).replace('#', '');- })- this.markers[asset.type!].colours = colors;- } else {- var ranges: AttributeMarkerColoursRange[] = [];- (this.widget!.widgetConfig.thresholds as [number, string][]).sort((x, y) => (x[0] > y[0]) ? -1 : 1).map((threshold, index) => {- var range: AttributeMarkerColoursRange = {- min: threshold[0],- colour: threshold[1].replace('#', '')- }- ranges.push(range);- })- var colorsNum: RangeAttributeMarkerColours = {- type: 'range',- ranges: ranges- };- this.markers[asset.type!].colours = colorsNum;+ <div style="height: 100%; display: flex; flex-direction: column; overflow: hidden;">+ <or-map id="miniMap" class="or-map" .zoom="${this.widget?.widgetConfig?.zoom}"+ .center="${this.widget?.widgetConfig?.center}"+ style="flex: 1;">+ ${(this.assets) ?+ this.assets.filter((asset: Asset) => {+ if (!asset.attributes) {+ return false;}- }- return html`+ const attr = asset.attributes[WellknownAttributes.LOCATION] as Attribute<GeoJSONPoint>;+ return !attr.meta || !attr.meta.hasOwnProperty(WellknownMetaItems.SHOWONDASHBOARD) || !!Util.getMetaValue(WellknownMetaItems.SHOWONDASHBOARD, attr);+ }).map(asset => {+ if (this.markers) {+ // Configure map marker asset settings+ this.markers[asset.type!] = {attributeName: this.widget!.widgetConfig.attributeName};+ this.markers[asset.type!].showUnits = this.widget!.widgetConfig.showUnits;+ this.markers[asset.type!].showLabel = this.widget!.widgetConfig.showLabels;+ if (this.widget!.widgetConfig.valueType == 'boolean') {+ this.widget!.widgetConfig.boolColors.true = this.widget!.widgetConfig.boolColors.true.replace("#", "");+ this.widget!.widgetConfig.boolColors.false = this.widget!.widgetConfig.boolColors.false.replace("#", "");+ this.markers[asset.type!].colours = this.widget!.widgetConfig.boolColors;+ } else if (this.widget!.widgetConfig.valueType == 'text') {+ var colors: AttributeMarkerColours = {type: 'string',};+ (this.widget!.widgetConfig.textColors as [string, string][]).map((threshold) => {+ colors[threshold[0] as string] = (threshold[1] as string).replace('#', '');+ })+ this.markers[asset.type!].colours = colors;+ } else {+ var ranges: AttributeMarkerColoursRange[] = [];+ (this.widget!.widgetConfig.thresholds as [number, string][]).sort((x, y) => (x[0] > y[0]) ? -1 : 1).map((threshold, index) => {+ var range: AttributeMarkerColoursRange = {+ min: threshold[0],+ colour: threshold[1].replace('#', '')+ }+ ranges.push(range);+ })+ var colorsNum: RangeAttributeMarkerColours = {+ type: 'range',+ ranges: ranges+ };+ this.markers[asset.type!].colours = colorsNum;+ }+ }+ return html`<or-map-marker-asset .asset="${asset}".config="${this.markers}"></or-map-marker-asset>`- }) : undefined}- }- </or-map>+ }) : undefined}+ }+ </or-map>+ </div>`}diff --git a/yarn.lock b/yarn.lockindex 304b3371c..86087fad9 100644--- a/yarn.lock+++ b/yarn.lock@@ -2454,6 +2454,7 @@ __metadata:dependencies:"@openremote/core": "workspace:*""@openremote/model": "workspace:*"+ "@openremote/or-chart": ^1.1.0"@openremote/rest": "workspace:*""@openremote/util": "workspace:*"gridstack: ^7.2.0