diff options
author | Nick Van Doorn <vandoorn.nick@gmail.com> | 2018-12-09 23:10:19 -0800 |
---|---|---|
committer | Nick Van Doorn <vandoorn.nick@gmail.com> | 2018-12-09 23:10:19 -0800 |
commit | f1764bcde62d121baf7a610c196333d1b6bbe965 (patch) | |
tree | 53a1a3f8c74981459e11d7f2d9965c67facc931d | |
parent | fff69c079204bb5e55328a715cfd59ad49fdc44b (diff) |
(WIP) Implement Google Sheets Reporter
There seems to be a problem with the module used to interface to Google
Sheets... however we should be able to swap that out relatively easy.
-rw-r--r-- | src/reporters/sheets/index.ts | 1 | ||||
-rw-r--r-- | src/reporters/sheets/sheets.ts | 74 |
2 files changed, 75 insertions, 0 deletions
diff --git a/src/reporters/sheets/index.ts b/src/reporters/sheets/index.ts new file mode 100644 index 0000000..5d3678f --- /dev/null +++ b/src/reporters/sheets/index.ts @@ -0,0 +1 @@ +export { Sheets } from './sheets' diff --git a/src/reporters/sheets/sheets.ts b/src/reporters/sheets/sheets.ts new file mode 100644 index 0000000..99cc53b --- /dev/null +++ b/src/reporters/sheets/sheets.ts @@ -0,0 +1,74 @@ +import { Reporter } from '../../models/reporter.model' +import { Report } from '../../models/report.model' +import { ReportTest } from '../../models/report-test.model' +import { ReportStat } from '../../models/report-stat.model' +import { Context } from '../../models/context.model' + +import toSheets from 'array-to-google-sheets' + +/** + * Implementation of a reporter for Google Sheets. + */ +export class Sheets implements Reporter { + name = 'Google Sheets Reporter' + buff: Report[] = [] + toSheets + constructor(private ctx: Context) { + const { sheetsDocKey, sheetsCredPath } = ctx.config + const creds = require(`${ctx.rootDir}/${sheetsCredPath}`) + this.toSheets = new toSheets(sheetsDocKey, creds) + } + + public record(r: Report): void { + this.buff.push(r) + } + + /** + * Convert this.buff to an array + * of strings where each string is + * a row in a spreadsheet + */ + private toRows(): any[] { + const rows = [] + if (!this.hasHeader()) { + rows.push(this.getHeader()) + } + for (let report of this.buff) { + for (let test of report.tests) { + for (let stat of test.stats) { + rows.push([ + stat.datetime, + report.adapterName, + test.testServiceName, + report.nConnectedClients, + stat.download, + stat.upload + ]) + } + } + } + return rows + } + + private getHeader(): string[] { + return [ + `Date/Time`, + `Adapter Name`, + `Test Service Name`, + `Number of Connected Clients`, + `Download (mb/s)`, + `Upload (mb/s)` + ] + } + + private hasHeader(): boolean { + return true + } + + public publish(): Promise<void> { + return this.toSheets.updateGoogleSheets( + this.ctx.config.sheetsSheetName, + this.toRows() + ) + } +} |