-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
221 lines (202 loc) · 6.88 KB
/
index.js
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
const getSqlEventParam = (
paramName,
paramType = "string",
columnName = false
) => getSqlUnnestParam(paramName, paramType, columnName);
const getSqlUserProperty = (
paramName,
paramType = "string",
columnName = false
) => getSqlUnnestParam(paramName, paramType, columnName, "user_properties");
const getSqlUnnestParam = (
paramName,
paramType = "string",
resultColumnName = false,
unnestColumnName = "event_params"
) => {
let paramTypeName;
const alias =
resultColumnName === null
? ""
: `AS ${resultColumnName ? resultColumnName : paramName} `;
if (paramType.toLowerCase() === "coalesce") {
return `(SELECT COALESCE(ep.value.string_value, SAFE_CAST(ep.value.int_value AS STRING), SAFE_CAST(ep.value.double_value AS STRING), SAFE_CAST(ep.value.float_value AS STRING)) FROM UNNEST(${unnestColumnName}) ep WHERE ep.key = '${paramName}' LIMIT 1) ${alias}`;
}
if (paramType.toLowerCase() === "coalesce_float") {
return `(SELECT COALESCE(ep.value.float_value, SAFE_CAST(ep.value.int_value AS FLOAT64), ep.value.double_value) FROM UNNEST(${unnestColumnName}) ep WHERE ep.key = '${paramName}' LIMIT 1) ${alias}`;
}
switch (paramType.toLowerCase()) {
case "string":
paramTypeName = "string_value";
break;
case "int":
paramTypeName = "int_value";
break;
case "double":
paramTypeName = "double_value";
break;
case "float":
paramTypeName = "float_value";
break;
default:
throw "eventType is not valid";
}
return `(SELECT ep.value.${paramTypeName} FROM UNNEST(${unnestColumnName}) ep WHERE ep.key = '${paramName}' LIMIT 1) ${alias}`;
};
const getSqlEventParams = (eventParams) => {
const sql = eventParams.map((eventParam) =>
getSqlEventParam(eventParam.name, eventParam.type, eventParam.columnName)
);
return eventParams.length > 0 ? sql.join(", ") : "";
};
const getSqlUserProperties = (userProperties) => {
const sql = userProperties.map((userProperty) =>
getSqlUserProperty(
userProperty.name,
userProperty.type,
userProperty.columnName
)
);
return userProperties.length > 0 ? sql.join(", ") : "";
};
const getSqlGetFirstNotNullValue = (
paramName,
columnName = false,
orderBy = "event_timestamp"
) => {
const alias =
columnName === null ? "" : `AS ${columnName ? columnName : paramName} `;
return `ARRAY_AGG(${paramName} IGNORE NULLS ORDER BY ${orderBy} LIMIT 1)[SAFE_OFFSET(0)] ${alias}`;
};
const getSqlGetFirstNotNullValues = (columns) => {
const sql = columns.map((column) =>
getSqlGetFirstNotNullValue(
column.columnName ? column.columnName : column.name
)
);
return columns.length > 0 ? sql.join(",") : "";
};
const getSqlColumns = (params) => {
const sql = params.map(
(param) =>
`${param.name} as ${param.columnName ? param.columnName : param.name}`
);
return params.length > 0 ? sql.join(", ") : "";
};
const getSqlQueryParameter = (url, param) => {
const sql = `REGEXP_EXTRACT(${url}, r'(?i).*[?&#]${param.name.toLowerCase()}=([^&#\?]*)') as ${
param.columnName ? param.columnName : param.name
}`;
return sql;
};
const getSqlQueryParameters = (url, params) => {
const sql = params.map((param) => getSqlQueryParameter(url, param));
return params.length > 0 ? sql.join(", ") : "";
};
const getDateFromTableName = (tblName) => {
return tblName.substring(7);
};
const getFormattedDateFromTableName = (tblName) => {
return `${tblName.substring(7, 11)}-${tblName.substring(
11,
13
)}-${tblName.substring(13)}`;
};
const getSqlList = (list) => {
return `('${list.join("','")}')`;
};
const getSqlEventId = (timestampEventParamName) => {
if (typeof timestampEventParamName === "undefined")
return `FARM_FINGERPRINT(CONCAT(event_timestamp, event_name, user_pseudo_id, ifnull((select ep.value.int_value from unnest(event_params) as ep where ep.key = 'engagement_time_msec' ),0))) as event_id`;
return `FARM_FINGERPRINT(CONCAT(ifnull((SELECT ep.value.int_value FROM UNNEST(event_params) ep WHERE ep.key = '${timestampEventParamName}'),event_timestamp), event_name, user_pseudo_id)) as event_id`;
};
const getSqlSessionId = () => {
return `FARM_FINGERPRINT(CONCAT((select value.int_value from unnest(event_params) where key = 'ga_session_id'), user_pseudo_id)) as session_id`;
};
const getSqlDate = (timezone = "Europe/London") =>
`DATE(TIMESTAMP_MICROS(event_timestamp), "${timezone}") as date`;
function isStringInteger(str) {
const num = Number(str);
return Number.isInteger(num);
}
const getSqlSelectFromRow = (config) => {
return Object.entries(config)
.map(([key, value]) => {
if (typeof value === "number") {
return `${value} AS ${key}`;
} else if (key === "date") {
return `DATE '${value}' AS ${key}`;
} else if (key === "event_timestamp" && !/^\d+$/.test(value)) {
return `TIMESTAMP '${value}' AS ${key}`;
} else if (key === "session_start" && !/^\d+$/.test(value)) {
return `TIMESTAMP '${value}' AS ${key}`;
} else if (key === "session_end" && !/^\d+$/.test(value)) {
return `TIMESTAMP '${value}' AS ${key}`;
} else if (typeof value === "string") {
if (key === "int_value") return `${parseInt(value)} AS ${key}`;
if (key.indexOf("timestamp") > -1)
return `${parseInt(value)} AS ${key}`;
if (key === "float_value" || key === "double_value")
return `${parseFloat(value)} AS ${key}`;
return `'${value}' AS ${key}`;
} else if (value === null) {
return `${value} AS ${key}`;
} else if (value instanceof Array) {
return `[${getSqlSelectFromRow(value)}] AS ${key}`;
} else {
if (isStringInteger(key))
return `STRUCT(${getSqlSelectFromRow(value)})`;
else return `STRUCT(${getSqlSelectFromRow(value)}) AS ${key}`;
}
})
.join(", ");
};
const getSqlUnionAllFromRows = (rows) => {
try {
const selectStatements = rows
.map((data) => "SELECT " + getSqlSelectFromRow(data))
.join("\nUNION ALL\n ");
return selectStatements;
} catch (err) {
console.error("Error reading or parsing the file", err);
}
};
const declareSources = ({
database = dataform.projectConfig.defaultDatabase,
dataset,
incrementalTableName,
nonIncrementalTableName = "events_*",
}) => {
declare({
database,
schema: dataset,
name: incrementalTableName,
});
if (incrementalTableName != nonIncrementalTableName) {
declare({
database,
schema: dataset,
name: nonIncrementalTableName,
});
}
};
module.exports = {
getDateFromTableName,
getFormattedDateFromTableName,
getSqlUnnestParam,
getSqlEventParam,
getSqlEventParams,
getSqlUserProperty,
getSqlUserProperties,
getSqlList,
getSqlEventId,
getSqlSessionId,
getSqlDate,
getSqlGetFirstNotNullValue,
getSqlGetFirstNotNullValues,
getSqlColumns,
getSqlQueryParameter,
getSqlQueryParameters,
getSqlUnionAllFromRows,
declareSources,
};