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;
}
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");
}
Aggregations