This repository was archived by the owner on Mar 31, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathKustoPartitioningPolicyBuilder.cs
More file actions
139 lines (122 loc) · 5.06 KB
/
KustoPartitioningPolicyBuilder.cs
File metadata and controls
139 lines (122 loc) · 5.06 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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Text.RegularExpressions;
using Microsoft.CodeAnalysis;
namespace NuGet.Insights
{
public class KustoPartitioningPolicyBuilder : IPropertyVisitor
{
private const string AttributeName = "KustoPartitionKeyAttribute";
private readonly int _intent;
private readonly bool _escapeQuotes;
private readonly StringBuilder _builder;
public KustoPartitioningPolicyBuilder(int indent, bool escapeQuotes)
{
_intent = indent;
_escapeQuotes = escapeQuotes;
_builder = new StringBuilder();
}
public void OnProperty(SourceProductionContext context, CsvRecordModel model, CsvPropertyModel property)
{
if (!property.IsKustoPartitionKey)
{
return;
}
if (property.IsKustoIgnore)
{
context.ReportDiagnostic(Diagnostic.Create(
new DiagnosticDescriptor(
id: DiagnosticIds.IgnoredKustoPartitioningKey,
title: "An attribute was marked as both a Kusto partition key and it was ignored.",
messageFormat: "An attribute was marked as both a Kusto partition key and it was ignored.",
Constants.Category,
DiagnosticSeverity.Error,
isEnabledByDefault: true),
property.Locations.FirstOrDefault() ?? Location.None));
return;
}
var policy = new PartitioningPolicy
{
PartitionKeys = new List<PartitionKey>
{
new PartitionKey
{
ColumnName = property.Name,
Kind = "Hash",
Properties = new PartitionKeyProperties
{
Function = "XxHash64",
MaxPartitionCount = 256,
}
}
}
};
var json = JsonSerializer.Serialize(policy, new JsonSerializerOptions { WriteIndented = true });
if (_escapeQuotes)
{
json = json.Replace("\"", "\"\"");
}
var jsonLines = json.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
if (_builder.Length > 0)
{
context.ReportDiagnostic(Diagnostic.Create(
new DiagnosticDescriptor(
id: DiagnosticIds.MultipleKustoPartioningKeys,
title: $"Multiple {AttributeName} attributes were defined on a single type.",
messageFormat: $"Multiple {AttributeName} attributes were defined on a single type.",
Constants.Category,
DiagnosticSeverity.Error,
isEnabledByDefault: true),
property.Locations.FirstOrDefault() ?? Location.None));
return;
}
foreach (var line in jsonLines)
{
if (_builder.Length > 0)
{
_builder.AppendLine();
_builder.Append(' ', _intent);
}
var leadingSpaces = Regex.Match(line, "^ *").Value.Length;
_builder.Append(' ', leadingSpaces);
_builder.Append("'");
_builder.Append(line.Substring(leadingSpaces));
_builder.Append("'");
}
}
public void Finish(SourceProductionContext context, CsvRecordModel model)
{
if (_builder.Length == 0)
{
context.ReportDiagnostic(Diagnostic.Create(
new DiagnosticDescriptor(
id: DiagnosticIds.NoKustoPartitioningKeyDefined,
title: $"No {AttributeName} attributes were defined on a type.",
messageFormat: $"No {AttributeName} attributes were defined on a type.",
Constants.Category,
DiagnosticSeverity.Error,
isEnabledByDefault: true),
model.Locations.FirstOrDefault() ?? Location.None));
}
}
public string GetResult()
{
return _builder.ToString();
}
private class PartitioningPolicy
{
public List<PartitionKey> PartitionKeys { get; set; }
}
private class PartitionKey
{
public string ColumnName { get; set; }
public string Kind { get; set; }
public PartitionKeyProperties Properties { get; set; }
}
private class PartitionKeyProperties
{
public string Function { get; set; }
public int MaxPartitionCount { get; set; }
}
}
}