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 pathConstants.cs
More file actions
230 lines (183 loc) · 7.97 KB
/
Constants.cs
File metadata and controls
230 lines (183 loc) · 7.97 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
// 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.
namespace NuGet.Insights
{
internal class Constants
{
public static readonly string Category = "NuGet.Insights.SourceGenerator";
public static readonly string KustoCsvMappingName = "BlobStorageMapping";
public static readonly string AutoGeneratedHeader =
"""
// <auto-generated />
// 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.
""";
public static readonly string CsvRecordTemplate = AutoGeneratedHeader +
"""
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using NuGet.Insights;
namespace {0}
{{
/* Kusto DDL:
.drop table {1} ifexists;
.create table {1} (
{2}
) with (docstring = "See https://github.com/NuGet/Insights/blob/main/docs/tables/{1}.md", folder = "");
.alter-merge table {1} policy retention softdelete = 30d;
.alter table {1} policy partitioning {3};
.create table {1} ingestion csv mapping '{13}'
'['
{4}
']'
*/
partial {5} {6}
{{
public static int FieldCount => {7};
public static void WriteHeader(TextWriter writer)
{{
{8}
}}
public void Write(List<string> fields)
{{
{9}
}}
public void Write(TextWriter writer)
{{
{10}
}}
public async Task WriteAsync(TextWriter writer)
{{
{11}
}}
public static {6} ReadNew(Func<string> getNextField)
{{
return new {6}
{{
{12}
}};
}}
public void SetEmptyStrings()
{{
{14}
}}{15}
}}
}}
""";
public static readonly string MessagePackTemplate = AutoGeneratedHeader +
"""
using MessagePack;
using MessagePack.Formatters;
using NuGet.Insights;
namespace {0}
{{
partial {1} {2}
{{
public class {2}MessagePackFormatter : IMessagePackFormatter<{2}>
{{
public void Serialize(ref MessagePackWriter writer, {2} value, MessagePackSerializerOptions options)
{{
if (value is null)
{{
writer.WriteNil();
return;
}}
writer.WriteArrayHeader({3});
{4}
}}
public {2} Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
{{
var count = reader.ReadArrayHeader();
if (count != {3})
{{
throw new MessagePackSerializationException($"Invalid array length: {{count}}");
}}
var record = new {2}();
{5}
return record;
}}
}}
}}
}}
""";
public static readonly string KustoDDLMainTemplate = AutoGeneratedHeader +
"""
namespace NuGet.Insights
{{
public static partial class {0}
{{
private static Dictionary<Type, string> InternalTypeToDefaultTableName;
private static Dictionary<Type, IReadOnlyList<string>> InternalTypeToDDL;
private static Dictionary<Type, string> InternalTypeToPartitioningPolicy;
public static IReadOnlyDictionary<Type, string> TypeToDefaultTableName => InternalTypeToDefaultTableName;
public static IReadOnlyDictionary<Type, IReadOnlyList<string>> TypeToDDL => InternalTypeToDDL;
public static IReadOnlyDictionary<Type, string> TypeToPartitioningPolicy => InternalTypeToPartitioningPolicy;
public const string CsvMappingName = "{1}";
private static bool AddTypeToDefaultTableName(Type type, string tableName)
{{
if (InternalTypeToDefaultTableName == null)
{{
InternalTypeToDefaultTableName = new Dictionary<Type, string>();
}}
InternalTypeToDefaultTableName.Add(type, tableName);
return true;
}}
private static bool AddTypeToDDL(Type type, IReadOnlyList<string> ddl)
{{
if (InternalTypeToDDL == null)
{{
InternalTypeToDDL = new Dictionary<Type, IReadOnlyList<string>>();
}}
InternalTypeToDDL.Add(type, ddl);
return true;
}}
private static bool AddTypeToPartitioningPolicy(Type type, string partitioningPolicy)
{{
if (InternalTypeToPartitioningPolicy == null)
{{
InternalTypeToPartitioningPolicy = new Dictionary<Type, string>();
}}
InternalTypeToPartitioningPolicy.Add(type, partitioningPolicy);
return true;
}}
}}
}}
""";
public static readonly string KustoDDLTemplate = AutoGeneratedHeader +
""""
using System.Collections.Generic;
namespace NuGet.Insights
{{
static partial class {7}
{{
public const string {3}DefaultTableName = "{1}";
public static readonly IReadOnlyList<string> {3}DDL = new[]
{{
".drop table __TABLENAME__ ifexists",
"""
.create table __TABLENAME__ (
{2}
) with (docstring = __DOCSTRING__, folder = __FOLDER__)
""",
".alter-merge table __TABLENAME__ policy retention softdelete = 30d",
"""
.create table __TABLENAME__ ingestion csv mapping '{6}'
'['
{5}
']'
""",
}};
public const string {3}PartitioningPolicy =
"""
.alter table __TABLENAME__ policy partitioning {4}
""";
private static readonly bool {3}AddTypeToDefaultTableName = AddTypeToDefaultTableName(typeof({0}.{3}), {3}DefaultTableName);
private static readonly bool {3}AddTypeToDDL = AddTypeToDDL(typeof({0}.{3}), {3}DDL);
private static readonly bool {3}AddTypeToPartitioningPolicy = AddTypeToPartitioningPolicy(typeof({0}.{3}), {3}PartitioningPolicy);
}}
}}
"""";
}
}