]> git.basschouten.com Git - openhab-addons.git/blob
415ae5616900e81ceebd48a9c4b8f7ef1bc5d446
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.dbquery.internal.dbimpl.influx2;
14
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18 import java.util.function.Function;
19 import java.util.stream.Collectors;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.dbquery.internal.domain.QueryResult;
24 import org.openhab.binding.dbquery.internal.domain.ResultRow;
25
26 import com.influxdb.query.FluxRecord;
27
28 /**
29  * Extracts results from Influx2 client query result to a {@link QueryResult}
30  *
31  * @author Joan Pujol - Initial contribution
32  */
33 @NonNullByDefault
34 public class Influx2QueryResultExtractor implements Function<List<FluxRecord>, QueryResult> {
35
36     @Override
37     public QueryResult apply(List<FluxRecord> records) {
38         var rows = records.stream().map(Influx2QueryResultExtractor::mapRecord2Row).collect(Collectors.toList());
39         return QueryResult.of(rows);
40     }
41
42     private static ResultRow mapRecord2Row(FluxRecord record) {
43         Map<String, @Nullable Object> values = record.getValues().entrySet().stream()
44                 .filter(entry -> !Set.of("result", "table").contains(entry.getKey()))
45                 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
46
47         return new ResultRow(values);
48     }
49 }