-
Notifications
You must be signed in to change notification settings - Fork 17
/
snowflake.js
213 lines (184 loc) · 5.63 KB
/
snowflake.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
'use strict'
const snowflake = require('snowflake-sdk'),
fs = require('fs'),
path = require('path'),
YAML = require('yaml'),
{
createLogger,
format,
transports,
} = require('winston'),
{ parseArgs } = require('node:util'),
parse = require('parse-duration')
const options = {
logLevel: {
type: 'string',
short: 'l',
default: 'info',
},
nriConfig: {
type: 'string',
short: 'c',
default: './config.yaml',
},
queryFile: {
type: 'string',
short: 'q',
},
interval: {
type: 'string',
short: 'i',
default: '30s',
}
},
logFormat = format.combine(
format.colorize(),
format.align(),
format.simple(),
)
let logger
function usage() {
console.log('usage: snowflake.js ', options)
process.exit(2)
}
function isDate(date) {
return (new Date(date).toString() !== "Invalid Date")
}
function setupLogging(parsedOptions) {
logger = createLogger({
format: logFormat,
level: parsedOptions.logLevel,
transports: [new transports.Console({stderrLevels: [
'error',
'warn',
'info',
'http',
'verbose',
'debug',
'silly'
]})]
})
}
function setLogLevel(logLevel) {
if (logLevel != null) {
logger.level = logLevel
}
}
function revealConfig() {
// if (config.obfuscationEncodingKey != null && config.obfuscationEncodingKey.toString().length > 0) {
// for (const [key, value] of Object.entries(config.connection)) {
// if (value == null) {
// continue
// }
// config.connection[key] = deobfuscate(config.obfuscationEncodingKey, value)
// }
// }
}
function authenticatorCheck(config) {
if (config.connection.authenticator != null) {
if (
config.connection.authenticator
.toString()
.toUpperCase() === 'EXTERNALBROWSER' ||
config.connection.authenticator
.toString()
.toLowerCase()
.includes('okta.com')) {
logger.error(
`authenticatorCheck: unsupported authenticator: ${config.connection.authenticator}`
)
process.exit(2)
}
}
}
function loadNriConfig(parsedOptions) {
// Legacy behavior, not great as cli should override env :-(
if (process.env.NEWRELIC_SNOWFLAKE_NRI_CONFIG != null) {
parsedOptions.nriConfig = process.env.NEWRELIC_SNOWFLAKE_NRI_CONFIG
}
let file
try {
file = fs.readFileSync(
path.resolve(process.cwd(), parsedOptions.nriConfig),
'utf8',
)
} catch (e) {
logger.error(
`Error reading nriConfig ${parsedOptions.nriConfig}. cwd: ${process.cwd()}. Error: ${e}`
)
process.exit(2)
}
try {
const config = YAML.parse(file)
logger.debug('config: ', config)
setLogLevel(config.logLevel)
revealConfig()
authenticatorCheck(config)
logger.debug(`loadNriConfig: config: ${JSON.stringify(config)}`)
return config
} catch (e) {
logger.error(`Error parsing nriConfig ${parsedOptions.nriConfig}. Error: ${e}`)
process.exit(2)
}
}
function parseOptions() {
try {
const { values: parsedOptions } = parseArgs(
{
options: options,
strict: true,
allowPositionals: false,
}
)
// FIXME command line options should override config file options
// TODO add interval to config file
setupLogging(parsedOptions)
parsedOptions.interval = parse(parsedOptions.interval)
logger.debug(`parsedOptions: ${JSON.stringify(parsedOptions)}`)
return parsedOptions
} catch (e) {
console.log('\n\n')
usage()
}
}
function execute() {
const parsedOptions = parseOptions(),
config = loadNriConfig(parsedOptions),
connection = snowflake.createConnection(config.connection)
connection.connect((err, connection) => {
if (err) {
logger.error(`Unable to connect to snowflake: ${e}\n ${e.message}`)
process.exit(2)
}
const queryFilePath = path.resolve(process.cwd(), parsedOptions.queryFile)
logger.debug(`queryFilePath: ${queryFilePath}`)
if (!fs.existsSync(queryFilePath)) {
logger.error(`queryFile ${queryFilePath} not found`)
process.exit(2)
}
const sqlQuery = fs.readFileSync(queryFilePath)
.toString()
.replaceAll('$interval', parsedOptions.interval)
connection.execute({
sqlText: `${sqlQuery}`,
complete: function (err, stmt, rows) {
if (err) {
logger.error(`Failed to execute statement due to the following error: ${err.message}`)
} else {
rows.forEach((row) => {
for (let key in row) {
if (isDate(row[key])) {
rows[key] = row[key] + ""
} else {
}
}
})
// FIXME JSON.stringify has a max length limit
// Output the data to the console
console.log(JSON.stringify(rows))
}
}
})
})
}
execute()