This repository was archived by the owner on Jul 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
118 lines (102 loc) · 3.4 KB
/
Program.cs
File metadata and controls
118 lines (102 loc) · 3.4 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Diagnostics;
using BabyKusto.Core;
using BabyKusto.Core.Evaluation;
using BabyKusto.Core.Extensions;
using BabyKusto.Core.Util;
using Kusto.Language.Symbols;
Console.WriteLine(@"/----------------------------------------------------------------\");
Console.WriteLine(@"| Welcome to BabyKusto.ProcessQuerier. You can write KQL queries |");
Console.WriteLine(@"| and explore the live list of processes on your machine. |");
Console.WriteLine(@"\----------------------------------------------------------------/");
Console.WriteLine();
ShowDemos();
while (true)
{
try
{
PrintCaret();
var query = Console.ReadLine();
if (query == null || query == "exit")
{
return;
}
ExecuteReplQuery(query);
}
catch (Exception ex)
{
var lastColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error:");
Console.WriteLine(ex);
Console.ForegroundColor = lastColor;
}
Console.WriteLine();
}
static void ShowDemos()
{
var demos = new[]
{
(Title: "Example: counting the total number of processes:", Query: @"Processes | count"),
(Title: "Example: Find the process using the most memory:", Query: @"Processes | project name, memMB=workingSet/1024/1024 | order by memMB desc | take 1")
};
foreach (var demo in demos)
{
ShowDemo(demo.Title, demo.Query);
}
}
static void ShowDemo(string title, string query)
{
var lastColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(title);
Console.ForegroundColor = lastColor;
PrintCaret();
Console.WriteLine(query);
ExecuteReplQuery(query);
}
static void ExecuteReplQuery(string query)
{
var processesTable = GetProcessesTable();
var engine = new BabyKustoEngine();
engine.AddGlobalTable("Processes", processesTable);
var result = engine.Evaluate(query, dumpIRTree: false); // Set dumpIRTree = true to see the internal tree representation
Console.WriteLine();
result.Dump(Console.Out);
Console.WriteLine();
}
static InMemoryTableSource GetProcessesTable()
{
var names = new ColumnBuilder<string?>(ScalarTypes.String);
var numThreads = new ColumnBuilder<int?>(ScalarTypes.Int);
var workingSets = new ColumnBuilder<long?>(ScalarTypes.Long);
foreach (var p in Process.GetProcesses())
{
string processName;
int processThreads;
long workingSet;
try
{
processName = p.ProcessName;
processThreads = p.Threads.Count;
workingSet = p.WorkingSet64;
}
catch { continue; }
names.Add(processName);
numThreads.Add(processThreads);
workingSets.Add(workingSet);
}
var myTable = new InMemoryTableSource(
TableSymbol.From("(name:string, numThreads:int, workingSet:long)"),
new Column[] { names.ToColumn(), numThreads.ToColumn(), workingSets.ToColumn() });
return myTable;
}
static void PrintCaret()
{
var lastColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("> ");
Console.ForegroundColor = lastColor;
}