forked from trimble-oss/dba-dash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXETools.cs
126 lines (116 loc) · 4.99 KB
/
XETools.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
using System;
using System.Data;
using System.Xml.Linq;
namespace DBADash
{
internal class RingBufferTargetAttributes
{
public int Truncated;
public int ProcessingTime;
public int TotalEventsProcessed;
public int EventCount;
public int DroppedCount;
public int MemoryUsed;
public DataTable GetTable()
{
DataTable dt = new();
dt.Columns.Add("Truncated", typeof(int));
dt.Columns.Add("ProcessingTime", typeof(int));
dt.Columns.Add("TotalEventsProcessed", typeof(int));
dt.Columns.Add("EventCount", typeof(int));
dt.Columns.Add("DroppedCount", typeof(int));
dt.Columns.Add("MemoryUsed", typeof(int));
var r = dt.NewRow();
r["Truncated"] = Truncated;
r["ProcessingTime"] = ProcessingTime;
r["TotalEventsProcessed"] = TotalEventsProcessed;
r["EventCount"] = EventCount;
r["DroppedCount"] = DroppedCount;
r["MemoryUsed"] = MemoryUsed;
dt.Rows.Add(r);
return dt;
}
}
internal class XETools
{
private static DataTable GetXELSchema()
{
DataTable dt = new("XEL");
dt.Columns.Add("event_type", typeof(string));
dt.Columns.Add("object_name", typeof(string));
dt.Columns.Add("timestamp", typeof(DateTime));
dt.Columns.Add("duration", typeof(long));
dt.Columns.Add("cpu_time", typeof(long));
dt.Columns.Add("logical_reads", typeof(long));
dt.Columns.Add("physical_reads", typeof(long));
dt.Columns.Add("writes", typeof(long));
dt.Columns.Add("username", typeof(string));
dt.Columns.Add("batch_text", typeof(string));
dt.Columns.Add("statement", typeof(string));
dt.Columns.Add("database_id", typeof(int));
dt.Columns.Add("client_hostname", typeof(string));
dt.Columns.Add("client_app_name", typeof(string));
dt.Columns.Add("result", typeof(string));
dt.Columns.Add("session_id", typeof(int));
dt.Columns.Add("context_info", typeof(byte[]));
return dt;
}
public static DataTable XEStrToDT(string xe, out RingBufferTargetAttributes ringBufferAtt)
{
var dt = GetXELSchema();
var el = XElement.Parse(xe);
ringBufferAtt = new RingBufferTargetAttributes();
if (int.TryParse(el.Attribute("truncated")?.Value, out var truncatedValue))
{
ringBufferAtt.Truncated = truncatedValue;
}
ringBufferAtt.DroppedCount = int.TryParse(el.Attribute("droppedCount")?.Value, out var droppedCountValue)
? droppedCountValue
: 0;
ringBufferAtt.ProcessingTime =
int.TryParse(el.Attribute("processingTime")?.Value, out var processingTimeValue)
? processingTimeValue
: 0;
ringBufferAtt.EventCount = int.TryParse(el.Attribute("eventCount")?.Value, out var eventCountValue)
? eventCountValue
: 0;
ringBufferAtt.MemoryUsed = int.TryParse(el.Attribute("memoryUsed")?.Value, out var memoryUsedValue)
? memoryUsedValue
: 0;
ringBufferAtt.TotalEventsProcessed =
int.TryParse(el.Attribute("totalEventsProcessed")?.Value, out var totalEventsProcessedValue)
? totalEventsProcessedValue
: 0;
foreach (var evt in el.Elements("event"))
{
var r = dt.Rows.Add();
r["event_type"] = evt.Attribute("name")?.Value!;
var timestamp = evt.Attribute("timestamp")?.Value;
if (timestamp == null) continue;
r["timestamp"] = DateTime.Parse(timestamp).ToUniversalTime();
ProcessEventElements(evt, r, "data");
ProcessEventElements(evt, r, "action");
}
return dt;
}
private static void ProcessEventElements(XContainer evt, DataRow row, string elementType)
{
foreach (var data in evt.Elements(elementType))
{
var name = data.Attribute("name")?.Value;
if (name != null && row.Table.Columns.Contains(name))
{
row[name] = elementType switch
{
"action" when name == "context_info" => Convert.FromHexString(data.Element("value")?.Value ??
string.Empty),
"data" when name == "result" => data.Element("value")?.Value + " - " + data.Element("text")?.Value,
"data" => data.Element("value")?.Value ?? string.Empty,
"action" => data.Value,
_ => row[name]
};
}
}
}
}
}