Skip to content
Snippets Groups Projects
Commit 9f3d7bcb authored by Marcel Nellesen's avatar Marcel Nellesen
Browse files

Merge branch 'Hotfix/1306-fixReporting' into 'Sprint/2021-07'

Fix: Working KPI Generator

See merge request kpi-generator!5
parents 75e02fe6 016e58c5
No related branches found
No related tags found
2 merge requests!6Sprint/2021 07,!5Fix: Working KPI Generator
Pipeline #451126 passed
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)
{
......
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)
{
......
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)
{
......
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)
{
......
......@@ -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;
......
......@@ -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" />
......
......@@ -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);
......@@ -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
// 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);
}
}
}
}
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,53 +19,59 @@ 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)
{
csv += "|";
}
}
var values = $"login={_name}&password={_password}&csv={csv}";
var values = $"login={_name}&password={_password}";
values += FormatSingleKpi(kpi);
if (_liveMode != "1")
{
values += "&debug=1";
}
var content = JsonConvert.SerializeObject(values);
await SendData(content);
var result = SendData(values);
// If everything works, output should be empty
if (result.Trim() != "")
{
Console.Error.WriteLine(result);
}
else
{
kpiModel.MarkAsSent(kpi);
}
}
}
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 "";
}
......
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);
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment