Created
November 27, 2021 17:21
-
-
Save nidhinkumar06/c54fe90a9428ed06c5ffa1b188a1aee0 to your computer and use it in GitHub Desktop.
Grafana Excel Report
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const moment = require('moment'); | |
const momentGrafana = require('moment-relativism'); | |
const { fluxDuration } = require("@influxdata/influxdb-client"); | |
const { METRIC_HEADERS } = require('./constants'); | |
const { getCPUUsage } = require("./cpuUsage"); | |
const { generateWorkBook } = require("./generateWorkBook"); | |
const app = express(); | |
const downloadExcel = async(req, res) => { | |
console.log('req body', JSON.stringify(req.body)); | |
let message = req.query?.message || req.body?.message || 'Hello World!'; | |
let startDuration; | |
let endDuration; | |
let fromDate; | |
let toDate; | |
if (req.query.from.includes('now')) { | |
const from = req.query.from; | |
const start = momentGrafana.relativism(from.replace('/', '|')); | |
const end = momentGrafana.relativism(req.query.to); | |
startDuration = moment.utc(start).unix(); | |
endDuration = moment.utc(end).unix(); | |
fromDate = moment(start).format('YYYY-MM-DDTHH:mm:ss'); | |
toDate = moment(end).format('YYYY-MM-DDTHH:mm:ss'); | |
} else { | |
startDuration = moment.utc(parseInt(req.query.from)).unix(); | |
endDuration = moment.utc(parseInt(req.query.to)).unix(); | |
fromDate = parseInt(req.query.from); | |
toDate = parseInt(req.query.to); | |
} | |
const cpuUsageData = await getCPUUsage(startDuration, endDuration); | |
const workBookDatas = [ | |
{ sheet_name: 'CPU Usage', sheet_data: cpuUsageData, column_name: METRIC_HEADERS } | |
]; | |
generateWorkBook(res, workBookDatas); | |
}; | |
app.get('/excelreport', downloadExcel); | |
app.listen(3333); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment