public async Task DeleteWithPredicate() { Client.Dispose(); WriteData(); string query = "from(bucket:\"" + _bucket.Name + "\") |> range(start: 1970-01-01T00:00:00.000000001Z) |> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")"; _queryApi = Client.GetQueryApi(); var tables = await _queryApi.QueryAsync(query, _organization.Id); Assert.AreEqual(2, tables.Count); Assert.AreEqual(1, tables[0].Records.Count); Assert.AreEqual(5, tables[1].Records.Count); var location = "east"; var field = "water_level"; var predicate = $"location=\"{location}\" AND _field=\"{field}\""; _deleteApi = Client.GetDeleteApi(); await _deleteApi.Delete(DateTime.Now.AddHours(-1), DateTime.Now, predicate, _bucket, _organization); var tablesAfterDelete = await _queryApi.QueryAsync(query, _organization.Id); Assert.AreEqual(1, tablesAfterDelete.Count); Assert.AreEqual(5, tablesAfterDelete[0].Records.Count); await _deleteApi.Delete(DateTime.Now.AddHours(-1), DateTime.Now, "location = \"west\"", _bucket, _organization); var tablesAfterDelete2 = await _queryApi.QueryAsync(query, _organization.Id); Assert.AreEqual(0, tablesAfterDelete2.Count); }
public async Task <PivotData> QueryAsync(string measurementName, QueryRange range) { try { PivotData result = new PivotData(); string flux = this.GetQueryString(measurementName, range); var fluxTables = await _queryApi.QueryAsync(flux, _org); var table = fluxTables.FirstOrDefault(); if (table != null) { string[] excludedColumns = { "result", "table", "_start", "_stop" }; foreach (var column in table.Columns) { if (!excludedColumns.Contains(column.Label)) { result.ColumnTypeInfo[column.Label] = column.DataType; } } foreach (var record in table.Records) { Dictionary <string, string> recordData = new Dictionary <string, string>(); foreach (var value in record.Values) { var keyName = value.Key.ToString(); if (!excludedColumns.Contains(keyName)) { recordData[keyName] = value.Value.ToString(); } } result.Records.Add(recordData); } } else { return(null); } return(result); } catch (Exception ex) { Console.WriteLine(ex.Message); return(null); } }
public async Task Delete() { Client.Dispose(); WriteData(); string query = "from(bucket:\"" + _bucket.Name + "\") |> range(start: 1970-01-01T00:00:00.000000001Z) |> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")"; _queryApi = Client.GetQueryApi(); var tables = await _queryApi.QueryAsync(query, _organization.Id); Assert.AreEqual(2, tables.Count); Assert.AreEqual(1, tables[0].Records.Count); Assert.AreEqual(5, tables[1].Records.Count); _deleteApi = Client.GetDeleteApi(); await _deleteApi.Delete(DateTime.Now.AddHours(-1), DateTime.Now, "", _bucket, _organization); var tablesAfterDelete1 = await _queryApi.QueryAsync(query, _organization.Id); Assert.AreEqual(0, tablesAfterDelete1.Count); }
public async Task DefaultTagsMeasurement() { Client.Dispose(); Environment.SetEnvironmentVariable("measurement-datacenter", "LA"); ConfigurationManager.AppSettings["measurement-sensor.version"] = "1.23a"; var options = new InfluxDBClientOptions.Builder().Url(InfluxDbUrl) .AuthenticateToken(_token.ToCharArray()) .AddDefaultTag("id", "132-987-655") .AddDefaultTag("customer", "California Miner") .AddDefaultTag("env-var", "${env.measurement-datacenter}") .AddDefaultTag("sensor-version", "${measurement-sensor.version}") .Build(); Client = InfluxDBClientFactory.Create(options); var measurement1 = new H20Measurement { Location = "coyote_creek", Level = 2.927, Time = DateTime.UtcNow }; _writeApi = Client.GetWriteApi(); var listener = new WriteApiTest.EventListener(_writeApi); _writeApi.WriteMeasurement(_bucket.Name, _organization.Id, WritePrecision.Ms, measurement1); _writeApi.Flush(); listener.WaitToSuccess(); _queryApi = Client.GetQueryApi(); var tables = await _queryApi.QueryAsync( "from(bucket:\"" + _bucket.Name + "\") |> range(start: 1970-01-01T00:00:00.000000001Z) |> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")", _organization.Id); Assert.AreEqual(1, tables.Count); Assert.AreEqual(1, tables[0].Records.Count); Assert.AreEqual("h2o", tables[0].Records[0].GetMeasurement()); Assert.AreEqual(2.927, tables[0].Records[0].GetValueByKey("level")); Assert.AreEqual("coyote_creek", tables[0].Records[0].GetValueByKey("location")); Assert.AreEqual("132-987-655", tables[0].Records[0].GetValueByKey("id")); Assert.AreEqual("California Miner", tables[0].Records[0].GetValueByKey("customer")); Assert.AreEqual("1.23a", tables[0].Records[0].GetValueByKey("sensor-version")); Assert.AreEqual("LA", tables[0].Records[0].GetValueByKey("env-var")); }
public async Task DefaultTagsPoint() { Client.Dispose(); Environment.SetEnvironmentVariable("point-datacenter", "LA"); ConfigurationManager.AppSettings["point-sensor.version"] = "1.23a"; var options = new InfluxDBClientOptions.Builder().Url(InfluxDbUrl) .AuthenticateToken(_token) .AddDefaultTag("id", "132-987-655") .AddDefaultTag("customer", "California Miner") .AddDefaultTag("env-var", "${env.point-datacenter}") .AddDefaultTag("sensor-version", "${point-sensor.version}") .Build(); Client = InfluxDBClientFactory.Create(options); var point = PointData.Measurement("h2o_feet").Tag("location", "west").Field("water_level", 1); _writeApi = Client.GetWriteApi(); var listener = new WriteApiTest.EventListener(_writeApi); _writeApi.WritePoint(_bucket.Name, _organization.Id, point); _writeApi.Flush(); listener.WaitToSuccess(); _queryApi = Client.GetQueryApi(); var tables = await _queryApi.QueryAsync( $"from(bucket:\"{_bucket.Name}\") |> range(start: 1970-01-01T00:00:00.000000001Z) |> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")", _organization.Id); Assert.AreEqual(1, tables.Count); Assert.AreEqual(1, tables[0].Records.Count); Assert.AreEqual("h2o_feet", tables[0].Records[0].GetMeasurement()); Assert.AreEqual(1, tables[0].Records[0].GetValueByKey("water_level")); Assert.AreEqual("west", tables[0].Records[0].GetValueByKey("location")); Assert.AreEqual("132-987-655", tables[0].Records[0].GetValueByKey("id")); Assert.AreEqual("California Miner", tables[0].Records[0].GetValueByKey("customer")); Assert.AreEqual("1.23a", tables[0].Records[0].GetValueByKey("sensor-version")); Assert.AreEqual("LA", tables[0].Records[0].GetValueByKey("env-var")); }
public async Task DefaultTagsConfiguration() { Client.Dispose(); var options = new InfluxDBClientOptions.Builder() .LoadConfig() .Url(InfluxDbUrl) .AuthenticateToken(_token) .Build(); Client = InfluxDBClientFactory.Create(options); var measurement1 = new H20Measurement { Location = "coyote_creek", Level = 2.927, Time = DateTime.UtcNow }; _writeApi = Client.GetWriteApi(); var listener = new WriteApiTest.EventListener(_writeApi); _writeApi.WriteMeasurement(_bucket.Name, _organization.Id, WritePrecision.Ms, measurement1); _writeApi.Flush(); listener.WaitToSuccess(); _queryApi = Client.GetQueryApi(); var tables = await _queryApi.QueryAsync( $"from(bucket:\"{_bucket.Name}\") |> range(start: 1970-01-01T00:00:00.000000001Z) |> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")", _organization.Id); Assert.AreEqual(1, tables.Count); Assert.AreEqual(1, tables[0].Records.Count); Assert.AreEqual("h2o", tables[0].Records[0].GetMeasurement()); Assert.AreEqual(2.927, tables[0].Records[0].GetValueByKey("level")); Assert.AreEqual("coyote_creek", tables[0].Records[0].GetValueByKey("location")); Assert.AreEqual("132-987-655", tables[0].Records[0].GetValueByKey("id")); Assert.AreEqual("California Miner", tables[0].Records[0].GetValueByKey("customer")); Assert.AreEqual("v1.00", tables[0].Records[0].GetValueByKey("version")); }
public async Task ParallelRequest() { MockServer .Given(Request.Create().WithPath("/api/v2/query").UsingPost()) .RespondWith(CreateResponse(Data)); var stopWatch = new Stopwatch(); stopWatch.Start(); var tasks = new List <Task <List <FluxTable> > >(); foreach (var _ in Enumerable.Range(0, 100)) { tasks.Add(_queryApi.QueryAsync("from(bucket:\"my-bucket\") |> range(start: 0)", "my-org")); } await Task.WhenAll(tasks); var ts = stopWatch.Elapsed; Assert.LessOrEqual(ts.TotalSeconds, 10, $"Elapsed time: {ts}"); }
public async Task EnabledGzip() { Client.EnableGzip(); var bucketName = _bucket.Name; const string record1 = "h2o_feet,location=coyote_creek level\\ water_level=1.0 1"; const string record2 = "h2o_feet,location=coyote_creek level\\ water_level=2.0 2"; _writeApi = Client.GetWriteApi(); var listener = new WriteApiTest.EventListener(_writeApi); _writeApi.WriteRecords(bucketName, _organization.Id, WritePrecision.Ns, new List <string> { record1, record2 }); _writeApi.Flush(); listener.WaitToSuccess(); var query = await _queryApi.QueryAsync( $"from(bucket:\"{bucketName}\") |> range(start: 1970-01-01T00:00:00.000000001Z)", _organization.Id); Assert.AreEqual(1, query.Count); var records = query[0].Records; Assert.AreEqual(2, records.Count); Assert.AreEqual("h2o_feet", records[0].GetMeasurement()); Assert.AreEqual(1, records[0].GetValue()); Assert.AreEqual("level water_level", records[0].GetField()); Assert.AreEqual("h2o_feet", records[1].GetMeasurement()); Assert.AreEqual(2, records[1].GetValue()); Assert.AreEqual("level water_level", records[1].GetField()); }
public async Task Delete() { Client.Dispose(); Environment.SetEnvironmentVariable("point-datacenter", "LA"); ConfigurationManager.AppSettings["point-sensor.version"] = "1.23a"; var options = new InfluxDBClientOptions.Builder().Url(InfluxDbUrl) .AuthenticateToken(_token.ToCharArray()) .AddDefaultTag("id", "132-987-655") .AddDefaultTag("customer", "California Miner") .AddDefaultTag("env-var", "${env.point-datacenter}") .AddDefaultTag("sensor-version", "${point-sensor.version}") .Build(); Client = InfluxDBClientFactory.Create(options); var point = PointData.Measurement("h2o_feet").Tag("location", "west").Field("water_level", 1); var point2 = PointData.Measurement("h2o_feet").Tag("location", "west").Field("water_level", 2); var point3 = PointData.Measurement("h2o_feet").Tag("location", "west").Field("water_level", 3); var point4 = PointData.Measurement("h2o_feet").Tag("location", "west").Field("water_level", 4); var point5 = PointData.Measurement("h2o_feet").Tag("location", "west").Field("water_level", 5); var point6 = PointData.Measurement("h2o_feet").Tag("location", "west").Field("water_level", 6); _writeApi = Client.GetWriteApi(); var listener = new WriteApiTest.EventListener(_writeApi); _writeApi.WritePoint(_bucket.Name, _organization.Id, point); _writeApi.Flush(); listener.WaitToSuccess(); _writeApi.WritePoint(_bucket.Name, _organization.Id, point2); _writeApi.Flush(); listener.WaitToSuccess(); _writeApi.WritePoint(_bucket.Name, _organization.Id, point3); _writeApi.Flush(); listener.WaitToSuccess(); _writeApi.WritePoint(_bucket.Name, _organization.Id, point4); _writeApi.Flush(); listener.WaitToSuccess(); _writeApi.WritePoint(_bucket.Name, _organization.Id, point5); _writeApi.Flush(); listener.WaitToSuccess(); _writeApi.WritePoint(_bucket.Name, _organization.Id, point6); _writeApi.Flush(); listener.WaitToSuccess(); string query = "from(bucket:\"" + _bucket.Name + "\") |> range(start: 1970-01-01T00:00:00.000000001Z) |> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")"; _queryApi = Client.GetQueryApi(); var tables = await _queryApi.QueryAsync(query, _organization.Id); Assert.AreEqual(1, tables.Count); Assert.AreEqual(6, tables[0].Records.Count); _deleteApi = Client.GetDeleteApi(); await _deleteApi.Delete(DateTime.Now.AddSeconds(-1), DateTime.Now, "", _bucket, _organization); var tablesAfterDelete = await _queryApi.QueryAsync(query, _organization.Id); Assert.AreNotEqual(0, tablesAfterDelete.Count); await _deleteApi.Delete(DateTime.Now.AddHours(-1), DateTime.Now, "", _bucket, _organization); var tablesAfterDelete2 = await _queryApi.QueryAsync(query, _organization.Id); Assert.AreEqual(0, tablesAfterDelete2.Count); }