public async System.Threading.Tasks.Task <string> WriteAsync(string database, InfluxDB.Net.Models.Point point, StatusCallBack statusCallBack) { InfluxDbApiResponse writeResponse = await _client.WriteAsync(database, point); statusCallBack?.Invoke(writeResponse.Success); return(writeResponse.Success ? "入库成功" : "入库失败"); }
public async void Query_DB_Test() { string dbName = GetNewDbName(); InfluxDbApiCreateResponse createResponse = await _db.CreateDatabaseAsync(dbName); const string TmpSerieName = "testSeries"; Serie serie = new Serie.Builder(TmpSerieName) .Columns("value1", "value2") .Values(DateTime.Now.Millisecond, 5) .Build(); InfluxDbApiResponse writeResponse = await _db.WriteAsync(dbName, TimeUnit.Milliseconds, serie); List <Serie> series = await _db.QueryAsync(dbName, string.Format("select * from {0}", TmpSerieName), TimeUnit.Milliseconds); InfluxDbApiDeleteResponse deleteResponse = await _db.DeleteDatabaseAsync(dbName); series.Should().NotBeNull(); series.Count.Should().Be(1); createResponse.Success.Should().BeTrue(); writeResponse.Success.Should().BeTrue(); deleteResponse.Success.Should().BeTrue(); }
private static async void SendTimerElapsed(object sender, ElapsedEventArgs e) { var pts = new List <Point>(); InfluxDbApiResponse result = null; bool createdNew; using (var mutex = new Mutex(false, MutexId, out createdNew, _securitySettings)) { mutex.WaitOne(); while (_queue.Count > 0) { var points = _queue.Dequeue(); pts.AddRange(points); } mutex.ReleaseMutex(); } if (pts.Count == 0) { return; } try { result = await _agent.WriteAsync(pts.ToArray()); } catch (Exception exception) { _queue.Enqueue(pts.ToArray()); Logger.Error(exception); } }
/// <summary> /// /// </summary> /// <returns></returns> public InfluxDbApiResponse Write() { Serie serie = new Serie.Builder(seriesName) .Columns(columns) .Values(values) .Build(); InfluxDbApiResponse writeResponse = dbConnection.WriteAsync(dbName, TimeUnit.Milliseconds, serie).Result; return(writeResponse); }
// Edit the selected user async Task EditUser() { try { var user = SelectedUser; if (user == null) { return; } editUserDialog.BindToUser(user); if (editUserDialog.ShowDialog() == DialogResult.OK) { // If there was no change in user status, there's nothing to do var updatedUser = editUserDialog.CreateUserFromDialog(); if (user.IsAdmin == updatedUser.IsAdmin) { return; } InfluxDbApiResponse response = null; // Otherwise carry out the appropriate API call based on adding or removing admin privileges if (updatedUser.IsAdmin) { response = await InfluxDbClient.GrantAdministratorAsync(updatedUser.Name); } else { response = await InfluxDbClient.RevokeAdministratorAsync(updatedUser.Name); } // Refresh the window if (response.Success) { await ExecuteRequestAsync(); } else { AppForm.DisplayError(response.Body); } // Update interface buttons UpdateUIState(); } } catch (Exception ex) { AppForm.DisplayException(ex); } }
private static async void SendTimerElapsed(object sender, ElapsedEventArgs e) { //_logger.Debug("SendTimerElapsed."); var pts = new List <Point>(); InfluxDbApiResponse result = null; bool createdNew; using (var mutex = new Mutex(false, MutexId, out createdNew, _securitySettings)) { mutex.WaitOne(); while (_queue.Count > 0) { var points = _queue.Dequeue(); pts.AddRange(points); } mutex.ReleaseMutex(); } if (pts.Count == 0) { //_logger.Debug("Nothing to send."); return; } try { _logger.Debug(string.Format("Sending {0} measurements.", pts.Count + 1)); var data = new StringBuilder(); foreach (var item in pts) { data.AppendLine(_formatter.PointToString(item)); } _logger.Debug(data.ToString()); result = await _agent.WriteAsync(pts.ToArray()); _logger.Info(result.StatusCode + ": " + result.Body); } catch (Exception exception) { _logger.Error(exception); //TODO: Only re-enqueue points in certain situations //_queue.Enqueue(pts.ToArray()); } }
public async void Write_DB_Test() { string dbName = GetNewDbName(); InfluxDbApiCreateResponse createResponse = await _db.CreateDatabaseAsync(dbName); Serie serie = new Serie.Builder("testSeries") .Columns("value1", "value2") .Values(DateTime.Now.Millisecond, 5) .Build(); InfluxDbApiResponse writeResponse = await _db.WriteAsync(dbName, TimeUnit.Milliseconds, serie); InfluxDbApiDeleteResponse deleteResponse = await _db.DeleteDatabaseAsync(dbName); createResponse.Success.Should().BeTrue(); writeResponse.Success.Should().BeTrue(); deleteResponse.Success.Should().BeTrue(); }
public async void SaveMetricsToInflux(string databaseName, string measurementName, IInfluxMetrics metrics) { await Task.Run(async() => { InfluxDb client = new InfluxDb(InfluxDbUrl, UserName, Password); Point point = new Point(); Dictionary <string, object> dict = new Dictionary <string, object>(); foreach (var measurement in metrics.GetMeasurements()) { dict.Add(measurement.Key, measurement.Value); } point.Measurement = measurementName; point.Fields = dict; InfluxDbApiResponse writeResponse = await client.WriteAsync(databaseName, point); }); }
public async void Write(string database, InfluxDB.Net.Models.Point point, StatusCallBack statusCallBack) { //基于InfluxData.Net.InfluxDb.Models.Point实体准备数据 //var point = new Point() //{ // Measurement = "logs",//表名 // Tags = new Dictionary<string, object>() // { // { "Id", 158} // }, // Fields = new Dictionary<string, object>() // { // { "Val", "webInfo" } // }, // Timestamp = DateTime.UtcNow //}; InfluxDbApiResponse writeResponse = await _client.WriteAsync(database, point); statusCallBack?.Invoke(writeResponse.Success); }
protected async Task <IDatabaseConfig> GetUsernameAsync(string paramList, int index, IDatabaseConfig config, string action) { var dataChanged = false; var url = config.Url; IInfluxDbAgent client = null; InfluxDbApiResponse response = null; try { if (!string.IsNullOrEmpty(config.Name) && !string.IsNullOrEmpty(config.Username) && !string.IsNullOrEmpty(config.Password)) { client = _influxDbAgentLoader.GetAgent(config); response = await MetaDataBusiness.TestWriteAccess(client, action); } } catch (Exception exception) { OutputError(exception.Message); } if (response == null || !response.Success) { OutputInformation("Enter the database, username and password for the InfluxDB."); } while (response == null || !response.Success) { var database = string.Empty; try { database = QueryParam <string>("DatabaseName", GetParam(paramList, index++)); var user = QueryParam <string>("Username", GetParam(paramList, index++)); var password = QueryParam <string>("Password", GetParam(paramList, index++)); config = new InfluxDatabaseConfig(url, user, password, database); client = _influxDbAgentLoader.GetAgent(config); response = await MetaDataBusiness.TestWriteAccess(client, action); dataChanged = true; } catch (CommandEscapeException) { return(null); } catch (InfluxDbApiException exception) { if (exception.StatusCode == HttpStatusCode.NotFound) { var create = QueryParam("Database does not exist, create?", GetParam(paramList, index++), new Dictionary <bool, string>() { { true, "Yes" }, { false, "No" } }); if (create) { client.CreateDatabaseAsync(database); response = MetaDataBusiness.TestWriteAccess(client, action).Result; dataChanged = true; } } } catch (Exception exception) { OutputError("{0}", exception.Message); } } OutputInformation("Access to database {0} confirmed.", config.Name); if (dataChanged) { _configBusiness.SaveDatabaseConfig(config.Name, config.Username, config.Password); } return(config); }
public async void DeleteDatabase(string database, StatusCallBack statusCallBack) { InfluxDbApiResponse deleteResponse = await _client.DropDatabaseAsync(database); statusCallBack?.Invoke(deleteResponse.Success); }
public async void CreateDatabase(string database, StatusCallBack statusCallBack) { InfluxDbApiResponse response = await _client.CreateDatabaseAsync(database); statusCallBack?.Invoke(response.Success); }
// Edit privilege async Task EditPrivilege() { try { var user = SelectedUser; if (user == null) { return; } var grant = SelectedPrivilegeGrant; if (grant == null) { return; } // Bind to data editPrivilegeDialog.BindToGrant(user.Name, grant); if (editPrivilegeDialog.ShowDialog() == DialogResult.OK) { // Get the selected database and privilege to grant var username = editPrivilegeDialog.Username; var database = editPrivilegeDialog.Database; var privilege = editPrivilegeDialog.SelectedPrivilege; // Ensure a database was selected if (string.IsNullOrWhiteSpace(database)) { AppForm.DisplayError("You must supply a database to update a privilege for."); return; } InfluxDbApiResponse response = null; // If "None" was selected then we actually need to revoke the existing privilege if (privilege == InfluxDbPrivileges.None) { response = await InfluxDbClient.RevokePrivilegeAsync(username, grant.Privilege, database); } // Otherwise just grant the new privilege else { response = await InfluxDbClient.GrantPrivilegeAsync(username, privilege, database); } // Refresh the window if (response.Success) { await ExecuteRequestAsync(); } else { AppForm.DisplayError(response.Body); } // Update interface buttons UpdateUIState(); } } catch (Exception ex) { AppForm.DisplayException(ex); } }
public static T ReadAs <T>(this InfluxDbApiResponse response) { var @object = JsonConvert.DeserializeObject <T>(response.Body); return(@object); }