Skip to content

Commit

Permalink
refactor to tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
jloleysens committed Jun 1, 2021
1 parent 6ae4825 commit 5b2d07d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { ConditionalHeaders } from '../../../common';
import { PdfMaker } from './pdf';
import { getTracker } from './tracker';

const REDIRECT_APP_URL = 'temp';

const getTimeRange = (urlScreenshots: ScreenshotResults[]) => {
const grouped = groupBy(urlScreenshots.map((u) => u.timeRange));
const values = Object.values(grouped);
Expand Down Expand Up @@ -51,7 +53,7 @@ export async function generatePdfObservableFactory(reporting: ReportingCore) {
tracker.startScreenshots();
const screenshots$ = getScreenshots({
logger,
locators,
urls: locators.map<[url: string, locator: Locator]>((locator) => [REDIRECT_APP_URL, locator]),
conditionalHeaders,
layout,
browserTimezone,
Expand Down
3 changes: 1 addition & 2 deletions x-pack/plugins/reporting/server/lib/screenshots/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ export { screenshotsObservableFactory } from './observable';

export interface ScreenshotObservableOpts {
logger: LevelLogger;
urls: string[];
locators?: Locator[];
urls: string[] | Array<[url: string, locator: Locator]>;
conditionalHeaders: ConditionalHeaders;
layout: LayoutInstance;
browserTimezone?: string;
Expand Down
27 changes: 12 additions & 15 deletions x-pack/plugins/reporting/server/lib/screenshots/observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
*/

import apm from 'elastic-apm-node';
import assert from 'assert';
import * as Rx from 'rxjs';
import { catchError, concatMap, first, mergeMap, take, takeUntil, toArray } from 'rxjs/operators';
import { Locator } from '../../../common/types';
import { HeadlessChromiumDriverFactory } from '../../browsers';
import { CaptureConfig } from '../../types';
import {
Expand Down Expand Up @@ -37,16 +37,13 @@ interface ScreenSetupData {
error?: Error;
}

const REDIRECT_APP_URL = 'temp';

export function screenshotsObservableFactory(
captureConfig: CaptureConfig,
browserDriverFactory: HeadlessChromiumDriverFactory
): ScreenshotsObservableFn {
return function screenshotsObservable({
logger,
urls,
locators,
urls: urlsOrTuples,
conditionalHeaders,
layout,
browserTimezone,
Expand All @@ -59,23 +56,23 @@ export function screenshotsObservableFactory(
logger
);

// TODO: Delete this code once we have migrated to the new PDF/PNG export type
let finalUrls = urls;
if (locators) {
finalUrls = locators.map(() => REDIRECT_APP_URL);
logger.debug(`Detected locators, browser will navigate to ${REDIRECT_APP_URL} for redirect`);
}

return create$.pipe(
mergeMap(({ driver, exit$ }) => {
apmCreatePage?.end();
exit$.subscribe({ error: () => apmTrans?.end() });

return Rx.from(finalUrls).pipe(
concatMap((url, index) => {
return Rx.from(urlsOrTuples).pipe(
concatMap((urlOrTuple, index) => {
const setup$: Rx.Observable<ScreenSetupData> = Rx.of(1).pipe(
mergeMap(() => {
const locator = locators && locators[index];
let url: string;
let locator: undefined | Locator;
if (Array.isArray(urlOrTuple)) {
[url, locator] = urlOrTuple;
} else {
url = urlOrTuple;
}
// const locator = locators && locators[index];
// If we're moving to another page in the app, we'll want to wait for the app to tell us
// it's loaded the next page.
const p = index + 1;
Expand Down

0 comments on commit 5b2d07d

Please sign in to comment.