Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const query = async(userName, //string
query // string
) => {
let user = await getUser(userName);
const queryApi = await new InfluxDB({url: __config.influx_url, token: user.token})
.getQueryApi(user.org);
//need to return array of strings as result
let result = [];
return await new Promise((resolve,reject) => {
queryApi.queryRows(query, {
next(row, tableMeta) {
const o = tableMeta.toObject(row);
result.push(o);
},
error(error) {
reject(error)
},
complete() {
const setupUserRest = async(user) => {
const setupAPI = new SetupAPI(new InfluxDB(__config.influx_url));
setupAPI.getSetup().then(async ({allowed}) => {
let body = {org: user.org, bucket: user.bucket, username: user.username, password: user.password };
if(user.token){
body.token = user.token;
}
if(allowed){
await setupAPI.postSetup({
body: body,
});
console.log(`--- Setup user ${user.username} at ${__config.influx_url} success ---`)
}else{
console.error(`--- Failed to setup user ${user.username} at ${__config.influx_url} ---`);
const createLabel = async(userName,
labelName,
labelDescr,
labelColor ) =>{
let user = getUser(userName);
const lblAPI = new LabelsAPI(new InfluxDB({url: __config.influx_url, token: user.token, timeout: 20000}));
let lblCreateReq = {body: {name: labelName, orgID: user.orgid,
properties: { description: labelDescr, color: labelColor }}};
await lblAPI.postLabels(lblCreateReq).catch(async error => {
console.log('--- Error Creating label ---');
console.error(error);
throw error;
});
};
const writeData = async (userName, //string
lines = ['testmeas value=300 ' + (nowNano - (3 * intervalNano)),
'testmeas value=200 ' + (nowNano - (2 * intervalNano)),
'testmeas value=100 ' + (nowNano - intervalNano)]) => {
let user = await getUser(userName);
const writeAPI = await new InfluxDB({url: __config.influx_url, token: user.token})
.getWriteApi(user.org, user.bucket, 'ns');
await writeAPI.writeRecords(lines);
await writeAPI.close().catch(e => {
console.error(`ERROR: closing write connection: ${e}`);
throw e;
})
};
const createDashboard = async(name, orgId) => {
const dbdsAPI = new DashboardsAPI(new InfluxDB({url: __config.influx_url, token: __defaultUser.token, timeout: 20000}));
await dbdsAPI.postDashboards({body: {name: name, orgID: orgId}}).catch(async error => {
console.log('--- Error Creating dashboard ---');
console.error(error);
throw error;
});
};
const createVariable = async(userName, name, type, values, selected = null ) => {
let user = getUser(userName);
let varAPI = new VariablesAPI(new InfluxDB({url: __config.influx_url, token: user.token, timeout: 20000}));
let parseValues = JSON.parse(values);
let reSel = selected === null ? selected : JSON.parse(selected);
return await varAPI.postVariables({body: {name: name,
orgID: user.orgid,
arguments: {
type: type,
values: parseValues
},
selected: reSel
}
})
};
const createAlertCheckFromFile = async(userName, filepath) => {
let user = getUser(userName);
let content = await readFileToBuffer(process.cwd() + '/' + filepath);
let newCheck = JSON.parse(content);
newCheck.orgID = user.orgid;
let chkAPI = new ChecksAPI(new InfluxDB({url: __config.influx_url, token: user.token, timeout: 20000}));
chkAPI.createCheck({body: newCheck});
};
const setUserOrgId = async(user) => {
//now grab the first org
let orgsAPI = new OrgsAPI(new InfluxDB({url: __config.influx_url, token: user.token, timeout: 20000}));
let orgs = await orgsAPI.getOrgs();
user.orgid = orgs.orgs[0].id;
};
const getAuthorizations = async(userName) => {
let user = getUser(userName);
let authsAPI = new AuthorizationsAPI(new InfluxDB({url: __config.influx_url, token: user.token, timeout: 20000}));
return await authsAPI.getAuthorizations();
};
const createTemplateFromFile = async(userName, filepath) => {
let user = getUser(userName);
let docsAPI = new DocumentsAPI(new InfluxDB({url: __config.influx_url, token: user.token, timeout: 20000}));
let content = await readFileToBuffer(process.cwd() + '/' + filepath);
let newTemplate = JSON.parse(content);
newTemplate.orgID = user.orgid;
docsAPI.postDocumentsTemplates({ body: newTemplate});
};