-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fbbd5c1
commit d02a24d
Showing
13 changed files
with
346 additions
and
176 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
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
66 changes: 0 additions & 66 deletions
66
x-pack/plugins/reporting/server/browsers/chromium/driver/screenshot_stitcher/combine.js
This file was deleted.
Oops, something went wrong.
101 changes: 101 additions & 0 deletions
101
x-pack/plugins/reporting/server/browsers/chromium/driver/screenshot_stitcher/combine.ts
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,101 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
// No types found for this package. May want to investigate an alternative with types. | ||
// @ts-ignore: implicit any for JS file | ||
import $streamToObservable from '@samverschueren/stream-to-observable'; | ||
import { PNG } from 'pngjs'; | ||
import * as Rx from 'rxjs'; | ||
import { ObservableInput } from 'rxjs'; | ||
import { map, mergeMap, reduce, switchMap, tap, toArray } from 'rxjs/operators'; | ||
import { Logger, Screenshot, Size } from './types'; | ||
|
||
// if we're only given one screenshot, and it matches the output dimensions | ||
// we're going to skip the combination and just use it | ||
const canUseFirstScreenshot = ( | ||
screenshots: Screenshot[], | ||
size: { width: number; height: number } | ||
) => { | ||
if (screenshots.length !== 1) { | ||
return false; | ||
} | ||
|
||
const firstScreenshot = screenshots[0]; | ||
return ( | ||
firstScreenshot.dimensions.width === size.width && | ||
firstScreenshot.dimensions.height === size.height | ||
); | ||
}; | ||
|
||
/** | ||
* Combines the screenshot clips into a single screenshot of size `outputDimensions`. | ||
* @param screenshots - Array of screenshots to combine | ||
* @param outputSize - Final output size that the screenshots should match up with | ||
* @param logger - logger for extra debug output | ||
*/ | ||
export function $combine( | ||
screenshots: Screenshot[], | ||
outputSize: Size, | ||
logger: Logger | ||
): Rx.Observable<string> { | ||
logger.debug( | ||
`Combining screenshot clips into final, scaled output dimension of ${JSON.stringify( | ||
outputSize | ||
)}` | ||
); | ||
|
||
if (screenshots.length === 0) { | ||
return Rx.throwError('Unable to combine 0 screenshots'); | ||
} | ||
|
||
if (canUseFirstScreenshot(screenshots, outputSize)) { | ||
return Rx.of(screenshots[0].data); | ||
} | ||
|
||
// Turn the screenshot data into actual PNGs | ||
const pngs$ = Rx.from(screenshots).pipe( | ||
mergeMap( | ||
(screenshot: Screenshot): ObservableInput<PNG> => { | ||
const png = new PNG(); | ||
const buffer = Buffer.from(screenshot.data, 'base64'); | ||
const parseAsObservable = Rx.bindNodeCallback(png.parse.bind(png)); | ||
return parseAsObservable(buffer); | ||
}, | ||
(screenshot: Screenshot, png: PNG) => ({ screenshot, png }) | ||
) | ||
); | ||
|
||
const output$ = pngs$.pipe( | ||
reduce((output: PNG, input: { screenshot: Screenshot; png: PNG }) => { | ||
const { png, screenshot } = input; | ||
// Spitting out a lot of output to help debug https://github.com/elastic/kibana/issues/19563. Once that is | ||
// fixed, this should probably get pared down. | ||
logger.debug(`Output dimensions is ${JSON.stringify(outputSize)}`); | ||
logger.debug(`Input png w: ${png.width} and h: ${png.height}`); | ||
logger.debug( | ||
`Creating output png with ${JSON.stringify(screenshot.dimensions)}` | ||
); | ||
const { dimensions } = screenshot; | ||
png.bitblt( | ||
output, | ||
0, | ||
0, | ||
dimensions.width, | ||
dimensions.height, | ||
dimensions.x, | ||
dimensions.y | ||
); | ||
return output; | ||
}, new PNG({ width: outputSize.width, height: outputSize.height })) | ||
); | ||
|
||
return output$.pipe( | ||
tap(png => png.pack()), | ||
switchMap<PNG, Buffer>($streamToObservable), | ||
toArray(), | ||
map((chunks: Buffer[]) => Buffer.concat(chunks).toString('base64')) | ||
); | ||
} |
25 changes: 0 additions & 25 deletions
25
x-pack/plugins/reporting/server/browsers/chromium/driver/screenshot_stitcher/get_clips.js
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.