Skip to content

Commit f7b084a

Browse files
committed
Refactor code structure for improved readability and maintainability
1 parent 928543d commit f7b084a

2 files changed

Lines changed: 1917 additions & 2163 deletions

File tree

definitions/output/reports/reports_dynamic.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,22 @@ const DATE_RANGE = {
4040
* @returns {string} - Cloud Storage object path
4141
*/
4242
function buildExportPath(reportConfig) {
43-
const { sql, date, metric } = reportConfig
43+
const { sql, date, metric, lens } = reportConfig
44+
const lensPath = lens && lens.name ? `${lens.name}/` : ''
4445
let objectPath = EXPORT_CONFIG.storagePath
4546

4647
if (sql.type === 'histogram') {
47-
// Histogram exports are organized by date folders
48+
// Histogram exports are organized under date and lens folders
4849
const dateFolder = date.replaceAll('-', '_')
49-
objectPath += `${dateFolder}/${metric.id}`
50+
objectPath += `${dateFolder}/${lensPath}${metric.id}${EXPORT_CONFIG.fileFormat}`
5051
} else if (sql.type === 'timeseries') {
51-
// Timeseries exports are organized by metric
52-
objectPath += metric.id
52+
// Timeseries exports are organized under lens folders
53+
objectPath += `${lensPath}${metric.id}${EXPORT_CONFIG.fileFormat}`
5354
} else {
5455
throw new Error(`Unknown SQL type: ${sql.type}`)
5556
}
5657

57-
return objectPath + EXPORT_CONFIG.fileFormat
58+
return objectPath
5859
}
5960

6061
/**
@@ -64,8 +65,8 @@ function buildExportPath(reportConfig) {
6465
*/
6566
function buildExportQuery(reportConfig) {
6667
const { sql, date, metric, lens, tableName } = reportConfig
67-
6868
let query
69+
6970
if (sql.type === 'histogram') {
7071
query = `
7172
SELECT
@@ -83,8 +84,7 @@ function buildExportQuery(reportConfig) {
8384
* EXCEPT(date, metric, lens)
8485
FROM \`${EXPORT_CONFIG.dataset}.${tableName}\`
8586
WHERE
86-
date = '${date}'
87-
AND metric = '${metric.id}'
87+
metric = '${metric.id}'
8888
AND lens = '${lens.name}'
8989
ORDER BY date, client DESC
9090
`
@@ -106,13 +106,22 @@ function buildExportQuery(reportConfig) {
106106
* @returns {Object} - Complete report configuration
107107
*/
108108
function createReportConfig(date, metric, sql, lensName, lensSQL) {
109+
let tableName
110+
if (sql.type === 'timeseries') {
111+
tableName = sql.type
112+
} else if (sql.type === 'histogram') {
113+
tableName = `${metric.id}_${sql.type}`
114+
} else {
115+
throw new Error(`Unknown SQL type: ${sql.type}`)
116+
}
117+
109118
return {
110119
date,
111120
metric,
112121
sql,
113122
lens: { name: lensName, sql: lensSQL },
114123
devRankFilter: constants.devRankFilter,
115-
tableName: sql.type === 'timeseries' ? sql.type : `${metric.id}_${sql.type}`
124+
tableName: tableName
116125
}
117126
}
118127

@@ -150,8 +159,10 @@ function generateReportConfigurations() {
150159
* @returns {string} - Operation name
151160
*/
152161
function createOperationName(reportConfig) {
153-
const { tableName, date, lens } = reportConfig
154-
return `${tableName}_${date}_${lens.name}`
162+
const { sql, date, lens, metric } = reportConfig
163+
const lensSuffix = lens && lens.name ? `_${lens.name}` : ''
164+
165+
return `${metric.id}_${sql.type}_${date}${lensSuffix}`
155166
}
156167

157168
/**
@@ -182,10 +193,11 @@ INSERT INTO ${EXPORT_CONFIG.dataset}.${tableName}
182193
--*/
183194
184195
SELECT
196+
client,
185197
DATE('${date}') AS date,
186198
'${metric.id}' AS metric,
187199
'${lens.name}' AS lens,
188-
*
200+
* EXCEPT(client)
189201
FROM (
190202
${sql.query(ctx, reportConfig)}
191203
);
@@ -208,6 +220,7 @@ SELECT reports.run_export_job(job_config);
208220
// Generate all report configurations
209221
const reportConfigurations = generateReportConfigurations()
210222

223+
211224
// Create Dataform operations for each report configuration
212225
reportConfigurations.forEach(reportConfig => {
213226
const operationName = createOperationName(reportConfig)

0 commit comments

Comments
 (0)