Search in sources :

Example 1 with AssetDatapoint

use of org.openremote.model.datapoint.AssetDatapoint in project openremote by openremote.

the class AssetDatapointService method processAssetUpdate.

@Override
public boolean processAssetUpdate(EntityManager em, ServerAsset asset, AssetAttribute attribute, Source source) throws AssetProcessingException {
    if (Datapoint.isDatapointsCapable(attribute) && attribute.isStoreDatapoints() && attribute.getStateEvent().isPresent() && attribute.getStateEvent().get().getValue().isPresent()) {
        // Don't store datapoints with null value
        LOG.finest("Storing datapoint for: " + attribute);
        AssetDatapoint assetDatapoint = new AssetDatapoint(attribute.getStateEvent().get());
        em.persist(assetDatapoint);
    }
    return false;
}
Also used : AssetDatapoint(org.openremote.model.datapoint.AssetDatapoint)

Example 2 with AssetDatapoint

use of org.openremote.model.datapoint.AssetDatapoint in project openremote by openremote.

the class AssetDatapointService method purgeDataPoints.

protected void purgeDataPoints() {
    LOG.info("Starting data points purge daily task");
    try {
        // Get list of attributes that have custom durations
        List<Asset<?>> assets = assetStorageService.findAll(new AssetQuery().attributes(new AttributePredicate().meta(new NameValuePredicate(MetaItemType.DATA_POINTS_MAX_AGE_DAYS, null))));
        List<Pair<String, Attribute<?>>> attributes = assets.stream().map(asset -> asset.getAttributes().stream().filter(assetAttribute -> assetAttribute.hasMeta(MetaItemType.DATA_POINTS_MAX_AGE_DAYS)).map(assetAttribute -> new Pair<String, Attribute<?>>(asset.getId(), assetAttribute)).collect(toList())).flatMap(List::stream).collect(toList());
        // Purge data points not in the above list using default duration
        LOG.fine("Purging data points of attributes that use default max age days of " + maxDatapointAgeDays);
        persistenceService.doTransaction(em -> em.createQuery("delete from AssetDatapoint dp " + "where dp.timestamp < :dt" + buildWhereClause(attributes, true)).setParameter("dt", Date.from(timerService.getNow().truncatedTo(DAYS).minus(maxDatapointAgeDays, DAYS))).executeUpdate());
        if (!attributes.isEmpty()) {
            // Purge data points that have specific age constraints
            Map<Integer, List<Pair<String, Attribute<?>>>> ageAttributeRefMap = attributes.stream().collect(groupingBy(attributeRef -> attributeRef.value.getMetaValue(MetaItemType.DATA_POINTS_MAX_AGE_DAYS).orElse(maxDatapointAgeDays)));
            ageAttributeRefMap.forEach((age, attrs) -> {
                LOG.fine("Purging data points of " + attrs.size() + " attributes that use a max age of " + age);
                try {
                    persistenceService.doTransaction(em -> em.createQuery("delete from AssetDatapoint dp " + "where dp.timestamp < :dt" + buildWhereClause(attrs, false)).setParameter("dt", Date.from(timerService.getNow().truncatedTo(DAYS).minus(age, DAYS))).executeUpdate());
                } catch (Exception e) {
                    LOG.log(Level.SEVERE, "An error occurred whilst deleting data points, this should not happen", e);
                }
            });
        }
    } catch (Exception e) {
        LOG.log(Level.WARNING, "Failed to run data points purge", e);
    }
    LOG.info("Finished data points purge daily task");
}
Also used : AssetStorageService(org.openremote.manager.asset.AssetStorageService) Arrays(java.util.Arrays) ScheduledFuture(java.util.concurrent.ScheduledFuture) AssetProcessingException(org.openremote.manager.asset.AssetProcessingException) AttributeRef(org.openremote.model.attribute.AttributeRef) LocalDateTime(java.time.LocalDateTime) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) AttributeWriteFailure(org.openremote.model.attribute.AttributeWriteFailure) STORE_DATA_POINTS(org.openremote.model.value.MetaItemType.STORE_DATA_POINTS) MapAccess.getInteger(org.openremote.container.util.MapAccess.getInteger) MapAccess.getString(org.openremote.container.util.MapAccess.getString) Level(java.util.logging.Level) UniqueIdentifierGenerator(org.openremote.container.util.UniqueIdentifierGenerator) Attribute(org.openremote.model.attribute.Attribute) Duration(java.time.Duration) Map(java.util.Map) NameValuePredicate(org.openremote.model.query.filter.NameValuePredicate) ManagerWebService(org.openremote.manager.web.ManagerWebService) Path(java.nio.file.Path) ManagerIdentityService(org.openremote.manager.security.ManagerIdentityService) Asset(org.openremote.model.asset.Asset) MetaItemType(org.openremote.model.value.MetaItemType) AssetQuery(org.openremote.model.query.AssetQuery) Pair(org.openremote.model.util.Pair) AssetDatapoint(org.openremote.model.datapoint.AssetDatapoint) AttributePredicate(org.openremote.model.query.filter.AttributePredicate) EntityManager(javax.persistence.EntityManager) Instant(java.time.Instant) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) File(java.io.File) ZoneId(java.time.ZoneId) Container(org.openremote.model.Container) Date(java.sql.Date) TimeUnit(java.util.concurrent.TimeUnit) DAYS(java.time.temporal.ChronoUnit.DAYS) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Paths(java.nio.file.Paths) TimerService(org.openremote.container.timer.TimerService) AssetUpdateProcessor(org.openremote.manager.asset.AssetUpdateProcessor) Source(org.openremote.model.attribute.AttributeEvent.Source) Attribute(org.openremote.model.attribute.Attribute) AssetQuery(org.openremote.model.query.AssetQuery) MapAccess.getString(org.openremote.container.util.MapAccess.getString) AttributePredicate(org.openremote.model.query.filter.AttributePredicate) AssetProcessingException(org.openremote.manager.asset.AssetProcessingException) MapAccess.getInteger(org.openremote.container.util.MapAccess.getInteger) Asset(org.openremote.model.asset.Asset) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) NameValuePredicate(org.openremote.model.query.filter.NameValuePredicate) Pair(org.openremote.model.util.Pair)

Aggregations

AssetDatapoint (org.openremote.model.datapoint.AssetDatapoint)2 File (java.io.File)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 Date (java.sql.Date)1 Duration (java.time.Duration)1 Instant (java.time.Instant)1 LocalDateTime (java.time.LocalDateTime)1 ZoneId (java.time.ZoneId)1 DAYS (java.time.temporal.ChronoUnit.DAYS)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Map (java.util.Map)1 ScheduledFuture (java.util.concurrent.ScheduledFuture)1 TimeUnit (java.util.concurrent.TimeUnit)1 Level (java.util.logging.Level)1 Logger (java.util.logging.Logger)1 Collectors (java.util.stream.Collectors)1 Collectors.groupingBy (java.util.stream.Collectors.groupingBy)1 Collectors.toList (java.util.stream.Collectors.toList)1