-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(influxdb-grafana): ajout de l'écriture des données dans une base (…
…#43) * feat(influx): écriture dans une base influx * add tag for test name * Mise au prope du code * Ajout d'un docker-compose avec une stack influxdb * Ajout d'une explication sur l'utilisation d'influxdb * Fait d'influxdb un format de rapport à part entière
- Loading branch information
Showing
12 changed files
with
1,705 additions
and
4 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# InfluxDB | ||
# Ne pas modifier les deux variables suivante | ||
INFLUXDB_HOST=greenit-cli-influxdb | ||
INFLUXDB_PORT=8086 | ||
|
||
INFLUXDB_ORG_NAME=default | ||
INFLUXDB_USERNAME=default | ||
INFLUXDB_PASSWORD=defaultinfluxdb | ||
INFLUXDB_BUCKET_NAME=db0 | ||
INFLUXDB_RETENTION=24w | ||
|
||
# Renseigner ces variables une fois influxdb démarré pour la première fois | ||
INFLUXDB_TOKEN=token | ||
INFLUXDB_ORG_ID=orgId | ||
|
||
# Grafana | ||
GRAFANA_USERNAME=admin | ||
GRAFANA_PASSWORD=admin | ||
|
||
# Image Version | ||
# Attention lors des changements de versions | ||
INFLUXDB_IMAGE_VERSION=influxdb:2.1.1 | ||
GRAFANA_IMAGE_VERSION=grafana/grafana:8.4.3 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ dist/ | |
results/ | ||
results_test/ | ||
*.xlsx | ||
*.yaml | ||
*.yaml | ||
!docker-compose.yaml |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
const {InfluxDB, Point, HttpError} = require('@influxdata/influxdb-client'); | ||
const fs = require('fs'); | ||
const ProgressBar = require('progress'); | ||
|
||
async function write(reports, options) { | ||
|
||
if (!options.influxdb_hostname) { | ||
throw `You must define an InfluxDB hostname.`; | ||
} | ||
if (!options.influxdb_token) { | ||
throw `You must define an InfluxDB token.`; | ||
} | ||
if (!options.influxdb_bucket) { | ||
throw `You must define an InfluxDB bucket name.`; | ||
} | ||
if (!options.influxdb_org) { | ||
throw `You must define an InfluxDB organisation.`; | ||
} | ||
|
||
const url = options.influxdb_hostname; | ||
|
||
//initialise progress bar | ||
let progressBar; | ||
if (!options.ci){ | ||
progressBar = new ProgressBar(' Push to InfluxDB [:bar] :percent Remaining: :etas Time: :elapseds', { | ||
complete: '=', | ||
incomplete: ' ', | ||
width: 40, | ||
total: reports.length+2 | ||
}); | ||
progressBar.tick(); | ||
} else { | ||
console.log('Push report to InfluxDB ...'); | ||
} | ||
|
||
// initialise client | ||
const client = new InfluxDB({url: options.influxdb_hostname, token: options.influxdb_token}); | ||
const writeApi = client.getWriteApi(options.influxdb_org, options.influxdb_bucket); | ||
|
||
// create points from reports | ||
const points = reports.map((file) => { | ||
let obj = JSON.parse(fs.readFileSync(file.path).toString()); | ||
let hostname = obj.url.split('/')[2]; | ||
let point = new Point("eco_index") | ||
.tag("pageName", obj.pageInformations.name) | ||
.tag("hostname", hostname) | ||
.stringField("url", obj.url) | ||
.stringField("hostname", hostname) | ||
.stringField("grade", obj.grade) | ||
.intField("ecoindex", obj.ecoIndex) | ||
.floatField("water", obj.waterConsumption) | ||
.floatField("ges", obj.greenhouseGasesEmission) | ||
.floatField("domSize", obj.domSize) | ||
.stringField("pageSize", `${Math.round(obj.responsesSize / 1000)} (${Math.round(obj.responsesSizeUncompress / 1000)})`) | ||
.floatField("nbRequest", obj.nbRequest) | ||
.floatField("nbPlugins", obj.pluginsNumber) | ||
.floatField("cssFilesNumber", obj.printStyleSheetsNumber) | ||
.floatField("cssInlineNumber", obj.inlineStyleSheetsNumber) | ||
.floatField("emptySrcTagNumber", obj.emptySrcTagNumber) | ||
.floatField("inlineJsScriptsNumber", obj.inlineJsScriptsNumber) | ||
.floatField("responsesSize", Math.round(obj.responsesSize / 1000)) | ||
.floatField("responsesSizeUncompress", Math.round(obj.responsesSizeUncompress / 1000)); | ||
|
||
Object.keys(obj.bestPractices) | ||
.map(key => point.stringField(key, obj.bestPractices[key].complianceLevel || 'A')); | ||
|
||
if (progressBar) progressBar.tick() | ||
|
||
return point | ||
}); | ||
|
||
//upload points and close connexion | ||
writeApi.writePoints(points); | ||
|
||
writeApi | ||
.close() | ||
.then(() => { | ||
if (progressBar) progressBar.tick(); | ||
}) | ||
.catch(e => { | ||
console.log('Writing to influx failed\n'); | ||
console.error(e); | ||
if (e instanceof HttpError && e.statusCode === 401) { | ||
console.log(`The InfluxDB database: ${bucket} doesn't exist.`); | ||
} | ||
}); | ||
} | ||
|
||
module.exports = { | ||
write | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
services: | ||
greenit-cli-influxdb: | ||
image: ${INFLUXDB_IMAGE_VERSION} | ||
container_name: ${INFLUXDB_HOST} | ||
ports: | ||
- '8086:8086' | ||
volumes: | ||
- greenit-cli-influxdb-storage:/var/lib/influxdb2 | ||
- ./influxdb/queries:/home/queries:rw | ||
environment: | ||
- DOCKER_INFLUXDB_INIT_MODE=setup | ||
- DOCKER_INFLUXDB_INIT_USERNAME=${INFLUXDB_USERNAME} | ||
- DOCKER_INFLUXDB_INIT_PASSWORD=${INFLUXDB_PASSWORD} | ||
- DOCKER_INFLUXDB_INIT_ORG=${INFLUXDB_ORG_NAME} | ||
- DOCKER_INFLUXDB_INIT_BUCKET=${INFLUXDB_BUCKET_NAME} | ||
- DOCKER_INFLUXDB_INIT_RETENTION=${INFLUXDB_RETENTION} | ||
|
||
greenit-cli-grafana: | ||
container_name: greenit-cli-grafana | ||
image: ${GRAFANA_IMAGE_VERSION} | ||
ports: | ||
- '3000:3000' | ||
volumes: | ||
- greenit-cli-grafana-storage:/var/lib/grafana | ||
- ./grafana-provisioning/:/etc/grafana/provisioning | ||
depends_on: | ||
- greenit-cli-influxdb | ||
environment: | ||
- GF_SECURITY_ADMIN_USER=${GRAFANA_USERNAME} | ||
- GF_SECURITY_ADMIN_PASSWORD=${GRAFANA_PASSWORD} | ||
- GF_AUTH_ANONYMOUS_ENABLED=true | ||
- GF_USERS_ALLOW_SIGN_UP=false | ||
- GF_USERS_ALLOW_ORG_CREATE=false | ||
- INFLUXDB_ORG_ID=${INFLUXDB_ORG_ID} | ||
- INFLUXDB_TOKEN=${INFLUXDB_TOKEN} | ||
- INFLUXDB_BUCKET_NAME=${INFLUXDB_BUCKET_NAME} | ||
- INFLUXDB_HOST=${INFLUXDB_HOST} | ||
- INFLUXDB_PORT=${INFLUXDB_PORT} | ||
|
||
volumes: | ||
greenit-cli-influxdb-storage: | ||
driver: local | ||
greenit-cli-grafana-storage: | ||
driver: local |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
apiVersion: 1 | ||
providers: | ||
- name: InfluxDB | ||
folder: '' | ||
type: file | ||
disableDeletion: false | ||
editable: true | ||
options: | ||
path: /etc/grafana/provisioning/dashboards | ||
|
Oops, something went wrong.