forked from NpgsqlRest/NpgsqlRest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatsEndpoints.cs
More file actions
451 lines (411 loc) · 16.4 KB
/
Copy pathStatsEndpoints.cs
File metadata and controls
451 lines (411 loc) · 16.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
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
using System.Net;
using System.Text;
using System.Text.Json;
using Npgsql;
using NpgsqlRest;
namespace NpgsqlRestClient;
/// <summary>
/// Provides endpoint handlers for PostgreSQL statistics endpoints.
/// Each method executes a query against pg_stat_* views and returns results as JSON, TSV, or HTML.
/// </summary>
public static class StatsEndpoints
{
private const string RoutinesQuery = """
select
funcid as oid,
schemaname as schema,
b.proname as name,
case
when b.prokind = 'f' then 'function'
when b.prokind = 'p' then 'procedure'
when b.prokind = 'a' then 'aggregate'
when b.prokind = 'w' then 'window'
else b.prokind::text
end || ' ' || funcid::regprocedure as signature,
calls,
ltrim(justify_interval(total_time * interval '1 ms')::text, '@ ') as total_time,
ltrim(justify_interval(self_time * interval '1 ms')::text, '@ ') as self_time
from pg_stat_user_functions a join pg_proc b on a.funcid = b.oid
where $1 is null or schemaname similar to $1
order by
a.schemaname,
b.proname
""";
private const string TablesQuery = """
select
schemaname as schema,
relname as name,
n_live_tup as live_tuples,
n_dead_tup as dead_tuples,
round(100.0 * n_dead_tup / nullif(n_live_tup + n_dead_tup, 0), 2) as dead_tuple_percent,
pg_size_pretty(pg_total_relation_size(relid)) as total_size,
pg_size_pretty(pg_relation_size(relid)) as table_size,
pg_size_pretty(pg_indexes_size(relid)) as index_size,
seq_scan as sequential_scans,
idx_scan as index_scans,
round(100.0 * idx_scan / nullif(seq_scan + idx_scan, 0), 2) as index_scan_percent,
n_tup_ins as inserts_count,
n_tup_upd as updates_count,
n_tup_del as deletes_count,
last_vacuum,
last_autovacuum,
last_analyze,
last_autoanalyze,
vacuum_count + autovacuum_count as total_vacuums,
analyze_count + autoanalyze_count as total_analyzes
from pg_stat_user_tables
where $1 is null or schemaname similar to $1
order by schemaname, relname
""";
private const string IndexesQuery = """
select
schemaname as schema,
relname as table,
indexrelname AS index,
indisunique as is_unique,
indisprimary as is_primary,
idx_scan AS scans,
last_idx_scan as last_scan,
idx_tup_fetch AS tuples_fetched,
pg_get_indexdef(indexrelid) as definition
from pg_stat_user_indexes s
join pg_index i using (indexrelid)
where $1 is null or schemaname similar to $1
order by schemaname, relname
""";
private const string ActivityQuery = """
select
pid,
usename as user,
application_name as app,
client_addr as client_ip,
datname as database,
state,
wait_event_type,
wait_event,
ltrim(justify_interval(now() - state_change)::text, '@ ') as state_duration,
ltrim(justify_interval(now() - backend_start)::text, '@ ') as connection_age,
ltrim(justify_interval(now() - xact_start)::text, '@ ') as transaction_duration,
case
when state = 'active' then ltrim(justify_interval(now() - query_start)::text, '@ ')
when state = 'idle' then 'finished ' || ltrim(justify_interval(now() - state_change)::text, '@ ') || ' ago'
else state
end as query_duration,
left(query, 100) as query_preview,
query
from pg_stat_activity
where pid != pg_backend_pid()
order by state_duration desc nulls last
""";
/// <summary>
/// Returns statistics for user-defined functions and procedures.
/// </summary>
public static async Task HandleRoutinesStats(HttpContext context, string connectionString, string outputFormat, string? schemaSimilarTo, bool requireAuthorization, string[]? authorizedRoles, string? roleClaimType, ILogger? logger)
{
if (await CheckAuthorization(context, requireAuthorization, authorizedRoles, roleClaimType) is false)
{
return;
}
await ExecuteQueryAndRespond(context, connectionString, RoutinesQuery, outputFormat, schemaSimilarTo, logger, "Stats.Routines", setupCommands: ["set local intervalstyle = 'postgres_verbose'"]);
}
/// <summary>
/// Returns statistics for user tables including size, tuple counts, and vacuum info.
/// </summary>
public static async Task HandleTablesStats(HttpContext context, string connectionString, string outputFormat, string? schemaSimilarTo, bool requireAuthorization, string[]? authorizedRoles, string? roleClaimType, ILogger? logger)
{
if (await CheckAuthorization(context, requireAuthorization, authorizedRoles, roleClaimType) is false)
{
return;
}
await ExecuteQueryAndRespond(context, connectionString, TablesQuery, outputFormat, schemaSimilarTo, logger, "Stats.Tables");
}
/// <summary>
/// Returns statistics for user indexes including scan counts and definitions.
/// </summary>
public static async Task HandleIndexesStats(HttpContext context, string connectionString, string outputFormat, string? schemaSimilarTo, bool requireAuthorization, string[]? authorizedRoles, string? roleClaimType, ILogger? logger)
{
if (await CheckAuthorization(context, requireAuthorization, authorizedRoles, roleClaimType) is false)
{
return;
}
await ExecuteQueryAndRespond(context, connectionString, IndexesQuery, outputFormat, schemaSimilarTo, logger, "Stats.Indexes");
}
/// <summary>
/// Returns current database activity from pg_stat_activity.
/// </summary>
public static async Task HandleActivityStats(HttpContext context, string connectionString, string outputFormat, bool requireAuthorization, string[]? authorizedRoles, string? roleClaimType, ILogger? logger)
{
if (await CheckAuthorization(context, requireAuthorization, authorizedRoles, roleClaimType) is false)
{
return;
}
await ExecuteQueryAndRespond(context, connectionString, ActivityQuery, outputFormat, null, logger, "Stats.Activity", setupCommands: ["set local intervalstyle = 'postgres_verbose'"]);
}
private static async Task<bool> CheckAuthorization(HttpContext context, bool requireAuthorization, string[]? authorizedRoles, string? roleClaimType)
{
if (requireAuthorization is false && authorizedRoles is null)
{
return true;
}
if (context.User?.Identity?.IsAuthenticated is false)
{
await Results.Problem(
type: null,
statusCode: (int)HttpStatusCode.Unauthorized,
title: "Unauthorized",
detail: null).ExecuteAsync(context);
return false;
}
if (authorizedRoles is not null)
{
bool ok = false;
foreach (var claim in context.User?.Claims ?? [])
{
if (string.Equals(claim.Type, roleClaimType, StringComparison.Ordinal))
{
if (authorizedRoles.Contains(claim.Value) is true)
{
ok = true;
break;
}
}
}
if (ok is false)
{
await Results.Problem(
type: null,
statusCode: (int)HttpStatusCode.Forbidden,
title: "Forbidden",
detail: null).ExecuteAsync(context);
return false;
}
}
return true;
}
private static async Task ExecuteQueryAndRespond(
HttpContext context,
string connectionString,
string query, string outputFormat,
string? schemaSimilarTo,
ILogger? logger,
string logName,
string[]? setupCommands = null)
{
try
{
await using var connection = new NpgsqlConnection(connectionString);
await connection.OpenAsync(context.RequestAborted);
// If setup commands are provided, wrap in a transaction with rollback
if (setupCommands is { Length: > 0 })
{
await using var beginCmd = new NpgsqlCommand("begin", connection);
CommandLogger.LogCommand(beginCmd, logger, logName);
await beginCmd.ExecuteNonQueryAsync(context.RequestAborted);
foreach (var setupSql in setupCommands)
{
await using var setupCmd = new NpgsqlCommand(setupSql, connection);
CommandLogger.LogCommand(setupCmd, logger, logName);
await setupCmd.ExecuteNonQueryAsync(context.RequestAborted);
}
}
try
{
await using var command = new NpgsqlCommand(query, connection);
if (query.Contains("$1"))
{
command.Parameters.Add(new NpgsqlParameter { Value = schemaSimilarTo ?? (object)DBNull.Value, NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Text });
}
CommandLogger.LogCommand(command, logger, logName);
await using var reader = await command.ExecuteReaderAsync(context.RequestAborted);
var format = context.Request.Query.ContainsKey("format") ? context.Request.Query["format"].ToString() : outputFormat;
if (string.Equals(format, "html", StringComparison.OrdinalIgnoreCase))
{
await WriteAsHtml(context, reader);
}
else
{
await WriteAsJson(context, reader);
}
}
finally
{
if (setupCommands is { Length: > 0 })
{
await using var rollbackCmd = new NpgsqlCommand("rollback", connection);
CommandLogger.LogCommand(rollbackCmd, logger, logName);
await rollbackCmd.ExecuteNonQueryAsync(CancellationToken.None);
}
}
}
catch (OperationCanceledException)
{
// Request was cancelled, don't log as error
}
catch (Exception ex)
{
logger?.LogError(ex, "Error executing stats query");
context.Response.StatusCode = StatusCodes.Status500InternalServerError;
await context.Response.WriteAsync("Error retrieving statistics", context.RequestAborted);
}
}
private static async Task WriteAsJson(HttpContext context, NpgsqlDataReader reader)
{
context.Response.ContentType = "application/json";
var columnNames = new string[reader.FieldCount];
for (int i = 0; i < reader.FieldCount; i++)
{
columnNames[i] = ToCamelCase(reader.GetName(i));
}
using var stream = new MemoryStream();
await using var writer = new Utf8JsonWriter(stream);
writer.WriteStartArray();
while (await reader.ReadAsync(context.RequestAborted))
{
writer.WriteStartObject();
for (int i = 0; i < reader.FieldCount; i++)
{
var value = reader.GetValue(i);
writer.WritePropertyName(columnNames[i]);
WriteJsonValue(writer, value);
}
writer.WriteEndObject();
}
writer.WriteEndArray();
await writer.FlushAsync(context.RequestAborted);
stream.Position = 0;
await stream.CopyToAsync(context.Response.Body, context.RequestAborted);
}
private static void WriteJsonValue(Utf8JsonWriter writer, object value)
{
if (value == DBNull.Value || value is null)
{
writer.WriteNullValue();
return;
}
switch (value)
{
case bool b:
writer.WriteBooleanValue(b);
break;
case int i:
writer.WriteNumberValue(i);
break;
case long l:
writer.WriteNumberValue(l);
break;
case decimal d:
writer.WriteNumberValue(d);
break;
case double dbl:
writer.WriteNumberValue(dbl);
break;
case float f:
writer.WriteNumberValue(f);
break;
case DateTime dt:
writer.WriteStringValue(dt.ToString("o"));
break;
case DateTimeOffset dto:
writer.WriteStringValue(dto.ToString("o"));
break;
case TimeSpan ts:
writer.WriteStringValue(ts.ToString());
break;
case System.Net.IPAddress ip:
writer.WriteStringValue(ip.ToString());
break;
case uint ui:
writer.WriteNumberValue(ui);
break;
default:
writer.WriteStringValue(value.ToString());
break;
}
}
private static string ToCamelCase(string name)
{
if (string.IsNullOrEmpty(name))
return name;
// Handle snake_case to camelCase conversion
var parts = name.Split('_');
if (parts.Length == 1)
{
// Simple case: just lowercase first char
return char.ToLowerInvariant(name[0]) + name[1..];
}
// Convert snake_case to camelCase
var sb = new StringBuilder();
for (int i = 0; i < parts.Length; i++)
{
if (string.IsNullOrEmpty(parts[i]))
continue;
if (sb.Length == 0)
{
sb.Append(parts[i].ToLowerInvariant());
}
else
{
sb.Append(char.ToUpperInvariant(parts[i][0]));
if (parts[i].Length > 1)
sb.Append(parts[i][1..].ToLowerInvariant());
}
}
return sb.ToString();
}
private static async Task WriteAsHtml(HttpContext context, NpgsqlDataReader reader)
{
context.Response.ContentType = "text/html; charset=utf-8";
var sb = new StringBuilder();
sb.AppendLine(
"<style>table{font-family:Calibri,Arial,sans-serif;font-size:11pt;border-collapse:collapse}th,td{border:1px solid #d4d4d4;padding:4px 8px}th{background-color:#f5f5f5;font-weight:600}</style>");
sb.AppendLine("<table>");
// Write header row
sb.Append("<tr>");
for (int i = 0; i < reader.FieldCount; i++)
{
sb.Append("<th>");
sb.Append(HtmlEncode(reader.GetName(i)));
sb.Append("</th>");
}
sb.AppendLine("</tr>");
// Write data rows
while (await reader.ReadAsync(context.RequestAborted))
{
sb.Append("<tr>");
for (int i = 0; i < reader.FieldCount; i++)
{
sb.Append("<td>");
var value = reader.GetValue(i);
if (value != DBNull.Value)
{
sb.Append(HtmlEncode(ConvertValueToString(value) ?? ""));
}
sb.Append("</td>");
}
sb.AppendLine("</tr>");
}
sb.AppendLine("</table>");
await context.Response.WriteAsync(sb.ToString(), context.RequestAborted);
}
private static string HtmlEncode(string value)
{
return value
.Replace("&", "&")
.Replace("<", "<")
.Replace(">", ">")
.Replace("\"", """);
}
private static string? ConvertValueToString(object value)
{
if (value == DBNull.Value || value is null)
return null;
return value switch
{
TimeSpan ts => ts.ToString(),
System.Net.IPAddress ip => ip.ToString(),
DateTime dt => dt.ToString("o"),
DateTimeOffset dto => dto.ToString("o"),
_ => value.ToString()
};
}
}