-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathRootCauseAnalyzer.cs
751 lines (646 loc) · 27.7 KB
/
RootCauseAnalyzer.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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using Microsoft.ML.Internal.Utilities;
namespace Microsoft.ML.TimeSeries
{
public class RootCauseAnalyzer
{
private static double _anomalyRatioThreshold = 0.5;
private static double _anomalyDeltaThreshold = 0.95;
private static double _anomalyPreDeltaThreshold = 2;
private RootCauseLocalizationInput _src;
private double _beta;
public RootCauseAnalyzer(RootCauseLocalizationInput src, double beta)
{
_src = src;
_beta = beta;
}
public RootCause Analyze()
{
return AnalyzeOneLayer(_src);
}
/// <summary>
/// This is a function for analyze one layer for root cause, we select one dimension with values who contributes the most to the anomaly.
/// </summary>
private RootCause AnalyzeOneLayer(RootCauseLocalizationInput src)
{
RootCause dst = new RootCause();
dst.Items = new List<RootCauseItem>();
DimensionInfo dimensionInfo = SeparateDimension(src.AnomalyDimension, src.AggregateSymbol);
Tuple<PointTree, PointTree, Dictionary<string, TimeSeriesPoint>> pointInfo = GetPointsInfo(src, dimensionInfo);
PointTree pointTree = pointInfo.Item1;
PointTree anomalyTree = pointInfo.Item2;
Dictionary<string, TimeSeriesPoint> dimPointMapping = pointInfo.Item3;
//which means there is no anomaly point with the anomaly dimension or no point under anomaly dimension
if (anomalyTree.ParentNode == null || dimPointMapping.Count == 0)
{
return dst;
}
dst.Items.AddRange(LocalizeRootCauseByDimension(anomalyTree, pointTree, src.AnomalyDimension, dimensionInfo.AggDims));
GetRootCauseDirectionAndScore(dimPointMapping, src.AnomalyDimension, dst, _beta, pointTree, src.AggregateType, src.AggregateSymbol);
return dst;
}
protected List<TimeSeriesPoint> GetTotalPointsForAnomalyTimestamp(RootCauseLocalizationInput src)
{
MetricSlice slice = src.Slices.Single(slice => slice.TimeStamp.Equals(src.AnomalyTimestamp));
return slice.Points;
}
private DimensionInfo SeparateDimension(Dictionary<string, Object> dimensions, Object aggSymbol)
{
DimensionInfo info = new DimensionInfo();
foreach (KeyValuePair<string, Object> entry in dimensions)
{
string key = entry.Key;
if (aggSymbol.Equals(entry.Value))
{
info.AggDims.Add(key);
}
else
{
info.DetailDims.Add(key);
}
}
return info;
}
private Tuple<PointTree, PointTree, Dictionary<string, TimeSeriesPoint>> GetPointsInfo(RootCauseLocalizationInput src, DimensionInfo dimensionInfo)
{
PointTree pointTree = new PointTree();
PointTree anomalyTree = new PointTree();
Dictionary<string, TimeSeriesPoint> dimPointMapping = new Dictionary<string, TimeSeriesPoint>();
List<TimeSeriesPoint> totalPoints = GetTotalPointsForAnomalyTimestamp(src);
Dictionary<string, Object> subDim = GetSubDim(src.AnomalyDimension, dimensionInfo.DetailDims);
foreach (TimeSeriesPoint point in totalPoints)
{
if (ContainsAll(point.Dimension, subDim))
{
if (!dimPointMapping.ContainsKey(GetDicCode(point.Dimension)))
{
dimPointMapping.Add(GetDicCode(point.Dimension), point);
bool isValidPoint = point.IsAnomaly == true;
if (ContainsAll(point.Dimension, subDim))
{
BuildTree(pointTree, dimensionInfo.AggDims, point, src.AggregateSymbol);
if (isValidPoint)
{
BuildTree(anomalyTree, dimensionInfo.AggDims, point, src.AggregateSymbol);
}
}
}
}
}
return new Tuple<PointTree, PointTree, Dictionary<string, TimeSeriesPoint>>(pointTree, anomalyTree, dimPointMapping);
}
protected Dictionary<string, Object> GetSubDim(Dictionary<string, Object> dimension, List<string> keyList)
{
return new Dictionary<string, object>(keyList.Select(dim => new KeyValuePair<string, object>(dim, dimension[dim])).ToDictionary(kvp => kvp.Key, kvp => kvp.Value));
}
private List<RootCauseItem> LocalizeRootCauseByDimension(PointTree anomalyTree, PointTree pointTree, Dictionary<string, Object> anomalyDimension, List<string> aggDims)
{
BestDimension best = null;
if (anomalyTree.ChildrenNodes.Count == 0)
{
//has no children node information, should use the leaves node(whose point has no aggrgated dimensions) information
best = SelectBestDimension(pointTree.Leaves, anomalyTree.Leaves, aggDims);
}
else
{
//has no leaves information, should calculate the entropy information according to the children nodes
best = SelectBestDimension(pointTree.ChildrenNodes, anomalyTree.ChildrenNodes, aggDims);
}
if (best == null)
{
return new List<RootCauseItem>() { new RootCauseItem(anomalyDimension) };
}
List<TimeSeriesPoint> children = null;
if (anomalyTree.ChildrenNodes.ContainsKey(best.DimensionKey))
{
//Use children node information to get top anomalies
children = GetTopAnomaly(anomalyTree.ChildrenNodes[best.DimensionKey], anomalyTree.ParentNode, pointTree.ChildrenNodes[best.DimensionKey].Count > 0 ? pointTree.ChildrenNodes[best.DimensionKey] : pointTree.Leaves, best.DimensionKey, !(pointTree.ChildrenNodes[best.DimensionKey].Count > 0));
}
else
{
//Use leaves node informatin to get top anomalies
children = GetTopAnomaly(anomalyTree.Leaves, anomalyTree.ParentNode, pointTree.Leaves, best.DimensionKey, true);
}
if (children == null)
{
//As the cause couldn't be found, the root cause should be itself
return new List<RootCauseItem>() { new RootCauseItem(anomalyDimension) };
}
else
{
List<RootCauseItem> causes = new List<RootCauseItem>();
// For the found causes, we return the result
foreach (TimeSeriesPoint anomaly in children)
{
causes.Add(new RootCauseItem(UpdateDimensionValue(anomalyDimension, best.DimensionKey, anomaly.Dimension[best.DimensionKey]), new List<string>() { best.DimensionKey }));
}
return causes;
}
}
protected double GetEntropy(int totalNum, int anomalyNum)
{
double ratio = (double)anomalyNum / totalNum;
if (ratio == 0 || ratio == 1)
{
return 0;
}
return -(ratio * Log2(ratio) + (1 - ratio) * Log2(1 - ratio));
}
protected List<TimeSeriesPoint> GetTopAnomaly(List<TimeSeriesPoint> anomalyPoints, TimeSeriesPoint root, List<TimeSeriesPoint> totalPoints, string dimKey, bool isLeaveslevel = false)
{
Dictionary<string, int> pointDistribution = new Dictionary<string, int>();
UpdateDistribution(pointDistribution, totalPoints, dimKey);
anomalyPoints = anomalyPoints.OrderBy(x => x.Delta).ToList();
if (root.Delta > 0)
{
anomalyPoints.Reverse();
}
else
{
anomalyPoints = anomalyPoints.FindAll(x => x.Delta < 0);
}
if (anomalyPoints.Count == 1)
{
return anomalyPoints;
}
double delta = 0;
double preDelta = 0;
List<TimeSeriesPoint> causeList = new List<TimeSeriesPoint>();
foreach (TimeSeriesPoint anomaly in anomalyPoints)
{
if (StopAnomalyComparison(delta, root.Delta, anomaly.Delta, preDelta))
{
break;
}
delta += anomaly.Delta;
causeList.Add(anomaly);
preDelta = anomaly.Delta;
}
int pointSize = isLeaveslevel ? pointDistribution.Count : GetTotalNumber(pointDistribution);
if (ShouldSeparateAnomaly(delta, root.Delta, pointSize, causeList.Count))
{
return causeList;
}
return null;
}
/// <summary>
/// Use leaves point information to select best dimension
/// </summary>
protected BestDimension SelectBestDimension(List<TimeSeriesPoint> totalPoints, List<TimeSeriesPoint> anomalyPoints, List<string> aggDim)
{
double totalEntropy = GetEntropy(totalPoints.Count, anomalyPoints.Count);
SortedDictionary<BestDimension, double> entroyGainMap = new SortedDictionary<BestDimension, double>();
Dictionary<BestDimension, double> entroyGainRatioMap = new Dictionary<BestDimension, double>();
double sumGain = 0;
foreach (string dimKey in aggDim)
{
BestDimension dimension = new BestDimension();
dimension.DimensionKey = dimKey;
UpdateDistribution(dimension.PointDis, totalPoints, dimKey);
UpdateDistribution(dimension.AnomalyDis, anomalyPoints, dimKey);
double relativeEntropy = GetDimensionEntropy(dimension.PointDis, dimension.AnomalyDis);
double gain = totalEntropy - relativeEntropy;
if (Double.IsNaN(gain))
{
gain = 0;
}
entroyGainMap.Add(dimension, gain);
double gainRatio = gain / GetDimensionIntrinsicValue(dimension.PointDis);
if (Double.IsInfinity(gainRatio))
{
gainRatio = 0;
}
entroyGainRatioMap.Add(dimension, gainRatio);
sumGain += gain;
}
double meanGain = sumGain / aggDim.Count();
BestDimension best = FindBestDimension(entroyGainMap, entroyGainRatioMap, meanGain);
return best;
}
/// <summary>
/// Use children point information to select best dimension
/// </summary>
private BestDimension SelectBestDimension(Dictionary<string, List<TimeSeriesPoint>> pointChildren, Dictionary<string, List<TimeSeriesPoint>> anomalyChildren, List<string> aggDim)
{
SortedDictionary<BestDimension, double> entropyMap = new SortedDictionary<BestDimension, double>();
Dictionary<BestDimension, double> entropyRatioMap = new Dictionary<BestDimension, double>();
double sumGain = 0;
foreach (string dimKey in aggDim)
{
BestDimension dimension = new BestDimension();
dimension.DimensionKey = dimKey;
if (pointChildren.ContainsKey(dimKey))
{
UpdateDistribution(dimension.PointDis, pointChildren[dimKey], dimKey);
}
if (anomalyChildren.ContainsKey(dimKey))
{
UpdateDistribution(dimension.AnomalyDis, anomalyChildren[dimKey], dimKey);
}
double entropy = GetEntropy(dimension.PointDis.Count, dimension.AnomalyDis.Count);
if (Double.IsNaN(entropy))
{
entropy = Double.MaxValue;
}
entropyMap.Add(dimension, entropy);
double gainRatio = entropy / GetDimensionIntrinsicValue(dimension.PointDis);
if (Double.IsInfinity(gainRatio))
{
gainRatio = 0;
}
entropyRatioMap.Add(dimension, gainRatio);
sumGain += entropy;
}
double meanGain = sumGain / aggDim.Count;
BestDimension best = FindBestDimension(entropyMap, entropyRatioMap, meanGain, false);
return best;
}
private AnomalyDirection GetRootCauseDirection(TimeSeriesPoint rootCausePoint)
{
if (rootCausePoint.ExpectedValue < rootCausePoint.Value)
{
return AnomalyDirection.Up;
}
else if (rootCausePoint.ExpectedValue > rootCausePoint.Value)
{
return AnomalyDirection.Down;
}
else
{
return AnomalyDirection.Same;
}
}
private void GetRootCauseDirectionAndScore(Dictionary<string, TimeSeriesPoint> dimPointMapping, Dictionary<string, Object> anomalyRoot, RootCause dst, double beta, PointTree pointTree, AggregateType aggType, Object aggSymbol)
{
TimeSeriesPoint anomalyPoint = GetPointByDimension(dimPointMapping, anomalyRoot, pointTree, aggType, aggSymbol);
if (dst.Items.Count > 1)
{
//get surprise value and explanatory power value
List<RootCauseScore> scoreList = new List<RootCauseScore>();
foreach (RootCauseItem item in dst.Items)
{
TimeSeriesPoint rootCausePoint = GetPointByDimension(dimPointMapping, item.Dimension, pointTree, aggType, aggSymbol);
if (anomalyPoint != null && rootCausePoint != null)
{
Tuple<double, double> scores = GetSurpriseAndExplanatoryScore(rootCausePoint, anomalyPoint);
scoreList.Add(new RootCauseScore(scores.Item1, scores.Item2));
item.Direction = GetRootCauseDirection(rootCausePoint);
}
}
//get final score
for (int i = 0; i < scoreList.Count; i++)
{
if (aggType.Equals(AggregateType.Max) || aggType.Equals(AggregateType.Min))
{
dst.Items[i].Score = 1;
}
else
{
dst.Items[i].Score = GetFinalScore(scoreList[i].Surprise, Math.Abs(scoreList[i].ExplanatoryScore), beta);
}
}
}
else if (dst.Items.Count == 1)
{
TimeSeriesPoint rootCausePoint = GetPointByDimension(dimPointMapping, dst.Items[0].Dimension, pointTree, aggType, aggSymbol);
if (anomalyPoint != null && rootCausePoint != null)
{
Tuple<double, double> scores = GetSurpriseAndExplanatoryScore(rootCausePoint, anomalyPoint);
if (aggType.Equals(AggregateType.Max) || aggType.Equals(AggregateType.Min))
{
dst.Items[0].Score = 1;
}
else
{
dst.Items[0].Score = GetFinalScore(scores.Item1, scores.Item2, beta);
}
dst.Items[0].Direction = GetRootCauseDirection(rootCausePoint);
}
}
}
private TimeSeriesPoint GetPointByDimension(Dictionary<string, TimeSeriesPoint> dimPointMapping, Dictionary<string, Object> dimension, PointTree pointTree, AggregateType aggType, Object aggSymbol)
{
if (dimPointMapping.ContainsKey(GetDicCode(dimension)))
{
return dimPointMapping[GetDicCode(dimension)];
}
int count = 0;
TimeSeriesPoint p = new TimeSeriesPoint(dimension);
DimensionInfo dimensionInfo = SeparateDimension(dimension, aggSymbol);
Dictionary<string, Object> subDim = GetSubDim(dimension, dimensionInfo.DetailDims);
foreach (TimeSeriesPoint leave in pointTree.Leaves)
{
if (ContainsAll(leave.Dimension, subDim))
{
count++;
p.Value = +leave.Value;
p.ExpectedValue = +leave.ExpectedValue;
p.Delta = +leave.Delta;
}
}
if (aggType.Equals(AggregateType.Avg))
{
p.Value = p.Value / count;
p.ExpectedValue = p.ExpectedValue / count;
p.Delta = p.Delta / count;
}
if (count > 0)
{
return p;
}
else
{
return null;
}
}
private static string GetDicCode(Dictionary<string, Object> dic)
{
return string.Join(";", dic.Select(x => x.Key + "=" + (string)x.Value).ToArray());
}
private void BuildTree(PointTree tree, List<string> aggDims, TimeSeriesPoint point, Object aggSymbol)
{
int aggNum = 0;
string nextDim = null;
foreach (string dim in aggDims)
{
if (IsAggregationDimension(point.Dimension[dim], aggSymbol))
{
aggNum++;
}
else
{
nextDim = dim;
}
}
if (aggNum == aggDims.Count)
{
tree.ParentNode = point;
}
else if (aggNum == aggDims.Count - 1)
{
if (!tree.ChildrenNodes.ContainsKey(nextDim))
{
tree.ChildrenNodes.Add(nextDim, new List<TimeSeriesPoint>());
}
tree.ChildrenNodes[nextDim].Add(point);
}
if (aggNum == 0)
{
tree.Leaves.Add(point);
}
}
private BestDimension FindBestDimension(SortedDictionary<BestDimension, double> valueMap, Dictionary<BestDimension, double> valueRatioMap, double meanGain, bool isLeavesLevel = true)
{
BestDimension best = null;
foreach (KeyValuePair<BestDimension, double> dimension in valueMap)
{
if (dimension.Key.AnomalyDis.Count == 1 || (isLeavesLevel ? dimension.Value >= meanGain : dimension.Value <= meanGain))
{
if (best == null)
{
best = dimension.Key;
}
else
{
bool isRatioNan = Double.IsNaN(valueRatioMap[best]);
if (dimension.Key.AnomalyDis.Count > 1)
{
if (!isRatioNan && (best.AnomalyDis.Count != 1 && (isLeavesLevel ? valueRatioMap[best].CompareTo(dimension.Value) <= 0 : valueRatioMap[best].CompareTo(dimension.Value) >= 0)))
{
best = dimension.Key;
}
}
else
{
if (best.AnomalyDis.Count > 1)
{
best = dimension.Key;
}
else
{
if (!isRatioNan && (isLeavesLevel ? valueRatioMap[best].CompareTo(dimension.Value) <= 0 : valueRatioMap[best].CompareTo(dimension.Value) >= 0))
{
best = dimension.Key;
}
}
}
}
}
}
return best;
}
/// <summary>
/// Calculate the surprise score according to root cause point and anomaly point
/// </summary>
/// <param name="rootCausePoint">A point which has been detected as root cause</param>
/// <param name="anomalyPoint">The anomaly point</param>
/// <remarks>
/// <format type="text/markdown">
/// [!include[io](~/../docs/samples/docs/api-reference/time-series-root-cause-surprise-score.md)]
/// </format>
/// </remarks>
/// <returns>Surprise score</returns>
private double GetSurpriseScore(TimeSeriesPoint rootCausePoint, TimeSeriesPoint anomalyPoint)
{
double p;
double q;
if (anomalyPoint.ExpectedValue == 0)
{
p = 0;
}
else
{
p = rootCausePoint.ExpectedValue / anomalyPoint.ExpectedValue;
}
if (anomalyPoint.Value == 0)
{
q = 0;
}
else
{
q = rootCausePoint.Value / anomalyPoint.Value;
}
double surprise = 0;
if (p == 0)
{
surprise = 0.5 * (q * Log2(2 * q / (p + q)));
}
else if (q == 0)
{
surprise = 0.5 * (p * Log2(2 * p / (p + q)));
}
else
{
surprise = 0.5 * (p * Log2(2 * p / (p + q)) + q * Log2(2 * q / (p + q)));
}
return surprise;
}
private double GetFinalScore(double surprise, double ep, double beta)
{
double a = 0;
double b = 0;
if (surprise == 0)
{
a = 0;
}
if (ep == 0)
{
b = 0;
}
else
{
a = (1 - Math.Pow(2, -surprise));
b = (1 - Math.Pow(2, -ep));
}
return beta * a + (1 - beta) * b;
}
private Tuple<double, double> GetSurpriseAndExplanatoryScore(TimeSeriesPoint rootCausePoint, TimeSeriesPoint anomalyPoint)
{
double surprise = GetSurpriseScore(rootCausePoint, anomalyPoint);
double ep = anomalyPoint.Value - anomalyPoint.ExpectedValue == 0 ? 0 : Math.Abs((rootCausePoint.Value - rootCausePoint.ExpectedValue) / (anomalyPoint.Value - anomalyPoint.ExpectedValue));
return new Tuple<double, double>(surprise, ep);
}
private static Dictionary<string, Object> UpdateDimensionValue(Dictionary<string, Object> dimension, string key, Object value)
{
Dictionary<string, Object> newDim = new Dictionary<string, Object>(dimension);
newDim[key] = value;
return newDim;
}
private bool StopAnomalyComparison(double preTotal, double parent, double current, double pre)
{
if (Math.Abs(preTotal) < Math.Abs(parent) * _anomalyDeltaThreshold)
{
return false;
}
return Math.Abs(pre) / Math.Abs(current) > _anomalyPreDeltaThreshold;
}
private bool ShouldSeparateAnomaly(double total, double parent, int totalSize, int size)
{
if (Math.Abs(total) < Math.Abs(parent) * _anomalyDeltaThreshold)
{
return false;
}
if (size == totalSize && size == 1)
{
return true;
}
return size <= totalSize * _anomalyRatioThreshold;
}
private double GetDimensionEntropy(Dictionary<string, int> pointDis, Dictionary<string, int> anomalyDis)
{
int total = GetTotalNumber(pointDis);
double entropy = 0;
foreach (string key in anomalyDis.Keys)
{
double dimEntropy = GetEntropy(pointDis[key], anomalyDis[key]);
entropy += dimEntropy * pointDis[key] / total;
}
return entropy;
}
private double GetDimensionIntrinsicValue(Dictionary<string, int> pointDis)
{
int total = GetTotalNumber(pointDis);
double intrinsicValue = 0;
foreach (string key in pointDis.Keys)
{
intrinsicValue -= Log2((double)pointDis[key] / total) * (double)pointDis[key] / total;
}
return intrinsicValue;
}
private int GetTotalNumber(Dictionary<string, int> distribution)
{
int total = 0;
foreach (int num in distribution.Values)
{
total += num;
}
return total;
}
private void UpdateDistribution(Dictionary<string, int> distribution, List<TimeSeriesPoint> points, string dimKey)
{
foreach (TimeSeriesPoint point in points)
{
string dimVal = (string)point.Dimension[dimKey];
if (!distribution.ContainsKey(dimVal))
{
distribution.Add(dimVal, 0);
}
distribution[dimVal] = distribution[dimVal] + 1;
}
}
[MethodImplAttribute(MethodImplOptions.AggressiveInlining)]
private double Log2(double val) => Double.IsNaN(val) ? 0 : Math.Log(val) / Math.Log(2);
private static bool ContainsAll(Dictionary<string, Object> bigDictionary, Dictionary<string, Object> smallDictionary)
{
foreach (var item in smallDictionary)
{
if (!bigDictionary.ContainsKey(item.Key) || !bigDictionary[item.Key].Equals(smallDictionary[item.Key]))
{
return false;
}
}
return true;
}
private bool IsAggregationDimension(Object val, Object aggSymbol)
{
return val.Equals(aggSymbol);
}
}
internal class DimensionInfo
{
internal List<string> DetailDims { get; set; }
internal List<string> AggDims { get; set; }
public DimensionInfo()
{
DetailDims = new List<string>();
AggDims = new List<string>();
}
}
internal class PointTree
{
internal TimeSeriesPoint ParentNode;
internal Dictionary<string, List<TimeSeriesPoint>> ChildrenNodes;
internal List<TimeSeriesPoint> Leaves;
public PointTree()
{
Leaves = new List<TimeSeriesPoint>();
ChildrenNodes = new Dictionary<string, List<TimeSeriesPoint>>();
}
}
public class BestDimension : IComparable
{
internal string DimensionKey;
internal Dictionary<string, int> AnomalyDis;
internal Dictionary<string, int> PointDis;
public BestDimension()
{
AnomalyDis = new Dictionary<string, int>();
PointDis = new Dictionary<string, int>();
}
public int CompareTo(object obj)
{
if (obj == null) return 1;
BestDimension other = obj as BestDimension;
if (other != null)
return DimensionKey.CompareTo(other.DimensionKey);
else
throw new ArgumentException("Object is not a BestDimension");
}
}
internal class RootCauseScore
{
internal double Surprise;
internal double ExplanatoryScore;
public RootCauseScore(double surprise, double explanatoryScore)
{
Surprise = surprise;
ExplanatoryScore = explanatoryScore;
}
}
}