forked from xcp-ng/xenadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataSet.cs
612 lines (537 loc) · 24.2 KB
/
DataSet.cs
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/* Copyright (c) Citrix Systems, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms,
* with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using XenAPI;
using XenCenterLib;
using XenAdmin.Core;
namespace XenAdmin.Controls.CustomDataGraph
{
public class DataSet : IComparable<DataSet>
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private const int NegativeValue = -1;
/// <summary>
/// Things can only be added to the beginning or end of this list; it should
/// be sorted by X co-ordinate (which will be larger at the beginning of the list)
/// </summary>
public List<DataPoint> Points = new List<DataPoint>();
public bool Selected;
public List<DataPoint> CurrentlyDisplayed = new List<DataPoint>();
public IXenObject XenObject;
public readonly string Id = "";
public string DataSourceName;
public string FriendlyName { get; }
private int MultiplyingFactor = 1;
public DataRange CustomYRange = new DataRange(1, 0, 1, Unit.None, RangeScaleMode.Auto);
public bool Hide { get; }
public DataSet(IXenObject xo, bool hide, string datasourceName, List<Data_source> datasources)
{
XenObject = xo;
Hide = datasourceName == "xapi_open_fds" ||
datasourceName == "pool_task_count" ||
datasourceName == "pool_session_count" ||
datasourceName == "memory" ||
datasourceName == "memory_total_kib" || hide;
DataSourceName = datasourceName;
if (xo is Host host)
Id = $"host:{host.uuid}:{datasourceName}";
else if (xo is VM vm)
Id = $"vm:{vm.uuid}:{datasourceName}";
if (datasourceName == "memory_free_kib")
FriendlyName = Helpers.GetFriendlyDataSourceName("memory_used_kib", xo);
else if (datasourceName == "memory_internal_free")
FriendlyName = Helpers.GetFriendlyDataSourceName("memory_internal_used", xo);
else
FriendlyName = Helpers.GetFriendlyDataSourceName(datasourceName, xo);
var units = datasources.FirstOrDefault(d => datasourceName == d.name_label)?.units;
switch (units)
{
case "count":
case "requests":
case "sessions":
case "tasks":
case "file descriptors":
break;
case "(fraction)":
//CP-34000: use Auto instead of Fixed scale
CustomYRange = new DataRange(100, 0, 10, Unit.Percentage, RangeScaleMode.Auto);
MultiplyingFactor = 100;
break;
case "MHz":
CustomYRange = new DataRange(1, 0, 1, Unit.MegaHertz, RangeScaleMode.Auto);
break;
case "requests/s":
case "err/s":
CustomYRange = new DataRange(1, 0, 1, Unit.CountsPerSecond, RangeScaleMode.Auto);
break;
case "s/s":
CustomYRange = new DataRange(1, 0, 1, Unit.SecondsPerSecond, RangeScaleMode.Auto);
break;
case "s":
CustomYRange = new DataRange(1, 0, 1, Unit.NanoSeconds, RangeScaleMode.Auto);
MultiplyingFactor = (int)Util.DEC_GIGA;
break;
case "ms":
CustomYRange = new DataRange(1, 0, 1, Unit.NanoSeconds, RangeScaleMode.Auto);
MultiplyingFactor = (int)Util.DEC_MEGA;
break;
case "μs":
CustomYRange = new DataRange(1, 0, 1, Unit.NanoSeconds, RangeScaleMode.Auto);
MultiplyingFactor = (int)Util.DEC_KILO;
break;
case "B":
CustomYRange = new DataRange(1, 0, 1, Unit.Bytes, RangeScaleMode.Auto);
break;
case "KiB":
CustomYRange = new DataRange(1, 0, 1, Unit.Bytes, RangeScaleMode.Auto);
MultiplyingFactor = (int)Util.BINARY_KILO;
break;
case "B/s":
CustomYRange = new DataRange(1, 0, 1, Unit.BytesPerSecond, RangeScaleMode.Auto);
break;
case "MiB/s":
MultiplyingFactor = (int)Util.BINARY_MEGA;
CustomYRange = new DataRange(1, 0, 1, Unit.BytesPerSecond, RangeScaleMode.Auto);
break;
case "mW":
CustomYRange = new DataRange(1, 0, 1, Unit.MilliWatt, RangeScaleMode.Auto);
break;
case "Centigrade":
CustomYRange = new DataRange(1, 0, 1, Unit.Centigrade, RangeScaleMode.Auto);
break;
case "unknown":
CustomYRange = new DataRange(1, 0, 1, Unit.Unknown, RangeScaleMode.Auto);
break;
default:
if (string.IsNullOrEmpty(units))
CustomYRange = new DataRange(1, 0, 1, Unit.Unknown, RangeScaleMode.Auto);
else
System.Diagnostics.Debug.Assert(false, $"Unhandled units {units}!");
break;
}
if (datasourceName == "memory_free_kib" || datasourceName == "memory_internal_free")
{
var max = GetMemoryMax(xo);
var resolution = GetMemoryResolution(max);
CustomYRange = new DataRange(max, 0, resolution, Unit.Bytes, RangeScaleMode.Delegate)
{
UpdateMax = GetMemoryMax,
UpdateResolution = GetMemoryResolution
};
}
}
public static bool ParseId(string id, out string objType, out string objUuid, out string dataSourceName)
{
var bits = id.Split(':').ToList();
if (bits.Count > 3)
bits.RemoveAt(0);
if (bits.Count >= 3)
{
objType = bits[0];
objUuid = bits[1];
dataSourceName = bits[2];
return true;
}
objType = null;
objUuid = null;
dataSourceName = null;
return false;
}
public List<DataPoint> GetRange(DataTimeRange xrange, long intervalneed, long intervalat)
{
List<DataPoint> fine = BinaryChop(Points, xrange);
if (fine.Count == 0)
return new List<DataPoint>();
fine.Reverse();
List<DataPoint> listout = new List<DataPoint>();
double cumulativey = 0;
int count = 0;
foreach (DataPoint p in fine)
{
var secSince1970 = Util.TicksToSecondsSince1970(new DateTime(p.X).ToUniversalTime().Ticks);
if (secSince1970 % (intervalneed / TimeSpan.TicksPerSecond) == secSince1970 % (intervalat / TimeSpan.TicksPerSecond))
{
listout.Insert(0,new DataPoint(p.X,count != 0 ? cumulativey / count : p.Y));
cumulativey = 0;
count = 0;
}
else
{
cumulativey += p.Y;
count++;
}
}
if(count != 0)
listout.Insert(0, new DataPoint(fine[fine.Count - 1].X, cumulativey / count));
return listout;
}
private static double GetMemoryMax(IXenObject xo)
{
if (xo is Host host)
return host.Connection.Resolve(host.metrics)?.memory_total ?? 100;
if (xo is VM vm)
return (vm.Connection.Resolve(vm.metrics))?.memory_actual ?? vm.memory_dynamic_max;
return 100;
}
private static double GetMemoryResolution(IXenObject xmo)
{
return GetMemoryMax(xmo) / 10d;
}
private static double GetMemoryResolution(double max)
{
return max / 10d;
}
public static double GetMaxY(List<DataPoint> dataPoints)
{
return dataPoints == null || dataPoints.Count == 0
? double.MinValue
: dataPoints.Max(dataPoint => dataPoint.Y);
}
public void RefreshCustomY(DataTimeRange range, List<DataPoint> points)
{
// find last element before beginning of xrange => binary chop
CurrentlyDisplayed = BinaryChop(points, range);
}
public List<DataPoint> BinaryChop(List<DataPoint> points, DataTimeRange xrange)
{
if (xrange.Delta > 0)
{
log.DebugFormat("Get range: Delta should be negative, max={0}, min={1}", xrange.Max, xrange.Min);
return new List<DataPoint>();
}
// if there are no points or none are in range
if (points == null || points.Count == 0 || points[points.Count - 1].X >= xrange.Min || points[0].X <= xrange.Max)
return new List<DataPoint>();
int startindex = GetStart(points, xrange.Min, 0, points.Count);
int endindex = GetStart(points, xrange.Max, 0, points.Count) + 1;
if (endindex > 0 && endindex != points.Count)
endindex++;
if (startindex == -1 && endindex <= 0)
return new List<DataPoint>();
if (startindex == -1)
if (endindex <= points.Count)
return points.GetRange(0, endindex);
else
return new List<DataPoint>();
if (endindex <= 0)
if (startindex < points.Count)
return points.GetRange(startindex, points.Count - startindex);
else
return new List<DataPoint>();
System.Diagnostics.Trace.Assert(startindex >= 0 && startindex < points.Count && endindex >= 0 && endindex <= points.Count && endindex > startindex, string.Format("Argument exception: startindex={0}; endindex={1}; points.Count={2}", startindex, endindex, points.Count));
return points.GetRange(startindex, endindex - startindex);
}
private int GetStart(List<DataPoint> points, long p, int start, int end)
{
if (start + 1 == end)
return start;
int halfway = (end - start) / 2;
if (p <= points[start + halfway].X)
return GetStart(points, p, start + halfway, end);
else
return GetStart(points, p, start, end - halfway);
}
public override string ToString()
{
return FriendlyName;
}
public DataPoint OnMouseMove(MouseActionArgs args)
{
LongPoint p = LongPoint.DeTranslateFromScreen(new LongPoint(args.Point), args.XRange, args.YRange, new LongRectangle(args.Rectangle));
return ClosestPointTo(p);
}
private DataPoint ClosestPointTo(LongPoint p)
{
if (CurrentlyDisplayed.Count == 0 || CurrentlyDisplayed[CurrentlyDisplayed.Count - 1].X > p.X || CurrentlyDisplayed[0].X < p.X)
return null;
for (int i = 1; i < CurrentlyDisplayed.Count; i++)
{
if (CurrentlyDisplayed[i - 1].X >= p.X && CurrentlyDisplayed[i].X < p.X)
{
if (p.X - CurrentlyDisplayed[i].X > (CurrentlyDisplayed[i - 1].X - CurrentlyDisplayed[i].X) / 2)
return CurrentlyDisplayed[i - 1];
else
return CurrentlyDisplayed[i];
}
}
return null;
}
public bool OnMouseClick(MouseActionArgs args)
{
DataRange yrange = this.CustomYRange ?? args.YRange;
List<DataPoint> range = BinaryChop(Points, args.XRange);
if (range.Count == 0)
return false;
List<LongPoint> polypoints = new List<LongPoint>();
foreach (DataPoint p in range)
{
LongPoint lp = LongPoint.TranslateToScreen(p.Point, args.XRange, yrange, new LongRectangle(args.Rectangle));
polypoints.Add(new LongPoint(lp.X,lp.Y + 10));
polypoints.Insert(0,new LongPoint(lp.X, lp.Y - 10));
}
Polygon poly = new Polygon(polypoints);
return poly.Contains(new LongPoint(args.Point));
}
public void AddPoint(string str, long currentTime, List<DataSet> setsAdded, List<Data_source> dataSources)
{
double value = Helpers.StringToDouble(str);
bool isNanOrInfinity = double.IsNaN(value) || double.IsInfinity(value);
double yValue = isNanOrInfinity ? NegativeValue : value * MultiplyingFactor;
#region cpu
var matchDelegate = new Func<string, bool>(s => Helpers.CpuRegex.IsMatch(s) &&
!Helpers.CpuStateRegex.IsMatch(s));
if (matchDelegate(DataSourceName))
{
DataSet other = setsAdded.FirstOrDefault(s => s.DataSourceName == "avg_cpu");
if (other == null)
{
other = new DataSet(XenObject, false, "avg_cpu", dataSources);
setsAdded.Add(other);
}
DataPoint pt = other.GetPointAt(currentTime);
if (pt == null)
{
pt = new DataPoint(currentTime, 0);
other.AddPoint(pt);
}
if (isNanOrInfinity || pt.Y < 0)
pt.Y = NegativeValue;
else
{
double cpu_vals_added = 0d;
foreach (DataSet s in setsAdded)
{
if (matchDelegate(s.DataSourceName) && s.GetPointAt(currentTime) != null && s != this)
cpu_vals_added++;
}
pt.Y = (((pt.Y * cpu_vals_added) + (value * 100d)) / (cpu_vals_added + 1d)); // update average in the usual way
}
}
#endregion
#region memory
if (DataSourceName == "memory_total_kib")
{
DataSet other = setsAdded.FirstOrDefault(s => s.DataSourceName == "memory_free_kib");
if (other != null && other.Points.Count - 1 == Points.Count)
{
yValue = isNanOrInfinity || other.Points[other.Points.Count - 1].Y < 0
? NegativeValue
: (value * MultiplyingFactor) - other.Points[other.Points.Count - 1].Y;
other.Points[other.Points.Count - 1].Y = yValue;
}
}
else if (DataSourceName == "memory_free_kib")
{
DataSet other = setsAdded.FirstOrDefault(s => s.DataSourceName == "memory_total_kib");
if (other != null && other.Points.Count - 1 == Points.Count)
{
yValue = isNanOrInfinity || other.Points[other.Points.Count - 1].Y < 0
? NegativeValue
: other.Points[other.Points.Count - 1].Y - (value * MultiplyingFactor);
}
}
else if (DataSourceName == "memory")
{
DataSet other = setsAdded.FirstOrDefault(s => s.DataSourceName == "memory_internal_free");
if (other != null && other.Points.Count - 1 == Points.Count)
{
yValue = isNanOrInfinity || other.Points[other.Points.Count - 1].Y < 0
? NegativeValue
: (value * MultiplyingFactor) - other.Points[other.Points.Count - 1].Y;
other.Points[other.Points.Count - 1].Y = yValue;
}
}
else if (DataSourceName == "memory_internal_free")
{
DataSet other = setsAdded.FirstOrDefault(s => s.DataSourceName == "memory");
if (other != null && other.Points.Count - 1 == Points.Count)
{
yValue = isNanOrInfinity || other.Points[other.Points.Count - 1].Y < 0
? NegativeValue
: other.Points[other.Points.Count - 1].Y - (value * MultiplyingFactor);
}
}
#endregion
AddPoint(new DataPoint(currentTime, yValue));
}
public void AddPoint(DataPoint dataPoint)
{
Points.Add(dataPoint);
if (CustomYRange == null || CustomYRange.ScaleMode == RangeScaleMode.Fixed)
return;
if (CustomYRange.ScaleMode == RangeScaleMode.Auto)
{
if (dataPoint.Y > CustomYRange.Max)
{
CustomYRange.Max = dataPoint.Y * 1.05;
CustomYRange.Resolution = CustomYRange.Delta / 10 > 0 ? CustomYRange.Delta / 10 : 1;
}
}
else if (CustomYRange.ScaleMode == RangeScaleMode.Delegate)
{
CustomYRange.UpdateAll(XenObject);
}
}
private void InsertPoint(int index, DataPoint dataPoint)
{
Points.Insert(index, dataPoint);
if (CustomYRange == null || CustomYRange.ScaleMode == RangeScaleMode.Fixed)
return;
if (CustomYRange.ScaleMode == RangeScaleMode.Auto)
{
if (dataPoint.Y > CustomYRange.Max)
{
CustomYRange.Max = dataPoint.Y * 1.05;
CustomYRange.Resolution = CustomYRange.Delta / 10 > 0 ? CustomYRange.Delta / 10 : 1;
}
}
else if (CustomYRange.ScaleMode == RangeScaleMode.Delegate)
{
CustomYRange.UpdateAll(XenObject);
}
}
public override bool Equals(object obj)
{
if (!(obj is DataSet other))
return false;
return Id == other.Id;
}
public override int GetHashCode()
{
return string.IsNullOrEmpty(Id) ? 0 : Id.GetHashCode();
}
internal void InsertPointCollection(List<DataPoint> list)
{
if (list.Count == 0)
return;
if (Points.Count == 0)
{
Points = list;
return;
}
if (list[list.Count - 1].X < Points[Points.Count - 1].X)
{
// filter out interesting shizzle
List<DataPoint> interesting = BinaryChop(list, new DataTimeRange(list[list.Count - 1].X, Points[Points.Count - 1].X, 1));
if (interesting.Count == 0)
return;
interesting.RemoveAt(0);
foreach (DataPoint point in interesting)
{
AddPoint(point);
}
}
if (list[0].X > Points[0].X)
{
//List<DataPoint> interesting = BinaryChop(list, new DataRange(Points[0].X, list[0].X, 1));
if (list.Count == 0)
return;
list.Reverse();
foreach (DataPoint point in list)
{
if(Points.Count > 0 && Points[0].X <point.X)
InsertPoint(0,point);
}
}
}
public void MergePointCollection(List<DataPoint> list, List<DataPoint> pointsin)
{
if (list.Count == 0)
return;
if (pointsin.Count == 0)
{
pointsin = list;
return;
}
if (list[list.Count - 1].X < pointsin[pointsin.Count - 1].X)
{
// filter out interesting shizzle
List<DataPoint> interesting = BinaryChop(list, new DataTimeRange(list[list.Count - 1].X, pointsin[pointsin.Count - 1].X, 1));
if (interesting.Count == 0)
return;
interesting.RemoveAt(0);
foreach (DataPoint point in interesting)
{
pointsin.Add(point);
}
}
if (list[0].X > pointsin[0].X)
{
List<DataPoint> interesting = BinaryChop(list, new DataTimeRange(pointsin[0].X, list[0].X, 1));
if (interesting.Count == 0)
return;
interesting.Reverse();
interesting.RemoveAt(0);
foreach (DataPoint point in interesting)
{
if (pointsin.Count > 0 && pointsin[0].X < point.X)
pointsin.Insert(0, point);
}
}
}
public void TrimEnd(int maxPoints)
{
if (Points.Count > maxPoints)
Points.RemoveRange(maxPoints, Points.Count - maxPoints);
}
private double DisplayArea
{
get
{
if (CurrentlyDisplayed.Count == 0 || Selected)
return 0;
double sum = 0;
foreach (DataPoint dp in CurrentlyDisplayed)
sum += dp.Y;
return sum / (CurrentlyDisplayed.Count * CustomYRange.Max);
}
}
public int CompareTo(DataSet other)
{
if (Id == other.Id)
return 0;
int comp = DisplayArea.CompareTo(other.DisplayArea);
if (comp == 0)
return StringUtility.NaturalCompare(FriendlyName, other.FriendlyName);
return comp;
}
private DataPoint GetPointAt(long currentTime)
{
List<DataPoint> pts = BinaryChop(Points, new DataTimeRange(currentTime - 1, currentTime + 1, 1));
foreach (DataPoint p in pts)
if (p.X == currentTime)
return p;
return null;
}
}
}