Skip to content

Commit 184ae3a

Browse files
committed
Refactor reservation handling to use configuration arrays and improve action name extraction
1 parent e48a7b1 commit 184ae3a

1 file changed

Lines changed: 55 additions & 70 deletions

File tree

includes/reservations.js

Lines changed: 55 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,84 @@
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+
]
3925
}
26+
]
4027

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+
}))
4332

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-
}
5433

5534
/**
5635
* Extracts action name from Dataform context object
5736
* @param {Object} ctx - Dataform context object
5837
* @returns {string|null} The extracted action name or null if not found
5938
*/
60-
function extractActionName(ctx) {
61-
if (!ctx) {
62-
return null
63-
}
39+
function getActionName(ctx) {
40+
if (!ctx) return null
6441

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()
6744
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
7150
}
7251

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+
}
7754

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
8162

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+
}
8367
}
8468

85-
return null
69+
return default_reservation
8670
}
8771

72+
8873
/**
8974
* Generates the reservation SQL statement for a given Dataform context
9075
* @param {Object} ctx - Dataform context object with self() method and/or proto.target
9176
* @returns {string} The SQL statement to set reservation or empty string
9277
*/
9378
function reservation_setter(ctx) {
94-
const actionName = extractActionName(ctx)
79+
const actionName = getActionName(ctx)
9580
const reservation = getReservation(actionName)
96-
return reservation ? `SET @@RESERVATION='${reservation}';` : ''
81+
return reservation ? `SET @@reservation='${reservation}';` : ''
9782
}
9883

9984
module.exports = {

0 commit comments

Comments
 (0)