Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Previews: support creating thumbnails and full-height screenshots #312

Merged
merged 24 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
8434842
allow scaled thumbnails
ryantxu Jan 6, 2022
44939a8
added support for full page images
ArturWierzbicki Jan 9, 2022
402223b
add `acceptBeforeUnload` handler
ArturWierzbicki Jan 10, 2022
72f315d
use png files
ryantxu Jan 11, 2022
1bcb5c6
produce requested thumbnail size
ryantxu Jan 11, 2022
35d9eef
Update src/browser/browser.ts
ArturWierzbicki Jan 18, 2022
b845641
review fix: move variable away from misleading comment
ArturWierzbicki Jan 18, 2022
7cb0a4c
review fix: added default `headed` option to the default and dev env …
ArturWierzbicki Jan 18, 2022
1369770
review fix: removed ternary expression
ArturWierzbicki Jan 18, 2022
fc308a3
review fix: remove `withFullPageViewport`
ArturWierzbicki Jan 18, 2022
efa7e73
review fix: fixed full page screenshots for non-kiosk mode
ArturWierzbicki Jan 19, 2022
aa56dcb
add script downloading platform-specific sharp binary
ArturWierzbicki Jan 20, 2022
8fe5525
update yarnlock
ArturWierzbicki Jan 20, 2022
bc6f4af
fix package_Target script
ArturWierzbicki Jan 20, 2022
f01214f
update circleci node version
ArturWierzbicki Jan 20, 2022
23ae8ad
update package json
ArturWierzbicki Jan 20, 2022
9310287
fix package.json -> pkg.assets paths
ArturWierzbicki Jan 20, 2022
ab07b2d
downgrade pkg to fix sharp build issue
ArturWierzbicki Jan 21, 2022
abd24d0
working m1 (#318)
ArturWierzbicki Jan 24, 2022
7f176e0
replace `mkdir -p` with `fs.mkdirSync {recursive: true}`
ArturWierzbicki Jan 26, 2022
57b8115
Merge branch 'scaled-thumb-result' of https://github.com/grafana/graf…
ArturWierzbicki Jan 26, 2022
cd6f79e
Update scripts/download_sharp.js
ArturWierzbicki Jan 26, 2022
e5bf620
Update scripts/download_sharp.js
ArturWierzbicki Jan 26, 2022
4bcf42a
Update scripts/package_target.sh
ArturWierzbicki Jan 26, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ aliases:
defaults: &defaults
working_directory: ~/grafana-image-renderer
docker:
- image: circleci/node:14.16.1-stretch
- image: cimg/node:14.18.3

commands:
install-grabpl:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ dist/**/*

# Ignore output from coverage report
coverage

scripts/sharp-downloader
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 3.4.0 (--)
- Support full height dashboards and scaled thumbnails [#312](https://github.com/grafana/grafana-image-renderer/pull/312)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AgnesToulet what is the process for documenting the changes here?

The behavior changes are:

  1. negative scale will create a scaled down thumbnail
  2. height=-1 will create an image as tall as it needs to be based on the webpage size

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation for the image renderer is in the grafana repo: https://github.com/grafana/grafana/blob/main/docs/sources/image-rendering but we don't really have documentation on how to call the image renderer (more on how to configure and run it).

I guess we could add a section in the main file or a new file for that. @achatterjee-grafana Do you have any opinion on that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good @AgnesToulet. Let me know if you need any help.


## 3.3.0 (2021-11-18)

- Chore: Bump pkg from 5.3.3 to 5.4.1 [#305](https://github.com/grafana/grafana-image-renderer/pull/305), [AgnesToulet](https://github.com/AgnesToulet)
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
ARCH = darwin-x64-unknown
SKIP_CHROMIUM =
OUT =
SKIP_SHARP_DOWNLOAD =
DOCKER_TAG = dev

all: clean build
Expand All @@ -23,7 +24,7 @@ clean_package:
./scripts/clean_target.sh ${ARCH} ${OUT}

package:
./scripts/package_target.sh ${ARCH} ${SKIP_CHROMIUM} ${OUT}
./scripts/package_target.sh ${ARCH} ${SKIP_CHROMIUM} ${OUT} ${SKIP_SHARP_DOWNLOAD}

archive:
./scripts/archive_target.sh ${ARCH} ${OUT}
Expand Down
1 change: 1 addition & 0 deletions default.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"maxWidth": 3080,
"maxHeight": 3000,
"maxDeviceScaleFactor": 4,
"headed": false,

"mode": "default",
"clustering": {
Expand Down
1 change: 1 addition & 0 deletions dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"maxWidth": 1980,
"maxHeight": 8000,
"maxDeviceScaleFactor": 10,
"headed": false,

"mode": "default",
"clustering": {
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"prom-client": "^11.5.3",
"puppeteer": "^10.0.0",
"puppeteer-cluster": "^0.22.0",
"sharp": "0.29.3",
"unique-filename": "^1.1.0",
"winston": "^3.2.1"
},
Expand All @@ -49,7 +50,7 @@
"eslint-plugin-react-hooks": "^4.2.0",
"husky": "^4.3.8",
"lint-staged": "^11.2.0",
"pkg": "^5.4.1",
"pkg": "5.5.2",
"prettier": "2.2.1",
"tsc-watch": "^4.2.3",
"typescript": "^4.3.2"
Expand All @@ -72,7 +73,7 @@
},
"bin": "build/app.js",
"engines": {
"node": ">=14 <15"
"node": ">=14 <=16"
},
"volta": {
"node": "14.16.1"
Expand Down
42 changes: 42 additions & 0 deletions scripts/download_sharp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const child_process = require('child_process');
const fs = require('fs')

const archArg = process.argv[2];

// https://sharp.pixelplumbing.com/install#cross-platform
const [
platform, // linux, darwin, win32
arch // x64, ia32, arm, arm64
] = archArg.split('-');

const packageJson = JSON.parse(fs.readFileSync('./package.json'))

const packageName = 'sharp';
const packageVersion = Object.entries(packageJson.dependencies).find(([depName]) => depName === packageName)[1]

console.log(`Found ${packageName} with version ${packageVersion}`)

const targetNodeModules = './node_modules'
const currentSharp = `${targetNodeModules}/sharp`
const sharpDownloader = './scripts/sharp-downloader'

if(!fs.existsSync(sharpDownloader)) {
fs.mkdirSync(sharpDownloader)
}

fs.writeFileSync(`${sharpDownloader}/package.json`, JSON.stringify({
"name": "sharp-downloader",
"dependencies": {
[packageName]: packageVersion
}
}
))

const downloadedSharp = `${sharpDownloader}/node_modules/sharp`

try {
child_process.execSync(`cd ${sharpDownloader} && npm install --platform=${platform} --arch=${arch}`, {stdio: 'inherit'})
child_process.execSync(`rm -rf ${currentSharp} && cp -RP ${downloadedSharp} ${targetNodeModules}`);
} finally {
fs.rmSync(sharpDownloader, {recursive: true, force: true})
}
7 changes: 7 additions & 0 deletions scripts/package_target.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@
ARCH="${1:-}"
SKIP_CHROMIUM=${2:-false}
OUT="${3:-}"
SKIP_SHARP_DOWNLOAD=${4:-false}

if [ -z "$ARCH" ]; then
echo "ARCH (arg 1) has to be set"
exit 1
fi

if [ ${SKIP_SHARP_DOWNLOAD} = false ]; then
node scripts/download_sharp.js ${ARCH} ${OUT}
else
echo "Skipping sharp download"
fi

mkdir -p dist
node scripts/pkg.js ${ARCH} ${OUT}
if [ $? != 0 ]; then
Expand Down
13 changes: 12 additions & 1 deletion scripts/pkg.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const childProcess = require('child_process');
const fs = require('fs')

const archArg = process.argv[2];
let [
Expand All @@ -22,6 +23,16 @@ const archTransform = {

platform = platformTransform[platform] || platform;
arch = archTransform[arch] || arch;

if(platform === 'macos' && (arch.includes('arm'))) {
arch = 'arm64'
}

const outputPath = "dist/" + (process.argv[3] || `plugin-${archArg}`);
const outputNodeModules = `${outputPath}/node_modules`

childProcess.execSync(`"./node_modules/.bin/pkg" -t node14-${platform}-${arch} . --out-path ${outputPath} --no-native-build`, {stdio: 'inherit'});

childProcess.execSync(`"./node_modules/.bin/pkg" -t node14-${platform}-${arch} . --out-path ${outputPath}`, {stdio: 'inherit'});
childProcess.execSync(`rm -rf ${outputNodeModules}`)
fs.mkdirSync(`${outputNodeModules}/sharp`, {recursive: true})
childProcess.execSync(`cp -RP ./node_modules/sharp/build ${outputNodeModules}/sharp && cp -RP ./node_modules/sharp/vendor ${outputNodeModules}/sharp`)
19 changes: 14 additions & 5 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import * as minimist from 'minimist';
import { defaultPluginConfig, defaultServiceConfig, readJSONFileSync, PluginConfig, ServiceConfig } from './config';
import { serve } from './node-plugin';

const chromeFolderPrefix = 'chrome-';

async function main() {
const argv = minimist(process.argv.slice(2));
const env = Object.assign({}, process.env);
Expand All @@ -19,12 +21,19 @@ async function main() {
populatePluginConfigFromEnv(config, env);
if (!config.rendering.chromeBin && (process as any).pkg) {
//@ts-ignore
const parts = puppeteer.executablePath().split(path.sep);
while (!parts[0].startsWith('chrome-')) {
parts.shift();
const executablePath = puppeteer.executablePath() as string;

if (executablePath.includes(chromeFolderPrefix)) {
const parts = executablePath.split(path.sep);
while (!parts[0].startsWith(chromeFolderPrefix)) {
parts.shift();
}

config.rendering.chromeBin = [path.dirname(process.execPath), ...parts].join(path.sep);
} else {
// local chrome installation in dev mode
config.rendering.chromeBin = env['PUPPETEER_EXECUTABLE_PATH'];
}

config.rendering.chromeBin = [path.dirname(process.execPath), ...parts].join(path.sep);
}

await serve({
Expand Down
Loading