Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contms-117 telemetry 5 xx on large project in trados #28

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 67 additions & 17 deletions Intento.MT.Plugin.PropertiesForm/Logs.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,44 @@
using Intento.MT.Plugin.PropertiesForm;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace Intento.MT.Plugin.PropertiesForm
{

public static class Logs
{
/// <summary>
/// Limiting the request rate, ms
/// </summary>
const int sleepTime = 5000;
/// <summary>
/// Limit of entries from the queue in one request
/// </summary>
const int maxEntries = 300;

static string _consumer_id;
static string _session_id;
public static string ApiKey { get; set; }
public static string PluginName { get; set; }

/// <summary>
/// A stream instance that checks the data queue and sends it to the cloud
/// </summary>
static Timer sender;

/// <summary>
/// Data queue
/// </summary>
static ConcurrentQueue<KeyValuePair<char, string>> queue = new ConcurrentQueue<KeyValuePair<char, string>>();

const string url = "https://api.inten.to/telemetry/upload_json";

public static string ConsumerId
{
get
Expand Down Expand Up @@ -119,7 +140,7 @@ public static IEnumerable<string> LoggingEx(char identificator, Exception ex)
return items;
}

private static async void WriteRemoteLog(char identificator, string text)
private static void WriteRemoteLog(char identificator, string text)
{
if (!IsLogging())
return;
Expand All @@ -130,22 +151,51 @@ private static async void WriteRemoteLog(char identificator, string text)
if (string.IsNullOrWhiteSpace(ApiKey))
return;

dynamic jsonResult;
string url = "https://api.inten.to/telemetry/upload_json";
JObject data = new JObject();
data["plugin_name"] = string.Format("{0}-{1}", PluginName, identificator);
data["session_id"] = SessionId;
data["logs"] = text;
var content = new StringContent(data.ToString(), Encoding.UTF8, "application/json");
// Call to Intento API
using (var conn = new HttpClient())
queue.Enqueue(new KeyValuePair<char, string>(identificator, text));
if (sender == null)
{
conn.DefaultRequestHeaders.Add("apikey", ApiKey);
conn.DefaultRequestHeaders.Add("x-consumer-id", ConsumerId);
jsonResult = await conn.PostAsync(url, content);
sender = new Timer();
sender.Interval = sleepTime;
sender.Elapsed += OnTimedEvent;
sender.Start();
}
}
private static async void OnTimedEvent(Object source, ElapsedEventArgs e)
{
try
{
Dictionary<char, string> inprogress = new Dictionary<char, string>();
KeyValuePair<char, string> item;
JObject data = new JObject();
data["session_id"] = SessionId;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
dynamic jsonResult;
int entries = 0;
while (queue.TryDequeue(out item) && entries < maxEntries)
{
if (inprogress.ContainsKey(item.Key))
inprogress[item.Key] += "\n" + item.Value;
else
inprogress.Add(item.Key, item.Value);
entries++;
}

foreach (KeyValuePair<char, string> kp in inprogress)
{
data["plugin_name"] = string.Format("{0}-{1}", PluginName, kp.Key);
data["logs"] = kp.Value;
var content = new StringContent(data.ToString(), Encoding.UTF8, "application/json");
// Call to Intento API
using (var conn = new HttpClient())
{
conn.DefaultRequestHeaders.Add("apikey", ApiKey);
conn.DefaultRequestHeaders.Add("x-consumer-id", ConsumerId);
jsonResult = await conn.PostAsync(url, content);
}
}
}
catch { }
}
}

}
2 changes: 0 additions & 2 deletions Intento.MT.Plugin.PropertiesForm/packages.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AWSSDK.Core" version="3.3.107.14" targetFramework="net45" />
<package id="AWSSDK.S3" version="3.3.111.14" targetFramework="net45" />
oerodger marked this conversation as resolved.
Show resolved Hide resolved
<package id="IntentoSDK" version="1.5.3" targetFramework="net45" />
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net45" />
</packages>
2 changes: 1 addition & 1 deletion TestForm/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
<bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="4.5.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Expand Down
14 changes: 8 additions & 6 deletions TestForm/TestForm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="IntentoSDK">
<HintPath>..\bin\IntentoSDK.dll</HintPath>
<Reference Include="IntentoSDK, Version=1.5.3.0, Culture=neutral, PublicKeyToken=7fbf26cf2c5f5508, processorArchitecture=MSIL">
<HintPath>..\..\packages\IntentoSDK.1.5.3\lib\net45\IntentoSDK.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\bin\Newtonsoft.Json.dll</HintPath>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down Expand Up @@ -97,7 +96,10 @@
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<Analyzer Include="..\..\Plugins\packages\AWSSDK.S3.3.5.0-beta\analyzers\dotnet\cs\AWSSDK.S3.CodeAnalysis.dll" />
<ProjectReference Include="..\Intento.MT.Plugin.PropertiesForm\Intento.MT.Plugin.PropertiesForm.csproj">
<Project>{a4374128-dd01-4ce3-b62c-4cc3460620c0}</Project>
<Name>Intento.MT.Plugin.PropertiesForm</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
4 changes: 2 additions & 2 deletions TestForm/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AWSSDK.Core" version="3.5.0-beta" targetFramework="net47" />
<package id="AWSSDK.S3" version="3.5.0-beta" targetFramework="net47" />
<package id="IntentoSDK" version="1.5.3" targetFramework="net47" />
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net47" />
</packages>