Showing
1 changed file
with
17 additions
and
14 deletions
1 | 1 | /** |
2 | 2 | * Copyright © 2016-2018 The Thingsboard Authors |
3 | - * | |
3 | + * <p> | |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
6 | 6 | * You may obtain a copy of the License at |
7 | - * | |
8 | - * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | - * | |
7 | + * <p> | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * <p> | |
10 | 10 | * Unless required by applicable law or agreed to in writing, software |
11 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
... | ... | @@ -26,10 +26,8 @@ import org.thingsboard.rule.engine.api.*; |
26 | 26 | import org.thingsboard.rule.engine.api.util.DonAsynchron; |
27 | 27 | import org.thingsboard.rule.engine.api.util.TbNodeUtils; |
28 | 28 | import org.thingsboard.server.common.data.kv.BaseReadTsKvQuery; |
29 | -import org.thingsboard.server.common.data.kv.BaseTsKvQuery; | |
30 | 29 | import org.thingsboard.server.common.data.kv.ReadTsKvQuery; |
31 | 30 | import org.thingsboard.server.common.data.kv.TsKvEntry; |
32 | -import org.thingsboard.server.common.data.kv.TsKvQuery; | |
33 | 31 | import org.thingsboard.server.common.data.plugin.ComponentType; |
34 | 32 | import org.thingsboard.server.common.msg.TbMsg; |
35 | 33 | |
... | ... | @@ -39,8 +37,7 @@ import java.util.concurrent.TimeUnit; |
39 | 37 | import java.util.stream.Collectors; |
40 | 38 | |
41 | 39 | import static org.thingsboard.rule.engine.api.TbRelationTypes.SUCCESS; |
42 | -import static org.thingsboard.rule.engine.metadata.TbGetTelemetryNodeConfiguration.FETCH_MODE_ALL; | |
43 | -import static org.thingsboard.rule.engine.metadata.TbGetTelemetryNodeConfiguration.MAX_FETCH_SIZE; | |
40 | +import static org.thingsboard.rule.engine.metadata.TbGetTelemetryNodeConfiguration.*; | |
44 | 41 | import static org.thingsboard.server.common.data.kv.Aggregation.NONE; |
45 | 42 | |
46 | 43 | /** |
... | ... | @@ -64,6 +61,7 @@ public class TbGetTelemetryNode implements TbNode { |
64 | 61 | private long endTsOffset; |
65 | 62 | private int limit; |
66 | 63 | private ObjectMapper mapper; |
64 | + private String fetchMode; | |
67 | 65 | |
68 | 66 | @Override |
69 | 67 | public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException { |
... | ... | @@ -72,6 +70,7 @@ public class TbGetTelemetryNode implements TbNode { |
72 | 70 | startTsOffset = TimeUnit.valueOf(config.getStartIntervalTimeUnit()).toMillis(config.getStartInterval()); |
73 | 71 | endTsOffset = TimeUnit.valueOf(config.getEndIntervalTimeUnit()).toMillis(config.getEndInterval()); |
74 | 72 | limit = config.getFetchMode().equals(FETCH_MODE_ALL) ? MAX_FETCH_SIZE : 1; |
73 | + fetchMode = config.getFetchMode(); | |
75 | 74 | mapper = new ObjectMapper(); |
76 | 75 | mapper.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, false); |
77 | 76 | mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true); |
... | ... | @@ -96,14 +95,18 @@ public class TbGetTelemetryNode implements TbNode { |
96 | 95 | } |
97 | 96 | } |
98 | 97 | |
99 | - //TODO: handle direction; | |
100 | 98 | private List<ReadTsKvQuery> buildQueries() { |
101 | 99 | long ts = System.currentTimeMillis(); |
102 | 100 | long startTs = ts - startTsOffset; |
103 | 101 | long endTs = ts - endTsOffset; |
104 | - | |
102 | + String orderBy; | |
103 | + if (fetchMode.equals(FETCH_MODE_FIRST) || fetchMode.equals(FETCH_MODE_ALL)) { | |
104 | + orderBy = "ASC"; | |
105 | + } else { | |
106 | + orderBy = "DESC"; | |
107 | + } | |
105 | 108 | return tsKeyNames.stream() |
106 | - .map(key -> new BaseReadTsKvQuery(key, startTs, endTs, 1, limit, NONE)) | |
109 | + .map(key -> new BaseReadTsKvQuery(key, startTs, endTs, 1, limit, NONE, orderBy)) | |
107 | 110 | .collect(Collectors.toList()); |
108 | 111 | } |
109 | 112 | |
... | ... | @@ -116,7 +119,7 @@ public class TbGetTelemetryNode implements TbNode { |
116 | 119 | } |
117 | 120 | |
118 | 121 | for (String key : tsKeyNames) { |
119 | - if(resultNode.has(key)){ | |
122 | + if (resultNode.has(key)) { | |
120 | 123 | msg.getMetaData().putValue(key, resultNode.get(key).toString()); |
121 | 124 | } |
122 | 125 | } |
... | ... | @@ -127,11 +130,11 @@ public class TbGetTelemetryNode implements TbNode { |
127 | 130 | } |
128 | 131 | |
129 | 132 | private void processArray(ObjectNode node, TsKvEntry entry) { |
130 | - if(node.has(entry.getKey())){ | |
133 | + if (node.has(entry.getKey())) { | |
131 | 134 | ArrayNode arrayNode = (ArrayNode) node.get(entry.getKey()); |
132 | 135 | ObjectNode obj = buildNode(entry); |
133 | 136 | arrayNode.add(obj); |
134 | - }else { | |
137 | + } else { | |
135 | 138 | ArrayNode arrayNode = mapper.createArrayNode(); |
136 | 139 | ObjectNode obj = buildNode(entry); |
137 | 140 | arrayNode.add(obj); | ... | ... |