|
1 | | -// Reservation references |
2 | | -const RESERVATIONS = { |
3 | | - HIGH_SLOTS: 'projects/httparchive/locations/US/reservations/pipeline', |
4 | | - LOW_SLOTS: null, |
5 | | - ON_DEMAND: 'none', |
6 | | - DEFAULT: null |
7 | | -} |
8 | | - |
9 | | -// Configuration for actions (JSON format for dynamic injection) |
10 | | -const RESERVATION_CONFIG = { |
11 | | - 'highSlots': [ |
12 | | - 'httparchive.crawl.pages', |
13 | | - 'httparchive.crawl.requests', |
14 | | - 'httparchive.crawl.parsed_css', |
15 | | - 'httparchive.f1.pages_latest', |
16 | | - 'httparchive.f1.requests_latest' |
17 | | - ], |
18 | | - 'lowSlots': [], |
19 | | - 'onDemand': [ |
20 | | - 'httparchive.dataform_assertions.corrupted_technology_values' |
21 | | - ] |
22 | | -} |
23 | | - |
24 | | -// Convert arrays to Sets for O(1) lookup performance |
25 | | -const RESERVATION_SETS = { |
26 | | - highSlots: new Set(RESERVATION_CONFIG.highSlots), |
27 | | - lowSlots: new Set(RESERVATION_CONFIG.lowSlots), |
28 | | - onDemand: new Set(RESERVATION_CONFIG.onDemand) |
29 | | -} |
30 | | - |
31 | | -/** |
32 | | - * Determines the appropriate reservation for a given action name |
33 | | - * @param {string} actionName - The fully qualified table name (with or without backticks) |
34 | | - * @returns {string|null} The reservation identifier or null if no reservation assignment needed |
35 | | - */ |
36 | | -function getReservation(actionName) { |
37 | | - if (!actionName || typeof actionName !== 'string') { |
38 | | - return RESERVATIONS.DEFAULT |
| 1 | +const default_reservation = null |
| 2 | +const RESERVATION_CONFIG = [ |
| 3 | + { |
| 4 | + tag: 'high_slots', |
| 5 | + reservation: 'projects/httparchive/locations/US/reservations/pipeline', |
| 6 | + actions: [ |
| 7 | + 'httparchive.crawl.pages', |
| 8 | + 'httparchive.crawl.requests', |
| 9 | + 'httparchive.crawl.parsed_css', |
| 10 | + 'httparchive.f1.pages_latest', |
| 11 | + 'httparchive.f1.requests_latest' |
| 12 | + ] |
| 13 | + }, |
| 14 | + { |
| 15 | + tag: 'low_slots', |
| 16 | + reservation: null, |
| 17 | + actions: [] |
| 18 | + }, |
| 19 | + { |
| 20 | + tag: 'on_demand', |
| 21 | + reservation: 'none', |
| 22 | + actions: [ |
| 23 | + 'httparchive.dataform_assertions.corrupted_technology_values' |
| 24 | + ] |
39 | 25 | } |
| 26 | +] |
40 | 27 |
|
41 | | - // Strip backticks if present and normalize |
42 | | - const normalizedName = actionName.replace(/`/g, '').trim() |
| 28 | +const RESERVATION_SETS = RESERVATION_CONFIG.map(cfg => ({ |
| 29 | + ...cfg, |
| 30 | + set: new Set(cfg.actions) |
| 31 | +})) |
43 | 32 |
|
44 | | - if (RESERVATION_SETS.highSlots.has(normalizedName)) { |
45 | | - return RESERVATIONS.HIGH_SLOTS |
46 | | - } else if (RESERVATION_SETS.lowSlots.has(normalizedName)) { |
47 | | - return RESERVATIONS.LOW_SLOTS |
48 | | - } else if (RESERVATION_SETS.onDemand.has(normalizedName)) { |
49 | | - return RESERVATIONS.ON_DEMAND |
50 | | - } else { |
51 | | - return RESERVATIONS.DEFAULT |
52 | | - } |
53 | | -} |
54 | 33 |
|
55 | 34 | /** |
56 | 35 | * Extracts action name from Dataform context object |
57 | 36 | * @param {Object} ctx - Dataform context object |
58 | 37 | * @returns {string|null} The extracted action name or null if not found |
59 | 38 | */ |
60 | | -function extractActionName(ctx) { |
61 | | - if (!ctx) { |
62 | | - return null |
63 | | - } |
| 39 | +function getActionName(ctx) { |
| 40 | + if (!ctx) return null |
64 | 41 |
|
65 | | - // Try primary method: ctx.self() |
66 | | - if (typeof ctx.self === 'function') { |
| 42 | + if (typeof ctx.self === 'function' && !ctx?.operation) { |
| 43 | + // Try primary method: ctx.self() |
67 | 44 | const selfName = ctx.self() |
68 | | - if (selfName) { |
69 | | - return selfName |
70 | | - } |
| 45 | + if (selfName) return selfName.replace(/`/g, '').trim() |
| 46 | + } else if (ctx?.operation?.proto?.target) { |
| 47 | + // Fallback: construct from proto target |
| 48 | + const t = ctx?.operation?.proto?.target |
| 49 | + return t ? [t.database, t.name].join('.') : null |
71 | 50 | } |
72 | 51 |
|
73 | | - // Fallback: construct from proto target |
74 | | - if (ctx?.operation?.proto?.target) { |
75 | | - const operationTarget = ctx?.operation?.proto?.target |
76 | | - const parts = [] |
| 52 | + return null |
| 53 | +} |
77 | 54 |
|
78 | | - if (operationTarget.database) parts.push(operationTarget.database) |
79 | | - if (operationTarget.schema) parts.push(operationTarget.schema) |
80 | | - if (operationTarget.name) parts.push(operationTarget.name) |
| 55 | +/** |
| 56 | + * Determines the appropriate reservation for a given action name |
| 57 | + * @param {string} actionName - The action name (without backticks) |
| 58 | + * @returns {string|null} The reservation identifier or null if no reservation assignment needed |
| 59 | + */ |
| 60 | +function getReservation(actionName) { |
| 61 | + if (!actionName || typeof actionName !== 'string') return default_reservation |
81 | 62 |
|
82 | | - return parts.length > 0 ? parts.join('.') : null |
| 63 | + for (const { reservation, set } of Object.values(RESERVATION_SETS)) { |
| 64 | + if (set.has(actionName)) { |
| 65 | + return reservation |
| 66 | + } |
83 | 67 | } |
84 | 68 |
|
85 | | - return null |
| 69 | + return default_reservation |
86 | 70 | } |
87 | 71 |
|
| 72 | + |
88 | 73 | /** |
89 | 74 | * Generates the reservation SQL statement for a given Dataform context |
90 | 75 | * @param {Object} ctx - Dataform context object with self() method and/or proto.target |
91 | 76 | * @returns {string} The SQL statement to set reservation or empty string |
92 | 77 | */ |
93 | 78 | function reservation_setter(ctx) { |
94 | | - const actionName = extractActionName(ctx) |
| 79 | + const actionName = getActionName(ctx) |
95 | 80 | const reservation = getReservation(actionName) |
96 | | - return reservation ? `SET @@RESERVATION='${reservation}';` : '' |
| 81 | + return reservation ? `SET @@reservation='${reservation}';` : '' |
97 | 82 | } |
98 | 83 |
|
99 | 84 | module.exports = { |
|
0 commit comments