diff --git a/src/KpiGenerator/Generators/GenerateProjectsPerIkz.cs b/src/KpiGenerator/Generators/GenerateProjectsPerIkz.cs index c50e3a8d5529f3b01afeff3bfb738e2b9850148e..8326aaa7a30da2e89d0438bd4f4f96c2ccf8f7c0 100644 --- a/src/KpiGenerator/Generators/GenerateProjectsPerIkz.cs +++ b/src/KpiGenerator/Generators/GenerateProjectsPerIkz.cs @@ -1,17 +1,12 @@ using Coscine.Configuration; using Coscine.Database.DataModel; using Coscine.Database.Models; -using Coscine.Database.Util; using Coscine.Metadata; -using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Coscine.KpiGenerator.Generators { - class GenerateProjectsPerIkz : KpiGenerator + public class GenerateProjectsPerIkz : KpiGenerator { public GenerateProjectsPerIkz(IConfiguration configuration, string measurementID) : base(configuration, measurementID) { diff --git a/src/KpiGenerator/Generators/GenerateRDSBucketsPerIkz.cs b/src/KpiGenerator/Generators/GenerateRDSBucketsPerIkz.cs index 4bfa0bc3c554d329d174d5cda1dd39cac078192d..2232310e5d8dbea5eba4dfbc63dd068746508f89 100644 --- a/src/KpiGenerator/Generators/GenerateRDSBucketsPerIkz.cs +++ b/src/KpiGenerator/Generators/GenerateRDSBucketsPerIkz.cs @@ -1,17 +1,12 @@ using Coscine.Configuration; using Coscine.Database.DataModel; using Coscine.Database.Models; -using Coscine.Database.Util; using Coscine.Metadata; -using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Coscine.KpiGenerator.Generators { - class GenerateRDSBucketsPerIkz : KpiGenerator + public class GenerateRDSBucketsPerIkz : KpiGenerator { public GenerateRDSBucketsPerIkz(IConfiguration configuration, string measurementID) : base(configuration, measurementID) { diff --git a/src/KpiGenerator/Generators/GenerateRDSQuotasPerIkz.cs b/src/KpiGenerator/Generators/GenerateRDSQuotasPerIkz.cs index 32ad835289e2db27ed4e488379eaf56b9f71cfde..d1dc5c2949cde9ecd897b4c9c40d3b7c9fdfaf9a 100644 --- a/src/KpiGenerator/Generators/GenerateRDSQuotasPerIkz.cs +++ b/src/KpiGenerator/Generators/GenerateRDSQuotasPerIkz.cs @@ -1,19 +1,12 @@ -using Amazon.S3; -using Amazon.S3.Model; -using Coscine.Configuration; +using Coscine.Configuration; using Coscine.Database.DataModel; using Coscine.Database.Models; -using Coscine.Database.Util; using Coscine.Metadata; -using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Coscine.KpiGenerator.Generators { - class GenerateRDSQuotasPerIkz : KpiGenerator + public class GenerateRDSQuotasPerIkz : KpiGenerator { public GenerateRDSQuotasPerIkz(IConfiguration configuration, string measurementID) : base(configuration, measurementID) { diff --git a/src/KpiGenerator/Generators/GenerateUsersPerIkz.cs b/src/KpiGenerator/Generators/GenerateUsersPerIkz.cs index 166530efc1e5869859ea7d3cdb033c25dc4f9821..1268b39c9dda378792fe7816ebfd173b5659f7b5 100644 --- a/src/KpiGenerator/Generators/GenerateUsersPerIkz.cs +++ b/src/KpiGenerator/Generators/GenerateUsersPerIkz.cs @@ -1,17 +1,14 @@ using Coscine.Configuration; using Coscine.Database.DataModel; using Coscine.Database.Models; -using Coscine.Database.Util; using Coscine.Metadata; using System; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Coscine.KpiGenerator.Generators { - class GenerateUsersPerIkz : KpiGenerator + public class GenerateUsersPerIkz : KpiGenerator { public GenerateUsersPerIkz(IConfiguration configuration, string measurementID) : base(configuration, measurementID) { diff --git a/src/KpiGenerator/KpiGenerator.cs b/src/KpiGenerator/KpiGenerator.cs index 57e70019a85744b70cebdb0eb6abd166a10f1777..eb5d665a9e186a61c0fb34f8584645578a82f473 100644 --- a/src/KpiGenerator/KpiGenerator.cs +++ b/src/KpiGenerator/KpiGenerator.cs @@ -2,13 +2,10 @@ using Coscine.Database.DataModel; using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Coscine.KpiGenerator { - abstract class KpiGenerator + public abstract class KpiGenerator { protected IConfiguration _configuration; protected string _measurementID; diff --git a/src/KpiGenerator/KpiGenerator.csproj b/src/KpiGenerator/KpiGenerator.csproj index a7cc124d857c8e27856bd5e62826237c342162be..032447d83e466c280dff7bc57077d7dd4a9c2921 100644 --- a/src/KpiGenerator/KpiGenerator.csproj +++ b/src/KpiGenerator/KpiGenerator.csproj @@ -114,6 +114,7 @@ <Compile Include="Program.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Reporter\HamsterReporter.cs" /> + <Compile Include="RestClient.cs" /> </ItemGroup> <ItemGroup> <None Include="App.config" /> diff --git a/src/KpiGenerator/Program.cs b/src/KpiGenerator/Program.cs index b68d52ff3d622762b71cd239ffa59872dd2b5c15..f349468243e54223f9ac0b8d2c774b84b5e36c6c 100644 --- a/src/KpiGenerator/Program.cs +++ b/src/KpiGenerator/Program.cs @@ -2,22 +2,17 @@ using Coscine.Database.Settings; using Coscine.Database.Models; using LinqToDB.Data; -using System; using System.Collections.Generic; -using VDS.RDF.Storage; using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Coscine.Metadata; using Coscine.KpiGenerator.Reporter; using Coscine.KpiGenerator.Generators; using Coscine.Database.DataModel; namespace Coscine.KpiGenerator { - class Program + public class Program { - static void Main(string[] args) + public static void Main(string[] args) { ConsulConfiguration configuration = new ConsulConfiguration(); DataConnection.DefaultSettings = new CoscineSettings(configuration); @@ -32,7 +27,7 @@ namespace Coscine.KpiGenerator newKpis.AddRange(new GenerateRDSQuotasPerIkz(configuration, "2973").GenerateKpis()); newKpis.AddRange(new GenerateUsersPerIkz(configuration, "2967").GenerateKpis()); - //when all generators are done + // when all generators are done var kpiModel = new KpiModel(); // each value is stored within the database foreach (Kpi kpi in newKpis) @@ -43,18 +38,12 @@ namespace Coscine.KpiGenerator // get all values from the database that have not been successfully sent yet var kpiToProcess = kpiModel.GetAllWhere((kpi) => (kpi.SentSuccessfully == false)).ToList(); - // try to sent them as a bundle with the csv method - HamsterReporter hamsterReporter = new HamsterReporter(configuration); + // try to sent them + HamsterReporter hamsterReporter = new HamsterReporter(configuration); if (liveMode == "1") { hamsterReporter.Send(kpiToProcess); } - - // if the kpi were sent successful mark all values within the database as successfully sent - foreach (Kpi kpi in kpiToProcess) - { - kpiModel.MarkAsSent(kpi); - } } } } diff --git a/src/KpiGenerator/Reporter/HamsterReporter.cs b/src/KpiGenerator/Reporter/HamsterReporter.cs index dd87b32f06433847613e5be1fcb5a5aacd365914..7a50e0cf4da12d3d08603ecadf865dfd40349d7f 100644 --- a/src/KpiGenerator/Reporter/HamsterReporter.cs +++ b/src/KpiGenerator/Reporter/HamsterReporter.cs @@ -1,20 +1,15 @@ using Coscine.Configuration; using Coscine.Database.DataModel; -using Newtonsoft.Json; +using Coscine.Database.Models; using System; -using System.Collections; using System.Collections.Generic; -using System.Linq; -using System.Net.Http; -using System.Text; -using System.Threading.Tasks; namespace Coscine.KpiGenerator.Reporter { - class HamsterReporter + public class HamsterReporter { - private readonly HttpClient httpClient; - private readonly string hamsterUrl = @"http://datahamster.itc.rwth-aachen.de/put.php"; + private readonly RestClient _restClient; + private readonly string _hamsterUrl = @"http://datahamster.itc.rwth-aachen.de/put.php"; private readonly string _name = "messdaten_lieferant"; private readonly string _password = ""; @@ -24,60 +19,66 @@ namespace Coscine.KpiGenerator.Reporter { _password = configuration.GetStringAndWait("coscine/global/reporting/password"); _liveMode = configuration.GetStringAndWait("coscine/global/reporting/live"); - httpClient = new HttpClient(); + _restClient = new RestClient(); } - /** - * Expects three parameters: - * login - * password - * csv - * Optional: - * debug - */ - public async void Send(List<Kpi> kpis) + public void Send(List<Kpi> kpis) { - var csv = ""; - for (var i = 0; i < kpis.Count; i++) + // when all generators are done + var kpiModel = new KpiModel(); + foreach (var kpi in kpis) { - csv += FormatKpi(kpis[i]); - if (i < kpis.Count - 1) + var values = $"login={_name}&password={_password}"; + values += FormatSingleKpi(kpi); + if (_liveMode != "1") { - csv += "|"; + values += "&debug=1"; } - } - var values = $"login={_name}&password={_password}&csv={csv}"; - if (_liveMode != "1") - { - values += "&debug=1"; + var result = SendData(values); + // If everything works, output should be empty + if (result.Trim() != "") + { + Console.Error.WriteLine(result); + } + else + { + kpiModel.MarkAsSent(kpi); + } } - - var content = JsonConvert.SerializeObject(values); - await SendData(content); } - private async Task<HttpResponseMessage> SendData(string data) + private string SendData(string data) { - HttpContent content = new StringContent(data, Encoding.UTF8, "application/json"); - return await httpClient.PostAsync(hamsterUrl, content); + return _restClient.HttpPost(new Uri(_hamsterUrl), data, null); } - private string FormatKpi(Kpi kpi) + private string FormatSingleKpi(Kpi kpi) { - return $"{kpi.MeasurementID};{kpi.Ikz};{FormatDate(kpi.Start)};{FormatDate(kpi.End)};{kpi.AdditionalInfo};{kpi.AdditionalInfo1};{kpi.AdditionalInfo2};{kpi.AdditionalInfo3};{kpi.AdditionalInfo4};{kpi.AdditionalInfo5};"; + return + $"&messgroesse_id={kpi.MeasurementID}" + + $"&wert={kpi.Value}" + + $"&start={FormatDate(kpi.Start)}" + + $"&ende={FormatDate(kpi.End)}" + + $"&ikz={kpi.Ikz ?? ""}" + + $"&zusatzinfo={kpi.AdditionalInfo ?? ""}" + + $"&zusatzinfo1={kpi.AdditionalInfo1 ?? ""}" + + $"&zusatzinfo2={kpi.AdditionalInfo2 ?? ""}" + + $"&zusatzinfo3={kpi.AdditionalInfo3 ?? ""}" + + $"&zusatzinfo4={kpi.AdditionalInfo4 ?? ""}" + + $"&zusatzinfo5={kpi.AdditionalInfo5 ?? ""}"; } private string FormatDate(DateTime? date) { - if(date == null) + if (!date.HasValue) { return ""; } else { - return ((DateTime) date).ToString("yyyy-MM-dd-HH-mm-ss"); - } + return ((DateTime)date).ToString("yyyy-MM-dd-HH-mm-ss"); + } } } } diff --git a/src/KpiGenerator/RestClient.cs b/src/KpiGenerator/RestClient.cs new file mode 100644 index 0000000000000000000000000000000000000000..62fa5f78239c2050e8d105cab93dea4c485e70ee --- /dev/null +++ b/src/KpiGenerator/RestClient.cs @@ -0,0 +1,110 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Net; + +namespace Coscine.KpiGenerator +{ + /// <summary> + /// This class is a legacy implementation and was adopted to ensure compatibility with the previous DataHamster implementation. + /// </summary> + public class RestClient + { + private static readonly string _urlTemplateWithQuery = "{0}?{1}"; + private static readonly string _urlTemplateWithoutQuery = "{0}"; + + private static WebResponse HttpRequest( + Uri endpoint, + string query = null, + string method = "GET", + string body = null, + string user = null, + string password = null, + string contentType = "application/json; charset=utf-8", + string accept = "application/json, */*", + string userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64)", + Dictionary<string, string> headers = null + ) + { + if (string.IsNullOrWhiteSpace(method)) + { + method = "GET"; + } + method = method.ToUpper(); + + var template = _urlTemplateWithQuery; + if (string.IsNullOrWhiteSpace(query)) + { + template = _urlTemplateWithoutQuery; + query = string.Empty; + } + + var request = (HttpWebRequest)WebRequest.Create(string.Format(template, endpoint.OriginalString, query)); + + if (headers != null) + { + foreach (var keyValuePair in headers) + { + request.Headers.Add(keyValuePair.Key, keyValuePair.Value); + } + } + + request.Method = method; + request.ContentType = contentType; + request.Accept = accept; + request.UserAgent = userAgent; + + if (!string.IsNullOrWhiteSpace(user) && !string.IsNullOrWhiteSpace(password)) + { + request.Credentials = new NetworkCredential(user, password); + request.PreAuthenticate = true; + } + + if (!string.IsNullOrWhiteSpace(body) && method != "GET") + { + using (var stream = request.GetRequestStream()) + using (var writer = new StreamWriter(stream)) + { + writer.Write(body); + writer.Flush(); + } + } + else + { + request.ContentLength = 0; + } + + WebResponse response = request.GetResponse(); + return response; + } + + private string ReadResponse(WebResponse response) + { + using (Stream responseStream = response.GetResponseStream()) + using (StreamReader reader = new StreamReader(responseStream)) + { + string responseFromServer = reader.ReadToEnd(); + return responseFromServer.Trim(); + } + } + + + #region HTTP POST Shortcuts + + public string HttpPost(Uri endpoint, string query, string body = null) + { + return Http("POST", endpoint, query, body); + } + + #endregion + + public string Http(string method, Uri endpoint, string query, string body = null, string user = null, string password = null) + { + using (var response = HttpRequest(endpoint, query, method, body, user, password)) + { + return ReadResponse(response); + } + } + + } +}