Skip to content

Commit

Permalink
Zoom page: Add CLI options --window-size and --zoom-factor
Browse files Browse the repository at this point in the history
I originally sent ctrl+ to browser to zoom, but the screenshot un-zooms
so I found this method instead.
  • Loading branch information
intermittentnrg authored and amotl committed Dec 3, 2024
1 parent 01dfd7c commit 2ade270
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
4 changes: 1 addition & 3 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ grafanimate changelog

in progress
===========

2024-12-03 0.8.1
================
- Optionally configure Firefox location using ``FIREFOX_BIN``
environment variable. Thanks, @gogglespisano and @intermittentnrg.
- Added CLI options ``--window-size`` and ``--zoom-factor``

2024-12-03 0.8.0
================
Expand Down
7 changes: 7 additions & 0 deletions grafanimate/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import os
import typing as t
from ast import literal_eval
from pathlib import Path

from docopt import DocoptExit, docopt
Expand Down Expand Up @@ -50,6 +51,8 @@ def run():
--dashboard-uid=<uid> Grafana dashboard UID.
Layout and scene options:
--window-size=<window-size> Customize window size, e.g. `(1920, 1180)`
--zoom-factor=<zoom-factor> Adjust zoom factor of page. [default: 1.0]
--use-panel-events Whether to enable using Grafana's panel events. [default: false]
Caveat: Does not work for "d-solo" panels
Expand Down Expand Up @@ -155,6 +158,10 @@ def run():
options["headless"] = asbool(options["headless"])
if options["use-panel-events"]:
options["exposure-time"] = 0
if options["window-size"]:
options["window-size"] = literal_eval(options["window-size"])
if options["zoom-factor"]:
options["zoom-factor"] = float(options["zoom-factor"])

# Prepare rendering options.
render_options = RenderingOptions(
Expand Down
2 changes: 2 additions & 0 deletions grafanimate/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ def make_grafana(
grafana = GrafanaWrapper(
baseurl=str(url),
use_panel_events=options["use-panel-events"],
window_size=options["window-size"],
zoom_factor=options["zoom-factor"],
)
grafana.boot_firefox(headless=headless)
grafana.boot_grafana()
Expand Down
19 changes: 16 additions & 3 deletions grafanimate/grafana.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ def __init__(
self,
baseurl: t.Optional[str] = None,
use_panel_events: bool = True,
window_size: t.Optional[tuple[int, int]] = None,
zoom_factor: float = 1.0,
dry_run: bool = False,
):
self.baseurl = baseurl
self.use_panel_events = use_panel_events
self.window_size = window_size
self.zoom_factor = zoom_factor
self.dry_run = dry_run
log.info("Starting GrafanaWrapper on %s", baseurl)
FirefoxMarionetteBase.__init__(self)
Expand All @@ -38,12 +42,17 @@ def boot_grafana(self):
Navigate to Grafana application and inject Grafana Sidecar service.
"""
log.info("Starting Grafana at %s", self.baseurl)
self.set_window_size(1920, 1080)

if self.window_size:
self.set_window_size(
int(self.window_size[0]),
int(self.window_size[1] + (85 * self.zoom_factor)),
)
self.navigate(self.baseurl)

rect = self.get_window_rect()
self.marionette.set_window_rect(height=rect["height"], width=rect["width"])
self.marionette.set_window_rect(
height=int(rect["height"]), width=int(rect["width"])
)

def navigate(self, url):
# Navigate to resource URL.
Expand All @@ -52,6 +61,10 @@ def navigate(self, url):
# Wait for Grafana application to load.
self.wait_for_grafana()

# Apply zoom factor to page.
if self.zoom_factor:
self.marionette.set_pref("layout.css.devPixelsPerPx", str(self.zoom_factor))

# Load Javascript for GrafanaStudio sidecar service.
jsfiles = ["grafana-util.js", "grafana-studio.js"]
for jsfile in jsfiles:
Expand Down

0 comments on commit 2ade270

Please sign in to comment.