-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(pages): default to org visibility, cached thumbnails, owner + delete in the grid #6932
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
Open
harshithmullapudi
wants to merge
3
commits into
superset-sh:main
Choose a base branch
from
harshithmullapudi:fix/pages-v2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
90aa685
feat(pages): default new pages to org, add cached thumbnails, owner, …
harshithmullapudi 65fbe12
fix(pages): address review on thumbnail caching and share popover
harshithmullapudi ca8ed61
fix(pages): guard blank captures and recover from evicted thumbnails
harshithmullapudi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 @@ | ||
| export { createPageThumbnailRouter } from "./page-thumbnail"; |
21 changes: 21 additions & 0 deletions
21
apps/desktop/src/lib/trpc/routers/page-thumbnail/page-thumbnail.ts
This file contains hidden or 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,21 @@ | ||
| import { ensureThumbnail, peekThumbnail } from "main/lib/pageThumbnails"; | ||
| import { z } from "zod"; | ||
| import { publicProcedure, router } from "../.."; | ||
|
|
||
| export const createPageThumbnailRouter = () => { | ||
| return router({ | ||
| peek: publicProcedure | ||
| .input(z.object({ pageId: z.string(), version: z.string() })) | ||
| .query(({ input }) => peekThumbnail(input.pageId, input.version)), | ||
|
|
||
| ensure: publicProcedure | ||
| .input( | ||
| z.object({ | ||
| pageId: z.string(), | ||
| version: z.string(), | ||
| html: z.string(), | ||
| }), | ||
| ) | ||
| .mutation(({ input }) => ensureThumbnail(input)), | ||
| }); | ||
| }; |
This file contains hidden or 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 hidden or 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,7 @@ | ||
| export { | ||
| ensureThumbnail, | ||
| peekThumbnail, | ||
| THUMBNAIL_SCHEME, | ||
| thumbnailProtocolHandler, | ||
| thumbnailUrl, | ||
| } from "./pageThumbnails"; |
299 changes: 299 additions & 0 deletions
299
apps/desktop/src/main/lib/pageThumbnails/pageThumbnails.ts
This file contains hidden or 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,299 @@ | ||
| import { | ||
| mkdir, | ||
| readdir, | ||
| readFile, | ||
| stat, | ||
| unlink, | ||
| writeFile, | ||
| } from "node:fs/promises"; | ||
| import { join } from "node:path"; | ||
| import { app, BrowserWindow, session } from "electron"; | ||
| import { | ||
| PAGE_SCHEME, | ||
| pageProtocolHandler, | ||
| registerPageContent, | ||
| releasePageContent, | ||
| } from "../pageContent"; | ||
|
|
||
| export const THUMBNAIL_SCHEME = "superset-thumb"; | ||
|
|
||
| const THUMBNAIL_PARTITION = "page-thumbnails"; | ||
|
|
||
| const FRAME_WIDTH = 1280; | ||
| const FRAME_HEIGHT = 880; | ||
| const THUMBNAIL_WIDTH = 640; | ||
| const THUMBNAIL_HEIGHT = 440; | ||
| const JPEG_QUALITY = 80; | ||
|
|
||
| const MAX_CACHED_THUMBNAILS = 512; | ||
| const MAX_CONCURRENT_CAPTURES = 2; | ||
|
|
||
| const CAPTURE_DEADLINE_MS = 15_000; | ||
| const CAPTURE_ATTEMPT_TIMEOUT_MS = 1_500; | ||
| const CAPTURE_RETRY_INTERVAL_MS = 100; | ||
| const LOAD_TIMEOUT_MS = 10_000; | ||
| const SETTLE_MS = 400; | ||
|
|
||
| const PAGE_ID_PATTERN = /^[a-zA-Z0-9-]+$/; | ||
| const VERSION_PATTERN = /^\d+$/; | ||
|
|
||
| function cacheDir(): string { | ||
| return join(app.getPath("userData"), "page-thumbnails"); | ||
| } | ||
|
|
||
| function thumbnailPath(pageId: string, version: string): string { | ||
| return join(cacheDir(), `${pageId}-${version}.jpg`); | ||
| } | ||
|
|
||
| export function thumbnailUrl(pageId: string, version: string): string { | ||
| return `${THUMBNAIL_SCHEME}://${pageId}/${version}`; | ||
| } | ||
|
|
||
| function isValidKey(pageId: string, version: string): boolean { | ||
| return PAGE_ID_PATTERN.test(pageId) && VERSION_PATTERN.test(version); | ||
| } | ||
|
|
||
| function delay(ms: number): Promise<void> { | ||
| return new Promise((resolve) => setTimeout(resolve, ms)); | ||
| } | ||
|
|
||
| async function withTimeout<T>( | ||
| promise: Promise<T>, | ||
| ms: number, | ||
| message: string, | ||
| ): Promise<T> { | ||
| let timer: ReturnType<typeof setTimeout> | undefined; | ||
| try { | ||
| return await Promise.race([ | ||
| promise, | ||
| new Promise<never>((_resolve, reject) => { | ||
| timer = setTimeout(() => reject(new Error(message)), ms); | ||
| }), | ||
| ]); | ||
| } finally { | ||
| if (timer) clearTimeout(timer); | ||
| } | ||
| } | ||
|
|
||
| let activeCaptures = 0; | ||
| const captureQueue: Array<() => void> = []; | ||
|
|
||
| async function acquireCaptureSlot(): Promise<void> { | ||
| if (activeCaptures < MAX_CONCURRENT_CAPTURES) { | ||
| activeCaptures += 1; | ||
| return; | ||
| } | ||
| await new Promise<void>((resolve) => captureQueue.push(resolve)); | ||
| activeCaptures += 1; | ||
| } | ||
|
|
||
| function releaseCaptureSlot(): void { | ||
| activeCaptures -= 1; | ||
| const next = captureQueue.shift(); | ||
| if (next) next(); | ||
| } | ||
|
|
||
| async function captureWithRetry( | ||
| window: BrowserWindow, | ||
| ): Promise<Electron.NativeImage> { | ||
| const deadline = Date.now() + CAPTURE_DEADLINE_MS; | ||
| let lastError: unknown = null; | ||
| do { | ||
| if (window.isDestroyed()) break; | ||
| try { | ||
| const image = await withTimeout( | ||
| window.webContents.capturePage(), | ||
| CAPTURE_ATTEMPT_TIMEOUT_MS, | ||
| "Thumbnail capture attempt timed out", | ||
| ); | ||
| if (!image.isEmpty()) return image; | ||
| lastError = new Error("Thumbnail capture produced an empty image"); | ||
| } catch (error) { | ||
| lastError = error; | ||
| } | ||
| await delay(CAPTURE_RETRY_INTERVAL_MS); | ||
| } while (Date.now() < deadline); | ||
|
|
||
| throw lastError instanceof Error | ||
| ? lastError | ||
| : new Error("Thumbnail capture failed"); | ||
| } | ||
|
|
||
| let partitionReady = false; | ||
|
|
||
| function ensurePartitionProtocol(): void { | ||
| if (partitionReady) return; | ||
| partitionReady = true; | ||
| const partition = session.fromPartition(THUMBNAIL_PARTITION); | ||
| if (!partition.protocol.isProtocolHandled(PAGE_SCHEME)) { | ||
| partition.protocol.handle(PAGE_SCHEME, pageProtocolHandler); | ||
| } | ||
| } | ||
|
|
||
| async function captureHtml(html: string): Promise<Buffer> { | ||
| ensurePartitionProtocol(); | ||
| const { token, url } = registerPageContent(html); | ||
| const window = new BrowserWindow({ | ||
| show: false, | ||
| paintWhenInitiallyHidden: true, | ||
| width: FRAME_WIDTH, | ||
| height: FRAME_HEIGHT, | ||
| webPreferences: { | ||
| partition: THUMBNAIL_PARTITION, | ||
| backgroundThrottling: false, | ||
| contextIsolation: true, | ||
| nodeIntegration: false, | ||
| sandbox: true, | ||
| spellcheck: false, | ||
| }, | ||
| }); | ||
|
|
||
| try { | ||
| window.webContents.setAudioMuted(true); | ||
| await Promise.race([ | ||
| window.loadURL(url).catch(() => undefined), | ||
| delay(LOAD_TIMEOUT_MS), | ||
| ]); | ||
| if (window.isDestroyed()) { | ||
| throw new Error("Thumbnail window closed before capture"); | ||
| } | ||
| await delay(SETTLE_MS); | ||
| const image = await captureWithRetry(window); | ||
| return image | ||
| .resize({ | ||
| width: THUMBNAIL_WIDTH, | ||
| height: THUMBNAIL_HEIGHT, | ||
| quality: "good", | ||
| }) | ||
| .toJPEG(JPEG_QUALITY); | ||
| } finally { | ||
| releasePageContent(token); | ||
| if (!window.isDestroyed()) window.destroy(); | ||
| } | ||
| } | ||
|
|
||
| let pruning = false; | ||
|
|
||
| async function pruneCache(): Promise<void> { | ||
| if (pruning) return; | ||
| pruning = true; | ||
| try { | ||
| const dir = cacheDir(); | ||
| const names = await readdir(dir); | ||
| if (names.length <= MAX_CACHED_THUMBNAILS) return; | ||
|
|
||
| const entries = await Promise.all( | ||
| names.map(async (name) => { | ||
| try { | ||
| const info = await stat(join(dir, name)); | ||
| return { name, mtimeMs: info.mtimeMs }; | ||
| } catch { | ||
| return null; | ||
| } | ||
| }), | ||
| ); | ||
|
|
||
| const sorted = entries | ||
| .filter((entry): entry is { name: string; mtimeMs: number } => | ||
| Boolean(entry), | ||
| ) | ||
| .sort((a, b) => a.mtimeMs - b.mtimeMs); | ||
|
|
||
| const excess = sorted.length - MAX_CACHED_THUMBNAILS; | ||
| await Promise.all( | ||
| sorted | ||
| .slice(0, excess) | ||
| .map((entry) => unlink(join(dir, entry.name)).catch(() => undefined)), | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| ); | ||
| } catch { | ||
| return; | ||
| } finally { | ||
| pruning = false; | ||
| } | ||
| } | ||
|
|
||
| async function hasThumbnail(pageId: string, version: string): Promise<boolean> { | ||
| try { | ||
| await stat(thumbnailPath(pageId, version)); | ||
| return true; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| export async function peekThumbnail( | ||
| pageId: string, | ||
| version: string, | ||
| ): Promise<string | null> { | ||
| if (!isValidKey(pageId, version)) return null; | ||
| return (await hasThumbnail(pageId, version)) | ||
| ? thumbnailUrl(pageId, version) | ||
| : null; | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| const inflight = new Map<string, Promise<string>>(); | ||
|
|
||
| export function ensureThumbnail({ | ||
| pageId, | ||
| version, | ||
| html, | ||
| }: { | ||
| pageId: string; | ||
| version: string; | ||
| html: string; | ||
| }): Promise<string> { | ||
| if (!isValidKey(pageId, version)) { | ||
| return Promise.reject(new Error("Invalid thumbnail key")); | ||
| } | ||
|
|
||
| const key = `${pageId}:${version}`; | ||
| const existing = inflight.get(key); | ||
| if (existing) return existing; | ||
|
|
||
| const pending: Promise<string> = (async () => { | ||
| if (await hasThumbnail(pageId, version)) { | ||
| return thumbnailUrl(pageId, version); | ||
| } | ||
|
|
||
| await acquireCaptureSlot(); | ||
| try { | ||
| const jpeg = await captureHtml(html); | ||
| await mkdir(cacheDir(), { recursive: true }); | ||
| await writeFile(thumbnailPath(pageId, version), jpeg); | ||
| void pruneCache(); | ||
| return thumbnailUrl(pageId, version); | ||
| } finally { | ||
| releaseCaptureSlot(); | ||
| } | ||
| })().finally(() => { | ||
| if (inflight.get(key) === pending) inflight.delete(key); | ||
| }); | ||
|
|
||
| inflight.set(key, pending); | ||
| return pending; | ||
| } | ||
|
|
||
| export async function thumbnailProtocolHandler( | ||
| request: Request, | ||
| ): Promise<Response> { | ||
| const url = new URL(request.url); | ||
| const pageId = url.hostname; | ||
| const version = url.pathname.replace(/^\//, ""); | ||
|
|
||
| if (!isValidKey(pageId, version)) { | ||
| return new Response("Not found", { status: 404 }); | ||
| } | ||
|
|
||
| try { | ||
| const bytes = await readFile(thumbnailPath(pageId, version)); | ||
| return new Response(new Uint8Array(bytes), { | ||
| status: 200, | ||
| headers: { | ||
| "Content-Type": "image/jpeg", | ||
| "Cache-Control": "public, max-age=31536000, immutable", | ||
| }, | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| }); | ||
| } catch { | ||
| return new Response("Not found", { status: 404 }); | ||
| } | ||
| } | ||
2 changes: 1 addition & 1 deletion
2
...esktop/src/renderer/routes/_authenticated/_dashboard/components/PageViewer/PageViewer.tsx
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.