mirror of https://github.com/buster-so/buster.git
should take screenshot logic
This commit is contained in:
parent
600f31faff
commit
f152075917
|
@ -3,9 +3,10 @@ import type { TakeMetricScreenshotTrigger } from '@buster-app/trigger/task-schem
|
||||||
import { getUserOrganizationId } from '@buster/database/queries';
|
import { getUserOrganizationId } from '@buster/database/queries';
|
||||||
import { MetricDataParamsSchema, MetricDataQuerySchema } from '@buster/server-shared';
|
import { MetricDataParamsSchema, MetricDataQuerySchema } from '@buster/server-shared';
|
||||||
import { zValidator } from '@hono/zod-validator';
|
import { zValidator } from '@hono/zod-validator';
|
||||||
|
import { shouldTakeScreenshot } from '@shared-helpers/screenshots';
|
||||||
import { runs, tasks } from '@trigger.dev/sdk';
|
import { runs, tasks } from '@trigger.dev/sdk';
|
||||||
|
import dayjs from 'dayjs';
|
||||||
import { Hono } from 'hono';
|
import { Hono } from 'hono';
|
||||||
import { id } from 'zod/v4/locales';
|
|
||||||
import { standardErrorHandler } from '../../../../../utils/response';
|
import { standardErrorHandler } from '../../../../../utils/response';
|
||||||
import { getMetricDataHandler } from './get-metric-data';
|
import { getMetricDataHandler } from './get-metric-data';
|
||||||
|
|
||||||
|
@ -16,8 +17,7 @@ const app = new Hono()
|
||||||
zValidator('query', MetricDataQuerySchema),
|
zValidator('query', MetricDataQuerySchema),
|
||||||
async (c) => {
|
async (c) => {
|
||||||
const { id } = c.req.valid('param');
|
const { id } = c.req.valid('param');
|
||||||
const { limit, version_number, report_file_id, password, is_screenshot } =
|
const { limit, version_number, report_file_id, password } = c.req.valid('query');
|
||||||
c.req.valid('query');
|
|
||||||
const user = c.get('busterUser');
|
const user = c.get('busterUser');
|
||||||
|
|
||||||
const response = await getMetricDataHandler(
|
const response = await getMetricDataHandler(
|
||||||
|
@ -30,16 +30,13 @@ const app = new Hono()
|
||||||
);
|
);
|
||||||
|
|
||||||
const tag = `take-metric-screenshot-${id}-${version_number}`;
|
const tag = `take-metric-screenshot-${id}-${version_number}`;
|
||||||
const lastTask = await runs
|
if (
|
||||||
.list({
|
await shouldTakeScreenshot({
|
||||||
status: ['EXECUTING', 'QUEUED'],
|
|
||||||
taskIdentifier: screenshots_task_keys.take_metric_screenshot,
|
|
||||||
tag,
|
tag,
|
||||||
limit: 1,
|
key: screenshots_task_keys.take_metric_screenshot,
|
||||||
|
context: c,
|
||||||
})
|
})
|
||||||
.then((res) => res.data[0]);
|
) {
|
||||||
|
|
||||||
if (!lastTask && !is_screenshot) {
|
|
||||||
const organizationId =
|
const organizationId =
|
||||||
(await getUserOrganizationId(user.id).then((res) => res?.organizationId)) || '';
|
(await getUserOrganizationId(user.id).then((res) => res?.organizationId)) || '';
|
||||||
await tasks.trigger(
|
await tasks.trigger(
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
import type { screenshots_task_keys } from '@buster-app/trigger/task-keys';
|
||||||
|
import { runs } from '@trigger.dev/sdk';
|
||||||
|
import type { Context } from 'hono';
|
||||||
|
|
||||||
|
export const shouldTakeScreenshot = async ({
|
||||||
|
tag,
|
||||||
|
key,
|
||||||
|
context,
|
||||||
|
}: {
|
||||||
|
tag: string;
|
||||||
|
key: (typeof screenshots_task_keys)[keyof typeof screenshots_task_keys];
|
||||||
|
context: Context;
|
||||||
|
}): Promise<boolean> => {
|
||||||
|
const hasReferrer = !!context.req.header('referer');
|
||||||
|
|
||||||
|
if (!hasReferrer) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const lastTask = await runs
|
||||||
|
.list({
|
||||||
|
status: ['EXECUTING', 'QUEUED'],
|
||||||
|
taskIdentifier: key,
|
||||||
|
tag,
|
||||||
|
limit: 1,
|
||||||
|
})
|
||||||
|
.then((res) => res.data[0]);
|
||||||
|
|
||||||
|
return !lastTask;
|
||||||
|
};
|
|
@ -5,12 +5,13 @@ import type {
|
||||||
DeleteMetricResponse,
|
DeleteMetricResponse,
|
||||||
DuplicateMetricRequest,
|
DuplicateMetricRequest,
|
||||||
DuplicateMetricResponse,
|
DuplicateMetricResponse,
|
||||||
GetMetricDataRequest,
|
|
||||||
GetMetricListRequest,
|
GetMetricListRequest,
|
||||||
GetMetricParams,
|
GetMetricParams,
|
||||||
GetMetricQuery,
|
GetMetricQuery,
|
||||||
GetMetricResponse,
|
GetMetricResponse,
|
||||||
ListMetricsResponse,
|
ListMetricsResponse,
|
||||||
|
MetricDataParams,
|
||||||
|
MetricDataQuery,
|
||||||
MetricDataResponse,
|
MetricDataResponse,
|
||||||
MetricDownloadParams,
|
MetricDownloadParams,
|
||||||
MetricDownloadQueryParams,
|
MetricDownloadQueryParams,
|
||||||
|
@ -40,7 +41,7 @@ export const getMetric = async ({
|
||||||
export const getMetricData = async ({
|
export const getMetricData = async ({
|
||||||
id,
|
id,
|
||||||
...params
|
...params
|
||||||
}: GetMetricDataRequest & GetMetricParams): Promise<MetricDataResponse> => {
|
}: MetricDataParams & MetricDataQuery): Promise<MetricDataResponse> => {
|
||||||
return mainApiV2
|
return mainApiV2
|
||||||
.get<MetricDataResponse>(`/metric_files/${id}/data`, { params })
|
.get<MetricDataResponse>(`/metric_files/${id}/data`, { params })
|
||||||
.then((res) => res.data);
|
.then((res) => res.data);
|
||||||
|
|
|
@ -61,7 +61,6 @@ export const BulkUpdateMetricVerificationStatusRequestSchema = z.array(
|
||||||
|
|
||||||
export type GetMetricParams = z.infer<typeof GetMetricParamsSchema>;
|
export type GetMetricParams = z.infer<typeof GetMetricParamsSchema>;
|
||||||
export type GetMetricQuery = z.infer<typeof GetMetricQuerySchema>;
|
export type GetMetricQuery = z.infer<typeof GetMetricQuerySchema>;
|
||||||
export type GetMetricDataRequest = z.infer<typeof GetMetricDataRequestSchema>;
|
|
||||||
export type GetMetricListRequest = z.infer<typeof GetMetricListRequestSchema>;
|
export type GetMetricListRequest = z.infer<typeof GetMetricListRequestSchema>;
|
||||||
export type UpdateMetricRequest = z.infer<typeof UpdateMetricRequestSchema>;
|
export type UpdateMetricRequest = z.infer<typeof UpdateMetricRequestSchema>;
|
||||||
export type DeleteMetricRequest = z.infer<typeof DeleteMetricRequestSchema>;
|
export type DeleteMetricRequest = z.infer<typeof DeleteMetricRequestSchema>;
|
||||||
|
|
|
@ -55,7 +55,6 @@ export const MetricDataQuerySchema = z.object({
|
||||||
version_number: z.coerce.number().int().min(1).optional(),
|
version_number: z.coerce.number().int().min(1).optional(),
|
||||||
report_file_id: z.string().uuid().optional(),
|
report_file_id: z.string().uuid().optional(),
|
||||||
password: z.string().min(1).optional(),
|
password: z.string().min(1).optional(),
|
||||||
is_screenshot: z.boolean().default(false).optional(),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export type MetricDataQuery = z.infer<typeof MetricDataQuerySchema>;
|
export type MetricDataQuery = z.infer<typeof MetricDataQuerySchema>;
|
||||||
|
|
Loading…
Reference in New Issue