ソースを参照

Merge pull request #213 from nickbabcock/inf-revert

Revert accidental influx experiment
Nick Babcock 4 年 前
コミット
ff8780fcd5
2 ファイル変更9 行追加74 行削除
  1. 0 33
      OhmGraphite.Test/InfluxTest.cs
  2. 9 41
      OhmGraphite/InfluxWriter.cs

+ 0 - 33
OhmGraphite.Test/InfluxTest.cs

@@ -93,38 +93,5 @@ namespace OhmGraphite.Test
                 }
             }
         }
-
-        //[Fact, Trait("Category", "integration")]
-        //public async void CanInsertIntoInflux2()
-        //{
-        //    var config = new InfluxConfig(new Uri("http://influx2:8086"), "mydb", "my_user", "my_pass");
-        //    using (var writer = new InfluxWriter(config, "my-pc"))
-        //    using (var client = new HttpClient())
-        //    {
-        //        for (int attempts = 0; ; attempts++)
-        //        {
-        //            try
-        //            {
-        //                await writer.ReportMetrics(DateTime.Now, TestSensorCreator.Values());
-
-        //                var resp = await client.GetAsync(
-        //                    "http://influx2:8086/query?pretty=true&db=mydb&q=SELECT%20*%20FROM%20Temperature");
-        //                Assert.True(resp.IsSuccessStatusCode);
-        //                var content = await resp.Content.ReadAsStringAsync();
-        //                Assert.Contains("/intelcpu/0/temperature/0", content);
-        //                break;
-        //            }
-        //            catch (Exception)
-        //            {
-        //                if (attempts >= 10)
-        //                {
-        //                    throw;
-        //                }
-
-        //                Thread.Sleep(TimeSpan.FromSeconds(1));
-        //            }
-        //        }
-        //    }
-        //}
     }
 }

+ 9 - 41
OhmGraphite/InfluxWriter.cs

@@ -1,20 +1,18 @@
 using System;
 using System.Collections.Generic;
-using System.IO;
 using System.Linq;
-using System.Net.Http;
-using System.Net.Http.Headers;
-using System.Text;
 using System.Threading.Tasks;
+using InfluxDB.LineProtocol.Client;
 using InfluxDB.LineProtocol.Payload;
 using NLog;
+using LibreHardwareMonitor.Hardware;
 
 namespace OhmGraphite
 {
     public class InfluxWriter : IWriteMetrics
     {
         private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
-        private readonly HttpClient _client = new HttpClient();
+
         private readonly InfluxConfig _config;
         private readonly string _localHost;
 
@@ -22,53 +20,23 @@ namespace OhmGraphite
         {
             _config = config;
             _localHost = localHost;
-
-            if (!string.IsNullOrEmpty(_config.User) && !string.IsNullOrEmpty(_config.Password))
-            {
-                var raw = Encoding.UTF8.GetBytes($"{_config.User}:{_config.Password}");
-                var encoded = Convert.ToBase64String(raw);
-                _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", encoded);
-            }
         }
 
         public async Task ReportMetrics(DateTime reportTime, IEnumerable<ReportedValue> sensors)
         {
             var payload = new LineProtocolPayload();
+            var password = _config.User != null ? (_config.Password ?? "") : null;
+            var client = new LineProtocolClient(_config.Address, _config.Db, _config.User, password);
+
             foreach (var point in sensors.Select(x => NewPoint(reportTime, x)))
             {
                 payload.Add(point);
             }
 
-            // can't use influx db client as they don't have one that is both 1.x and 2.x compatible
-            // so we implement our own compatible client
-            var formattedData = new StringWriter();
-            payload.Format(formattedData);
-            formattedData.Flush();
-            var outData = Encoding.UTF8.GetBytes(formattedData.ToString());
-            var content = new ByteArrayContent(outData);
-
-            var queries = new List<string>();
-
-            // passwordless authentication
-            if (!string.IsNullOrEmpty(_config.User) && string.IsNullOrEmpty(_config.Password))
-            {
-                queries.Add($"u={_config.User}&p=");
-            }
-
-            if (!string.IsNullOrEmpty(_config.Db))
-            {
-                queries.Add($"db={_config.Db}");
-            }
-
-            var qs = string.Join("&", queries);
-            qs = !string.IsNullOrEmpty(qs) ? $"?{qs}" : "";
-            var addrPath = new Uri(_config.Address, "/write");
-            var url = $"{addrPath}{qs}";
-            var response = await _client.PostAsync(url, content);
-            if (!response.IsSuccessStatusCode)
+            var result = await client.WriteAsync(payload);
+            if (!result.Success)
             {
-                var err = await response.Content.ReadAsStringAsync();
-                Logger.Error("Influxdb encountered an error: {0}", err);
+                Logger.Error("Influxdb encountered an error: {0}", result.ErrorMessage);
             }
         }