-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathRule.cs
More file actions
500 lines (424 loc) · 12.5 KB
/
Copy pathRule.cs
File metadata and controls
500 lines (424 loc) · 12.5 KB
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
//-----------------------------------------------------------------------
// This file is part of the Microsoft Code Samples.
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
// This source code is intended only as a supplement to Microsoft
// Development Tools and/or on-line documentation. See these other
// materials for detailed information regarding Microsoft code samples.
//
// THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace Microsoft.AnalysisServices.Samples.ActivityViewer
{
public enum RuleOperator
{
lt,
lte,
eq,
gte,
gt
}
#region conditions
public abstract class Condition
{
public static bool ApplyOperator(float lhs, RuleOperator op, float rhs)
{
switch (op)
{
case RuleOperator.lt:
return lhs < rhs;
case RuleOperator.lte:
return lhs <= rhs;
case RuleOperator.gt:
return lhs > rhs;
case RuleOperator.gte:
return lhs >= rhs;
default:
return lhs == rhs;
}
}
public static string OperatorToString(RuleOperator op)
{
switch (op)
{
case RuleOperator.lt:
return "<";
case RuleOperator.lte:
return "<=";
case RuleOperator.gt:
return ">";
case RuleOperator.gte:
return ">=";
default:
return "=";
}
}
public abstract bool Verify(DataRow row);
public abstract override string ToString();
}
public class ColumnCondition : Condition
{
internal RuleOperator op;
internal float arg;
internal string colName;
public ColumnCondition(string colName, RuleOperator op, float arg)
{
this.op = op;
this.arg = arg;
this.colName = colName;
}
public override bool Verify(DataRow row)
{
try
{
return ApplyOperator(float.Parse(row[colName].ToString()), op, arg);
}
catch
{
return false;
}
}
public override string ToString()
{
return colName + " " + OperatorToString(op) + " " + arg.ToString();
}
}
public class MatchColInSetCondition : Condition
{
internal string[] set;
internal string colName;
public MatchColInSetCondition(string colName, string[] set)
{
this.colName = colName;
this.set = set;
}
public override bool Verify(DataRow row)
{
if (row == null)
return true;
try
{
return set.Contains(row[colName]);
}
catch
{
return false;
}
}
public override string ToString()
{
string constring = colName + " IN { ";
foreach (string s in set)
constring += s + " ";
return constring + "}";
}
}
public class TotalCondition : Condition
{
private Dictionary<string, float> lookup;
private string userCol;
private string totalCol;
private RuleOperator op;
private float arg;
public TotalCondition(string userCol, string totalCol, RuleOperator op, float arg)
{
this.userCol = userCol;
this.totalCol = totalCol;
this.op = op;
this.arg = arg;
this.lookup = new Dictionary<string, float>();
}
public override bool Verify(DataRow row)
{
if (row == null)
{
lookup.Clear();
return false;
}
string name = (string)row[userCol];
if (lookup.ContainsKey(name)){
float tmp = float.Parse(row[totalCol].ToString());
lookup[name] += tmp;
}
else
lookup.Add(name, 0);
return ApplyOperator(lookup[name], op, arg);
}
public override string ToString()
{
return "USER-TOTAL " + totalCol + " " + OperatorToString(op) + " " + arg.ToString();
}
}
public class OrCondition : Condition
{
Condition con1;
Condition con2;
public OrCondition(Condition a, Condition b)
{
con1 = a;
con2 = b;
}
public override bool Verify(DataRow row)
{
return (con1.Verify(row) || con2.Verify(row));
}
public override string ToString()
{
return con1.ToString() + " OR " + con2.ToString();
}
}
public class AndCondition : Condition
{
Condition con1;
Condition con2;
public AndCondition(Condition a, Condition b)
{
con1 = a;
con2 = b;
}
public override bool Verify(DataRow row)
{
return (con1.Verify(row) && con2.Verify(row));
}
public override string ToString()
{
return con1.ToString() + " AND " + con2.ToString();
}
}
public class XorCondition : Condition
{
Condition con1;
Condition con2;
public XorCondition(Condition a, Condition b)
{
con1 = a;
con2 = b;
}
public override bool Verify(DataRow row)
{
return (con1.Verify(row) ^ con2.Verify(row));
}
public override string ToString()
{
return con1.ToString() + " XOR " + con2.ToString();
}
}
public class NegCondition : Condition
{
Condition con;
public NegCondition(Condition con)
{
this.con = con;
}
public override bool Verify(DataRow row)
{
return !con.Verify(row);
}
public override string ToString()
{
return "NOT " + con.ToString();
}
}
public class PlaceholderCondition : Condition
{
string placehold;
public PlaceholderCondition(string placehold)
{
this.placehold = placehold;
}
public override bool Verify(DataRow row)
{
return true;
}
public override string ToString()
{
return placehold;
}
}
#endregion conditions
#region actions
public delegate void ActionDel(object arg1, object arg2);
public abstract class Action
{
public abstract void Act(DataRow row, Rule violated);
public abstract override string ToString();
}
public class CancelAction : Action
{
ActionDel kill;
public CancelAction(ActionDel killDel)
{
kill = killDel;
}
public override void Act(DataRow row, Rule violated)
{
kill(row["SESSION_ID"],row["SESSION_SPID"]);
}
public override string ToString()
{
return "KILL";
}
}
public class NotifyAction : Action
{
private string[] emails;
public NotifyAction(string emailList)
{
if (!ParseEmails(emailList))
throw new ArgumentException("One or more emails is not valid");
}
private bool ParseEmails(string emails)
{
char[] breaks = { ',', ';',' ', '{', '}' };
this.emails = emails.Split(breaks,StringSplitOptions.RemoveEmptyEntries);
System.Text.RegularExpressions.Regex r =
new System.Text.RegularExpressions.Regex(@"^[\w-]+@([\w-]+\.)+[\w-]+$");
for (int c = 0; c < emails.Length; c++)
{
this.emails[c] = this.emails[c].Trim();
if(!r.IsMatch(this.emails[c]))
return false;
}
return true;
}
public override void Act(DataRow row, Rule violated)
{
throw new NotImplementedException();
}
public override string ToString()
{
string action = "NOTIFY { ";
foreach (string s in emails)
action += s + " ";
return action+"}";
}
}
public class AlertAction : Action
{
ActionDel addAlert;
public AlertAction(ActionDel addAlert)
{
this.addAlert = addAlert;
}
public override void Act(DataRow row, Rule violated)
{
addAlert(row, violated);
}
public override string ToString()
{
return "ALERT";
}
}
#endregion
public class Rule
{
internal Condition condition;
internal Action[] actions;
internal int priority;
private string myString="";
public Rule(Condition condition, Action[] actions)
{
this.condition = condition;
this.actions = actions;
priority = 1;
}
public virtual void Process(DataRow row)
{
if (condition.Verify(row))
foreach (Action a in actions)
try
{
a.Act(row, this);
}
catch { }
}
public void SetPriority(int newPriority)
{
priority = newPriority;
}
public static string PriorityToString(int priority)
{
switch(priority){
case 0:
return "HIGH";
case 1:
return "MEDIUM";
default:
return "LOW";
}
}
public static int StringToPriority(string priority)
{
switch (priority)
{
case "HIGH":
return 0;
case "MEDIUM":
return 1;
default:
return 2;
}
}
public override string ToString()
{
if (String.IsNullOrEmpty(myString))
{
myString = "IF " + condition.ToString() + " THEN ";
for (int c = 0; c < actions.Length; c++)
myString += actions[c].ToString() + (c != actions.Length - 1 ? " AND " : "");
}
return myString;
}
}
public delegate DataTable QueryToData(string query);
public class WhereRule : Rule
{
internal string dataQuery = "select * from $System.DISCOVER_SESSIONS WHERE ";
private QueryToData getData;
private string myString = "";
private bool canProcess = true;
public WhereRule(QueryToData getData, string whereClause, Action[] actions)
: base(new PlaceholderCondition(whereClause), actions)
{
this.getData = getData;
dataQuery += whereClause;
}
public override void Process(DataRow row)
{ //null resets rule to be processed
//this way rule is processed once per scan, not once per row
if (row == null)
{
canProcess = true;
return;
}
if (canProcess)
{
DataTable theData = getData(dataQuery);
foreach (DataRow datarow in theData.Rows)
foreach (Action action in actions)
if(datarow != null)
action.Act(datarow, this);
canProcess = false;
}
}
public override string ToString()
{
if (String.IsNullOrEmpty(myString))
{
myString = "WHERE " + condition.ToString() + " THEN ";
for (int c = 0; c < actions.Length; c++)
myString += actions[c].ToString() + (c != actions.Length - 1 ? " AND " : "");
}
return myString;
}
}
}