1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using Tango.Core;
using Tango.Insights;
using Tango.Integration.Operation;
using Tango.PMR.Diagnostics;
using Tango.PMR.Insights;
namespace Tango.Telemetry
{
public class TelemetryPublisher : ExtendedObject, IDisposable
{
public const int MIN_SAMPLING_INTERVAL_SECONDS = 1;
public event EventHandler<TelemetryPackagePublishingEventArgs> PublishingPackage;
public event EventHandler<TelemetryPackagePublishedEventArgs> PackagePublished;
public event EventHandler<TelemetryPackagePublishFailedEventArgs> PublishPackageFailed;
private System.Timers.Timer _diagnosticsSamplingTimer;
private System.Timers.Timer _pendingStorageCheckTimer;
private Thread _publishThread;
private ProducerConsumerQueue<TelemetryPublishPackage> _publishQueue;
private List<StartDiagnosticsResponse> _diagnosticsQueue;
private bool _writing;
private bool _emptyWritten;
private IMachineOperator _machineOperator;
private TelemetryPendingStorageManager _pendingStorageManager;
#region Properties
public TelemetryPublisherConfiguration Config { get; private set; }
public bool IsStarted { get; private set; }
#endregion
#region Constructor
public TelemetryPublisher(IMachineOperator machineOperator, TelemetryPendingStorageManager storageManager, TelemetryPublisherConfiguration config)
{
_machineOperator = machineOperator;
_pendingStorageManager = storageManager;
Config = config ?? new TelemetryPublisherConfiguration();
_publishQueue = new ProducerConsumerQueue<TelemetryPublishPackage>();
_publishThread = new Thread(PublishThreadMethod);
_publishThread.IsBackground = true;
_diagnosticsQueue = new List<StartDiagnosticsResponse>();
RegisterForEvents();
}
#endregion
#region Register / Unregister Events
private void RegisterForEvents()
{
_machineOperator.DiagnosticsDataAvailable += MachineOperator_DiagnosticsDataAvailable;
}
private void UnregisterEvents()
{
_machineOperator.DiagnosticsDataAvailable -= MachineOperator_DiagnosticsDataAvailable;
}
#endregion
#region Start / Stop
public void Start()
{
if (!IsStarted)
{
Config.Validate();
IsStarted = true;
if (Config.DiagnosticsSamplingInterval.TotalSeconds < MIN_SAMPLING_INTERVAL_SECONDS)
{
Config.DiagnosticsSamplingInterval = TimeSpan.FromSeconds(MIN_SAMPLING_INTERVAL_SECONDS);
}
if (_diagnosticsSamplingTimer == null)
{
_diagnosticsSamplingTimer = new System.Timers.Timer();
_diagnosticsSamplingTimer.Interval = Config.DiagnosticsSamplingInterval.TotalMilliseconds;
_diagnosticsSamplingTimer.Elapsed += DiagnosticsSamplingTimer_Elapsed;
}
if (_pendingStorageCheckTimer == null)
{
_pendingStorageCheckTimer = new System.Timers.Timer();
_pendingStorageCheckTimer.Interval = Config.PendingStorageCheckInterval.TotalMilliseconds;
_pendingStorageCheckTimer.Elapsed += PendingStorageCheckTimer_Elapsed;
}
_diagnosticsQueue.Clear();
_writing = false;
_diagnosticsSamplingTimer.Start();
_pendingStorageCheckTimer.Start();
_publishThread.Start();
}
}
public void Stop()
{
if (IsStarted)
{
IsStarted = false;
_diagnosticsSamplingTimer.Stop();
_pendingStorageCheckTimer.Stop();
_diagnosticsQueue.Clear();
_publishQueue.BlockEnqueue(null);
}
}
#endregion
#region Incoming Data Event Handlers
private void MachineOperator_DiagnosticsDataAvailable(object sender, StartDiagnosticsResponse diagnostics)
{
if (IsStarted && diagnostics.Monitors != null)
{
_diagnosticsQueue.Add(diagnostics);
}
}
#endregion
#region Timers
private void DiagnosticsSamplingTimer_Elapsed(object sender, ElapsedEventArgs e)
{
if (!IsStarted || _writing) return;
try
{
_writing = true;
if (_diagnosticsQueue.Count > 0)
{
var queue = _diagnosticsQueue.ToList();
_diagnosticsQueue.Clear();
_emptyWritten = false;
var monitorsAvg = InsightsHelper.AverageMonitors(queue.Select(x => x.Monitors).ToList());
queue.Clear();
TelemetryDiagnosticsFrame frame = new TelemetryDiagnosticsFrame();
frame.Monitors = monitorsAvg;
frame.Time = DateTime.UtcNow.Subtract(Config.DiagnosticsSamplingInterval);
PushTelemetryPackage(frame, TelemetrySource.Streaming);
}
else
{
if (!_emptyWritten)
{
TelemetryDiagnosticsFrame frame = new TelemetryDiagnosticsFrame();
frame.Monitors = new InsightsMonitors();
frame.Time = DateTime.UtcNow.Subtract(Config.DiagnosticsSamplingInterval);
PushTelemetryPackage(frame, TelemetrySource.Streaming);
_emptyWritten = true;
}
}
}
catch (Exception ex)
{
LogManager.Log(ex, "Error occurred on insights frame insertion.");
}
finally
{
_writing = false;
}
}
private void PendingStorageCheckTimer_Elapsed(object sender, ElapsedEventArgs e)
{
var telemetryAll = _pendingStorageManager.GetTelemetryAll();
foreach (var t in telemetryAll)
{
PushTelemetryPackage(t, TelemetrySource.PendingStorage);
}
}
#endregion
#region Push
private void PushTelemetryPackage(ITelemetry telemetryObject, TelemetrySource source)
{
_publishQueue.BlockEnqueue(new TelemetryPublishPackage() { TelemetryObject = telemetryObject, Source = source });
}
private void PushTelemetryPackage(TelemetryPublishPackage package)
{
_publishQueue.BlockEnqueue(package);
}
#endregion
#region Publish
private async void PublishThreadMethod()
{
while (IsStarted)
{
TelemetryPublishPackage package = _publishQueue.BlockDequeue();
if (package == null)
{
_publishQueue = new ProducerConsumerQueue<TelemetryPublishPackage>();
return;
}
try
{
await PublishTelemetryPackage(package);
}
catch
{
Thread.Sleep(1000);
}
}
}
private async Task PublishTelemetryPackage(TelemetryPublishPackage package)
{
List<KeyValuePair<String, String>> properties = new List<KeyValuePair<string, string>>();
properties.Add(new KeyValuePair<string, string>("MachineID", Config.MachineID));
properties.Add(new KeyValuePair<string, string>("Model", Config.MachineType.ToShortName()));
properties.Add(new KeyValuePair<string, string>("Environment", Config.Environment));
List<String> pendingDestinations = package.TelemetryObject.PendingDestinations.ToList();
//Add all destinations if streaming or external (They will be remove later if successfull)
//If source is "PendingStorage" the "PendingDestination" would be already propagated from the pending storage db.
if (package.Source == TelemetrySource.Streaming || package.Source == TelemetrySource.ExternalStorage)
{
pendingDestinations.AddRange(Config.TelemetryDestinations.Select(x => x.GetType().Name));
}
pendingDestinations = pendingDestinations.Distinct().ToList();
foreach (var destination in Config.TelemetryDestinations.Where(x => x.SupportedSources.Contains(package.Source)))
{
String destinationName = destination.GetType().Name;
if (pendingDestinations.Contains(destinationName))
{
try
{
pendingDestinations.Remove(destinationName);
if (OnPublishingPackage(package, destination))
{
await destination.Publish(package, properties);
OnPackagePublished(package, destination);
}
}
catch (Exception ex)
{
LogManager.Log(ex, $"Error publishing telemetry package to destination {destinationName}.");
OnPackagePublishFailed(package, destination, ex);
if (destination.SupportedSources.Contains(TelemetrySource.PendingStorage))
{
if (!pendingDestinations.Contains(destinationName))
{
pendingDestinations.Add(destinationName);
}
}
}
}
}
package.TelemetryObject.PendingDestinations = pendingDestinations;
if (package.Source == TelemetrySource.PendingStorage && package.TelemetryObject.PendingDestinations.Count == 0)
{
_pendingStorageManager.DeleteTelemetryObject(package.TelemetryObject);
}
else
{
_pendingStorageManager.InsertOrUpdateTelemetryObject(package.TelemetryObject);
}
}
#endregion
#region Virtual Methods
protected virtual bool OnPublishingPackage(TelemetryPublishPackage package, ITelemetryDestination destination)
{
try
{
var args = new TelemetryPackagePublishingEventArgs() { Package = package, Destination = destination };
PublishingPackage?.Invoke(this, args);
return !args.Cancel;
}
catch
{
return true;
}
}
protected virtual void OnPackagePublished(TelemetryPublishPackage package, ITelemetryDestination destination)
{
try
{
PublishingPackage?.Invoke(this, new TelemetryPackagePublishingEventArgs() { Package = package, Destination = destination });
}
catch { }
}
protected virtual void OnPackagePublishFailed(TelemetryPublishPackage package, ITelemetryDestination destination, Exception exception)
{
try
{
PublishPackageFailed?.Invoke(this, new TelemetryPackagePublishFailedEventArgs() { Package = package, Destination = destination, Exception = exception });
}
catch { }
}
#endregion
#region Dispose
public void Dispose()
{
UnregisterEvents();
if (IsStarted)
{
Stop();
}
foreach (var destination in Config.TelemetryDestinations)
{
destination.Dispose();
}
}
#endregion
}
}
|