|
| 1 | +using System.Data; |
| 2 | +using System.Data.Common; |
| 3 | +using System.Text.Json; |
| 4 | + |
| 5 | +namespace CmdScale.EntityFrameworkCore.TimescaleDB.Design.Scaffolding |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Extracts continuous aggregate policy metadata from a TimescaleDB database for scaffolding. |
| 9 | + /// </summary> |
| 10 | + public sealed class ContinuousAggregatePolicyScaffoldingExtractor : ITimescaleFeatureExtractor |
| 11 | + { |
| 12 | + public sealed record ContinuousAggregatePolicyInfo( |
| 13 | + string? StartOffset, |
| 14 | + string? EndOffset, |
| 15 | + string? ScheduleInterval, |
| 16 | + DateTime? InitialStart, |
| 17 | + bool? IncludeTieredData, |
| 18 | + int? BucketsPerBatch, |
| 19 | + int? MaxBatchesPerExecution, |
| 20 | + bool? RefreshNewestFirst |
| 21 | + ); |
| 22 | + |
| 23 | + public Dictionary<(string Schema, string TableName), object> Extract(DbConnection connection) |
| 24 | + { |
| 25 | + bool wasOpen = connection.State == ConnectionState.Open; |
| 26 | + if (!wasOpen) |
| 27 | + { |
| 28 | + connection.Open(); |
| 29 | + } |
| 30 | + |
| 31 | + try |
| 32 | + { |
| 33 | + Dictionary<(string, string), ContinuousAggregatePolicyInfo> policies = []; |
| 34 | + |
| 35 | + using (DbCommand command = connection.CreateCommand()) |
| 36 | + { |
| 37 | + // Query continuous aggregate policies from TimescaleDB jobs table |
| 38 | + command.CommandText = @" |
| 39 | + SELECT |
| 40 | + ca.user_view_schema, |
| 41 | + ca.user_view_name, |
| 42 | + j.config, |
| 43 | + j.schedule_interval::text, |
| 44 | + j.initial_start |
| 45 | + FROM timescaledb_information.jobs j |
| 46 | + INNER JOIN _timescaledb_catalog.continuous_agg ca |
| 47 | + ON (j.config->>'mat_hypertable_id')::integer = ca.mat_hypertable_id |
| 48 | + WHERE j.proc_name = 'policy_refresh_continuous_aggregate';"; |
| 49 | + |
| 50 | + using DbDataReader reader = command.ExecuteReader(); |
| 51 | + while (reader.Read()) |
| 52 | + { |
| 53 | + string viewSchema = reader.GetString(0); |
| 54 | + string viewName = reader.GetString(1); |
| 55 | + string? configJson = reader.IsDBNull(2) ? null : reader.GetString(2); |
| 56 | + string? scheduleInterval = reader.IsDBNull(3) ? null : reader.GetString(3); |
| 57 | + DateTime? initialStart = reader.IsDBNull(4) ? null : reader.GetDateTime(4); |
| 58 | + |
| 59 | + // Parse the JSONB config to extract policy parameters |
| 60 | + string? startOffset = null; |
| 61 | + string? endOffset = null; |
| 62 | + bool? includeTieredData = null; |
| 63 | + int? bucketsPerBatch = null; |
| 64 | + int? maxBatchesPerExecution = null; |
| 65 | + bool? refreshNewestFirst = null; |
| 66 | + |
| 67 | + if (!string.IsNullOrWhiteSpace(configJson)) |
| 68 | + { |
| 69 | + using JsonDocument doc = JsonDocument.Parse(configJson); |
| 70 | + JsonElement root = doc.RootElement; |
| 71 | + |
| 72 | + // Extract start_offset |
| 73 | + if (root.TryGetProperty("start_offset", out JsonElement startOffsetElement)) |
| 74 | + { |
| 75 | + startOffset = IntervalParsingHelper.ParseIntervalOrInteger(startOffsetElement); |
| 76 | + } |
| 77 | + |
| 78 | + // Extract end_offset |
| 79 | + if (root.TryGetProperty("end_offset", out JsonElement endOffsetElement)) |
| 80 | + { |
| 81 | + endOffset = IntervalParsingHelper.ParseIntervalOrInteger(endOffsetElement); |
| 82 | + } |
| 83 | + |
| 84 | + // Extract include_tiered_data (optional) |
| 85 | + if (root.TryGetProperty("include_tiered_data", out JsonElement includeTieredDataElement) |
| 86 | + && (includeTieredDataElement.ValueKind == JsonValueKind.True || includeTieredDataElement.ValueKind == JsonValueKind.False)) |
| 87 | + { |
| 88 | + includeTieredData = includeTieredDataElement.GetBoolean(); |
| 89 | + } |
| 90 | + |
| 91 | + // Extract buckets_per_batch (optional, defaults to 1) |
| 92 | + if (root.TryGetProperty("buckets_per_batch", out JsonElement bucketsPerBatchElement) |
| 93 | + && bucketsPerBatchElement.ValueKind == JsonValueKind.Number) |
| 94 | + { |
| 95 | + bucketsPerBatch = bucketsPerBatchElement.GetInt32(); |
| 96 | + } |
| 97 | + |
| 98 | + // Extract max_batches_per_execution (optional, defaults to 0) |
| 99 | + if (root.TryGetProperty("max_batches_per_execution", out JsonElement maxBatchesElement) |
| 100 | + && maxBatchesElement.ValueKind == JsonValueKind.Number) |
| 101 | + { |
| 102 | + maxBatchesPerExecution = maxBatchesElement.GetInt32(); |
| 103 | + } |
| 104 | + |
| 105 | + // Extract refresh_newest_first (optional, defaults to true) |
| 106 | + if (root.TryGetProperty("refresh_newest_first", out JsonElement refreshNewestFirstElement) |
| 107 | + && (refreshNewestFirstElement.ValueKind == JsonValueKind.True || refreshNewestFirstElement.ValueKind == JsonValueKind.False)) |
| 108 | + { |
| 109 | + refreshNewestFirst = refreshNewestFirstElement.GetBoolean(); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + policies[(viewSchema, viewName)] = new ContinuousAggregatePolicyInfo( |
| 114 | + StartOffset: startOffset, |
| 115 | + EndOffset: endOffset, |
| 116 | + ScheduleInterval: scheduleInterval, |
| 117 | + InitialStart: initialStart, |
| 118 | + IncludeTieredData: includeTieredData, |
| 119 | + BucketsPerBatch: bucketsPerBatch, |
| 120 | + MaxBatchesPerExecution: maxBatchesPerExecution, |
| 121 | + RefreshNewestFirst: refreshNewestFirst |
| 122 | + ); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // Convert to object dictionary to match interface |
| 127 | + return policies.ToDictionary( |
| 128 | + kvp => kvp.Key, |
| 129 | + kvp => (object)kvp.Value |
| 130 | + ); |
| 131 | + } |
| 132 | + finally |
| 133 | + { |
| 134 | + if (!wasOpen) |
| 135 | + { |
| 136 | + connection.Close(); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments