From c112b3c640286129255aed7b585398aed5dd316f Mon Sep 17 00:00:00 2001 From: Dominique Clarke Date: Wed, 15 Feb 2023 16:15:04 -0500 Subject: [PATCH] handle project monitors --- src/core/index.ts | 8 +++----- src/core/runner.ts | 43 ++++++++++++++++++++++++++++++++++--------- src/loader.ts | 1 + 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/src/core/index.ts b/src/core/index.ts index 208d6d12..4151d5cd 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -22,7 +22,7 @@ * THE SOFTWARE. * */ -import { Journey, JourneyCallback, JourneyOptions, Suite } from '../dsl'; +import { Journey, JourneyCallback, JourneyOptions } from '../dsl'; import Runner from './runner'; import { VoidCallback, HooksCallback, Location } from '../common_types'; import { wrapFnWithLocation } from '../helpers'; @@ -33,7 +33,8 @@ import { MonitorConfig } from '../dsl/monitor'; /* TODO: Testing * Local vs global matrix: Local matrix fully overwrites global matrix, rather than merging * Adjustments: Duplicates in adjustments do not run extra journeys - * Regular params are combina + * Regular params are combined with matrix params + * Project monitors: name and id are overwritten only for matrix monitors /** * Use a gloabl Runner which would be accessed by the runtime and @@ -55,11 +56,8 @@ export const journey = wrapFnWithLocation( if (typeof options === 'string') { options = { name: options, id: options }; } - const suite = new Suite(location); const j = new Journey(options, callback, location); - suite.addJourney(j); runner.addJourney(j); - runner.addSuite(suite); return j; } ); diff --git a/src/core/runner.ts b/src/core/runner.ts index 11b308af..6c47512e 100644 --- a/src/core/runner.ts +++ b/src/core/runner.ts @@ -141,6 +141,14 @@ export default class Runner { } addJourney(journey: Journey) { + const journeySuite = this.suites.get(journey.location); + if (journeySuite) { + journeySuite.addJourney(journey); + } else { + const suite = new Suite(journey.location); + suite.addJourney(journey); + this.addSuite(suite); + } this.journeys.push(journey); this.#currentJourney = journey; } @@ -392,10 +400,13 @@ export default class Runner { } buildMonitors(options: RunOptions) { + /* Build out monitors according to matrix specs */ + this.parseMatrix(options); + /** * Update the global monitor configuration required for * setting defaults - */ + */ this.updateMonitor({ throttling: options.throttling, schedule: options.schedule, @@ -407,7 +418,10 @@ export default class Runner { const { match, tags } = options; const monitors: Monitor[] = []; - for (const journey of this.journeys) { + + const journeys = this.getAllJourneys(); + + for (const journey of journeys) { const params = Object.freeze({ ...this.monitor.config?.params, ...options.params, ...journey.params }); if (!journey.isMatch(match, tags)) { continue; @@ -419,9 +433,15 @@ export default class Runner { */ journey.callback({ params: params } as any); journey.monitor.update({ - ...this.monitor?.config, + ...this.monitor?.config, params: Object.keys(params).length ? params : undefined }); + + /* Only overwrite name and id values when using matrix */ + if (journey.matrix) { + journey.monitor.config.name = journey.name; + journey.monitor.config.id = journey.id; + } journey.monitor.validate(); monitors.push(journey.monitor); } @@ -453,11 +473,19 @@ export default class Runner { j.name = name; j.id = name; j.params = matrixParams; + j.matrix = matrix; this.addJourney(j); suite.addJourney(j); }); - }) - + }) + } + + getAllJourneys() { + const journeys = Array.from(this.suites.values()).reduce((acc, suite) => { + const suiteJourneys = suite.entries; + return [...acc, ...suiteJourneys]; + }, []); + return journeys; } async run(options: RunOptions) { @@ -477,10 +505,7 @@ export default class Runner { this.parseMatrix(options); - const journeys = Array.from(this.suites.values()).reduce((acc, suite) => { - const suiteJourneys = suite.entries; - return [...acc, ...suiteJourneys]; - }, []); + const journeys = this.getAllJourneys(); for (const journey of journeys) { /** diff --git a/src/loader.ts b/src/loader.ts index ec378308..2a3ad617 100644 --- a/src/loader.ts +++ b/src/loader.ts @@ -71,6 +71,7 @@ export async function loadTestFiles(options: CliArgs, args: string[]) { loadInlineScript(source); return; } + /** * Handle piped files by reading the STDIN * ex: ls example/suites/*.js | npx @elastic/synthetics