15 changed files with 64 additions and 172 deletions
@ -1,79 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2024 The Thingsboard Authors |
|
||||
* |
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||
* you may not use this file except in compliance with the License. |
|
||||
* You may obtain a copy of the License at |
|
||||
* |
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||
* |
|
||||
* Unless required by applicable law or agreed to in writing, software |
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||
* See the License for the specific language governing permissions and |
|
||||
* limitations under the License. |
|
||||
*/ |
|
||||
package org.thingsboard.server.service.cf.ctx.state; |
|
||||
|
|
||||
import com.google.common.util.concurrent.Futures; |
|
||||
import com.google.common.util.concurrent.ListenableFuture; |
|
||||
import com.google.common.util.concurrent.MoreExecutors; |
|
||||
import lombok.Data; |
|
||||
import org.thingsboard.server.common.data.cf.CalculatedFieldType; |
|
||||
import org.thingsboard.server.common.data.cf.configuration.Argument; |
|
||||
import org.thingsboard.server.common.data.cf.configuration.Output; |
|
||||
import org.thingsboard.server.service.cf.CalculatedFieldResult; |
|
||||
|
|
||||
import java.util.Map; |
|
||||
import java.util.TreeMap; |
|
||||
|
|
||||
@Data |
|
||||
public class LastRecordsCalculatedFieldState extends BaseCalculatedFieldState { |
|
||||
|
|
||||
public LastRecordsCalculatedFieldState() { |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public CalculatedFieldType getType() { |
|
||||
return CalculatedFieldType.LAST_RECORDS; |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public void initState(Map<String, ArgumentEntry> argumentValues) { |
|
||||
if (arguments == null) { |
|
||||
arguments = new TreeMap<>(); |
|
||||
} |
|
||||
argumentValues.forEach((key, argumentEntry) -> { |
|
||||
LastRecordsArgumentEntry existingArgumentEntry = (LastRecordsArgumentEntry) |
|
||||
arguments.computeIfAbsent(key, k -> new LastRecordsArgumentEntry(new TreeMap<>())); |
|
||||
if (argumentEntry instanceof LastRecordsArgumentEntry lastRecordsArgumentEntry) { |
|
||||
existingArgumentEntry.getTsRecords().putAll(lastRecordsArgumentEntry.getTsRecords()); |
|
||||
} else if (argumentEntry instanceof SingleValueArgumentEntry singleValueArgumentEntry) { |
|
||||
existingArgumentEntry.getTsRecords().put(singleValueArgumentEntry.getTs(), singleValueArgumentEntry.getValue()); |
|
||||
} |
|
||||
}); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public ListenableFuture<CalculatedFieldResult> performCalculation(CalculatedFieldCtx ctx) { |
|
||||
if (isValid(ctx.getArguments())) { |
|
||||
arguments.forEach((key, argumentEntry) -> { |
|
||||
Argument argument = ctx.getArguments().get(key); |
|
||||
TreeMap<Long, Object> tsRecords = ((LastRecordsArgumentEntry) argumentEntry).getTsRecords(); |
|
||||
if (tsRecords.size() > argument.getLimit()) { |
|
||||
tsRecords.pollFirstEntry(); |
|
||||
} |
|
||||
tsRecords.entrySet().removeIf(tsRecord -> tsRecord.getKey() < System.currentTimeMillis() - argument.getTimeWindow()); |
|
||||
}); |
|
||||
Object[] args = arguments.values().stream().map(ArgumentEntry::getValue).toArray(); |
|
||||
ListenableFuture<Map<String, Object>> resultFuture = ctx.getCalculatedFieldScriptEngine().executeToMapAsync(args); |
|
||||
Output output = ctx.getOutput(); |
|
||||
return Futures.transform(resultFuture, |
|
||||
result -> new CalculatedFieldResult(output.getType(), output.getScope(), result), |
|
||||
MoreExecutors.directExecutor() |
|
||||
); |
|
||||
} |
|
||||
return null; |
|
||||
} |
|
||||
|
|
||||
} |
|
||||
@ -1,39 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2024 The Thingsboard Authors |
|
||||
* |
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||
* you may not use this file except in compliance with the License. |
|
||||
* You may obtain a copy of the License at |
|
||||
* |
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||
* |
|
||||
* Unless required by applicable law or agreed to in writing, software |
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||
* See the License for the specific language governing permissions and |
|
||||
* limitations under the License. |
|
||||
*/ |
|
||||
package org.thingsboard.server.common.data.cf.configuration; |
|
||||
|
|
||||
import com.fasterxml.jackson.databind.JsonNode; |
|
||||
import lombok.Data; |
|
||||
import org.thingsboard.server.common.data.EntityType; |
|
||||
import org.thingsboard.server.common.data.cf.CalculatedFieldType; |
|
||||
|
|
||||
import java.util.UUID; |
|
||||
|
|
||||
@Data |
|
||||
public class LastRecordsCalculatedFieldConfiguration extends BaseCalculatedFieldConfiguration implements CalculatedFieldConfiguration { |
|
||||
|
|
||||
public LastRecordsCalculatedFieldConfiguration() { |
|
||||
} |
|
||||
|
|
||||
public LastRecordsCalculatedFieldConfiguration(JsonNode config, EntityType entityType, UUID entityId) { |
|
||||
super(config, entityType, entityId); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public CalculatedFieldType getType() { |
|
||||
return CalculatedFieldType.LAST_RECORDS; |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue