-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1746 from OneUptime/take-screenshot-on-incident-c…
…reate Take screenshot on incident create
- Loading branch information
Showing
26 changed files
with
574 additions
and
275 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
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
17 changes: 17 additions & 0 deletions
17
Common/Server/Infrastructure/Postgres/SchemaMigrations/1731433043136-MigrationName.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,17 @@ | ||
import { MigrationInterface, QueryRunner } from "typeorm"; | ||
|
||
export class MigrationName1731433043136 implements MigrationInterface { | ||
public name = "MigrationName1731433043136"; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`DROP INDEX "public"."IDX_5218e92f700d91afe6a8db79cb"`, | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`CREATE INDEX "IDX_5218e92f700d91afe6a8db79cb" ON "Incident" ("rootCause") `, | ||
); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
Common/Server/Infrastructure/Postgres/SchemaMigrations/1731433309124-MigrationName.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,17 @@ | ||
import { MigrationInterface, QueryRunner } from "typeorm"; | ||
|
||
export class MigrationName1731433309124 implements MigrationInterface { | ||
public name = "MigrationName1731433309124"; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`DROP INDEX "public"."IDX_fc40ea6a9ad55f29bca4f4a15d"`, | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`CREATE INDEX "IDX_fc40ea6a9ad55f29bca4f4a15d" ON "Alert" ("rootCause") `, | ||
); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
Common/Server/Infrastructure/Postgres/SchemaMigrations/1731435267537-MigrationName.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,17 @@ | ||
import { MigrationInterface, QueryRunner } from "typeorm"; | ||
|
||
export class MigrationName1731435267537 implements MigrationInterface { | ||
public name = "MigrationName1731435267537"; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`DROP INDEX "public"."IDX_01ac1d1ef9e72aeb6dac6575dd"`, | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`CREATE INDEX "IDX_01ac1d1ef9e72aeb6dac6575dd" ON "MonitorStatusTimeline" ("rootCause") `, | ||
); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
Common/Server/Infrastructure/Postgres/SchemaMigrations/1731435514287-MigrationName.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,17 @@ | ||
import { MigrationInterface, QueryRunner } from "typeorm"; | ||
|
||
export class MigrationName1731435514287 implements MigrationInterface { | ||
public name = "MigrationName1731435514287"; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`DROP INDEX "public"."IDX_7db6b1a8fbbc9eb44c2e7f5047"`, | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`CREATE INDEX "IDX_7db6b1a8fbbc9eb44c2e7f5047" ON "IncidentStateTimeline" ("rootCause") `, | ||
); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
import { | ||
Page as PlaywrightPage, | ||
Browser as PlaywrightBrowser, | ||
chromium, | ||
firefox, | ||
} from "playwright"; | ||
import LocalFile from "./LocalFile"; | ||
import BadDataException from "../../Types/Exception/BadDataException"; | ||
import ScreenSizeType from "../../Types/ScreenSizeType"; | ||
import BrowserType from "../../Types/BrowserType"; | ||
import logger from "./Logger"; | ||
|
||
export type Page = PlaywrightPage; | ||
export type Browser = PlaywrightBrowser; | ||
|
||
export default class BrowserUtil { | ||
public static async convertHtmlToBase64Screenshot(data: { | ||
html: string; | ||
}): Promise<string | null> { | ||
try { | ||
const html: string = data.html; | ||
|
||
const pageAndBrowser: { | ||
page: Page; | ||
browser: Browser; | ||
} = await BrowserUtil.getPageByBrowserType({ | ||
browserType: BrowserType.Chromium, | ||
screenSizeType: ScreenSizeType.Desktop, | ||
}); | ||
|
||
const page: Page = pageAndBrowser.page; | ||
const browser: Browser = pageAndBrowser.browser; | ||
await page.setContent(html); | ||
const screenshot: Buffer = await page.screenshot({ type: "png" }); | ||
|
||
await browser.close(); | ||
|
||
return screenshot.toString("base64"); | ||
} catch (e) { | ||
logger.debug(e); | ||
return null; | ||
} | ||
} | ||
|
||
public static async getPageByBrowserType(data: { | ||
browserType: BrowserType; | ||
screenSizeType: ScreenSizeType; | ||
}): Promise<{ | ||
page: Page; | ||
browser: Browser; | ||
}> { | ||
const viewport: { | ||
height: number; | ||
width: number; | ||
} = BrowserUtil.getViewportHeightAndWidth({ | ||
screenSizeType: data.screenSizeType, | ||
}); | ||
|
||
let page: Page | null = null; | ||
let browser: Browser | null = null; | ||
|
||
if (data.browserType === BrowserType.Chromium) { | ||
browser = await chromium.launch({ | ||
executablePath: await BrowserUtil.getChromeExecutablePath(), | ||
}); | ||
page = await browser.newPage(); | ||
} | ||
|
||
if (data.browserType === BrowserType.Firefox) { | ||
browser = await firefox.launch({ | ||
executablePath: await BrowserUtil.getFirefoxExecutablePath(), | ||
}); | ||
page = await browser.newPage(); | ||
} | ||
|
||
// if (data.browserType === BrowserType.Webkit) { | ||
// browser = await webkit.launch(); | ||
// page = await browser.newPage(); | ||
// } | ||
|
||
await page?.setViewportSize({ | ||
width: viewport.width, | ||
height: viewport.height, | ||
}); | ||
|
||
if (!browser) { | ||
throw new BadDataException("Invalid Browser Type."); | ||
} | ||
|
||
if (!page) { | ||
// close the browser if page is not created | ||
await browser.close(); | ||
throw new BadDataException("Invalid Browser Type."); | ||
} | ||
|
||
return { | ||
page: page, | ||
browser: browser, | ||
}; | ||
} | ||
|
||
public static getViewportHeightAndWidth(options: { | ||
screenSizeType: ScreenSizeType; | ||
}): { | ||
height: number; | ||
width: number; | ||
} { | ||
let viewPortHeight: number = 0; | ||
let viewPortWidth: number = 0; | ||
|
||
switch (options.screenSizeType) { | ||
case ScreenSizeType.Desktop: | ||
viewPortHeight = 1080; | ||
viewPortWidth = 1920; | ||
break; | ||
case ScreenSizeType.Mobile: | ||
viewPortHeight = 640; | ||
viewPortWidth = 360; | ||
break; | ||
case ScreenSizeType.Tablet: | ||
viewPortHeight = 768; | ||
viewPortWidth = 1024; | ||
break; | ||
default: | ||
viewPortHeight = 1080; | ||
viewPortWidth = 1920; | ||
break; | ||
} | ||
|
||
return { height: viewPortHeight, width: viewPortWidth }; | ||
} | ||
|
||
public static async getChromeExecutablePath(): Promise<string> { | ||
const doesDirectoryExist: boolean = await LocalFile.doesDirectoryExist( | ||
"/root/.cache/ms-playwright", | ||
); | ||
if (!doesDirectoryExist) { | ||
throw new BadDataException("Chrome executable path not found."); | ||
} | ||
|
||
// get list of files in the directory | ||
const directories: string[] = await LocalFile.getListOfDirectories( | ||
"/root/.cache/ms-playwright", | ||
); | ||
|
||
if (directories.length === 0) { | ||
throw new BadDataException("Chrome executable path not found."); | ||
} | ||
|
||
const chromeInstallationName: string | undefined = directories.find( | ||
(directory: string) => { | ||
return directory.includes("chromium"); | ||
}, | ||
); | ||
|
||
if (!chromeInstallationName) { | ||
throw new BadDataException("Chrome executable path not found."); | ||
} | ||
|
||
return `/root/.cache/ms-playwright/${chromeInstallationName}/chrome-linux/chrome`; | ||
} | ||
|
||
public static async getFirefoxExecutablePath(): Promise<string> { | ||
const doesDirectoryExist: boolean = await LocalFile.doesDirectoryExist( | ||
"/root/.cache/ms-playwright", | ||
); | ||
if (!doesDirectoryExist) { | ||
throw new BadDataException("Firefox executable path not found."); | ||
} | ||
|
||
// get list of files in the directory | ||
const directories: string[] = await LocalFile.getListOfDirectories( | ||
"/root/.cache/ms-playwright", | ||
); | ||
|
||
if (directories.length === 0) { | ||
throw new BadDataException("Firefox executable path not found."); | ||
} | ||
|
||
const firefoxInstallationName: string | undefined = directories.find( | ||
(directory: string) => { | ||
return directory.includes("firefox"); | ||
}, | ||
); | ||
|
||
if (!firefoxInstallationName) { | ||
throw new BadDataException("Firefox executable path not found."); | ||
} | ||
|
||
return `/root/.cache/ms-playwright/${firefoxInstallationName}/firefox/firefox`; | ||
} | ||
} |
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
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
Oops, something went wrong.