/
polyglot telemetry.dib
588 lines (502 loc) · 18.7 KB
/
polyglot telemetry.dib
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
#!meta
{"kernelInfo":{"defaultKernelName":"csharp","items":[{"aliases":["C#","c#"],"languageName":"C#","name":"csharp"},{"aliases":["F#","f#"],"languageName":"F#","name":"fsharp"},{"aliases":[],"languageName":"HTML","name":"html"},{"aliases":["js"],"languageName":"JavaScript","name":"javascript"},{"aliases":[],"languageName":"KQL","name":"kql"},{"aliases":[],"languageName":"KQL","name":"kql-telemetry"},{"aliases":[],"languageName":"Mermaid","name":"mermaid"},{"aliases":["powershell"],"languageName":"PowerShell","name":"pwsh"},{"aliases":[],"name":"value"}]}}
#!csharp
#r "nuget: Microsoft.DotNet.Interactive.Kql, *-*"
#!csharp
using System.Security.Cryptography;
using System.Text;
string ToSha256Hash(string text)
{
var sha256 = SHA256.Create();
return HashInFormat(sha256, text);
}
string ToSha256HashWithNormalizedCasing(string text)
{
return ToSha256Hash(text.ToUpperInvariant());
}
string HashInFormat(SHA256 sha256, string text)
{
byte[] bytes = Encoding.UTF8.GetBytes(text);
byte[] hash = sha256.ComputeHash(bytes);
StringBuilder hashString = new StringBuilder();
foreach (byte x in hash)
{
hashString.AppendFormat("{0:x2}", x);
}
return hashString.ToString();
}
#!kql
#!connect kql --kernel-name telemetry --cluster "https://dotnettel.kusto.windows.net" --database "trydotnetcli"
#!kql
#!kql-telemetry --name langauges
let languageMapping = dynamic(
{
"040228846ead4a4195145fe089343cb0894d00a9380176a41a8f6c5ee70b4824":"C#",
"5a18c3116772f98a0ace349c5eb62990ca66989fd801f2e9e8c38443e5eff6aa":"F#",
"07239dbd2a1a1dd793be9062a205eb6be88c36af3fe7e4d6426aea45aa253815":"HTML",
"b27ad06d123c6145fba7e9217a4cfbf2488b4743810ed8c8ff14ad01a5bbe515":"JavaScript",
"55947b6cce205ad35af8e90940a0a3f4517a57311590ca38aa161cb5146faa52":"PowerShell",
"2d2eef3e092af52b5837160fe2e3954907a4bfed1469edd2e6422749a6b16b7a":"SQLite",
"78d962797297852b12ac46b6957e4143a332b4b1aa22c7310fcb726eabec6754":"KQL",
"a7056a455639d1c7deec82ee787db24a0c1878e2792b4597709f0facf7cc7b35":"SQL",
"b27ba710997adaf6099374edb801d44bef36c92e6d37e0190c7c1d12d4b32209":"T-SQL",
"1356a57a7a4b2869bf5e612c3d1761f1d26180b0577aec9267c964907b014d47":"Mermaid",
"8c2574892063f995fdf756bce07f46c1a5193e54cd52837ed91e32008ccf41ac":"R",
"18885f27b5af9012df19e496460f9294d5ab76128824c6f993787004f6d9a7db":"Python",
"11a4a60b518bf24989d481468076e5d5982884626aed9faeb35b8576fcd223e1":"Python",
"454349e422f05297191ead13e21d3db520e5abef52055e4964b82fb213f593a1":"R",
"d277670919a94ba361be1887d39852c3f31d7eed817343cbb70fcd8910841f86":"julia",
"eca37636c0711d96f605f252e43a4f3d0811a2ce7495c004b43eea2a9f7216e8":"Perl",
"f0c929a9e723bc62724e30c7e396e576019dfcb8cfd0a3f264ee5d72e64e49d1":"Perl",
"99f9df7b726d831976eb87646f4f89cc019dc122a2d4ddcd1f838f1dbfb340a2":"lisp",
"300264028c161bff5835271073e5d5dd5c77a20e74ec800a0eb3117e33b985a8":"SysML",
"f43b8a78baae3439cc492cbe037fab51052149b029a900a8b4568d2b09f83fbd":"SysML",
"8ae1251edbc3535c55721fcca5db6361375e90bd2d16c4abfe75aafd60188470":"SysML",
"f911b3e79594cd5ec195df509650912fb1a0ee9327f49d9c397847b00602c96f":"dotLang"
});
let mapKernel = (sha:string) {
let mapped = tostring(languageMapping[sha]);
case(mapped != "", mapped, sha )
};
RawEventsCustomEvents
| where EventName contains "dotnet/interactive/cli/codesubmitted"
| where tostring(Properties["kernellanguagename"]) != "" and tostring(Properties["kernelsessionid"]) != ""
| project EventTimestamp, LanguageName = mapKernel(tostring(Properties["kernellanguagename"])), KernelSessionId = tostring(Properties["kernelsessionid"])
| where LanguageName != ""
| summarize CommandCount = count(), LanguageCount = array_length(make_set(LanguageName)) , KernelLanguages = array_strcat( array_sort_asc( make_set(LanguageName) ), ":")by bin(EventTimestamp, 1d), KernelSessionId
| where CommandCount > 1
| summarize LangaugeSetOccurences = count(), LanguageSetSize = max(LanguageCount) by bin(EventTimestamp, 1d), KernelLanguages
| order by LanguageSetSize
#!csharp
#!share --from kql-telemetry langauges
#!csharp
public class polyglotNotebook{
public string LanguageSet {get;set;}
public int Popularity {get;set;}
public double Temperature {get;set;}
public string[] LanguageNames {get;set;}
public DateTime LastUsed {get;set;}
public DateTime FirstUsed {get;set;}
}
#!csharp
var notebooks = new List<polyglotNotebook>();
var dataPoints = langauges[0].Data.GroupBy(d => (DateTime)d.First(f => (f.Key == "EventTimestamp")).Value).ToDictionary(e => e.Key, e => e.ToList());
var minDate = dataPoints.Keys.Min();
var maxDate = dataPoints.Keys.Max();
foreach(var day in Enumerable.Range(0, (maxDate - minDate).Days + 1).Select(m => minDate.AddDays(m))){
if(dataPoints.TryGetValue(day, out var data)){
var today = new HashSet<string>();
foreach(var lang in data){
var kernelLanguageNames = (lang.First(f => f.Key == "KernelLanguages").Value as string);
today.Add(kernelLanguageNames);
var polyglotNotebook = notebooks.FirstOrDefault(pn => pn.LanguageSet == kernelLanguageNames);
if(polyglotNotebook is null){
polyglotNotebook = new polyglotNotebook();
polyglotNotebook.LanguageSet = kernelLanguageNames;
polyglotNotebook.Popularity = 1;
polyglotNotebook.Temperature = 1.0;
polyglotNotebook.LanguageNames = kernelLanguageNames.Split(":");
polyglotNotebook.FirstUsed = day;
polyglotNotebook.LastUsed = day;
notebooks.Add(polyglotNotebook);
}
else{
polyglotNotebook.LastUsed = day;
polyglotNotebook.Popularity++;
polyglotNotebook.Temperature *= 1.1;
}
var languageNames = kernelLanguageNames.Split(":");
var langaugeSetOccurences = (long)lang.First(f => f.Key == "LangaugeSetOccurences").Value;
}
foreach(var notebook in notebooks){
if(!today.Contains(notebook.LanguageSet)){
notebook.Temperature *= 0.7;
}
}
}else{
foreach(var polyglotNotebook in notebooks){
polyglotNotebook.Temperature *= 0.7;
}
}
}
#!csharp
notebooks
#!csharp
public class Edge{
public long weight {get;set;}
public double temperature {get;set;}
public int source {get;set;}
public int destination {get;set;}
}
public class Node{
public string language {get;set;}
public long popularity {get;set;}
}
public class Graph{
public List<Edge> edges {get;} = new List<Edge>();
public List<Node> nodes {get;} = new List<Node>();
}
#!csharp
var nodelookup = new Dictionary<string, Node>();
var graph = new Graph();
foreach(var notebook in notebooks){
var languageNames = notebook.LanguageNames;
if(languageNames.Length > 1)
{
for(var i = 0; i < languageNames.Length; i++){
var languageName = languageNames[i];
if(!nodelookup.ContainsKey(languageName)){
var node = new Node(){
language = languageName,
popularity = notebook.Popularity
};
nodelookup.Add(languageName, node);
graph.nodes.Add(node);
}else
{
nodelookup[languageName].popularity += notebook.Popularity;
}
}
for(var i = 0; i < languageNames.Length; i++){
for(var j = i+1; j < languageNames.Length; j++){
var languageName = languageNames[i];
var otherLanguageName = languageNames[j];
if(languageName != otherLanguageName){
var source = graph.nodes.IndexOf(nodelookup[languageName]);
var destination = graph.nodes.IndexOf(nodelookup[otherLanguageName]);
var edge = graph.edges.FirstOrDefault(e => e.source ==source && e.destination == destination);
if(edge == null){
edge = new Edge(){
source = source,
destination = destination,
weight = notebook.Popularity,
temperature = notebook.Temperature
};
graph.edges.Add(edge);
}else{
edge.weight += notebook.Popularity;
edge.temperature += notebook.Temperature;
}
}
}
}
}
else{
var languageName = languageNames[0] + " (single language)";
if(!nodelookup.ContainsKey(languageName)){
var node = new Node(){
language = languageName,
popularity = notebook.Popularity
};
nodelookup.Add(languageName, node);
graph.nodes.Add(node);
}else
{
nodelookup[languageName].popularity += notebook.Popularity;
}
}
}
#!csharp
graph.nodes.Display();
graph.edges.Display();
#!javascript
#!share --from csharp graph
#!javascript
loadd3 = (require.config({
paths: {
d3: 'https://cdn.jsdelivr.net/npm/d3@7.4.4/dist/d3.min'
}
}) || require);
delay = (delayAmount) => new Promise(resolve => setTimeout(resolve, delayAmount));
#!html
<svg id="forceLayout"></svg>
#!javascript
await delay(500);
loadd3(["d3"], (d3) => {
const width = 2048;
const height = 2048;
const maxR = 50;
const maxStrength = d3.max(graph.edges, (e) => e.temperature);
const minStrength = d3.min(graph.edges, (e) => e.temperature);
const maxPopularity = d3.max(graph.nodes, (n) => n.popularity);
const minPopularity = d3.min(graph.nodes, (n) => n.popularity);
const scale = d3.scaleSqrt()
.domain([0, 1])
.range([0, 1]);
const tempScale = d3.scaleLog()
.domain([minStrength, maxStrength])
.range([0, 1]);
const nodes = d3.map(graph.nodes, (n, i) => ({id: i, language: n.language, popularity: n.popularity/maxPopularity}));
const links = d3.map(graph.edges, (e, i) => ({source: e.source, target: e.destination, strength: e.temperature/maxStrength, weight: e.weight, id: i}));
const color = d3.scaleOrdinal(d3.map(graph.nodes, (n, i) => n.language), d3.schemeTableau10);
nodes.forEach((n,i)=>{
if(n.language.includes(`single`)){
n.x = 0;
}else{
n.x = width;
}
n.y = i + (height / nodes.lenght);
});
const collisionScale = 2.5;
const svg = d3.select("#forceLayout")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height])
.attr("style", "max-width: 100%; height: auto; height: intrinsic; ");
const notToTruncate = new Set();
notToTruncate.add("C#");
notToTruncate.add("F#");
notToTruncate.add("HTML");
notToTruncate.add("JavaScript");
notToTruncate.add("PowerShell");
notToTruncate.add("SQLite");
notToTruncate.add("KQL");
notToTruncate.add("SQL");
notToTruncate.add("T-SQL");
notToTruncate.add("Mermaid");
notToTruncate.add("R");
notToTruncate.add("Python");
notToTruncate.add("Python");
notToTruncate.add("R");
notToTruncate.add("julia");
notToTruncate.add("SysML");
notToTruncate.add("dotLang");
function linkArc(d) {
const r = Math.hypot(d.target.x - d.source.x, d.target.y - d.source.y);
return `
M${d.source.x},${d.source.y}
A${r},${r} 0 0,1 ${d.target.x},${d.target.y}
`;
}
let quadtree = d3.quadtree()
.x(function(d) {return d.x;})
.y(function(d) {return d.y;});
function dragStuff(s) {
function dragstarted(event) {
if (!event.active) s.alphaTarget(0.3).restart();
event.subject.fx = event.subject.x;
event.subject.fy = event.subject.y;
}
function dragged(event) {
event.subject.fx = event.x;
event.subject.fy = event.y;
}
function dragended(event) {
if (!event.active) s.alphaTarget(0);
event.subject.fx = null;
event.subject.fy = null;
}
return d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
}
svg.selectAll("*").remove();
link = svg
.selectAll(".link")
.data(links)
.join("path")
.attr("id", d => d.id)
.attr("fill", "none")
.attr("stroke", d => color(nodes[d.source].language))
.attr("stroke-width", (d) => 3 + tempScale(d.strength) * 5)
.classed("link", true);
node = svg
.selectAll("g")
.data(nodes)
.join("g")
.classed("node_group", true);
linkLabels = svg
.selectAll(".linkLabel")
.data(links)
.join("text")
.append("textPath")
.attr("font-family", "Arial, Helvetica, sans-serif")
.attr("dominant-baseline","Hanging")
.style("font", "normal 16px Arial")
.text(d => "")
.classed("linkLabel",true);
function tick() {
link.attr("d", linkArc);
node.attr("transform", d => `translate(${d.x},${d.y})`);
linkLabels.attr("xlink:href", d => `#${d.id}`) //place the ID of the path here
.style("text-anchor","middle") //place the text halfway on the arc
.attr("startOffset", "50%")
.attr("fill", "white");
quadtree.addAll(nodes);
}
// Custom force to put all nodes in a box
function boxingForce() {
for (let node of nodes) {
if(node.x < 0 || node.x > width) {
node.vx = -node.vx/2;
}
if(node.y < 0 || node.y > height) {
node.vy = -node.vy/2;
}
// Of the positions exceed the box, set them to the boundary position.
// You may want to include your nodes width to not overlap with the box.
//node.x = Math.max(-x, Math.min(x, node.x));
//node.y = Math.max(-x, Math.min(x, node.y));
}
}
function truncateText(text, length) {
var l = text.split(" ");
if(notToTruncate.has(l[0])) {
return text;
}
if (text.length > length) {
return text.substring(0, length) + "...";
} else {
return text;
}
}
simulation = d3
.forceSimulation()
.nodes(nodes)
.force('collision', d3.forceCollide().radius(d => scale( d.popularity ) * maxR * collisionScale))
.force("charge", d3.forceManyBody().strength(d => scale( d.popularity ) * -200))
.force("center", d3.forceCenter(width / 2, height / 2))
.force("link", d3.forceLink(links).strength ( (d) => tempScale(d.strength) * 0.01))
.force("bounds", boxingForce)
.on("tick", tick);
node.append("circle")
.attr("fill", d => color(d.language))
.attr("stroke", "#666")
.attr("stroke-width", 1.5)
.attr("r", d => 5 + scale( d.popularity ) * maxR)
.classed("nodecircle", true);
//.call(dragStuff(simulation));
node.append("text")
.attr("x", d=> 8 +scale( d.popularity ) * maxR)
.attr("y", "0.31em")
.text(d => truncateText( d.language, 10 ))
.attr("fill", d => color(d.language))
.classed("nodeText", true)
.raise ()
.clone(true).lower()
.attr("fill", "none")
.attr("stroke", "black")
.attr("stroke-width", 3)
.clone(true).lower()
.attr("fill", "none")
.attr("stroke", "rgba(255, 255, 255, 0.2)")
.attr("stroke-width", 4);
svg.on('mousemove', (e) => handleMousemove(e, quadtree));
function handleMousemove(e,q) {
let pos = d3.pointer(e, svg.node());
let d = q.find(pos[0], pos[1], 20);
hoveredId = d ? d.id : undefined;
let selectedNode = undefined;
let colorOverride = undefined;
svg
.selectAll(".nodecircle")
.attr("fill", d => {
let c = color(d.language);
if(d.id === hoveredId) {
selectedNode = d;
c = d3.color(c).brighter();
colorOverride = d3.color(c).brighter(0.5);
}
return c;
});
const network = new Set();
if(hoveredId){
network.add(hoveredId);
}
svg
.selectAll(".link")
.attr("stroke", d => {
let node = d.source;
let c = color(node.language);
if(selectedNode){
const found = d.source.id === selectedNode.id || d.target.id === selectedNode.id;
if(found){
c = colorOverride;
network.add(d.source.id);
network.add(d.target.id);
}
}
return c;
});
svg
.selectAll(".nodecircle")
.attr("stroke", d => {
let c = "#666";
if(network.has(d.id)) {
c = colorOverride;
}
return c;
})
.attr("stroke-width", d => {
let c = 1.5;
if(network.has(d.id)) {
c = 4;
}
return c;
});
svg
.selectAll(".link")
.each(function (d) {
if(selectedNode){
const found = d.source.id === selectedNode.id || d.target.id === selectedNode.id;
if(found){
d3.select(this).attr("opacity",1).raise();
}else{
d3.select(this).attr("opacity",0.2);
}
}else{
d3.select(this).attr("opacity",1);
}
});
svg
.selectAll(".node_group")
.each(function (d) {
if(selectedNode){
if(network.has(d.id)) {
d3.select(this).attr("opacity",1).raise();
d3.select(this).selectAll("text").raise();
}else{
d3.select(this).attr("opacity",0.2);
}
}else{
d3.select(this).attr("opacity",1);
}
});
svg
.selectAll(".nodecircle")
.each(function (d) {
if(network.has(d.id)) {
d3.select(this).raise();
}else{
d3.select(this).lower();
}
});
svg
.selectAll(".nodeText")
.each(function (d) {
if(network.has(d.id)) {
d3.select(this).raise();
}else{
d3.select(this);
}
});
svg
.selectAll(".linkLabel")
.data(links)
.each(function (d) {
let node = d.source;
let c = color(node.language);
if(selectedNode){
const found = d.source.id === selectedNode.id || d.target.id === selectedNode.id;
if(found){
d3.select(this).text(d => d.weight);
}else{
d3.select(this).text("");
}
}else{
d3.select(this).text("");
}
});
}
});