-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconfig.js
101 lines (92 loc) · 2.46 KB
/
config.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
const errors = require('./errors');
const fs = require('fs');
const DEFAULT_CONF_FILE_NAME = 'csv2influx.conf.json';
const INIT_CONFIG = {
influxdbUri: 'http://127.0.0.1:8086/database_name',
measurementName: 'measurment_name',
mapping: {
time: {
from: 'date',
type: 'timestamp',
format: 'jsDate'
},
fieldSchema: {
lng: {
from: 'some-value',
type: 'float'
},
name: {
from: 'name',
type: 'string'
},
descr: {
from: 'description',
type: 'string'
},
},
},
csv: {
delimiter: ','
}
}
function initConfig() {
console.log('Writing ' + DEFAULT_CONF_FILE_NAME);
fs.writeFileSync(DEFAULT_CONF_FILE_NAME, JSON.stringify(INIT_CONFIG, null, 2));
console.log('ok');
}
function _checkConfigObject(confObj) {
if(!confObj.measurementName) {
return 'no measurementName field';
}
if(!(confObj.influxdbUri || confObj.influxdbUrl)) {
return 'no influxdbUri field';
}
if(!confObj.mapping) {
return 'no mapping field';
}
if(!confObj.mapping.fieldSchema) {
return 'no fieldSchema specified';
}
if(!confObj.mapping.time) {
console.log(`WARNING: you didn't set time field, importing rows with current time`);
} else {
if(!confObj.mapping.time.format) {
return 'no format specified for time';
}
}
return undefined;
}
function _ensureInfluxUri(confObj) {
if(confObj.influxdbUri === undefined) {
console.log('Trying influxdbUrl instead of influxdbUri');
console.log(confObj.influxdbUrl);
confObj.influxdbUri = confObj.influxdbUrl;
}
}
function loadConfig(configFilename) {
configFilename = configFilename !== undefined ? configFilename : DEFAULT_CONF_FILE_NAME;
console.log('Reading ' + configFilename);
if(!fs.existsSync(configFilename)) {
console.error(configFilename + ' doesn`t exist. Can`t continue.');
console.error('csv2influx init to create template config')
process.exit(errors.ERROR_BAD_CONFIG_FILE);
}
var str = fs.readFileSync(configFilename).toString();
var confObj = JSON.parse(str);
console.log('ok');
console.log('checking format');
var checkErr = _checkConfigObject(confObj);
if(checkErr !== undefined) {
console.log('Config format error: ' + checkErr);
process.exit(errors.ERROR_BAD_CONFIG_FORMAT);
}
_ensureInfluxUri(confObj);
console.log('ok');
return confObj;
}
module.exports = {
initConfig,
loadConfig,
// only for testing
_checkConfigObject
}