-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.dart
77 lines (68 loc) · 2.35 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import 'dart:async';
import 'dart:math';
import 'package:influxdb_client/api.dart';
void main() async {
// Create InfluxDBClient
var client = InfluxDBClient(
url: 'http://localhost:8086',
token: 'my-token',
org: 'my-org',
bucket: 'my-bucket',
debug: true);
// Create write service
var writeApi = client.getWriteService(WriteOptions().merge(
precision: WritePrecision.s,
batchSize: 100,
flushInterval: 5000,
gzip: true));
// Create data in list of point structure
var data = List<Point>.empty(growable: true);
var random = Random();
for (var i = 0; i < 10; i++) {
var temperature = random.nextInt(30);
data.add(Point('weather')
.addTag('location', 'Prague')
.addField('temperature', temperature)
.time(DateTime.now().subtract(Duration(days: i)).toUtc()));
}
// Write data to InfluxDB
print(
'\n\n-------------------------------- Write data -------------------------------\n');
await writeApi.write(data).then((value) {
print('Write completed 1');
}).catchError((exception) {
print('Handle write error here!');
print(exception);
});
// Create Query service and query
var queryService = client.getQueryService();
var fluxQuery = '''
from(bucket: "my-bucket")
|> range(start: -20d)
|> filter(fn: (r) => r["_measurement"] == "weather"
and r["location"] == "Prague")''';
// query to stream and iterate all records
print(
'\n\n---------------------------------- Query ---------------------------------\n');
var recordStream = await queryService.query(fluxQuery);
print(
'\n\n------------------------------ Query result ------------------------------\n');
await recordStream.forEach((record) {
print(
'Temperature in ${record['location']} at ${record['_time']} is ${record['_value']} °C');
});
// delete data
print(
'\n\n------------------------------- Delete data -------------------------------\n');
await client
.getDeleteService()
.delete(
predicate: '_measurement="weather"',
start: DateTime.parse('1970-01-01T00:00:00Z'),
stop: DateTime.now().toUtc(),
bucket: 'my-bucket',
org: 'my-org')
.catchError((e) => print(e));
await Future.delayed(Duration(seconds: 10));
client.close();
}