mirror of
https://github.com/SigNoz/signoz.git
synced 2026-06-14 20:50:35 +01:00
Compare commits
1 Commits
nv/dashboa
...
nv/dashboa
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
86fc0e81ba |
@@ -2583,41 +2583,6 @@ components:
|
||||
- panels
|
||||
- layouts
|
||||
type: object
|
||||
DashboardtypesDashboardView:
|
||||
properties:
|
||||
createdAt:
|
||||
format: date-time
|
||||
type: string
|
||||
data:
|
||||
$ref: '#/components/schemas/DashboardtypesDashboardViewData'
|
||||
id:
|
||||
type: string
|
||||
name:
|
||||
type: string
|
||||
orgId:
|
||||
type: string
|
||||
updatedAt:
|
||||
format: date-time
|
||||
type: string
|
||||
required:
|
||||
- id
|
||||
- name
|
||||
- data
|
||||
- orgId
|
||||
type: object
|
||||
DashboardtypesDashboardViewData:
|
||||
properties:
|
||||
order:
|
||||
$ref: '#/components/schemas/DashboardtypesListOrder'
|
||||
query:
|
||||
type: string
|
||||
sort:
|
||||
$ref: '#/components/schemas/DashboardtypesListSort'
|
||||
version:
|
||||
type: string
|
||||
required:
|
||||
- version
|
||||
type: object
|
||||
DashboardtypesDatasourcePlugin:
|
||||
discriminator:
|
||||
mapping:
|
||||
@@ -2902,15 +2867,6 @@ components:
|
||||
- total
|
||||
- tags
|
||||
type: object
|
||||
DashboardtypesListableDashboardView:
|
||||
properties:
|
||||
views:
|
||||
items:
|
||||
$ref: '#/components/schemas/DashboardtypesDashboardView'
|
||||
type: array
|
||||
required:
|
||||
- views
|
||||
type: object
|
||||
DashboardtypesListedDashboardForUserV2:
|
||||
properties:
|
||||
createdAt:
|
||||
@@ -3215,16 +3171,6 @@ components:
|
||||
- tags
|
||||
- spec
|
||||
type: object
|
||||
DashboardtypesPostableDashboardView:
|
||||
properties:
|
||||
data:
|
||||
$ref: '#/components/schemas/DashboardtypesDashboardViewData'
|
||||
name:
|
||||
type: string
|
||||
required:
|
||||
- name
|
||||
- data
|
||||
type: object
|
||||
DashboardtypesPostablePublicDashboard:
|
||||
properties:
|
||||
defaultTimeRange:
|
||||
@@ -13375,235 +13321,6 @@ paths:
|
||||
summary: Update user preference
|
||||
tags:
|
||||
- preferences
|
||||
/api/v2/dashboard_views:
|
||||
get:
|
||||
deprecated: false
|
||||
description: Returns every saved view in the calling user's org. Saved views
|
||||
are shared org-wide.
|
||||
operationId: ListDashboardViews
|
||||
responses:
|
||||
"200":
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
properties:
|
||||
data:
|
||||
$ref: '#/components/schemas/DashboardtypesListableDashboardView'
|
||||
status:
|
||||
type: string
|
||||
required:
|
||||
- status
|
||||
- data
|
||||
type: object
|
||||
description: OK
|
||||
"401":
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/RenderErrorResponse'
|
||||
description: Unauthorized
|
||||
"403":
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/RenderErrorResponse'
|
||||
description: Forbidden
|
||||
"500":
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/RenderErrorResponse'
|
||||
description: Internal Server Error
|
||||
security:
|
||||
- api_key:
|
||||
- VIEWER
|
||||
- tokenizer:
|
||||
- VIEWER
|
||||
summary: List dashboard saved views
|
||||
tags:
|
||||
- dashboard
|
||||
post:
|
||||
deprecated: false
|
||||
description: Persists the calling user's dashboard listing state (query, sort,
|
||||
order) as a named, reusable view shared across the org.
|
||||
operationId: CreateDashboardView
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/DashboardtypesPostableDashboardView'
|
||||
responses:
|
||||
"201":
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
properties:
|
||||
data:
|
||||
$ref: '#/components/schemas/DashboardtypesDashboardView'
|
||||
status:
|
||||
type: string
|
||||
required:
|
||||
- status
|
||||
- data
|
||||
type: object
|
||||
description: Created
|
||||
"400":
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/RenderErrorResponse'
|
||||
description: Bad Request
|
||||
"401":
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/RenderErrorResponse'
|
||||
description: Unauthorized
|
||||
"403":
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/RenderErrorResponse'
|
||||
description: Forbidden
|
||||
"500":
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/RenderErrorResponse'
|
||||
description: Internal Server Error
|
||||
security:
|
||||
- api_key:
|
||||
- EDITOR
|
||||
- tokenizer:
|
||||
- EDITOR
|
||||
summary: Create dashboard saved view
|
||||
tags:
|
||||
- dashboard
|
||||
/api/v2/dashboard_views/{id}:
|
||||
delete:
|
||||
deprecated: false
|
||||
description: Removes a saved view. Saved views are shared org-wide. Deleting
|
||||
a non-existent view returns 404.
|
||||
operationId: DeleteDashboardView
|
||||
parameters:
|
||||
- in: path
|
||||
name: id
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
responses:
|
||||
"204":
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: string
|
||||
description: No Content
|
||||
"400":
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/RenderErrorResponse'
|
||||
description: Bad Request
|
||||
"401":
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/RenderErrorResponse'
|
||||
description: Unauthorized
|
||||
"403":
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/RenderErrorResponse'
|
||||
description: Forbidden
|
||||
"404":
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/RenderErrorResponse'
|
||||
description: Not Found
|
||||
"500":
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/RenderErrorResponse'
|
||||
description: Internal Server Error
|
||||
security:
|
||||
- api_key:
|
||||
- EDITOR
|
||||
- tokenizer:
|
||||
- EDITOR
|
||||
summary: Delete dashboard saved view
|
||||
tags:
|
||||
- dashboard
|
||||
put:
|
||||
deprecated: false
|
||||
description: Replaces a saved view's name and data. Saved views are shared org-wide.
|
||||
operationId: UpdateDashboardView
|
||||
parameters:
|
||||
- in: path
|
||||
name: id
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/DashboardtypesPostableDashboardView'
|
||||
responses:
|
||||
"200":
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
properties:
|
||||
data:
|
||||
$ref: '#/components/schemas/DashboardtypesDashboardView'
|
||||
status:
|
||||
type: string
|
||||
required:
|
||||
- status
|
||||
- data
|
||||
type: object
|
||||
description: OK
|
||||
"400":
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/RenderErrorResponse'
|
||||
description: Bad Request
|
||||
"401":
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/RenderErrorResponse'
|
||||
description: Unauthorized
|
||||
"403":
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/RenderErrorResponse'
|
||||
description: Forbidden
|
||||
"404":
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/RenderErrorResponse'
|
||||
description: Not Found
|
||||
"500":
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/RenderErrorResponse'
|
||||
description: Internal Server Error
|
||||
security:
|
||||
- api_key:
|
||||
- EDITOR
|
||||
- tokenizer:
|
||||
- EDITOR
|
||||
summary: Update dashboard saved view
|
||||
tags:
|
||||
- dashboard
|
||||
/api/v2/dashboards:
|
||||
get:
|
||||
deprecated: false
|
||||
|
||||
@@ -262,22 +262,6 @@ func (module *module) DeletePreferencesForUser(ctx context.Context, orgID valuer
|
||||
return module.pkgDashboardModule.DeletePreferencesForUser(ctx, orgID, userID)
|
||||
}
|
||||
|
||||
func (module *module) CreateView(ctx context.Context, orgID valuer.UUID, postable dashboardtypes.PostableDashboardView) (*dashboardtypes.DashboardView, error) {
|
||||
return module.pkgDashboardModule.CreateView(ctx, orgID, postable)
|
||||
}
|
||||
|
||||
func (module *module) ListViews(ctx context.Context, orgID valuer.UUID) (*dashboardtypes.ListableDashboardView, error) {
|
||||
return module.pkgDashboardModule.ListViews(ctx, orgID)
|
||||
}
|
||||
|
||||
func (module *module) UpdateView(ctx context.Context, orgID valuer.UUID, id valuer.UUID, updateable dashboardtypes.UpdatableDashboardView) (*dashboardtypes.DashboardView, error) {
|
||||
return module.pkgDashboardModule.UpdateView(ctx, orgID, id, updateable)
|
||||
}
|
||||
|
||||
func (module *module) DeleteView(ctx context.Context, orgID valuer.UUID, id valuer.UUID) error {
|
||||
return module.pkgDashboardModule.DeleteView(ctx, orgID, id)
|
||||
}
|
||||
|
||||
func (module *module) Get(ctx context.Context, orgID valuer.UUID, id valuer.UUID) (*dashboardtypes.Dashboard, error) {
|
||||
return module.pkgDashboardModule.Get(ctx, orgID, id)
|
||||
}
|
||||
|
||||
@@ -19,17 +19,14 @@ import type {
|
||||
|
||||
import type {
|
||||
CreateDashboardV2201,
|
||||
CreateDashboardView201,
|
||||
CreatePublicDashboard201,
|
||||
CreatePublicDashboardPathParameters,
|
||||
DashboardtypesPatchableDashboardV2DTO,
|
||||
DashboardtypesPostableDashboardV2DTO,
|
||||
DashboardtypesPostableDashboardViewDTO,
|
||||
DashboardtypesPostablePublicDashboardDTO,
|
||||
DashboardtypesUpdatableDashboardV2DTO,
|
||||
DashboardtypesUpdatablePublicDashboardDTO,
|
||||
DeleteDashboardV2PathParameters,
|
||||
DeleteDashboardViewPathParameters,
|
||||
DeletePublicDashboardPathParameters,
|
||||
GetDashboardV2200,
|
||||
GetDashboardV2PathParameters,
|
||||
@@ -39,7 +36,6 @@ import type {
|
||||
GetPublicDashboardPathParameters,
|
||||
GetPublicDashboardWidgetQueryRange200,
|
||||
GetPublicDashboardWidgetQueryRangePathParameters,
|
||||
ListDashboardViews200,
|
||||
ListDashboardsForUserV2200,
|
||||
ListDashboardsForUserV2Params,
|
||||
ListDashboardsV2200,
|
||||
@@ -53,8 +49,6 @@ import type {
|
||||
UnpinDashboardV2PathParameters,
|
||||
UpdateDashboardV2200,
|
||||
UpdateDashboardV2PathParameters,
|
||||
UpdateDashboardView200,
|
||||
UpdateDashboardViewPathParameters,
|
||||
UpdatePublicDashboardPathParameters,
|
||||
} from '../sigNoz.schemas';
|
||||
|
||||
@@ -654,354 +648,6 @@ export const invalidateGetPublicDashboardWidgetQueryRange = async (
|
||||
return queryClient;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns every saved view in the calling user's org. Saved views are shared org-wide.
|
||||
* @summary List dashboard saved views
|
||||
*/
|
||||
export const listDashboardViews = (signal?: AbortSignal) => {
|
||||
return GeneratedAPIInstance<ListDashboardViews200>({
|
||||
url: `/api/v2/dashboard_views`,
|
||||
method: 'GET',
|
||||
signal,
|
||||
});
|
||||
};
|
||||
|
||||
export const getListDashboardViewsQueryKey = () => {
|
||||
return [`/api/v2/dashboard_views`] as const;
|
||||
};
|
||||
|
||||
export const getListDashboardViewsQueryOptions = <
|
||||
TData = Awaited<ReturnType<typeof listDashboardViews>>,
|
||||
TError = ErrorType<RenderErrorResponseDTO>,
|
||||
>(options?: {
|
||||
query?: UseQueryOptions<
|
||||
Awaited<ReturnType<typeof listDashboardViews>>,
|
||||
TError,
|
||||
TData
|
||||
>;
|
||||
}) => {
|
||||
const { query: queryOptions } = options ?? {};
|
||||
|
||||
const queryKey = queryOptions?.queryKey ?? getListDashboardViewsQueryKey();
|
||||
|
||||
const queryFn: QueryFunction<
|
||||
Awaited<ReturnType<typeof listDashboardViews>>
|
||||
> = ({ signal }) => listDashboardViews(signal);
|
||||
|
||||
return { queryKey, queryFn, ...queryOptions } as UseQueryOptions<
|
||||
Awaited<ReturnType<typeof listDashboardViews>>,
|
||||
TError,
|
||||
TData
|
||||
> & { queryKey: QueryKey };
|
||||
};
|
||||
|
||||
export type ListDashboardViewsQueryResult = NonNullable<
|
||||
Awaited<ReturnType<typeof listDashboardViews>>
|
||||
>;
|
||||
export type ListDashboardViewsQueryError = ErrorType<RenderErrorResponseDTO>;
|
||||
|
||||
/**
|
||||
* @summary List dashboard saved views
|
||||
*/
|
||||
|
||||
export function useListDashboardViews<
|
||||
TData = Awaited<ReturnType<typeof listDashboardViews>>,
|
||||
TError = ErrorType<RenderErrorResponseDTO>,
|
||||
>(options?: {
|
||||
query?: UseQueryOptions<
|
||||
Awaited<ReturnType<typeof listDashboardViews>>,
|
||||
TError,
|
||||
TData
|
||||
>;
|
||||
}): UseQueryResult<TData, TError> & { queryKey: QueryKey } {
|
||||
const queryOptions = getListDashboardViewsQueryOptions(options);
|
||||
|
||||
const query = useQuery(queryOptions) as UseQueryResult<TData, TError> & {
|
||||
queryKey: QueryKey;
|
||||
};
|
||||
|
||||
return { ...query, queryKey: queryOptions.queryKey };
|
||||
}
|
||||
|
||||
/**
|
||||
* @summary List dashboard saved views
|
||||
*/
|
||||
export const invalidateListDashboardViews = async (
|
||||
queryClient: QueryClient,
|
||||
options?: InvalidateOptions,
|
||||
): Promise<QueryClient> => {
|
||||
await queryClient.invalidateQueries(
|
||||
{ queryKey: getListDashboardViewsQueryKey() },
|
||||
options,
|
||||
);
|
||||
|
||||
return queryClient;
|
||||
};
|
||||
|
||||
/**
|
||||
* Persists the calling user's dashboard listing state (query, sort, order) as a named, reusable view shared across the org.
|
||||
* @summary Create dashboard saved view
|
||||
*/
|
||||
export const createDashboardView = (
|
||||
dashboardtypesPostableDashboardViewDTO?: BodyType<DashboardtypesPostableDashboardViewDTO>,
|
||||
signal?: AbortSignal,
|
||||
) => {
|
||||
return GeneratedAPIInstance<CreateDashboardView201>({
|
||||
url: `/api/v2/dashboard_views`,
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
data: dashboardtypesPostableDashboardViewDTO,
|
||||
signal,
|
||||
});
|
||||
};
|
||||
|
||||
export const getCreateDashboardViewMutationOptions = <
|
||||
TError = ErrorType<RenderErrorResponseDTO>,
|
||||
TContext = unknown,
|
||||
>(options?: {
|
||||
mutation?: UseMutationOptions<
|
||||
Awaited<ReturnType<typeof createDashboardView>>,
|
||||
TError,
|
||||
{ data?: BodyType<DashboardtypesPostableDashboardViewDTO> },
|
||||
TContext
|
||||
>;
|
||||
}): UseMutationOptions<
|
||||
Awaited<ReturnType<typeof createDashboardView>>,
|
||||
TError,
|
||||
{ data?: BodyType<DashboardtypesPostableDashboardViewDTO> },
|
||||
TContext
|
||||
> => {
|
||||
const mutationKey = ['createDashboardView'];
|
||||
const { mutation: mutationOptions } = options
|
||||
? options.mutation &&
|
||||
'mutationKey' in options.mutation &&
|
||||
options.mutation.mutationKey
|
||||
? options
|
||||
: { ...options, mutation: { ...options.mutation, mutationKey } }
|
||||
: { mutation: { mutationKey } };
|
||||
|
||||
const mutationFn: MutationFunction<
|
||||
Awaited<ReturnType<typeof createDashboardView>>,
|
||||
{ data?: BodyType<DashboardtypesPostableDashboardViewDTO> }
|
||||
> = (props) => {
|
||||
const { data } = props ?? {};
|
||||
|
||||
return createDashboardView(data);
|
||||
};
|
||||
|
||||
return { mutationFn, ...mutationOptions };
|
||||
};
|
||||
|
||||
export type CreateDashboardViewMutationResult = NonNullable<
|
||||
Awaited<ReturnType<typeof createDashboardView>>
|
||||
>;
|
||||
export type CreateDashboardViewMutationBody =
|
||||
| BodyType<DashboardtypesPostableDashboardViewDTO>
|
||||
| undefined;
|
||||
export type CreateDashboardViewMutationError =
|
||||
ErrorType<RenderErrorResponseDTO>;
|
||||
|
||||
/**
|
||||
* @summary Create dashboard saved view
|
||||
*/
|
||||
export const useCreateDashboardView = <
|
||||
TError = ErrorType<RenderErrorResponseDTO>,
|
||||
TContext = unknown,
|
||||
>(options?: {
|
||||
mutation?: UseMutationOptions<
|
||||
Awaited<ReturnType<typeof createDashboardView>>,
|
||||
TError,
|
||||
{ data?: BodyType<DashboardtypesPostableDashboardViewDTO> },
|
||||
TContext
|
||||
>;
|
||||
}): UseMutationResult<
|
||||
Awaited<ReturnType<typeof createDashboardView>>,
|
||||
TError,
|
||||
{ data?: BodyType<DashboardtypesPostableDashboardViewDTO> },
|
||||
TContext
|
||||
> => {
|
||||
return useMutation(getCreateDashboardViewMutationOptions(options));
|
||||
};
|
||||
/**
|
||||
* Removes a saved view. Saved views are shared org-wide. Deleting a non-existent view returns 404.
|
||||
* @summary Delete dashboard saved view
|
||||
*/
|
||||
export const deleteDashboardView = (
|
||||
{ id }: DeleteDashboardViewPathParameters,
|
||||
signal?: AbortSignal,
|
||||
) => {
|
||||
return GeneratedAPIInstance<string>({
|
||||
url: `/api/v2/dashboard_views/${id}`,
|
||||
method: 'DELETE',
|
||||
signal,
|
||||
});
|
||||
};
|
||||
|
||||
export const getDeleteDashboardViewMutationOptions = <
|
||||
TError = ErrorType<RenderErrorResponseDTO>,
|
||||
TContext = unknown,
|
||||
>(options?: {
|
||||
mutation?: UseMutationOptions<
|
||||
Awaited<ReturnType<typeof deleteDashboardView>>,
|
||||
TError,
|
||||
{ pathParams: DeleteDashboardViewPathParameters },
|
||||
TContext
|
||||
>;
|
||||
}): UseMutationOptions<
|
||||
Awaited<ReturnType<typeof deleteDashboardView>>,
|
||||
TError,
|
||||
{ pathParams: DeleteDashboardViewPathParameters },
|
||||
TContext
|
||||
> => {
|
||||
const mutationKey = ['deleteDashboardView'];
|
||||
const { mutation: mutationOptions } = options
|
||||
? options.mutation &&
|
||||
'mutationKey' in options.mutation &&
|
||||
options.mutation.mutationKey
|
||||
? options
|
||||
: { ...options, mutation: { ...options.mutation, mutationKey } }
|
||||
: { mutation: { mutationKey } };
|
||||
|
||||
const mutationFn: MutationFunction<
|
||||
Awaited<ReturnType<typeof deleteDashboardView>>,
|
||||
{ pathParams: DeleteDashboardViewPathParameters }
|
||||
> = (props) => {
|
||||
const { pathParams } = props ?? {};
|
||||
|
||||
return deleteDashboardView(pathParams);
|
||||
};
|
||||
|
||||
return { mutationFn, ...mutationOptions };
|
||||
};
|
||||
|
||||
export type DeleteDashboardViewMutationResult = NonNullable<
|
||||
Awaited<ReturnType<typeof deleteDashboardView>>
|
||||
>;
|
||||
|
||||
export type DeleteDashboardViewMutationError =
|
||||
ErrorType<RenderErrorResponseDTO>;
|
||||
|
||||
/**
|
||||
* @summary Delete dashboard saved view
|
||||
*/
|
||||
export const useDeleteDashboardView = <
|
||||
TError = ErrorType<RenderErrorResponseDTO>,
|
||||
TContext = unknown,
|
||||
>(options?: {
|
||||
mutation?: UseMutationOptions<
|
||||
Awaited<ReturnType<typeof deleteDashboardView>>,
|
||||
TError,
|
||||
{ pathParams: DeleteDashboardViewPathParameters },
|
||||
TContext
|
||||
>;
|
||||
}): UseMutationResult<
|
||||
Awaited<ReturnType<typeof deleteDashboardView>>,
|
||||
TError,
|
||||
{ pathParams: DeleteDashboardViewPathParameters },
|
||||
TContext
|
||||
> => {
|
||||
return useMutation(getDeleteDashboardViewMutationOptions(options));
|
||||
};
|
||||
/**
|
||||
* Replaces a saved view's name and data. Saved views are shared org-wide.
|
||||
* @summary Update dashboard saved view
|
||||
*/
|
||||
export const updateDashboardView = (
|
||||
{ id }: UpdateDashboardViewPathParameters,
|
||||
dashboardtypesPostableDashboardViewDTO?: BodyType<DashboardtypesPostableDashboardViewDTO>,
|
||||
signal?: AbortSignal,
|
||||
) => {
|
||||
return GeneratedAPIInstance<UpdateDashboardView200>({
|
||||
url: `/api/v2/dashboard_views/${id}`,
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
data: dashboardtypesPostableDashboardViewDTO,
|
||||
signal,
|
||||
});
|
||||
};
|
||||
|
||||
export const getUpdateDashboardViewMutationOptions = <
|
||||
TError = ErrorType<RenderErrorResponseDTO>,
|
||||
TContext = unknown,
|
||||
>(options?: {
|
||||
mutation?: UseMutationOptions<
|
||||
Awaited<ReturnType<typeof updateDashboardView>>,
|
||||
TError,
|
||||
{
|
||||
pathParams: UpdateDashboardViewPathParameters;
|
||||
data?: BodyType<DashboardtypesPostableDashboardViewDTO>;
|
||||
},
|
||||
TContext
|
||||
>;
|
||||
}): UseMutationOptions<
|
||||
Awaited<ReturnType<typeof updateDashboardView>>,
|
||||
TError,
|
||||
{
|
||||
pathParams: UpdateDashboardViewPathParameters;
|
||||
data?: BodyType<DashboardtypesPostableDashboardViewDTO>;
|
||||
},
|
||||
TContext
|
||||
> => {
|
||||
const mutationKey = ['updateDashboardView'];
|
||||
const { mutation: mutationOptions } = options
|
||||
? options.mutation &&
|
||||
'mutationKey' in options.mutation &&
|
||||
options.mutation.mutationKey
|
||||
? options
|
||||
: { ...options, mutation: { ...options.mutation, mutationKey } }
|
||||
: { mutation: { mutationKey } };
|
||||
|
||||
const mutationFn: MutationFunction<
|
||||
Awaited<ReturnType<typeof updateDashboardView>>,
|
||||
{
|
||||
pathParams: UpdateDashboardViewPathParameters;
|
||||
data?: BodyType<DashboardtypesPostableDashboardViewDTO>;
|
||||
}
|
||||
> = (props) => {
|
||||
const { pathParams, data } = props ?? {};
|
||||
|
||||
return updateDashboardView(pathParams, data);
|
||||
};
|
||||
|
||||
return { mutationFn, ...mutationOptions };
|
||||
};
|
||||
|
||||
export type UpdateDashboardViewMutationResult = NonNullable<
|
||||
Awaited<ReturnType<typeof updateDashboardView>>
|
||||
>;
|
||||
export type UpdateDashboardViewMutationBody =
|
||||
| BodyType<DashboardtypesPostableDashboardViewDTO>
|
||||
| undefined;
|
||||
export type UpdateDashboardViewMutationError =
|
||||
ErrorType<RenderErrorResponseDTO>;
|
||||
|
||||
/**
|
||||
* @summary Update dashboard saved view
|
||||
*/
|
||||
export const useUpdateDashboardView = <
|
||||
TError = ErrorType<RenderErrorResponseDTO>,
|
||||
TContext = unknown,
|
||||
>(options?: {
|
||||
mutation?: UseMutationOptions<
|
||||
Awaited<ReturnType<typeof updateDashboardView>>,
|
||||
TError,
|
||||
{
|
||||
pathParams: UpdateDashboardViewPathParameters;
|
||||
data?: BodyType<DashboardtypesPostableDashboardViewDTO>;
|
||||
},
|
||||
TContext
|
||||
>;
|
||||
}): UseMutationResult<
|
||||
Awaited<ReturnType<typeof updateDashboardView>>,
|
||||
TError,
|
||||
{
|
||||
pathParams: UpdateDashboardViewPathParameters;
|
||||
data?: BodyType<DashboardtypesPostableDashboardViewDTO>;
|
||||
},
|
||||
TContext
|
||||
> => {
|
||||
return useMutation(getUpdateDashboardViewMutationOptions(options));
|
||||
};
|
||||
/**
|
||||
* Returns a page of v2-shape dashboards for the org. This is the pure, user-independent list — it carries no pin state. Use ListDashboardsForUserV2 for the personalized, pin-aware list. Supports a filter DSL (`query`), sort (`updated_at`/`created_at`/`name`), order (`asc`/`desc`), and offset-based pagination (`limit`/`offset`).
|
||||
* @summary List dashboards (v2)
|
||||
|
||||
@@ -4625,54 +4625,6 @@ export interface DashboardtypesDashboardSpecDTO {
|
||||
variables: DashboardtypesVariableDTO[];
|
||||
}
|
||||
|
||||
export enum DashboardtypesListOrderDTO {
|
||||
asc = 'asc',
|
||||
desc = 'desc',
|
||||
}
|
||||
export enum DashboardtypesListSortDTO {
|
||||
updated_at = 'updated_at',
|
||||
created_at = 'created_at',
|
||||
name = 'name',
|
||||
}
|
||||
export interface DashboardtypesDashboardViewDataDTO {
|
||||
order?: DashboardtypesListOrderDTO;
|
||||
/**
|
||||
* @type string
|
||||
*/
|
||||
query?: string;
|
||||
sort?: DashboardtypesListSortDTO;
|
||||
/**
|
||||
* @type string
|
||||
*/
|
||||
version: string;
|
||||
}
|
||||
|
||||
export interface DashboardtypesDashboardViewDTO {
|
||||
/**
|
||||
* @type string
|
||||
* @format date-time
|
||||
*/
|
||||
createdAt?: string;
|
||||
data: DashboardtypesDashboardViewDataDTO;
|
||||
/**
|
||||
* @type string
|
||||
*/
|
||||
id: string;
|
||||
/**
|
||||
* @type string
|
||||
*/
|
||||
name: string;
|
||||
/**
|
||||
* @type string
|
||||
*/
|
||||
orgId: string;
|
||||
/**
|
||||
* @type string
|
||||
* @format date-time
|
||||
*/
|
||||
updatedAt?: string;
|
||||
}
|
||||
|
||||
export enum DashboardtypesDatasourcePluginKindDTO {
|
||||
'signoz/Datasource' = 'signoz/Datasource',
|
||||
}
|
||||
@@ -4784,6 +4736,15 @@ export interface DashboardtypesJSONPatchOperationDTO {
|
||||
value?: unknown;
|
||||
}
|
||||
|
||||
export enum DashboardtypesListOrderDTO {
|
||||
asc = 'asc',
|
||||
desc = 'desc',
|
||||
}
|
||||
export enum DashboardtypesListSortDTO {
|
||||
updated_at = 'updated_at',
|
||||
created_at = 'created_at',
|
||||
name = 'name',
|
||||
}
|
||||
export interface DashboardtypesListedDashboardV2SpecDTO {
|
||||
display?: DashboardtypesDisplayDTO;
|
||||
}
|
||||
@@ -4926,13 +4887,6 @@ export interface DashboardtypesListableDashboardV2DTO {
|
||||
total: number;
|
||||
}
|
||||
|
||||
export interface DashboardtypesListableDashboardViewDTO {
|
||||
/**
|
||||
* @type array
|
||||
*/
|
||||
views: DashboardtypesDashboardViewDTO[];
|
||||
}
|
||||
|
||||
export enum DashboardtypesPanelPluginKindDTO {
|
||||
'signoz/TimeSeriesPanel' = 'signoz/TimeSeriesPanel',
|
||||
'signoz/BarChartPanel' = 'signoz/BarChartPanel',
|
||||
@@ -4984,14 +4938,6 @@ export interface DashboardtypesPostableDashboardV2DTO {
|
||||
tags: TagtypesPostableTagDTO[] | null;
|
||||
}
|
||||
|
||||
export interface DashboardtypesPostableDashboardViewDTO {
|
||||
data: DashboardtypesDashboardViewDataDTO;
|
||||
/**
|
||||
* @type string
|
||||
*/
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface DashboardtypesPostablePublicDashboardDTO {
|
||||
/**
|
||||
* @type string
|
||||
@@ -9870,36 +9816,6 @@ export type GetUserPreference200 = {
|
||||
export type UpdateUserPreferencePathParameters = {
|
||||
name: string;
|
||||
};
|
||||
export type ListDashboardViews200 = {
|
||||
data: DashboardtypesListableDashboardViewDTO;
|
||||
/**
|
||||
* @type string
|
||||
*/
|
||||
status: string;
|
||||
};
|
||||
|
||||
export type CreateDashboardView201 = {
|
||||
data: DashboardtypesDashboardViewDTO;
|
||||
/**
|
||||
* @type string
|
||||
*/
|
||||
status: string;
|
||||
};
|
||||
|
||||
export type DeleteDashboardViewPathParameters = {
|
||||
id: string;
|
||||
};
|
||||
export type UpdateDashboardViewPathParameters = {
|
||||
id: string;
|
||||
};
|
||||
export type UpdateDashboardView200 = {
|
||||
data: DashboardtypesDashboardViewDTO;
|
||||
/**
|
||||
* @type string
|
||||
*/
|
||||
status: string;
|
||||
};
|
||||
|
||||
export type ListDashboardsV2Params = {
|
||||
/**
|
||||
* @type string
|
||||
|
||||
@@ -212,74 +212,6 @@ func (provider *provider) addDashboardRoutes(router *mux.Router) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := router.Handle("/api/v2/dashboard_views", handler.New(provider.authzMiddleware.ViewAccess(provider.dashboardHandler.ListViews), handler.OpenAPIDef{
|
||||
ID: "ListDashboardViews",
|
||||
Tags: []string{"dashboard"},
|
||||
Summary: "List dashboard saved views",
|
||||
Description: "Returns every saved view in the calling user's org. Saved views are shared org-wide.",
|
||||
Request: nil,
|
||||
RequestContentType: "",
|
||||
Response: new(dashboardtypes.ListableDashboardView),
|
||||
ResponseContentType: "application/json",
|
||||
SuccessStatusCode: http.StatusOK,
|
||||
ErrorStatusCodes: []int{},
|
||||
Deprecated: false,
|
||||
SecuritySchemes: newSecuritySchemes(types.RoleViewer),
|
||||
})).Methods(http.MethodGet).GetError(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := router.Handle("/api/v2/dashboard_views", handler.New(provider.authzMiddleware.EditAccess(provider.dashboardHandler.CreateView), handler.OpenAPIDef{
|
||||
ID: "CreateDashboardView",
|
||||
Tags: []string{"dashboard"},
|
||||
Summary: "Create dashboard saved view",
|
||||
Description: "Persists the calling user's dashboard listing state (query, sort, order) as a named, reusable view shared across the org.",
|
||||
Request: new(dashboardtypes.PostableDashboardView),
|
||||
RequestContentType: "application/json",
|
||||
Response: new(dashboardtypes.DashboardView),
|
||||
ResponseContentType: "application/json",
|
||||
SuccessStatusCode: http.StatusCreated,
|
||||
ErrorStatusCodes: []int{http.StatusBadRequest},
|
||||
Deprecated: false,
|
||||
SecuritySchemes: newSecuritySchemes(types.RoleEditor),
|
||||
})).Methods(http.MethodPost).GetError(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := router.Handle("/api/v2/dashboard_views/{id}", handler.New(provider.authzMiddleware.EditAccess(provider.dashboardHandler.UpdateView), handler.OpenAPIDef{
|
||||
ID: "UpdateDashboardView",
|
||||
Tags: []string{"dashboard"},
|
||||
Summary: "Update dashboard saved view",
|
||||
Description: "Replaces a saved view's name and data. Saved views are shared org-wide.",
|
||||
Request: new(dashboardtypes.UpdatableDashboardView),
|
||||
RequestContentType: "application/json",
|
||||
Response: new(dashboardtypes.DashboardView),
|
||||
ResponseContentType: "application/json",
|
||||
SuccessStatusCode: http.StatusOK,
|
||||
ErrorStatusCodes: []int{http.StatusBadRequest, http.StatusNotFound},
|
||||
Deprecated: false,
|
||||
SecuritySchemes: newSecuritySchemes(types.RoleEditor),
|
||||
})).Methods(http.MethodPut).GetError(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := router.Handle("/api/v2/dashboard_views/{id}", handler.New(provider.authzMiddleware.EditAccess(provider.dashboardHandler.DeleteView), handler.OpenAPIDef{
|
||||
ID: "DeleteDashboardView",
|
||||
Tags: []string{"dashboard"},
|
||||
Summary: "Delete dashboard saved view",
|
||||
Description: "Removes a saved view. Saved views are shared org-wide. Deleting a non-existent view returns 404.",
|
||||
Request: nil,
|
||||
RequestContentType: "",
|
||||
Response: nil,
|
||||
ResponseContentType: "application/json",
|
||||
SuccessStatusCode: http.StatusNoContent,
|
||||
ErrorStatusCodes: []int{http.StatusBadRequest, http.StatusNotFound},
|
||||
Deprecated: false,
|
||||
SecuritySchemes: newSecuritySchemes(types.RoleEditor),
|
||||
})).Methods(http.MethodDelete).GetError(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := router.Handle("/api/v1/dashboards/{id}/public", handler.New(provider.authzMiddleware.AdminAccess(provider.dashboardHandler.CreatePublic), handler.OpenAPIDef{
|
||||
ID: "CreatePublicDashboard",
|
||||
Tags: []string{"dashboard"},
|
||||
|
||||
@@ -78,14 +78,6 @@ type Module interface {
|
||||
DeleteV2(ctx context.Context, orgID valuer.UUID, id valuer.UUID) error
|
||||
|
||||
DeletePreferencesForUser(ctx context.Context, orgID valuer.UUID, userID valuer.UUID) error
|
||||
|
||||
CreateView(ctx context.Context, orgID valuer.UUID, postable dashboardtypes.PostableDashboardView) (*dashboardtypes.DashboardView, error)
|
||||
|
||||
ListViews(ctx context.Context, orgID valuer.UUID) (*dashboardtypes.ListableDashboardView, error)
|
||||
|
||||
UpdateView(ctx context.Context, orgID valuer.UUID, id valuer.UUID, updateable dashboardtypes.UpdatableDashboardView) (*dashboardtypes.DashboardView, error)
|
||||
|
||||
DeleteView(ctx context.Context, orgID valuer.UUID, id valuer.UUID) error
|
||||
}
|
||||
|
||||
type Handler interface {
|
||||
@@ -133,12 +125,4 @@ type Handler interface {
|
||||
UnpinV2(http.ResponseWriter, *http.Request)
|
||||
|
||||
DeleteV2(http.ResponseWriter, *http.Request)
|
||||
|
||||
CreateView(http.ResponseWriter, *http.Request)
|
||||
|
||||
ListViews(http.ResponseWriter, *http.Request)
|
||||
|
||||
UpdateView(http.ResponseWriter, *http.Request)
|
||||
|
||||
DeleteView(http.ResponseWriter, *http.Request)
|
||||
}
|
||||
|
||||
@@ -472,92 +472,3 @@ func (store *store) DeletePreferencesForUser(ctx context.Context, orgID valuer.U
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *store) CreateDashboardView(ctx context.Context, view *dashboardtypes.DashboardView) error {
|
||||
_, err := store.
|
||||
sqlstore.
|
||||
BunDBCtx(ctx).
|
||||
NewInsert().
|
||||
Model(view).
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
return store.sqlstore.WrapAlreadyExistsErrf(err, errors.CodeAlreadyExists, "dashboard view with id %s already exists", view.ID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *store) GetDashboardView(ctx context.Context, orgID valuer.UUID, id valuer.UUID) (*dashboardtypes.DashboardView, error) {
|
||||
view := new(dashboardtypes.DashboardView)
|
||||
err := store.
|
||||
sqlstore.
|
||||
BunDB().
|
||||
NewSelect().
|
||||
Model(view).
|
||||
Where("id = ?", id).
|
||||
Where("org_id = ?", orgID).
|
||||
Scan(ctx)
|
||||
if err != nil {
|
||||
return nil, store.sqlstore.WrapNotFoundErrf(err, dashboardtypes.ErrCodeDashboardViewNotFound, "dashboard view with id %s doesn't exist", id)
|
||||
}
|
||||
return view, nil
|
||||
}
|
||||
|
||||
func (store *store) ListDashboardViews(ctx context.Context, orgID valuer.UUID) ([]*dashboardtypes.DashboardView, error) {
|
||||
views := make([]*dashboardtypes.DashboardView, 0)
|
||||
err := store.
|
||||
sqlstore.
|
||||
BunDB().
|
||||
NewSelect().
|
||||
Model(&views).
|
||||
Where("org_id = ?", orgID).
|
||||
OrderExpr("updated_at DESC").
|
||||
Scan(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.WrapInternalf(err, errors.CodeInternal, "couldn't list dashboard views")
|
||||
}
|
||||
return views, nil
|
||||
}
|
||||
|
||||
func (store *store) UpdateDashboardView(ctx context.Context, view *dashboardtypes.DashboardView) error {
|
||||
res, err := store.
|
||||
sqlstore.
|
||||
BunDBCtx(ctx).
|
||||
NewUpdate().
|
||||
Model(view).
|
||||
WherePK().
|
||||
Where("org_id = ?", view.OrgID).
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
return errors.WrapInternalf(err, errors.CodeInternal, "couldn't update dashboard view")
|
||||
}
|
||||
rows, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return errors.WrapInternalf(err, errors.CodeInternal, "couldn't read dashboard view update result")
|
||||
}
|
||||
if rows == 0 {
|
||||
return errors.Newf(errors.TypeNotFound, dashboardtypes.ErrCodeDashboardViewNotFound, "dashboard view with id %s doesn't exist", view.ID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *store) DeleteDashboardView(ctx context.Context, orgID valuer.UUID, id valuer.UUID) error {
|
||||
res, err := store.
|
||||
sqlstore.
|
||||
BunDBCtx(ctx).
|
||||
NewDelete().
|
||||
Model(new(dashboardtypes.DashboardView)).
|
||||
Where("id = ?", id).
|
||||
Where("org_id = ?", orgID).
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
return errors.WrapInternalf(err, errors.CodeInternal, "couldn't delete dashboard view")
|
||||
}
|
||||
rows, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return errors.WrapInternalf(err, errors.CodeInternal, "couldn't read dashboard view delete result")
|
||||
}
|
||||
if rows == 0 {
|
||||
return errors.Newf(errors.TypeNotFound, dashboardtypes.ErrCodeDashboardViewNotFound, "dashboard view with id %s doesn't exist", id)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,132 +0,0 @@
|
||||
package impldashboard
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/SigNoz/signoz/pkg/errors"
|
||||
"github.com/SigNoz/signoz/pkg/http/binding"
|
||||
"github.com/SigNoz/signoz/pkg/http/render"
|
||||
"github.com/SigNoz/signoz/pkg/types/authtypes"
|
||||
"github.com/SigNoz/signoz/pkg/types/dashboardtypes"
|
||||
"github.com/SigNoz/signoz/pkg/valuer"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
func (handler *handler) CreateView(rw http.ResponseWriter, r *http.Request) {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
claims, err := authtypes.ClaimsFromContext(ctx)
|
||||
if err != nil {
|
||||
render.Error(rw, err)
|
||||
return
|
||||
}
|
||||
|
||||
orgID := valuer.MustNewUUID(claims.OrgID)
|
||||
|
||||
var req dashboardtypes.PostableDashboardView
|
||||
if err := binding.JSON.BindBody(r.Body, &req); err != nil {
|
||||
render.Error(rw, err)
|
||||
return
|
||||
}
|
||||
|
||||
view, err := handler.module.CreateView(ctx, orgID, req)
|
||||
if err != nil {
|
||||
render.Error(rw, err)
|
||||
return
|
||||
}
|
||||
|
||||
render.Success(rw, http.StatusCreated, view)
|
||||
}
|
||||
|
||||
func (handler *handler) ListViews(rw http.ResponseWriter, r *http.Request) {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
claims, err := authtypes.ClaimsFromContext(ctx)
|
||||
if err != nil {
|
||||
render.Error(rw, err)
|
||||
return
|
||||
}
|
||||
|
||||
orgID := valuer.MustNewUUID(claims.OrgID)
|
||||
|
||||
out, err := handler.module.ListViews(ctx, orgID)
|
||||
if err != nil {
|
||||
render.Error(rw, err)
|
||||
return
|
||||
}
|
||||
|
||||
render.Success(rw, http.StatusOK, out)
|
||||
}
|
||||
|
||||
func (handler *handler) UpdateView(rw http.ResponseWriter, r *http.Request) {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
claims, err := authtypes.ClaimsFromContext(ctx)
|
||||
if err != nil {
|
||||
render.Error(rw, err)
|
||||
return
|
||||
}
|
||||
|
||||
orgID := valuer.MustNewUUID(claims.OrgID)
|
||||
|
||||
id := mux.Vars(r)["id"]
|
||||
if id == "" {
|
||||
render.Error(rw, errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "id is missing in the path"))
|
||||
return
|
||||
}
|
||||
viewID, err := valuer.NewUUID(id)
|
||||
if err != nil {
|
||||
render.Error(rw, err)
|
||||
return
|
||||
}
|
||||
|
||||
var req dashboardtypes.UpdatableDashboardView
|
||||
if err := binding.JSON.BindBody(r.Body, &req); err != nil {
|
||||
render.Error(rw, err)
|
||||
return
|
||||
}
|
||||
|
||||
view, err := handler.module.UpdateView(ctx, orgID, viewID, req)
|
||||
if err != nil {
|
||||
render.Error(rw, err)
|
||||
return
|
||||
}
|
||||
|
||||
render.Success(rw, http.StatusOK, view)
|
||||
}
|
||||
|
||||
func (handler *handler) DeleteView(rw http.ResponseWriter, r *http.Request) {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
claims, err := authtypes.ClaimsFromContext(ctx)
|
||||
if err != nil {
|
||||
render.Error(rw, err)
|
||||
return
|
||||
}
|
||||
|
||||
orgID := valuer.MustNewUUID(claims.OrgID)
|
||||
|
||||
id := mux.Vars(r)["id"]
|
||||
if id == "" {
|
||||
render.Error(rw, errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "id is missing in the path"))
|
||||
return
|
||||
}
|
||||
viewID, err := valuer.NewUUID(id)
|
||||
if err != nil {
|
||||
render.Error(rw, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := handler.module.DeleteView(ctx, orgID, viewID); err != nil {
|
||||
render.Error(rw, err)
|
||||
return
|
||||
}
|
||||
|
||||
render.Success(rw, http.StatusNoContent, nil)
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
package impldashboard
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/SigNoz/signoz/pkg/types/dashboardtypes"
|
||||
"github.com/SigNoz/signoz/pkg/valuer"
|
||||
)
|
||||
|
||||
func (module *module) CreateView(ctx context.Context, orgID valuer.UUID, postable dashboardtypes.PostableDashboardView) (*dashboardtypes.DashboardView, error) {
|
||||
if err := postable.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
view := postable.NewDashboardView(orgID)
|
||||
if err := module.store.CreateDashboardView(ctx, view); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return view, nil
|
||||
}
|
||||
|
||||
func (module *module) ListViews(ctx context.Context, orgID valuer.UUID) (*dashboardtypes.ListableDashboardView, error) {
|
||||
views, err := module.store.ListDashboardViews(ctx, orgID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &dashboardtypes.ListableDashboardView{Views: views}, nil
|
||||
}
|
||||
|
||||
func (module *module) UpdateView(ctx context.Context, orgID valuer.UUID, id valuer.UUID, updateable dashboardtypes.UpdatableDashboardView) (*dashboardtypes.DashboardView, error) {
|
||||
if err := updateable.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
view, err := module.store.GetDashboardView(ctx, orgID, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
view.Update(updateable)
|
||||
if err := module.store.UpdateDashboardView(ctx, view); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return view, nil
|
||||
}
|
||||
|
||||
func (module *module) DeleteView(ctx context.Context, orgID valuer.UUID, id valuer.UUID) error {
|
||||
return module.store.DeleteDashboardView(ctx, orgID, id)
|
||||
}
|
||||
@@ -214,7 +214,6 @@ func NewSQLMigrationProviderFactories(
|
||||
sqlmigration.NewAddUserDashboardPreferenceFactory(sqlstore, sqlschema),
|
||||
sqlmigration.NewRecreateUserDashboardPreferenceFactory(sqlstore, sqlschema),
|
||||
sqlmigration.NewMigrateRecurrenceBoundsFactory(sqlstore),
|
||||
sqlmigration.NewAddDashboardViewFactory(sqlstore, sqlschema),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
package sqlmigration
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/SigNoz/signoz/pkg/factory"
|
||||
"github.com/SigNoz/signoz/pkg/sqlschema"
|
||||
"github.com/SigNoz/signoz/pkg/sqlstore"
|
||||
"github.com/uptrace/bun"
|
||||
"github.com/uptrace/bun/migrate"
|
||||
)
|
||||
|
||||
type addDashboardView struct {
|
||||
sqlstore sqlstore.SQLStore
|
||||
sqlschema sqlschema.SQLSchema
|
||||
}
|
||||
|
||||
func NewAddDashboardViewFactory(sqlstore sqlstore.SQLStore, sqlschema sqlschema.SQLSchema) factory.ProviderFactory[SQLMigration, Config] {
|
||||
return factory.NewProviderFactory(factory.MustNewName("add_dashboard_view"), func(ctx context.Context, ps factory.ProviderSettings, c Config) (SQLMigration, error) {
|
||||
return &addDashboardView{
|
||||
sqlstore: sqlstore,
|
||||
sqlschema: sqlschema,
|
||||
}, nil
|
||||
})
|
||||
}
|
||||
|
||||
func (migration *addDashboardView) Register(migrations *migrate.Migrations) error {
|
||||
return migrations.Register(migration.Up, migration.Down)
|
||||
}
|
||||
|
||||
func (migration *addDashboardView) Up(ctx context.Context, db *bun.DB) error {
|
||||
tx, err := db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
sqls := migration.sqlschema.Operator().CreateTable(&sqlschema.Table{
|
||||
Name: "dashboard_view",
|
||||
Columns: []*sqlschema.Column{
|
||||
{Name: "id", DataType: sqlschema.DataTypeText, Nullable: false},
|
||||
{Name: "name", DataType: sqlschema.DataTypeText, Nullable: false},
|
||||
{Name: "data", DataType: sqlschema.DataTypeText, Nullable: false},
|
||||
{Name: "org_id", DataType: sqlschema.DataTypeText, Nullable: false},
|
||||
{Name: "created_at", DataType: sqlschema.DataTypeTimestamp, Nullable: false},
|
||||
{Name: "updated_at", DataType: sqlschema.DataTypeTimestamp, Nullable: false},
|
||||
},
|
||||
PrimaryKeyConstraint: &sqlschema.PrimaryKeyConstraint{ColumnNames: []sqlschema.ColumnName{"id"}},
|
||||
ForeignKeyConstraints: []*sqlschema.ForeignKeyConstraint{
|
||||
{
|
||||
ReferencingColumnName: sqlschema.ColumnName("org_id"),
|
||||
ReferencedTableName: sqlschema.TableName("organizations"),
|
||||
ReferencedColumnName: sqlschema.ColumnName("id"),
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
for _, sql := range sqls {
|
||||
if _, err := tx.ExecContext(ctx, string(sql)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (migration *addDashboardView) Down(_ context.Context, _ *bun.DB) error {
|
||||
return nil
|
||||
}
|
||||
@@ -1,125 +0,0 @@
|
||||
package dashboardtypes
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/SigNoz/signoz/pkg/errors"
|
||||
"github.com/SigNoz/signoz/pkg/types"
|
||||
"github.com/SigNoz/signoz/pkg/valuer"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
const (
|
||||
DashboardViewSchemaVersion = "v1"
|
||||
MaxDashboardViewNameLen = 32
|
||||
)
|
||||
|
||||
var (
|
||||
ErrCodeDashboardViewInvalidInput = errors.MustNewCode("dashboard_view_invalid_input")
|
||||
ErrCodeDashboardViewNotFound = errors.MustNewCode("dashboard_view_not_found")
|
||||
)
|
||||
|
||||
type DashboardView struct {
|
||||
bun.BaseModel `bun:"table:dashboard_view,alias:dashboard_view"`
|
||||
|
||||
types.Identifiable
|
||||
types.TimeAuditable
|
||||
|
||||
Name string `bun:"name,type:text,notnull" json:"name" required:"true"`
|
||||
Data DashboardViewData `bun:"data,type:text,notnull" json:"data" required:"true"`
|
||||
OrgID valuer.UUID `bun:"org_id,type:text,notnull" json:"orgId" required:"true"`
|
||||
}
|
||||
|
||||
type DashboardViewData struct {
|
||||
Version string `json:"version" required:"true"`
|
||||
ListFilter
|
||||
}
|
||||
|
||||
func (d *DashboardViewData) Validate() error {
|
||||
if d.Version != DashboardViewSchemaVersion {
|
||||
return errors.NewInvalidInputf(ErrCodeDashboardViewInvalidInput,
|
||||
"version must be %q, got %q", DashboardViewSchemaVersion, d.Version)
|
||||
}
|
||||
return d.ListFilter.Validate()
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════
|
||||
// Postable
|
||||
// ════════════════════════════════════════════════════════════════════════
|
||||
|
||||
type PostableDashboardView struct {
|
||||
Name string `json:"name" required:"true"`
|
||||
Data DashboardViewData `json:"data" required:"true"`
|
||||
}
|
||||
|
||||
func (p *PostableDashboardView) UnmarshalJSON(data []byte) error {
|
||||
dec := json.NewDecoder(bytes.NewReader(data))
|
||||
dec.DisallowUnknownFields()
|
||||
type alias PostableDashboardView
|
||||
var tmp alias
|
||||
if err := dec.Decode(&tmp); err != nil {
|
||||
return errors.WrapInvalidInputf(err, ErrCodeDashboardViewInvalidInput, "invalid saved view request body").WithAdditional(err.Error())
|
||||
}
|
||||
*p = PostableDashboardView(tmp)
|
||||
return p.Validate()
|
||||
}
|
||||
|
||||
func (p *PostableDashboardView) Validate() error {
|
||||
if err := validateDashboardViewName(p.Name); err != nil {
|
||||
return err
|
||||
}
|
||||
return p.Data.Validate()
|
||||
}
|
||||
|
||||
func (p PostableDashboardView) NewDashboardView(orgID valuer.UUID) *DashboardView {
|
||||
now := time.Now()
|
||||
return &DashboardView{
|
||||
Identifiable: types.Identifiable{ID: valuer.GenerateUUID()},
|
||||
TimeAuditable: types.TimeAuditable{CreatedAt: now, UpdatedAt: now},
|
||||
OrgID: orgID,
|
||||
Name: p.Name,
|
||||
Data: p.Data,
|
||||
}
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════
|
||||
// Updateable
|
||||
// ════════════════════════════════════════════════════════════════════════
|
||||
|
||||
type UpdatableDashboardView = PostableDashboardView
|
||||
|
||||
func (v *DashboardView) Update(updateable UpdatableDashboardView) {
|
||||
v.Name = updateable.Name
|
||||
v.Data = updateable.Data
|
||||
v.UpdatedAt = time.Now()
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════
|
||||
// Listable
|
||||
// ════════════════════════════════════════════════════════════════════════
|
||||
|
||||
type ListableDashboardView struct {
|
||||
Views []*DashboardView `json:"views" required:"true" nullable:"false"`
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════
|
||||
// Helpers
|
||||
// ════════════════════════════════════════════════════════════════════════
|
||||
|
||||
func validateDashboardViewName(name string) error {
|
||||
if name == "" {
|
||||
return errors.NewInvalidInputf(ErrCodeDashboardViewInvalidInput, "name is required")
|
||||
}
|
||||
if name != strings.TrimSpace(name) {
|
||||
return errors.NewInvalidInputf(ErrCodeDashboardViewInvalidInput, "name must not have leading or trailing whitespace")
|
||||
}
|
||||
if n := utf8.RuneCountInString(name); n > MaxDashboardViewNameLen {
|
||||
return errors.NewInvalidInputf(ErrCodeDashboardViewInvalidInput,
|
||||
"name must be at most %d characters, got %d", MaxDashboardViewNameLen, n)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,157 +0,0 @@
|
||||
package dashboardtypes
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/SigNoz/signoz/pkg/valuer"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestDashboardViewDataValidate(t *testing.T) {
|
||||
cases := []struct {
|
||||
description string
|
||||
data DashboardViewData
|
||||
expectError bool
|
||||
}{
|
||||
{
|
||||
description: "valid with all fields set",
|
||||
data: DashboardViewData{Version: DashboardViewSchemaVersion, ListFilter: ListFilter{Query: "name=foo", Sort: ListSortName, Order: ListOrderAsc}},
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
description: "query over the cap is rejected",
|
||||
data: DashboardViewData{Version: DashboardViewSchemaVersion, ListFilter: ListFilter{Query: strings.Repeat("x", MaxListQueryLen+1)}},
|
||||
expectError: true,
|
||||
},
|
||||
{
|
||||
description: "valid with zero sort and order",
|
||||
data: DashboardViewData{Version: DashboardViewSchemaVersion},
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
description: "wrong version is rejected",
|
||||
data: DashboardViewData{Version: "v2"},
|
||||
expectError: true,
|
||||
},
|
||||
{
|
||||
description: "empty version is rejected",
|
||||
data: DashboardViewData{},
|
||||
expectError: true,
|
||||
},
|
||||
{
|
||||
description: "unknown sort is rejected",
|
||||
data: DashboardViewData{Version: DashboardViewSchemaVersion, ListFilter: ListFilter{Sort: ListSort{valuer.NewString("bogus")}}},
|
||||
expectError: true,
|
||||
},
|
||||
{
|
||||
description: "unknown order is rejected",
|
||||
data: DashboardViewData{Version: DashboardViewSchemaVersion, ListFilter: ListFilter{Order: ListOrder{valuer.NewString("sideways")}}},
|
||||
expectError: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
t.Run(c.description, func(t *testing.T) {
|
||||
err := c.data.Validate()
|
||||
if c.expectError {
|
||||
assert.Error(t, err)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostableDashboardViewUnmarshalJSON(t *testing.T) {
|
||||
cases := []struct {
|
||||
description string
|
||||
body string
|
||||
expectError bool
|
||||
expectedName string
|
||||
}{
|
||||
{
|
||||
description: "valid body keeps name as-is",
|
||||
body: `{"name":"my view","data":{"version":"v1","sort":"name","order":"asc"}}`,
|
||||
expectError: false,
|
||||
expectedName: "my view",
|
||||
},
|
||||
{
|
||||
description: "name with surrounding whitespace is rejected",
|
||||
body: `{"name":" my view ","data":{"version":"v1","sort":"name","order":"asc"}}`,
|
||||
expectError: true,
|
||||
},
|
||||
{
|
||||
description: "unknown field is rejected",
|
||||
body: `{"name":"my view","data":{"version":"v1"},"extra":true}`,
|
||||
expectError: true,
|
||||
},
|
||||
{
|
||||
description: "blank name is rejected",
|
||||
body: `{"name":" ","data":{"version":"v1"}}`,
|
||||
expectError: true,
|
||||
},
|
||||
{
|
||||
description: "name over max length is rejected",
|
||||
body: `{"name":"` + strings.Repeat("x", MaxDashboardViewNameLen+1) + `","data":{"version":"v1"}}`,
|
||||
expectError: true,
|
||||
},
|
||||
{
|
||||
description: "invalid data version is rejected",
|
||||
body: `{"name":"my view","data":{"version":"v9"}}`,
|
||||
expectError: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
t.Run(c.description, func(t *testing.T) {
|
||||
var p PostableDashboardView
|
||||
err := json.Unmarshal([]byte(c.body), &p)
|
||||
if c.expectError {
|
||||
assert.Error(t, err)
|
||||
return
|
||||
}
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, c.expectedName, p.Name)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostableDashboardViewNewDashboardView(t *testing.T) {
|
||||
orgID := valuer.GenerateUUID()
|
||||
postable := PostableDashboardView{
|
||||
Name: "my view",
|
||||
Data: DashboardViewData{Version: DashboardViewSchemaVersion, ListFilter: ListFilter{Sort: ListSortName, Order: ListOrderAsc}},
|
||||
}
|
||||
|
||||
view := postable.NewDashboardView(orgID)
|
||||
|
||||
assert.Equal(t, orgID, view.OrgID)
|
||||
assert.Equal(t, "my view", view.Name)
|
||||
assert.Equal(t, postable.Data, view.Data)
|
||||
assert.False(t, view.ID.IsZero())
|
||||
assert.False(t, view.CreatedAt.IsZero())
|
||||
assert.Equal(t, view.CreatedAt, view.UpdatedAt)
|
||||
}
|
||||
|
||||
func TestDashboardViewUpdate(t *testing.T) {
|
||||
orgID := valuer.GenerateUUID()
|
||||
view := PostableDashboardView{
|
||||
Name: "original",
|
||||
Data: DashboardViewData{Version: DashboardViewSchemaVersion, ListFilter: ListFilter{Sort: ListSortName, Order: ListOrderAsc}},
|
||||
}.NewDashboardView(orgID)
|
||||
createdAt := view.CreatedAt
|
||||
|
||||
view.Update(UpdatableDashboardView{
|
||||
Name: "renamed",
|
||||
Data: DashboardViewData{Version: DashboardViewSchemaVersion, ListFilter: ListFilter{Sort: ListSortCreatedAt, Order: ListOrderDesc}},
|
||||
})
|
||||
|
||||
assert.Equal(t, "renamed", view.Name)
|
||||
assert.Equal(t, ListSortCreatedAt, view.Data.Sort)
|
||||
assert.Equal(t, ListOrderDesc, view.Data.Order)
|
||||
assert.Equal(t, createdAt, view.CreatedAt)
|
||||
assert.True(t, view.UpdatedAt.After(createdAt) || view.UpdatedAt.Equal(createdAt))
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package dashboardtypes
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/SigNoz/signoz/pkg/errors"
|
||||
"github.com/SigNoz/signoz/pkg/types"
|
||||
@@ -13,7 +12,6 @@ import (
|
||||
const (
|
||||
DefaultListLimit = 20
|
||||
MaxListLimit = 200
|
||||
MaxListQueryLen = 1024
|
||||
)
|
||||
|
||||
// ListSort is the sort field for the dashboard list endpoint. The value is a
|
||||
@@ -51,45 +49,31 @@ func (o ListOrder) IsValid() bool {
|
||||
|
||||
var ErrCodeDashboardListInvalid = errors.MustNewCode("dashboard_list_invalid")
|
||||
|
||||
type ListFilter struct {
|
||||
Query string `query:"query" json:"query"`
|
||||
Sort ListSort `query:"sort" json:"sort"`
|
||||
Order ListOrder `query:"order" json:"order"`
|
||||
}
|
||||
|
||||
func (f *ListFilter) Validate() error {
|
||||
if n := utf8.RuneCountInString(f.Query); n > MaxListQueryLen {
|
||||
return errors.NewInvalidInputf(ErrCodeDashboardListInvalid,
|
||||
"query cannot be longer than %d characters, got %d", MaxListQueryLen, n)
|
||||
}
|
||||
if !f.Sort.IsZero() && !f.Sort.IsValid() {
|
||||
return errors.NewInvalidInputf(ErrCodeDashboardListInvalid,
|
||||
"invalid sort %q — expected one of: `updated_at`, `created_at`, `name`", f.Sort)
|
||||
}
|
||||
if !f.Order.IsZero() && !f.Order.IsValid() {
|
||||
return errors.NewInvalidInputf(ErrCodeDashboardListInvalid,
|
||||
"invalid order %q — expected `asc` or `desc`", f.Order)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type ListDashboardsV2Params struct {
|
||||
ListFilter
|
||||
Limit int `query:"limit"`
|
||||
Offset int `query:"offset"`
|
||||
Query string `query:"query"`
|
||||
Sort ListSort `query:"sort"`
|
||||
Order ListOrder `query:"order"`
|
||||
Limit int `query:"limit"`
|
||||
Offset int `query:"offset"`
|
||||
}
|
||||
|
||||
// Validate fills in defaults (sort=updated_at, order=desc, limit=20) and
|
||||
// rejects out-of-allowlist sort/order values and bad limit/offset. Limit is
|
||||
// clamped to MaxListLimit on the high side. Sort/order are case-insensitive —
|
||||
// valuer.String lowercases them at bind time.
|
||||
func (p *ListDashboardsV2Params) Validate() error {
|
||||
if err := p.ListFilter.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if p.Sort.IsZero() {
|
||||
p.Sort = ListSortUpdatedAt
|
||||
} else if !p.Sort.IsValid() {
|
||||
return errors.NewInvalidInputf(ErrCodeDashboardListInvalid,
|
||||
"invalid sort %q — expected one of: `updated_at`, `created_at`, `name`", p.Sort)
|
||||
}
|
||||
|
||||
if p.Order.IsZero() {
|
||||
p.Order = ListOrderDesc
|
||||
} else if !p.Order.IsValid() {
|
||||
return errors.NewInvalidInputf(ErrCodeDashboardListInvalid,
|
||||
"invalid order %q — expected `asc` or `desc`", p.Order)
|
||||
}
|
||||
|
||||
if p.Limit == 0 {
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
package dashboardtypes
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestListDashboardsV2ParamsValidate(t *testing.T) {
|
||||
t.Run("fills defaults when sort, order and limit are unset", func(t *testing.T) {
|
||||
p := &ListDashboardsV2Params{}
|
||||
require.NoError(t, p.Validate())
|
||||
assert.Equal(t, ListSortUpdatedAt, p.Sort)
|
||||
assert.Equal(t, ListOrderDesc, p.Order)
|
||||
assert.Equal(t, DefaultListLimit, p.Limit)
|
||||
})
|
||||
|
||||
t.Run("clamps limit to MaxListLimit", func(t *testing.T) {
|
||||
p := &ListDashboardsV2Params{Limit: MaxListLimit + 50}
|
||||
require.NoError(t, p.Validate())
|
||||
assert.Equal(t, MaxListLimit, p.Limit)
|
||||
})
|
||||
|
||||
t.Run("query at the cap is accepted", func(t *testing.T) {
|
||||
p := &ListDashboardsV2Params{ListFilter: ListFilter{Query: strings.Repeat("x", MaxListQueryLen)}}
|
||||
assert.NoError(t, p.Validate())
|
||||
})
|
||||
|
||||
t.Run("query over the cap is rejected", func(t *testing.T) {
|
||||
p := &ListDashboardsV2Params{ListFilter: ListFilter{Query: strings.Repeat("x", MaxListQueryLen+1)}}
|
||||
assert.Error(t, p.Validate())
|
||||
})
|
||||
}
|
||||
67
pkg/types/dashboardtypes/perses_v1_to_v2.go
Normal file
67
pkg/types/dashboardtypes/perses_v1_to_v2.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package dashboardtypes
|
||||
|
||||
import (
|
||||
"github.com/SigNoz/signoz/pkg/errors"
|
||||
)
|
||||
|
||||
// V1 → V2 migration. The v1 storable shape is the frontend's `DashboardData`
|
||||
// (see frontend/src/types/api/dashboard/getAll.ts); v2 is DashboardV2 /
|
||||
// DashboardSpec.
|
||||
//
|
||||
// Assumes the v1 widget query data has already been migrated to v5 shape
|
||||
// (transition.dashboardMigrateV5). Pre-v5 builder queries will produce
|
||||
// invalid v2 envelopes — run the v4→v5 migration first.
|
||||
//
|
||||
// The conversion is split across sibling files by concern:
|
||||
// - perses_v1_to_v2_tags.go tags
|
||||
// - perses_v1_to_v2_panels.go widgets → panels (+ panel field mappers)
|
||||
// - perses_v1_to_v2_queries.go widget queries
|
||||
// - perses_v1_to_v2_layouts.go grid layouts and sections
|
||||
// - perses_v1_to_v2_variables.go variables
|
||||
// - perses_v1_to_v2_helpers.go generic map/slice accessors
|
||||
|
||||
// ══════════════════════════════════════════════
|
||||
// Entry point
|
||||
// ══════════════════════════════════════════════
|
||||
|
||||
func (storable StorableDashboard) IsV2() bool {
|
||||
metadata, _ := storable.Data["metadata"].(map[string]any)
|
||||
if metadata == nil {
|
||||
return false
|
||||
}
|
||||
version, _ := metadata["schemaVersion"].(string)
|
||||
return version == SchemaVersion
|
||||
}
|
||||
|
||||
func (storable StorableDashboard) ConvertV1ToV2() (*DashboardV2, error) {
|
||||
if storable.IsV2() {
|
||||
return nil, errors.Newf(errors.TypeInvalidInput, ErrCodeDashboardInvalidData, "dashboard %s is already in %s schema", storable.ID, SchemaVersion)
|
||||
}
|
||||
|
||||
image, _ := storable.Data["image"].(string)
|
||||
title, _ := storable.Data["title"].(string)
|
||||
description, _ := storable.Data["description"].(string)
|
||||
|
||||
spec := DashboardSpec{
|
||||
Display: Display{Name: title, Description: description},
|
||||
Variables: convertV1Variables(storable.Data["variables"]),
|
||||
Panels: convertV1Panels(storable.Data["widgets"]),
|
||||
Layouts: convertV1Layouts(storable.Data),
|
||||
}
|
||||
|
||||
return &DashboardV2{
|
||||
Identifiable: storable.Identifiable,
|
||||
TimeAuditable: storable.TimeAuditable,
|
||||
UserAuditable: storable.UserAuditable,
|
||||
OrgID: storable.OrgID,
|
||||
Locked: storable.Locked,
|
||||
Source: storable.Source,
|
||||
DashboardV2MetadataBase: DashboardV2MetadataBase{
|
||||
SchemaVersion: SchemaVersion,
|
||||
Image: image,
|
||||
},
|
||||
Name: storable.Name,
|
||||
Tags: convertV1TagsForOrg(storable.OrgID, storable.Data["tags"]),
|
||||
Spec: spec,
|
||||
}, nil
|
||||
}
|
||||
85
pkg/types/dashboardtypes/perses_v1_to_v2_helpers.go
Normal file
85
pkg/types/dashboardtypes/perses_v1_to_v2_helpers.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package dashboardtypes
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
// ══════════════════════════════════════════════
|
||||
// Generic helpers
|
||||
// ══════════════════════════════════════════════
|
||||
|
||||
// ptrValueAt is the pointer-returning sibling of valueAt: it returns *T so the
|
||||
// caller can tell "absent / wrong type" (nil) apart from a present zero value.
|
||||
// Used for optional fields like soft axis bounds and histogram bucket sizing.
|
||||
func ptrValueAt[T any](raw any, key string) *T {
|
||||
m, ok := raw.(map[string]any)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
v, ok := m[key].(T)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return &v
|
||||
}
|
||||
|
||||
func readStringMap(raw any) map[string]string {
|
||||
m, ok := raw.(map[string]any)
|
||||
if !ok || len(m) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make(map[string]string, len(m))
|
||||
for k, v := range m {
|
||||
if s, ok := v.(string); ok {
|
||||
out[k] = s
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func readSliceOfMaps(raw any) []map[string]any {
|
||||
rawSlice, ok := raw.([]any)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
out := make([]map[string]any, 0, len(rawSlice))
|
||||
for _, item := range rawSlice {
|
||||
if m, ok := item.(map[string]any); ok {
|
||||
out = append(out, m)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// valueAt reads key from raw (when raw is a map[string]any) and returns its
|
||||
// value as T, or the zero value of T if raw isn't a map, the key is absent, or
|
||||
// the stored value isn't a T. Used to pull typed fields out of the untyped v1
|
||||
// dashboard blob.
|
||||
func valueAt[T any](raw any, key string) T {
|
||||
var zero T
|
||||
m, ok := raw.(map[string]any)
|
||||
if !ok {
|
||||
return zero
|
||||
}
|
||||
v, _ := m[key].(T)
|
||||
return v
|
||||
}
|
||||
|
||||
// intAt is a thin wrapper over valueAt: JSON decodes numbers as float64, so an
|
||||
// integer field must be read as float64 and narrowed.
|
||||
func intAt(raw any, key string) int {
|
||||
return int(valueAt[float64](raw, key))
|
||||
}
|
||||
|
||||
// decodeMapInto converts an untyped map[string]any into a typed T by
|
||||
// round-tripping through JSON, letting encoding/json (struct tags, custom
|
||||
// UnmarshalJSON) do the field mapping instead of hand-copying out of the map.
|
||||
func decodeMapInto[T any](src map[string]any) (T, error) {
|
||||
var dst T
|
||||
bytes, err := json.Marshal(src)
|
||||
if err != nil {
|
||||
return dst, err
|
||||
}
|
||||
if err := json.Unmarshal(bytes, &dst); err != nil {
|
||||
return dst, err
|
||||
}
|
||||
return dst, nil
|
||||
}
|
||||
138
pkg/types/dashboardtypes/perses_v1_to_v2_layouts.go
Normal file
138
pkg/types/dashboardtypes/perses_v1_to_v2_layouts.go
Normal file
@@ -0,0 +1,138 @@
|
||||
package dashboardtypes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"github.com/perses/spec/go/common"
|
||||
"github.com/perses/spec/go/dashboard"
|
||||
)
|
||||
|
||||
// ══════════════════════════════════════════════
|
||||
// Layouts (data.layout + data.panelMap)
|
||||
// ══════════════════════════════════════════════
|
||||
|
||||
// convertV1Layouts groups v1 react-grid-layout entries by section. Each row
|
||||
// widget (panelTypes == "row") in `widgets` plus its `panelMap` entry becomes
|
||||
// a separate v2 grid layout with a collapsible display. Widgets that are not
|
||||
// part of any section land in a default unnamed grid (added only if any such
|
||||
// widgets exist).
|
||||
func convertV1Layouts(data StorableDashboardData) []Layout {
|
||||
layoutsRaw := readSliceOfMaps(data["layout"])
|
||||
if len(layoutsRaw) == 0 {
|
||||
return nil
|
||||
}
|
||||
panelMap, _ := data["panelMap"].(map[string]any)
|
||||
|
||||
rows, widgetIDToRow := indexRows(data["widgets"], panelMap)
|
||||
|
||||
type bucket struct {
|
||||
title string
|
||||
open bool
|
||||
isRow bool
|
||||
layouts []map[string]any
|
||||
ordering int
|
||||
}
|
||||
rootBucket := &bucket{}
|
||||
rowBuckets := make(map[string]*bucket, len(rows))
|
||||
for _, row := range rows {
|
||||
rowBuckets[row.id] = &bucket{
|
||||
title: row.title,
|
||||
open: !row.collapsed,
|
||||
isRow: true,
|
||||
ordering: row.ordering,
|
||||
}
|
||||
}
|
||||
|
||||
for _, item := range layoutsRaw {
|
||||
widgetID, _ := item["i"].(string)
|
||||
if widgetID == "" {
|
||||
continue
|
||||
}
|
||||
if rowID, ok := widgetIDToRow[widgetID]; ok {
|
||||
if b, ok := rowBuckets[rowID]; ok {
|
||||
b.layouts = append(b.layouts, item)
|
||||
continue
|
||||
}
|
||||
}
|
||||
// row widgets themselves shouldn't end up as items in the root grid;
|
||||
// they exist only to anchor their section.
|
||||
if _, isRow := rowBuckets[widgetID]; isRow {
|
||||
continue
|
||||
}
|
||||
rootBucket.layouts = append(rootBucket.layouts, item)
|
||||
}
|
||||
|
||||
out := make([]Layout, 0, len(rows)+1)
|
||||
if len(rootBucket.layouts) > 0 {
|
||||
out = append(out, gridLayoutFromBucket("", true, false, rootBucket.layouts))
|
||||
}
|
||||
|
||||
rowKeys := make([]string, 0, len(rowBuckets))
|
||||
for id := range rowBuckets {
|
||||
rowKeys = append(rowKeys, id)
|
||||
}
|
||||
sort.SliceStable(rowKeys, func(i, j int) bool {
|
||||
return rowBuckets[rowKeys[i]].ordering < rowBuckets[rowKeys[j]].ordering
|
||||
})
|
||||
for _, id := range rowKeys {
|
||||
b := rowBuckets[id]
|
||||
out = append(out, gridLayoutFromBucket(b.title, b.open, true, b.layouts))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
type rowInfo struct {
|
||||
id string
|
||||
title string
|
||||
collapsed bool
|
||||
ordering int
|
||||
}
|
||||
|
||||
func indexRows(widgetsRaw any, panelMap map[string]any) ([]rowInfo, map[string]string) {
|
||||
widgets := readSliceOfMaps(widgetsRaw)
|
||||
rows := make([]rowInfo, 0)
|
||||
widgetToRow := make(map[string]string)
|
||||
for i, w := range widgets {
|
||||
if t, _ := w["panelTypes"].(string); t != "row" {
|
||||
continue
|
||||
}
|
||||
id, _ := w["id"].(string)
|
||||
if id == "" {
|
||||
continue
|
||||
}
|
||||
title, _ := w["title"].(string)
|
||||
row := rowInfo{id: id, title: title, ordering: i}
|
||||
if pm, ok := panelMap[id].(map[string]any); ok {
|
||||
row.collapsed = valueAt[bool](pm, "collapsed")
|
||||
for _, child := range readSliceOfMaps(pm["widgets"]) {
|
||||
if childID, _ := child["i"].(string); childID != "" {
|
||||
widgetToRow[childID] = id
|
||||
}
|
||||
}
|
||||
}
|
||||
rows = append(rows, row)
|
||||
}
|
||||
return rows, widgetToRow
|
||||
}
|
||||
|
||||
func gridLayoutFromBucket(title string, open, isRow bool, items []map[string]any) Layout {
|
||||
spec := dashboard.GridLayoutSpec{Items: make([]dashboard.GridItem, 0, len(items))}
|
||||
if title != "" || isRow {
|
||||
spec.Display = &dashboard.GridLayoutDisplay{Title: title}
|
||||
if isRow {
|
||||
spec.Display.Collapse = &dashboard.GridLayoutCollapse{Open: open}
|
||||
}
|
||||
}
|
||||
for _, item := range items {
|
||||
widgetID, _ := item["i"].(string)
|
||||
spec.Items = append(spec.Items, dashboard.GridItem{
|
||||
X: intAt(item, "x"),
|
||||
Y: intAt(item, "y"),
|
||||
Width: intAt(item, "w"),
|
||||
Height: intAt(item, "h"),
|
||||
Content: &common.JSONRef{Ref: fmt.Sprintf("#/spec/panels/%s", widgetID)},
|
||||
})
|
||||
}
|
||||
return Layout{Kind: dashboard.KindGridLayout, Spec: &spec}
|
||||
}
|
||||
449
pkg/types/dashboardtypes/perses_v1_to_v2_panels.go
Normal file
449
pkg/types/dashboardtypes/perses_v1_to_v2_panels.go
Normal file
@@ -0,0 +1,449 @@
|
||||
package dashboardtypes
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/SigNoz/signoz/pkg/types/telemetrytypes"
|
||||
"github.com/SigNoz/signoz/pkg/valuer"
|
||||
)
|
||||
|
||||
// ══════════════════════════════════════════════
|
||||
// Widgets → Panels
|
||||
// ══════════════════════════════════════════════
|
||||
|
||||
// convertV1Panels walks the v1 `widgets` array and produces v2 panels keyed by
|
||||
// the v1 widget id. WidgetRow entries (panelTypes == "row") are dropped here
|
||||
// and consumed by convertV1Layouts as section headers.
|
||||
func convertV1Panels(raw any) map[string]*Panel {
|
||||
rawSlice, ok := raw.([]any)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
panels := make(map[string]*Panel, len(rawSlice))
|
||||
for _, item := range rawSlice {
|
||||
widget, ok := item.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
id, _ := widget["id"].(string)
|
||||
if id == "" {
|
||||
continue
|
||||
}
|
||||
panelType, _ := widget["panelTypes"].(string)
|
||||
var panel *Panel
|
||||
switch panelType {
|
||||
case "graph":
|
||||
panel = convertGraphWidget(widget)
|
||||
case "bar":
|
||||
panel = convertBarWidget(widget)
|
||||
case "value":
|
||||
panel = convertValueWidget(widget)
|
||||
case "pie":
|
||||
panel = convertPieWidget(widget)
|
||||
case "table":
|
||||
panel = convertTableWidget(widget)
|
||||
case "histogram":
|
||||
panel = convertHistogramWidget(widget)
|
||||
case "list":
|
||||
panel = convertListWidget(widget)
|
||||
default:
|
||||
// "row" (section header) is handled by the layout pass; unknown kinds skipped.
|
||||
continue
|
||||
}
|
||||
if panel == nil {
|
||||
continue
|
||||
}
|
||||
panels[id] = panel
|
||||
}
|
||||
return panels
|
||||
}
|
||||
|
||||
func convertGraphWidget(w map[string]any) *Panel {
|
||||
return &Panel{
|
||||
Kind: "Panel",
|
||||
Spec: PanelSpec{
|
||||
Display: widgetDisplay(w),
|
||||
Plugin: PanelPlugin{
|
||||
Kind: PanelKindTimeSeries,
|
||||
Spec: &TimeSeriesPanelSpec{
|
||||
Visualization: TimeSeriesVisualization{
|
||||
BasicVisualization: basicVisualization(w),
|
||||
FillSpans: valueAt[bool](w, "fillSpans"),
|
||||
},
|
||||
Formatting: panelFormatting(w),
|
||||
ChartAppearance: TimeSeriesChartAppearance{
|
||||
LineInterpolation: mapV1Enum(w["lineInterpolation"], LineInterpolationSpline,
|
||||
LineInterpolationLinear, LineInterpolationSpline, LineInterpolationStepAfter, LineInterpolationStepBefore),
|
||||
ShowPoints: valueAt[bool](w, "showPoints"),
|
||||
LineStyle: mapV1Enum(w["lineStyle"], LineStyleSolid, LineStyleSolid, LineStyleDashed),
|
||||
FillMode: mapV1Enum(w["fillMode"], FillModeSolid, FillModeSolid, FillModeGradient, FillModeNone),
|
||||
SpanGaps: mapV1SpanGaps(w["spanGaps"]),
|
||||
},
|
||||
Axes: axesFromWidget(w),
|
||||
Legend: legendFromWidget(w),
|
||||
Thresholds: mapV1ThresholdsWithLabel(w["thresholds"]),
|
||||
},
|
||||
},
|
||||
Queries: convertV1WidgetQuery(w, PanelKindTimeSeries),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func convertBarWidget(w map[string]any) *Panel {
|
||||
return &Panel{
|
||||
Kind: "Panel",
|
||||
Spec: PanelSpec{
|
||||
Display: widgetDisplay(w),
|
||||
Plugin: PanelPlugin{
|
||||
Kind: PanelKindBarChart,
|
||||
Spec: &BarChartPanelSpec{
|
||||
Visualization: BarChartVisualization{
|
||||
BasicVisualization: basicVisualization(w),
|
||||
FillSpans: valueAt[bool](w, "fillSpans"),
|
||||
StackedBarChart: valueAt[bool](w, "stackedBarChart"),
|
||||
},
|
||||
Formatting: panelFormatting(w),
|
||||
Axes: axesFromWidget(w),
|
||||
Legend: legendFromWidget(w),
|
||||
Thresholds: mapV1ThresholdsWithLabel(w["thresholds"]),
|
||||
},
|
||||
},
|
||||
Queries: convertV1WidgetQuery(w, PanelKindBarChart),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func convertValueWidget(w map[string]any) *Panel {
|
||||
return &Panel{
|
||||
Kind: "Panel",
|
||||
Spec: PanelSpec{
|
||||
Display: widgetDisplay(w),
|
||||
Plugin: PanelPlugin{
|
||||
Kind: PanelKindNumber,
|
||||
Spec: &NumberPanelSpec{
|
||||
Visualization: basicVisualization(w),
|
||||
Formatting: panelFormatting(w),
|
||||
Thresholds: mapV1ComparisonThresholds(w["thresholds"]),
|
||||
},
|
||||
},
|
||||
Queries: convertV1WidgetQuery(w, PanelKindNumber),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func convertPieWidget(w map[string]any) *Panel {
|
||||
return &Panel{
|
||||
Kind: "Panel",
|
||||
Spec: PanelSpec{
|
||||
Display: widgetDisplay(w),
|
||||
Plugin: PanelPlugin{
|
||||
Kind: PanelKindPieChart,
|
||||
Spec: &PieChartPanelSpec{
|
||||
Visualization: basicVisualization(w),
|
||||
Formatting: panelFormatting(w),
|
||||
Legend: legendFromWidget(w),
|
||||
},
|
||||
},
|
||||
Queries: convertV1WidgetQuery(w, PanelKindPieChart),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func convertTableWidget(w map[string]any) *Panel {
|
||||
return &Panel{
|
||||
Kind: "Panel",
|
||||
Spec: PanelSpec{
|
||||
Display: widgetDisplay(w),
|
||||
Plugin: PanelPlugin{
|
||||
Kind: PanelKindTable,
|
||||
Spec: &TablePanelSpec{
|
||||
Visualization: basicVisualization(w),
|
||||
Formatting: TableFormatting{
|
||||
ColumnUnits: readStringMap(w["columnUnits"]),
|
||||
DecimalPrecision: mapV1Precision(w["decimalPrecision"]),
|
||||
},
|
||||
Thresholds: mapV1TableThresholds(w["thresholds"]),
|
||||
},
|
||||
},
|
||||
Queries: convertV1WidgetQuery(w, PanelKindTable),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func convertHistogramWidget(w map[string]any) *Panel {
|
||||
return &Panel{
|
||||
Kind: "Panel",
|
||||
Spec: PanelSpec{
|
||||
Display: widgetDisplay(w),
|
||||
Plugin: PanelPlugin{
|
||||
Kind: PanelKindHistogram,
|
||||
Spec: &HistogramPanelSpec{
|
||||
HistogramBuckets: HistogramBuckets{
|
||||
BucketCount: ptrValueAt[float64](w, "bucketCount"),
|
||||
BucketWidth: ptrValueAt[float64](w, "bucketWidth"),
|
||||
MergeAllActiveQueries: valueAt[bool](w, "mergeAllActiveQueries"),
|
||||
},
|
||||
Legend: legendFromWidget(w),
|
||||
},
|
||||
},
|
||||
Queries: convertV1WidgetQuery(w, PanelKindHistogram),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func convertListWidget(w map[string]any) *Panel {
|
||||
return &Panel{
|
||||
Kind: "Panel",
|
||||
Spec: PanelSpec{
|
||||
Display: widgetDisplay(w),
|
||||
Plugin: PanelPlugin{
|
||||
Kind: PanelKindList,
|
||||
Spec: &ListPanelSpec{
|
||||
SelectFields: mapV1SelectFields(w),
|
||||
},
|
||||
},
|
||||
Queries: convertV1WidgetQuery(w, PanelKindList),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// ══════════════════════════════════════════════
|
||||
// Panel-spec shared helpers
|
||||
// ══════════════════════════════════════════════
|
||||
|
||||
func widgetDisplay(w map[string]any) Display {
|
||||
title, _ := w["title"].(string)
|
||||
description, _ := w["description"].(string)
|
||||
return Display{Name: title, Description: description}
|
||||
}
|
||||
|
||||
func basicVisualization(w map[string]any) BasicVisualization {
|
||||
return BasicVisualization{TimePreference: mapV1TimePreference(w["timePreferance"])}
|
||||
}
|
||||
|
||||
func panelFormatting(w map[string]any) PanelFormatting {
|
||||
unit, _ := w["yAxisUnit"].(string)
|
||||
return PanelFormatting{Unit: unit, DecimalPrecision: mapV1Precision(w["decimalPrecision"])}
|
||||
}
|
||||
|
||||
func axesFromWidget(w map[string]any) Axes {
|
||||
return Axes{
|
||||
SoftMin: ptrValueAt[float64](w, "softMin"),
|
||||
SoftMax: ptrValueAt[float64](w, "softMax"),
|
||||
IsLogScale: valueAt[bool](w, "isLogScale"),
|
||||
}
|
||||
}
|
||||
|
||||
func legendFromWidget(w map[string]any) Legend {
|
||||
return Legend{
|
||||
Position: mapV1Enum(w["legendPosition"], LegendPositionBottom, LegendPositionBottom, LegendPositionRight),
|
||||
CustomColors: readStringMap(w["customLegendColors"]),
|
||||
}
|
||||
}
|
||||
|
||||
func mapV1SelectFields(w map[string]any) []telemetrytypes.TelemetryFieldKey {
|
||||
if raw, ok := w["selectedLogFields"].([]any); ok && len(raw) > 0 {
|
||||
return decodeTelemetryFields(raw)
|
||||
}
|
||||
if raw, ok := w["selectedTracesFields"].([]any); ok && len(raw) > 0 {
|
||||
return decodeTelemetryFields(raw)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func decodeTelemetryFields(raw []any) []telemetrytypes.TelemetryFieldKey {
|
||||
bytes, err := json.Marshal(raw)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
var fields []telemetrytypes.TelemetryFieldKey
|
||||
if err := json.Unmarshal(bytes, &fields); err != nil {
|
||||
return nil
|
||||
}
|
||||
return fields
|
||||
}
|
||||
|
||||
// ══════════════════════════════════════════════
|
||||
// Panel field mappers
|
||||
// ══════════════════════════════════════════════
|
||||
|
||||
// v1 stores timePreferance as `GLOBAL_TIME`, `LAST_5_MIN`, … (see
|
||||
// frontend/src/container/NewWidget/RightContainer/timeItems.ts). v2 uses the
|
||||
// lowercase form, so the translation is just downcase.
|
||||
func mapV1TimePreference(raw any) TimePreference {
|
||||
s, ok := raw.(string)
|
||||
if !ok || s == "" {
|
||||
return TimePreferenceGlobalTime
|
||||
}
|
||||
candidate := TimePreference{valuer.NewString(strings.ToLower(s))}
|
||||
for _, allowed := range candidate.Enum() {
|
||||
if allowed == candidate {
|
||||
return candidate
|
||||
}
|
||||
}
|
||||
return TimePreferenceGlobalTime
|
||||
}
|
||||
|
||||
func mapV1Precision(raw any) PrecisionOption {
|
||||
switch v := raw.(type) {
|
||||
case string:
|
||||
candidate := PrecisionOption{valuer.NewString(v)}
|
||||
for _, allowed := range candidate.Enum() {
|
||||
if allowed == candidate {
|
||||
return candidate
|
||||
}
|
||||
}
|
||||
case float64:
|
||||
n := int(v)
|
||||
if n >= 0 && n <= 4 {
|
||||
return PrecisionOption{valuer.NewString(strconv.Itoa(n))}
|
||||
}
|
||||
}
|
||||
return PrecisionOption2
|
||||
}
|
||||
|
||||
// mapV1Enum picks the v1 string value if it matches one of the allowed v2
|
||||
// values, otherwise returns the fallback. v1 frontend enums (lineInterpolation,
|
||||
// lineStyle, fillMode, legendPosition) already use the v2 lowercase form.
|
||||
func mapV1Enum[T interface{ StringValue() string }](raw any, fallback T, allowed ...T) T {
|
||||
s, ok := raw.(string)
|
||||
if !ok || s == "" {
|
||||
return fallback
|
||||
}
|
||||
for _, a := range allowed {
|
||||
if a.StringValue() == s {
|
||||
return a
|
||||
}
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
// v1 spanGaps is `boolean | number`. true → span every gap; false → never span;
|
||||
// a number is interpreted (per frontend SeriesProps.spanGaps docs) as an
|
||||
// X-axis threshold in seconds.
|
||||
func mapV1SpanGaps(raw any) SpanGaps {
|
||||
switch v := raw.(type) {
|
||||
case bool:
|
||||
if v {
|
||||
return SpanGaps{FillOnlyBelow: false}
|
||||
}
|
||||
return SpanGaps{FillOnlyBelow: true}
|
||||
case float64:
|
||||
dur, err := valuer.ParseTextDuration(time.Duration(v * float64(time.Second)).String())
|
||||
if err != nil {
|
||||
return SpanGaps{FillOnlyBelow: false}
|
||||
}
|
||||
return SpanGaps{FillOnlyBelow: true, FillLessThan: dur}
|
||||
}
|
||||
return SpanGaps{FillOnlyBelow: false}
|
||||
}
|
||||
|
||||
func mapV1ThresholdsWithLabel(raw any) []ThresholdWithLabel {
|
||||
rawSlice := readSliceOfMaps(raw)
|
||||
if len(rawSlice) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make([]ThresholdWithLabel, 0, len(rawSlice))
|
||||
for _, t := range rawSlice {
|
||||
color, _ := t["thresholdColor"].(string)
|
||||
label, _ := t["thresholdLabel"].(string)
|
||||
if color == "" || label == "" {
|
||||
// v2 ThresholdWithLabel requires both; drop entries that wouldn't validate.
|
||||
continue
|
||||
}
|
||||
value, _ := t["thresholdValue"].(float64)
|
||||
unit, _ := t["thresholdUnit"].(string)
|
||||
out = append(out, ThresholdWithLabel{Value: value, Unit: unit, Color: color, Label: label})
|
||||
}
|
||||
if len(out) == 0 {
|
||||
return nil
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func mapV1ComparisonThresholds(raw any) []ComparisonThreshold {
|
||||
rawSlice := readSliceOfMaps(raw)
|
||||
if len(rawSlice) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make([]ComparisonThreshold, 0, len(rawSlice))
|
||||
for _, t := range rawSlice {
|
||||
color, _ := t["thresholdColor"].(string)
|
||||
if color == "" {
|
||||
continue
|
||||
}
|
||||
out = append(out, ComparisonThreshold{
|
||||
Value: valueAt[float64](t, "thresholdValue"),
|
||||
Operator: mapV1ComparisonOperator(t["thresholdOperator"]),
|
||||
Unit: valueAt[string](t, "thresholdUnit"),
|
||||
Color: color,
|
||||
Format: mapV1ThresholdFormat(t["thresholdFormat"]),
|
||||
})
|
||||
}
|
||||
if len(out) == 0 {
|
||||
return nil
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func mapV1TableThresholds(raw any) []TableThreshold {
|
||||
rawSlice := readSliceOfMaps(raw)
|
||||
if len(rawSlice) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make([]TableThreshold, 0, len(rawSlice))
|
||||
for _, t := range rawSlice {
|
||||
color, _ := t["thresholdColor"].(string)
|
||||
columnName, _ := t["thresholdTableOptions"].(string)
|
||||
if color == "" || columnName == "" {
|
||||
continue
|
||||
}
|
||||
out = append(out, TableThreshold{
|
||||
ComparisonThreshold: ComparisonThreshold{
|
||||
Value: valueAt[float64](t, "thresholdValue"),
|
||||
Operator: mapV1ComparisonOperator(t["thresholdOperator"]),
|
||||
Unit: valueAt[string](t, "thresholdUnit"),
|
||||
Color: color,
|
||||
Format: mapV1ThresholdFormat(t["thresholdFormat"]),
|
||||
},
|
||||
ColumnName: columnName,
|
||||
})
|
||||
}
|
||||
if len(out) == 0 {
|
||||
return nil
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func mapV1ComparisonOperator(raw any) ComparisonOperator {
|
||||
s, _ := raw.(string)
|
||||
switch s {
|
||||
case ">":
|
||||
return ComparisonOperatorAbove
|
||||
case ">=":
|
||||
return ComparisonOperatorAboveOrEqual
|
||||
case "<":
|
||||
return ComparisonOperatorBelow
|
||||
case "<=":
|
||||
return ComparisonOperatorBelowOrEqual
|
||||
case "=":
|
||||
return ComparisonOperatorEqual
|
||||
case "!=":
|
||||
return ComparisonOperatorNotEqual
|
||||
}
|
||||
return ComparisonOperatorAbove
|
||||
}
|
||||
|
||||
func mapV1ThresholdFormat(raw any) ThresholdFormat {
|
||||
s, _ := raw.(string)
|
||||
switch strings.ToLower(s) {
|
||||
case "background":
|
||||
return ThresholdFormatBackground
|
||||
case "text":
|
||||
return ThresholdFormatText
|
||||
}
|
||||
return ThresholdFormatText
|
||||
}
|
||||
249
pkg/types/dashboardtypes/perses_v1_to_v2_queries.go
Normal file
249
pkg/types/dashboardtypes/perses_v1_to_v2_queries.go
Normal file
@@ -0,0 +1,249 @@
|
||||
package dashboardtypes
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/SigNoz/signoz/pkg/errors"
|
||||
"github.com/SigNoz/signoz/pkg/transition"
|
||||
qb "github.com/SigNoz/signoz/pkg/types/querybuildertypes/querybuildertypesv5"
|
||||
"github.com/SigNoz/signoz/pkg/types/telemetrytypes"
|
||||
)
|
||||
|
||||
// ══════════════════════════════════════════════
|
||||
// Queries
|
||||
// ══════════════════════════════════════════════
|
||||
|
||||
// convertV1WidgetQuery returns exactly one Query (per Spec.Validate). The
|
||||
// kind chosen depends on the v1 widget query shape:
|
||||
// - single promql → signoz/PromQLQuery
|
||||
// - single clickhouse_sql → signoz/ClickHouseSQL
|
||||
// - exactly one builder query → signoz/BuilderQuery (PanelKindList only)
|
||||
// - everything else → signoz/CompositeQuery wrapping all envelopes
|
||||
//
|
||||
// Builder queries are routed through transition.WrapInV5Envelope, which
|
||||
// translates v4 builder-field names (orderBy/selectColumns/dataSource) into
|
||||
// their v5 equivalents and adds the `signal` field required by
|
||||
// BuilderQuerySpec's per-signal dispatch.
|
||||
func convertV1WidgetQuery(widget map[string]any, panelKind PanelPluginKind) []Query {
|
||||
envelopes, signal := collectV1QueryEnvelopes(widget)
|
||||
if len(envelopes) == 0 {
|
||||
return nil
|
||||
}
|
||||
requestType := requestTypeForPanel(panelKind)
|
||||
|
||||
// List panels must use signoz/BuilderQuery (the only kind in
|
||||
// allowedQueryKinds[PanelKindList]).
|
||||
if panelKind == PanelKindList {
|
||||
first := envelopes[0]
|
||||
if t, _ := first["type"].(string); t == string(qb.QueryTypeBuilder.StringValue()) {
|
||||
spec := parseBuilderQuerySpec(first["spec"], signal)
|
||||
if spec == nil {
|
||||
return nil
|
||||
}
|
||||
return []Query{{
|
||||
Kind: requestType,
|
||||
Spec: QuerySpec{
|
||||
Name: valueAt[string](first["spec"], "name"),
|
||||
Plugin: QueryPlugin{
|
||||
Kind: QueryKindBuilder,
|
||||
Spec: &BuilderQuerySpec{Spec: spec},
|
||||
},
|
||||
},
|
||||
}}
|
||||
}
|
||||
}
|
||||
|
||||
// Single non-builder query → use its native kind directly. Cleaner JSON
|
||||
// than wrapping in CompositeQuery for the common single-query case.
|
||||
if len(envelopes) == 1 {
|
||||
if q := singleQueryFromEnvelope(envelopes[0], requestType); q != nil {
|
||||
return []Query{*q}
|
||||
}
|
||||
}
|
||||
|
||||
// Default: wrap in CompositeQuery.
|
||||
composite, err := parseCompositeFromEnvelopes(envelopes)
|
||||
if err != nil || composite == nil {
|
||||
return nil
|
||||
}
|
||||
return []Query{{
|
||||
Kind: requestType,
|
||||
Spec: QuerySpec{
|
||||
Plugin: QueryPlugin{Kind: QueryKindComposite, Spec: composite},
|
||||
},
|
||||
}}
|
||||
}
|
||||
|
||||
// requestTypeForPanel maps a v2 panel plugin kind to the request type (result
|
||||
// shape) its queries produce. Mirrors the shape each visualization consumes:
|
||||
// time series for line/bar, scalar for number/pie/table, distribution for
|
||||
// histogram, raw rows for list.
|
||||
func requestTypeForPanel(panelKind PanelPluginKind) qb.RequestType {
|
||||
switch panelKind {
|
||||
case PanelKindTimeSeries, PanelKindBarChart:
|
||||
return qb.RequestTypeTimeSeries
|
||||
case PanelKindNumber, PanelKindPieChart, PanelKindTable:
|
||||
return qb.RequestTypeScalar
|
||||
case PanelKindHistogram:
|
||||
return qb.RequestTypeDistribution
|
||||
case PanelKindList:
|
||||
return qb.RequestTypeRaw
|
||||
}
|
||||
return qb.RequestTypeTimeSeries
|
||||
}
|
||||
|
||||
// collectV1QueryEnvelopes inspects widget.query.queryType and produces a
|
||||
// flattened list of v5-shaped envelopes. The returned signal is the dominant
|
||||
// builder signal (if any), used for typed builder-query dispatch.
|
||||
func collectV1QueryEnvelopes(widget map[string]any) ([]map[string]any, telemetrytypes.Signal) {
|
||||
queryMap, ok := widget["query"].(map[string]any)
|
||||
if !ok {
|
||||
return nil, telemetrytypes.Signal{}
|
||||
}
|
||||
queryType, _ := queryMap["queryType"].(string)
|
||||
|
||||
switch queryType {
|
||||
case "promql":
|
||||
var out []map[string]any
|
||||
for _, q := range readSliceOfMaps(queryMap["promql"]) {
|
||||
out = append(out, promQLEnvelope(q))
|
||||
}
|
||||
return out, telemetrytypes.Signal{}
|
||||
|
||||
case "clickhouse_sql":
|
||||
var out []map[string]any
|
||||
for _, q := range readSliceOfMaps(queryMap["clickhouse_sql"]) {
|
||||
out = append(out, clickhouseEnvelope(q))
|
||||
}
|
||||
return out, telemetrytypes.Signal{}
|
||||
|
||||
case "builder":
|
||||
builder, _ := queryMap["builder"].(map[string]any)
|
||||
if builder == nil {
|
||||
return nil, telemetrytypes.Signal{}
|
||||
}
|
||||
var out []map[string]any
|
||||
var signal telemetrytypes.Signal
|
||||
wrap := transition.NewMigrateCommon(nil)
|
||||
for _, q := range readSliceOfMaps(builder["queryData"]) {
|
||||
name := valueAt[string](q, "queryName")
|
||||
out = append(out, wrap.WrapInV5Envelope(name, q, string(qb.QueryTypeBuilder.StringValue())))
|
||||
if signal.IsZero() {
|
||||
signal = signalFromDataSource(q["dataSource"])
|
||||
}
|
||||
}
|
||||
for _, f := range readSliceOfMaps(builder["queryFormulas"]) {
|
||||
name := valueAt[string](f, "queryName")
|
||||
out = append(out, wrap.WrapInV5Envelope(name, f, string(qb.QueryTypeFormula.StringValue())))
|
||||
}
|
||||
for _, op := range readSliceOfMaps(builder["queryTraceOperator"]) {
|
||||
name := valueAt[string](op, "queryName")
|
||||
out = append(out, wrap.WrapInV5Envelope(name, op, string(qb.QueryTypeTraceOperator.StringValue())))
|
||||
}
|
||||
return out, signal
|
||||
}
|
||||
return nil, telemetrytypes.Signal{}
|
||||
}
|
||||
|
||||
func promQLEnvelope(q map[string]any) map[string]any {
|
||||
return map[string]any{
|
||||
"type": qb.QueryTypePromQL.StringValue(),
|
||||
"spec": map[string]any{
|
||||
"name": q["name"],
|
||||
"query": q["query"],
|
||||
"disabled": q["disabled"],
|
||||
"legend": q["legend"],
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func clickhouseEnvelope(q map[string]any) map[string]any {
|
||||
return map[string]any{
|
||||
"type": qb.QueryTypeClickHouseSQL.StringValue(),
|
||||
"spec": map[string]any{
|
||||
"name": q["name"],
|
||||
"query": q["query"],
|
||||
"disabled": q["disabled"],
|
||||
"legend": q["legend"],
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// singleQueryFromEnvelope returns a typed Query for an envelope whose type is
|
||||
// promql/clickhouse_sql. Builder envelopes always fall through to Composite so
|
||||
// composite-only panel kinds (TimeSeries/BarChart/etc.) get uniform queries.
|
||||
func singleQueryFromEnvelope(envelope map[string]any, requestType qb.RequestType) *Query {
|
||||
t, _ := envelope["type"].(string)
|
||||
spec, _ := envelope["spec"].(map[string]any)
|
||||
switch t {
|
||||
case qb.QueryTypePromQL.StringValue():
|
||||
prom, err := decodeMapInto[qb.PromQuery](spec)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return &Query{
|
||||
Kind: requestType,
|
||||
Spec: QuerySpec{
|
||||
Name: prom.Name,
|
||||
Plugin: QueryPlugin{Kind: QueryKindPromQL, Spec: &prom},
|
||||
},
|
||||
}
|
||||
case qb.QueryTypeClickHouseSQL.StringValue():
|
||||
ch, err := decodeMapInto[qb.ClickHouseQuery](spec)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return &Query{
|
||||
Kind: requestType,
|
||||
Spec: QuerySpec{
|
||||
Name: ch.Name,
|
||||
Plugin: QueryPlugin{Kind: QueryKindClickHouseSQL, Spec: &ch},
|
||||
},
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseCompositeFromEnvelopes(envelopes []map[string]any) (*CompositeQuerySpec, error) {
|
||||
bytes, err := json.Marshal(envelopes)
|
||||
if err != nil {
|
||||
return nil, errors.WrapInternalf(err, errors.CodeInternal, "marshal v1 query envelopes")
|
||||
}
|
||||
var parsed []qb.QueryEnvelope
|
||||
if err := json.Unmarshal(bytes, &parsed); err != nil {
|
||||
return nil, errors.WrapInvalidInputf(err, ErrCodeDashboardInvalidWidgetQuery, "decode v5 query envelopes")
|
||||
}
|
||||
return &CompositeQuerySpec{Queries: parsed}, nil
|
||||
}
|
||||
|
||||
func parseBuilderQuerySpec(rawSpec any, signal telemetrytypes.Signal) any {
|
||||
spec, ok := rawSpec.(map[string]any)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
if !signal.IsZero() {
|
||||
spec["signal"] = signal.StringValue()
|
||||
}
|
||||
bytes, err := json.Marshal(spec)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
parsed, err := qb.UnmarshalBuilderQueryBySignal(bytes)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return parsed
|
||||
}
|
||||
|
||||
func signalFromDataSource(raw any) telemetrytypes.Signal {
|
||||
s, _ := raw.(string)
|
||||
switch s {
|
||||
case "traces":
|
||||
return telemetrytypes.SignalTraces
|
||||
case "logs":
|
||||
return telemetrytypes.SignalLogs
|
||||
case "metrics":
|
||||
return telemetrytypes.SignalMetrics
|
||||
}
|
||||
return telemetrytypes.Signal{}
|
||||
}
|
||||
110
pkg/types/dashboardtypes/perses_v1_to_v2_tags.go
Normal file
110
pkg/types/dashboardtypes/perses_v1_to_v2_tags.go
Normal file
@@ -0,0 +1,110 @@
|
||||
package dashboardtypes
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/SigNoz/signoz/pkg/types/coretypes"
|
||||
"github.com/SigNoz/signoz/pkg/types/tagtypes"
|
||||
"github.com/SigNoz/signoz/pkg/valuer"
|
||||
)
|
||||
|
||||
// ══════════════════════════════════════════════
|
||||
// Tags
|
||||
// ══════════════════════════════════════════════
|
||||
|
||||
// v1 carries tags as a flat []string; v2 tags are (key, value) pairs. Each v1
|
||||
// string is normalized into a pair following the rules in pkg/types/migration.md
|
||||
// (separator split, empty-side fallback, reserved-key prefix, `/` scrub). Tags
|
||||
// that normalize to the same (lower(key), lower(value)) within a dashboard are
|
||||
// collapsed, first occurrence winning the display casing.
|
||||
//
|
||||
// Characters still illegal after normalization (e.g. spaces) are left intact:
|
||||
// such tags fail tag validation downstream and are logged for the customer to
|
||||
// fix, per the migration's dry-run plan.
|
||||
|
||||
// defaultV1TagKey is the key assigned when a v1 tag string has no usable
|
||||
// separator (or one side of the split is empty).
|
||||
const defaultV1TagKey = "tag"
|
||||
|
||||
func convertV1TagsForOrg(orgID valuer.UUID, raw any) []*tagtypes.Tag {
|
||||
rawSlice, ok := raw.([]any)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
seen := make(map[string]struct{}, len(rawSlice))
|
||||
out := make([]*tagtypes.Tag, 0, len(rawSlice))
|
||||
for _, item := range rawSlice {
|
||||
s, ok := item.(string)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
key, value, ok := normalizeV1Tag(s)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
dedupKey := strings.ToLower(key) + "\x00" + strings.ToLower(value)
|
||||
if _, dup := seen[dedupKey]; dup {
|
||||
continue
|
||||
}
|
||||
seen[dedupKey] = struct{}{}
|
||||
out = append(out, tagtypes.NewTag(orgID, coretypes.KindDashboard, key, value))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// normalizeV1Tag derives a (key, value) pair from one v1 tag string per the
|
||||
// ordered rules in pkg/types/migration.md. ok is false when the string has no
|
||||
// usable content (empty after trimming, or a bare separator).
|
||||
func normalizeV1Tag(s string) (string, string, bool) {
|
||||
s = strings.TrimSpace(s)
|
||||
if s == "" {
|
||||
return "", "", false
|
||||
}
|
||||
|
||||
var key, value string
|
||||
var ok bool
|
||||
switch {
|
||||
case strings.Contains(s, ":"):
|
||||
key, value, ok = splitV1Tag(s, ":")
|
||||
// Only the first ":" separates key from value; collapse the rest.
|
||||
value = strings.ReplaceAll(value, ":", "_")
|
||||
case strings.Contains(s, "/"):
|
||||
key, value, ok = splitV1Tag(s, "/")
|
||||
default:
|
||||
key, value, ok = defaultV1TagKey, s, true
|
||||
}
|
||||
if !ok {
|
||||
return "", "", false
|
||||
}
|
||||
|
||||
// Reserved-key collision: prefix with "_" so the list-query DSL stays
|
||||
// unambiguous. Matched case-insensitively against the DSL column names.
|
||||
if _, reserved := reservedDSLKeys[DSLKey(strings.ToLower(key))]; reserved {
|
||||
key = "_" + key
|
||||
}
|
||||
|
||||
// Stored tags must never contain "/" (input validation forbids it).
|
||||
key = strings.ReplaceAll(key, "/", "_")
|
||||
value = strings.ReplaceAll(value, "/", "_")
|
||||
|
||||
return key, value, true
|
||||
}
|
||||
|
||||
// splitV1Tag splits s at the first occurrence of sep, trimming each side. An
|
||||
// empty side collapses to the default key with the non-empty side as the value;
|
||||
// if both sides are empty (a bare separator) ok is false.
|
||||
func splitV1Tag(s, sep string) (string, string, bool) {
|
||||
left, right, _ := strings.Cut(s, sep)
|
||||
left = strings.TrimSpace(left)
|
||||
right = strings.TrimSpace(right)
|
||||
switch {
|
||||
case left == "" && right == "":
|
||||
return "", "", false
|
||||
case left == "":
|
||||
return defaultV1TagKey, right, true
|
||||
case right == "":
|
||||
return defaultV1TagKey, left, true
|
||||
default:
|
||||
return left, right, true
|
||||
}
|
||||
}
|
||||
766
pkg/types/dashboardtypes/perses_v1_to_v2_test.go
Normal file
766
pkg/types/dashboardtypes/perses_v1_to_v2_test.go
Normal file
@@ -0,0 +1,766 @@
|
||||
package dashboardtypes
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/SigNoz/signoz/pkg/types"
|
||||
"github.com/SigNoz/signoz/pkg/types/coretypes"
|
||||
qb "github.com/SigNoz/signoz/pkg/types/querybuildertypes/querybuildertypesv5"
|
||||
"github.com/SigNoz/signoz/pkg/valuer"
|
||||
"github.com/perses/spec/go/dashboard"
|
||||
"github.com/perses/spec/go/dashboard/variable"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestConvertV1TagsForOrg(t *testing.T) {
|
||||
orgID := valuer.GenerateUUID()
|
||||
|
||||
type kv struct{ key, value string }
|
||||
|
||||
cases := []struct {
|
||||
scenario string
|
||||
rawTags any
|
||||
expectedTags []kv
|
||||
}{
|
||||
{
|
||||
scenario: "no separator uses the default key",
|
||||
rawTags: []any{"apm", "latency", "throughput"},
|
||||
expectedTags: []kv{{"tag", "apm"}, {"tag", "latency"}, {"tag", "throughput"}},
|
||||
},
|
||||
{
|
||||
scenario: "colon splits into key and value",
|
||||
rawTags: []any{"env:prod", "team : backend"},
|
||||
expectedTags: []kv{{"env", "prod"}, {"team", "backend"}},
|
||||
},
|
||||
{
|
||||
scenario: "slash splits into key and value when no colon present",
|
||||
rawTags: []any{"team/backend"},
|
||||
expectedTags: []kv{{"team", "backend"}},
|
||||
},
|
||||
{
|
||||
scenario: "colon takes precedence over slash and slash is scrubbed",
|
||||
rawTags: []any{"team/eng:prod", "team/eng:my/path"},
|
||||
expectedTags: []kv{{"team_eng", "prod"}, {"team_eng", "my_path"}},
|
||||
},
|
||||
{
|
||||
scenario: "empty left side falls back to the default key",
|
||||
rawTags: []any{":prod"},
|
||||
expectedTags: []kv{{"tag", "prod"}},
|
||||
},
|
||||
{
|
||||
scenario: "empty right side keeps the left side as the value",
|
||||
rawTags: []any{"env:"},
|
||||
expectedTags: []kv{{"tag", "env"}},
|
||||
},
|
||||
{
|
||||
scenario: "extra colons in the value collapse to underscores",
|
||||
rawTags: []any{"a:b:c"},
|
||||
expectedTags: []kv{{"a", "b_c"}},
|
||||
},
|
||||
{
|
||||
scenario: "extra slashes in the value are scrubbed",
|
||||
rawTags: []any{"a/b/c"},
|
||||
expectedTags: []kv{{"a", "b_c"}},
|
||||
},
|
||||
{
|
||||
scenario: "reserved key gets an underscore prefix",
|
||||
rawTags: []any{"name:foo", "Source:bar"},
|
||||
expectedTags: []kv{{"_name", "foo"}, {"_Source", "bar"}},
|
||||
},
|
||||
{
|
||||
scenario: "drops empty, whitespace-only, and bare-separator entries",
|
||||
rawTags: []any{"", " ", ":", "/", "apm"},
|
||||
expectedTags: []kv{{"tag", "apm"}},
|
||||
},
|
||||
{
|
||||
scenario: "dedupes case-insensitive duplicates, first casing wins",
|
||||
rawTags: []any{"Env:Prod", "env:PROD"},
|
||||
expectedTags: []kv{{"Env", "Prod"}},
|
||||
},
|
||||
{
|
||||
scenario: "returns nil for missing tags field",
|
||||
rawTags: nil,
|
||||
expectedTags: nil,
|
||||
},
|
||||
{
|
||||
scenario: "ignores non-string elements",
|
||||
rawTags: []any{"apm", 42, true, "logs"},
|
||||
expectedTags: []kv{{"tag", "apm"}, {"tag", "logs"}},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.scenario, func(t *testing.T) {
|
||||
tags := convertV1TagsForOrg(orgID, tc.rawTags)
|
||||
require.Len(t, tags, len(tc.expectedTags))
|
||||
for i, expected := range tc.expectedTags {
|
||||
assert.Equal(t, expected.key, tags[i].Key)
|
||||
assert.Equal(t, expected.value, tags[i].Value)
|
||||
assert.Equal(t, orgID, tags[i].OrgID)
|
||||
assert.Equal(t, coretypes.KindDashboard, tags[i].Kind)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertGraphWidgetToTimeSeriesPanel(t *testing.T) {
|
||||
widget := map[string]any{
|
||||
"id": "widget-1",
|
||||
"panelTypes": "graph",
|
||||
"title": "Request rate",
|
||||
"description": "RPS over time",
|
||||
"timePreferance": "LAST_1_HR",
|
||||
"fillSpans": true,
|
||||
"yAxisUnit": "reqps",
|
||||
"decimalPrecision": float64(3),
|
||||
"lineInterpolation": "linear",
|
||||
"lineStyle": "dashed",
|
||||
"fillMode": "gradient",
|
||||
"showPoints": true,
|
||||
"spanGaps": float64(60),
|
||||
"softMin": float64(0),
|
||||
"softMax": float64(100),
|
||||
"isLogScale": true,
|
||||
"legendPosition": "right",
|
||||
"customLegendColors": map[string]any{"A": "#ff0000", "B": "#00ff00"},
|
||||
"thresholds": []any{
|
||||
map[string]any{
|
||||
"thresholdValue": float64(90),
|
||||
"thresholdUnit": "reqps",
|
||||
"thresholdColor": "#ff0000",
|
||||
"thresholdLabel": "high",
|
||||
},
|
||||
map[string]any{
|
||||
"thresholdValue": float64(50),
|
||||
"thresholdColor": "", // missing — must be dropped
|
||||
"thresholdLabel": "missing-color",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
panel := convertGraphWidget(widget)
|
||||
require.NotNil(t, panel)
|
||||
|
||||
assert.Equal(t, PanelKindPanel, panel.Kind)
|
||||
assert.Equal(t, "Request rate", panel.Spec.Display.Name)
|
||||
assert.Equal(t, "RPS over time", panel.Spec.Display.Description)
|
||||
|
||||
assert.Equal(t, PanelKindTimeSeries, panel.Spec.Plugin.Kind)
|
||||
spec, ok := panel.Spec.Plugin.Spec.(*TimeSeriesPanelSpec)
|
||||
require.True(t, ok, "panel plugin spec should be *TimeSeriesPanelSpec")
|
||||
|
||||
assert.Equal(t, TimePreferenceLast1Hr, spec.Visualization.TimePreference)
|
||||
assert.True(t, spec.Visualization.FillSpans)
|
||||
|
||||
assert.Equal(t, "reqps", spec.Formatting.Unit)
|
||||
assert.Equal(t, PrecisionOption3, spec.Formatting.DecimalPrecision)
|
||||
|
||||
assert.Equal(t, LineInterpolationLinear, spec.ChartAppearance.LineInterpolation)
|
||||
assert.True(t, spec.ChartAppearance.ShowPoints)
|
||||
assert.Equal(t, LineStyleDashed, spec.ChartAppearance.LineStyle)
|
||||
assert.Equal(t, FillModeGradient, spec.ChartAppearance.FillMode)
|
||||
assert.True(t, spec.ChartAppearance.SpanGaps.FillOnlyBelow)
|
||||
assert.Equal(t, "1m0s", spec.ChartAppearance.SpanGaps.FillLessThan.StringValue())
|
||||
|
||||
require.NotNil(t, spec.Axes.SoftMin)
|
||||
assert.Equal(t, float64(0), *spec.Axes.SoftMin)
|
||||
require.NotNil(t, spec.Axes.SoftMax)
|
||||
assert.Equal(t, float64(100), *spec.Axes.SoftMax)
|
||||
assert.True(t, spec.Axes.IsLogScale)
|
||||
|
||||
assert.Equal(t, LegendPositionRight, spec.Legend.Position)
|
||||
assert.Equal(t, map[string]string{"A": "#ff0000", "B": "#00ff00"}, spec.Legend.CustomColors)
|
||||
|
||||
require.Len(t, spec.Thresholds, 1, "threshold with missing color should be dropped")
|
||||
assert.Equal(t, ThresholdWithLabel{Value: 90, Unit: "reqps", Color: "#ff0000", Label: "high"}, spec.Thresholds[0])
|
||||
}
|
||||
|
||||
func TestConvertGraphWidgetDefaultsForMissingFields(t *testing.T) {
|
||||
widget := map[string]any{
|
||||
"id": "widget-1",
|
||||
"panelTypes": "graph",
|
||||
"title": "minimal",
|
||||
}
|
||||
|
||||
panel := convertGraphWidget(widget)
|
||||
require.NotNil(t, panel)
|
||||
|
||||
spec, ok := panel.Spec.Plugin.Spec.(*TimeSeriesPanelSpec)
|
||||
require.True(t, ok)
|
||||
|
||||
assert.Equal(t, TimePreferenceGlobalTime, spec.Visualization.TimePreference)
|
||||
assert.Equal(t, PrecisionOption2, spec.Formatting.DecimalPrecision)
|
||||
assert.Equal(t, LineInterpolationSpline, spec.ChartAppearance.LineInterpolation)
|
||||
assert.Equal(t, LineStyleSolid, spec.ChartAppearance.LineStyle)
|
||||
assert.Equal(t, FillModeSolid, spec.ChartAppearance.FillMode)
|
||||
assert.Equal(t, LegendPositionBottom, spec.Legend.Position)
|
||||
assert.False(t, spec.ChartAppearance.SpanGaps.FillOnlyBelow)
|
||||
assert.Nil(t, spec.Axes.SoftMin)
|
||||
assert.Nil(t, spec.Axes.SoftMax)
|
||||
assert.Empty(t, spec.Thresholds)
|
||||
}
|
||||
|
||||
func TestConvertV1ToV2HappyPath(t *testing.T) {
|
||||
orgID := valuer.GenerateUUID()
|
||||
storable := &StorableDashboard{
|
||||
Identifiable: types.Identifiable{ID: valuer.GenerateUUID()},
|
||||
TimeAuditable: types.TimeAuditable{CreatedAt: time.Now(), UpdatedAt: time.Now()},
|
||||
UserAuditable: types.UserAuditable{CreatedBy: "alice", UpdatedBy: "bob"},
|
||||
OrgID: orgID,
|
||||
Source: SourceUser,
|
||||
Name: "apm-metrics",
|
||||
Data: StorableDashboardData{
|
||||
"title": "APM Metrics",
|
||||
"description": "service overview",
|
||||
"image": "data:image/png;base64,abc",
|
||||
"tags": []any{"apm", "team:platform"},
|
||||
"widgets": []any{
|
||||
// section header — owned by the layout pass, not a panel
|
||||
map[string]any{"id": "row-1", "panelTypes": "row", "title": "Overview"},
|
||||
// graph widget → TimeSeries panel
|
||||
map[string]any{
|
||||
"id": "panel-1",
|
||||
"panelTypes": "graph",
|
||||
"title": "Latency",
|
||||
},
|
||||
// table widget → Table panel
|
||||
map[string]any{"id": "panel-2", "panelTypes": "table"},
|
||||
// widget with missing id — dropped
|
||||
map[string]any{"panelTypes": "graph", "title": "no id"},
|
||||
// unknown panel kind — silently dropped
|
||||
map[string]any{"id": "panel-3", "panelTypes": "totally-new"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
dashboard, err := storable.ConvertV1ToV2()
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, dashboard)
|
||||
|
||||
assert.Equal(t, storable.ID, dashboard.ID)
|
||||
assert.Equal(t, storable.OrgID, dashboard.OrgID)
|
||||
assert.Equal(t, storable.Source, dashboard.Source)
|
||||
assert.Equal(t, storable.Name, dashboard.Name)
|
||||
assert.Equal(t, SchemaVersion, dashboard.SchemaVersion)
|
||||
assert.Equal(t, "data:image/png;base64,abc", dashboard.Image)
|
||||
|
||||
assert.Equal(t, "APM Metrics", dashboard.Spec.Display.Name)
|
||||
assert.Equal(t, "service overview", dashboard.Spec.Display.Description)
|
||||
|
||||
require.Len(t, dashboard.Tags, 2)
|
||||
assert.Equal(t, "tag", dashboard.Tags[0].Key)
|
||||
assert.Equal(t, "apm", dashboard.Tags[0].Value)
|
||||
assert.Equal(t, "team", dashboard.Tags[1].Key)
|
||||
assert.Equal(t, "platform", dashboard.Tags[1].Value)
|
||||
|
||||
require.Len(t, dashboard.Spec.Panels, 2, "graph and table map; row, no-id, and unknown kinds are dropped")
|
||||
require.Contains(t, dashboard.Spec.Panels, "panel-1")
|
||||
require.Contains(t, dashboard.Spec.Panels, "panel-2")
|
||||
assert.Equal(t, PanelKindTimeSeries, dashboard.Spec.Panels["panel-1"].Spec.Plugin.Kind)
|
||||
assert.Equal(t, PanelKindTable, dashboard.Spec.Panels["panel-2"].Spec.Plugin.Kind)
|
||||
}
|
||||
|
||||
func TestConvertV1ToV2RejectsAlreadyV2(t *testing.T) {
|
||||
storable := &StorableDashboard{
|
||||
Identifiable: types.Identifiable{ID: valuer.GenerateUUID()},
|
||||
OrgID: valuer.GenerateUUID(),
|
||||
Source: SourceUser,
|
||||
Name: "already-v2",
|
||||
Data: StorableDashboardData{
|
||||
"metadata": map[string]any{"schemaVersion": SchemaVersion},
|
||||
"spec": map[string]any{},
|
||||
},
|
||||
}
|
||||
|
||||
dashboard, err := storable.ConvertV1ToV2()
|
||||
assert.Nil(t, dashboard)
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "already in")
|
||||
}
|
||||
|
||||
func TestSpanGapsMapping(t *testing.T) {
|
||||
cases := []struct {
|
||||
scenario string
|
||||
rawSpanGaps any
|
||||
expectedFillOnlyBelow bool
|
||||
expectedFillLessThan string
|
||||
}{
|
||||
{scenario: "true spans every gap", rawSpanGaps: true, expectedFillOnlyBelow: false, expectedFillLessThan: "0s"},
|
||||
{scenario: "false spans no gaps", rawSpanGaps: false, expectedFillOnlyBelow: true, expectedFillLessThan: "0s"},
|
||||
{scenario: "number is seconds threshold", rawSpanGaps: float64(30), expectedFillOnlyBelow: true, expectedFillLessThan: "30s"},
|
||||
{scenario: "missing defaults to span all", rawSpanGaps: nil, expectedFillOnlyBelow: false, expectedFillLessThan: "0s"},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.scenario, func(t *testing.T) {
|
||||
got := mapV1SpanGaps(tc.rawSpanGaps)
|
||||
assert.Equal(t, tc.expectedFillOnlyBelow, got.FillOnlyBelow)
|
||||
assert.Equal(t, tc.expectedFillLessThan, got.FillLessThan.StringValue())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// ══════════════════════════════════════════════
|
||||
// Other panel-kind converters
|
||||
// ══════════════════════════════════════════════
|
||||
|
||||
func TestConvertBarWidgetToBarChartPanel(t *testing.T) {
|
||||
widget := map[string]any{
|
||||
"id": "bar-1",
|
||||
"panelTypes": "bar",
|
||||
"title": "Requests by status",
|
||||
"fillSpans": true,
|
||||
"stackedBarChart": true,
|
||||
"yAxisUnit": "reqps",
|
||||
"softMin": float64(0),
|
||||
"isLogScale": true,
|
||||
"legendPosition": "right",
|
||||
}
|
||||
|
||||
panel := convertBarWidget(widget)
|
||||
require.NotNil(t, panel)
|
||||
assert.Equal(t, PanelKindBarChart, panel.Spec.Plugin.Kind)
|
||||
|
||||
spec, ok := panel.Spec.Plugin.Spec.(*BarChartPanelSpec)
|
||||
require.True(t, ok)
|
||||
assert.True(t, spec.Visualization.FillSpans)
|
||||
assert.True(t, spec.Visualization.StackedBarChart)
|
||||
assert.Equal(t, "reqps", spec.Formatting.Unit)
|
||||
require.NotNil(t, spec.Axes.SoftMin)
|
||||
assert.Equal(t, float64(0), *spec.Axes.SoftMin)
|
||||
assert.True(t, spec.Axes.IsLogScale)
|
||||
assert.Equal(t, LegendPositionRight, spec.Legend.Position)
|
||||
}
|
||||
|
||||
func TestConvertValueWidgetToNumberPanel(t *testing.T) {
|
||||
widget := map[string]any{
|
||||
"id": "val-1",
|
||||
"panelTypes": "value",
|
||||
"title": "Active services",
|
||||
"yAxisUnit": "count",
|
||||
"thresholds": []any{
|
||||
map[string]any{
|
||||
"thresholdValue": float64(100),
|
||||
"thresholdOperator": ">=",
|
||||
"thresholdColor": "#ff0000",
|
||||
"thresholdFormat": "Background",
|
||||
"thresholdUnit": "count",
|
||||
},
|
||||
map[string]any{
|
||||
// missing color — must be dropped
|
||||
"thresholdValue": float64(10),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
panel := convertValueWidget(widget)
|
||||
require.NotNil(t, panel)
|
||||
assert.Equal(t, PanelKindNumber, panel.Spec.Plugin.Kind)
|
||||
|
||||
spec, ok := panel.Spec.Plugin.Spec.(*NumberPanelSpec)
|
||||
require.True(t, ok)
|
||||
require.Len(t, spec.Thresholds, 1)
|
||||
assert.Equal(t, float64(100), spec.Thresholds[0].Value)
|
||||
assert.Equal(t, ComparisonOperatorAboveOrEqual, spec.Thresholds[0].Operator)
|
||||
assert.Equal(t, "#ff0000", spec.Thresholds[0].Color)
|
||||
assert.Equal(t, ThresholdFormatBackground, spec.Thresholds[0].Format)
|
||||
}
|
||||
|
||||
func TestConvertTableWidgetToTablePanel(t *testing.T) {
|
||||
widget := map[string]any{
|
||||
"id": "tbl-1",
|
||||
"panelTypes": "table",
|
||||
"title": "Top services",
|
||||
"columnUnits": map[string]any{
|
||||
"latency": "ms",
|
||||
"errors": "count",
|
||||
},
|
||||
"thresholds": []any{
|
||||
map[string]any{
|
||||
"thresholdValue": float64(500),
|
||||
"thresholdColor": "#ff0000",
|
||||
"thresholdTableOptions": "latency",
|
||||
"thresholdOperator": ">",
|
||||
},
|
||||
map[string]any{
|
||||
// missing columnName — dropped
|
||||
"thresholdValue": float64(1),
|
||||
"thresholdColor": "#00ff00",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
panel := convertTableWidget(widget)
|
||||
require.NotNil(t, panel)
|
||||
assert.Equal(t, PanelKindTable, panel.Spec.Plugin.Kind)
|
||||
|
||||
spec, ok := panel.Spec.Plugin.Spec.(*TablePanelSpec)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, "ms", spec.Formatting.ColumnUnits["latency"])
|
||||
assert.Equal(t, "count", spec.Formatting.ColumnUnits["errors"])
|
||||
require.Len(t, spec.Thresholds, 1)
|
||||
assert.Equal(t, "latency", spec.Thresholds[0].ColumnName)
|
||||
assert.Equal(t, ComparisonOperatorAbove, spec.Thresholds[0].Operator)
|
||||
}
|
||||
|
||||
func TestConvertPieWidgetToPieChartPanel(t *testing.T) {
|
||||
widget := map[string]any{
|
||||
"id": "pie-1",
|
||||
"panelTypes": "pie",
|
||||
"title": "Share",
|
||||
"legendPosition": "right",
|
||||
}
|
||||
|
||||
panel := convertPieWidget(widget)
|
||||
require.NotNil(t, panel)
|
||||
assert.Equal(t, PanelKindPieChart, panel.Spec.Plugin.Kind)
|
||||
|
||||
spec, ok := panel.Spec.Plugin.Spec.(*PieChartPanelSpec)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, LegendPositionRight, spec.Legend.Position)
|
||||
}
|
||||
|
||||
func TestConvertHistogramWidget(t *testing.T) {
|
||||
bucketCount := float64(20)
|
||||
widget := map[string]any{
|
||||
"id": "hist-1",
|
||||
"panelTypes": "histogram",
|
||||
"title": "Latency distribution",
|
||||
"bucketCount": bucketCount,
|
||||
"mergeAllActiveQueries": true,
|
||||
}
|
||||
|
||||
panel := convertHistogramWidget(widget)
|
||||
require.NotNil(t, panel)
|
||||
assert.Equal(t, PanelKindHistogram, panel.Spec.Plugin.Kind)
|
||||
|
||||
spec, ok := panel.Spec.Plugin.Spec.(*HistogramPanelSpec)
|
||||
require.True(t, ok)
|
||||
require.NotNil(t, spec.HistogramBuckets.BucketCount)
|
||||
assert.Equal(t, bucketCount, *spec.HistogramBuckets.BucketCount)
|
||||
assert.Nil(t, spec.HistogramBuckets.BucketWidth)
|
||||
assert.True(t, spec.HistogramBuckets.MergeAllActiveQueries)
|
||||
}
|
||||
|
||||
func TestConvertListWidget(t *testing.T) {
|
||||
widget := map[string]any{
|
||||
"id": "list-1",
|
||||
"panelTypes": "list",
|
||||
"title": "Recent logs",
|
||||
"selectedLogFields": []any{
|
||||
map[string]any{"name": "body", "fieldDataType": "string", "fieldContext": "log"},
|
||||
map[string]any{"name": "severity_text", "fieldDataType": "string", "fieldContext": "log"},
|
||||
},
|
||||
}
|
||||
|
||||
panel := convertListWidget(widget)
|
||||
require.NotNil(t, panel)
|
||||
assert.Equal(t, PanelKindList, panel.Spec.Plugin.Kind)
|
||||
|
||||
spec, ok := panel.Spec.Plugin.Spec.(*ListPanelSpec)
|
||||
require.True(t, ok)
|
||||
require.Len(t, spec.SelectFields, 2)
|
||||
assert.Equal(t, "body", spec.SelectFields[0].Name)
|
||||
}
|
||||
|
||||
// ══════════════════════════════════════════════
|
||||
// Query translation
|
||||
// ══════════════════════════════════════════════
|
||||
|
||||
func TestConvertV1WidgetQuerySinglePromQL(t *testing.T) {
|
||||
widget := map[string]any{
|
||||
"id": "p-1",
|
||||
"panelTypes": "graph",
|
||||
"query": map[string]any{
|
||||
"queryType": "promql",
|
||||
"promql": []any{
|
||||
map[string]any{"name": "A", "query": "up", "legend": "{{job}}"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
queries := convertV1WidgetQuery(widget, PanelKindTimeSeries)
|
||||
require.Len(t, queries, 1)
|
||||
assert.Equal(t, qb.RequestTypeTimeSeries, queries[0].Kind)
|
||||
assert.Equal(t, QueryKindPromQL, queries[0].Spec.Plugin.Kind)
|
||||
|
||||
prom, ok := queries[0].Spec.Plugin.Spec.(*qb.PromQuery)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, "A", prom.Name)
|
||||
assert.Equal(t, "up", prom.Query)
|
||||
assert.Equal(t, "{{job}}", prom.Legend)
|
||||
}
|
||||
|
||||
func TestConvertV1WidgetQuerySingleClickHouse(t *testing.T) {
|
||||
widget := map[string]any{
|
||||
"id": "c-1",
|
||||
"panelTypes": "table",
|
||||
"query": map[string]any{
|
||||
"queryType": "clickhouse_sql",
|
||||
"clickhouse_sql": []any{
|
||||
map[string]any{"name": "Q", "query": "SELECT 1", "legend": "x"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
queries := convertV1WidgetQuery(widget, PanelKindTable)
|
||||
require.Len(t, queries, 1)
|
||||
assert.Equal(t, qb.RequestTypeScalar, queries[0].Kind)
|
||||
assert.Equal(t, QueryKindClickHouseSQL, queries[0].Spec.Plugin.Kind)
|
||||
|
||||
ch, ok := queries[0].Spec.Plugin.Spec.(*qb.ClickHouseQuery)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, "Q", ch.Name)
|
||||
assert.Equal(t, "SELECT 1", ch.Query)
|
||||
}
|
||||
|
||||
func TestConvertV1WidgetQueryMultipleBuilderWrapsInComposite(t *testing.T) {
|
||||
widget := map[string]any{
|
||||
"id": "b-1",
|
||||
"panelTypes": "graph",
|
||||
"query": map[string]any{
|
||||
"queryType": "builder",
|
||||
"builder": map[string]any{
|
||||
"queryData": []any{
|
||||
map[string]any{
|
||||
"queryName": "A",
|
||||
"expression": "A",
|
||||
"dataSource": "metrics",
|
||||
"aggregations": []any{map[string]any{"metricName": "signoz_calls_total"}},
|
||||
},
|
||||
map[string]any{
|
||||
"queryName": "B",
|
||||
"expression": "B",
|
||||
"dataSource": "logs",
|
||||
"aggregations": []any{map[string]any{"expression": "count()"}},
|
||||
},
|
||||
},
|
||||
"queryFormulas": []any{
|
||||
map[string]any{"queryName": "F1", "expression": "A + B"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
queries := convertV1WidgetQuery(widget, PanelKindTimeSeries)
|
||||
require.Len(t, queries, 1)
|
||||
assert.Equal(t, qb.RequestTypeTimeSeries, queries[0].Kind)
|
||||
assert.Equal(t, QueryKindComposite, queries[0].Spec.Plugin.Kind)
|
||||
|
||||
composite, ok := queries[0].Spec.Plugin.Spec.(*CompositeQuerySpec)
|
||||
require.True(t, ok)
|
||||
require.Len(t, composite.Queries, 3)
|
||||
assert.Equal(t, qb.QueryTypeBuilder, composite.Queries[0].Type)
|
||||
assert.Equal(t, qb.QueryTypeBuilder, composite.Queries[1].Type)
|
||||
assert.Equal(t, qb.QueryTypeFormula, composite.Queries[2].Type)
|
||||
}
|
||||
|
||||
func TestConvertV1WidgetQueryListPanelUsesBuilderDirectly(t *testing.T) {
|
||||
widget := map[string]any{
|
||||
"id": "l-1",
|
||||
"panelTypes": "list",
|
||||
"query": map[string]any{
|
||||
"queryType": "builder",
|
||||
"builder": map[string]any{
|
||||
"queryData": []any{
|
||||
map[string]any{
|
||||
"queryName": "A",
|
||||
"expression": "A",
|
||||
"dataSource": "logs",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
queries := convertV1WidgetQuery(widget, PanelKindList)
|
||||
require.Len(t, queries, 1)
|
||||
assert.Equal(t, qb.RequestTypeRaw, queries[0].Kind)
|
||||
assert.Equal(t, QueryKindBuilder, queries[0].Spec.Plugin.Kind)
|
||||
|
||||
wrapper, ok := queries[0].Spec.Plugin.Spec.(*BuilderQuerySpec)
|
||||
require.True(t, ok)
|
||||
spec, ok := wrapper.Spec.(qb.QueryBuilderQuery[qb.LogAggregation])
|
||||
require.True(t, ok, "list builder query should dispatch to LogAggregation, got %T", wrapper.Spec)
|
||||
assert.Equal(t, "A", spec.Name)
|
||||
}
|
||||
|
||||
func TestConvertV1WidgetQueryNoQuery(t *testing.T) {
|
||||
widget := map[string]any{"id": "x", "panelTypes": "graph"}
|
||||
queries := convertV1WidgetQuery(widget, PanelKindTimeSeries)
|
||||
assert.Nil(t, queries)
|
||||
}
|
||||
|
||||
// ══════════════════════════════════════════════
|
||||
// Layouts and sections
|
||||
// ══════════════════════════════════════════════
|
||||
|
||||
func TestConvertV1LayoutsRootOnly(t *testing.T) {
|
||||
data := StorableDashboardData{
|
||||
"layout": []any{
|
||||
map[string]any{"i": "p-1", "x": float64(0), "y": float64(0), "w": float64(6), "h": float64(6)},
|
||||
map[string]any{"i": "p-2", "x": float64(6), "y": float64(0), "w": float64(6), "h": float64(6)},
|
||||
},
|
||||
"widgets": []any{
|
||||
map[string]any{"id": "p-1", "panelTypes": "graph"},
|
||||
map[string]any{"id": "p-2", "panelTypes": "graph"},
|
||||
},
|
||||
}
|
||||
|
||||
layouts := convertV1Layouts(data)
|
||||
require.Len(t, layouts, 1)
|
||||
assert.Equal(t, dashboard.KindGridLayout, layouts[0].Kind)
|
||||
|
||||
spec, ok := layouts[0].Spec.(*dashboard.GridLayoutSpec)
|
||||
require.True(t, ok)
|
||||
require.Len(t, spec.Items, 2)
|
||||
assert.Equal(t, "#/spec/panels/p-1", spec.Items[0].Content.Ref)
|
||||
assert.Equal(t, 6, spec.Items[1].Width)
|
||||
assert.Nil(t, spec.Display, "root-only grid should have no display block")
|
||||
}
|
||||
|
||||
func TestConvertV1LayoutsWithCollapsedSection(t *testing.T) {
|
||||
data := StorableDashboardData{
|
||||
"widgets": []any{
|
||||
map[string]any{"id": "row-1", "panelTypes": "row", "title": "Latency"},
|
||||
map[string]any{"id": "p-1", "panelTypes": "graph"},
|
||||
map[string]any{"id": "p-2", "panelTypes": "graph"},
|
||||
},
|
||||
"layout": []any{
|
||||
map[string]any{"i": "row-1", "x": float64(0), "y": float64(0), "w": float64(12), "h": float64(1)},
|
||||
map[string]any{"i": "p-1", "x": float64(0), "y": float64(1), "w": float64(6), "h": float64(6)},
|
||||
map[string]any{"i": "p-2", "x": float64(0), "y": float64(7), "w": float64(6), "h": float64(6)},
|
||||
},
|
||||
"panelMap": map[string]any{
|
||||
"row-1": map[string]any{
|
||||
"collapsed": true,
|
||||
"widgets": []any{
|
||||
map[string]any{"i": "p-1", "x": float64(0), "y": float64(1), "w": float64(6), "h": float64(6)},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
layouts := convertV1Layouts(data)
|
||||
require.Len(t, layouts, 2, "one root grid (p-2) + one section grid (row-1 with p-1)")
|
||||
|
||||
rootSpec, ok := layouts[0].Spec.(*dashboard.GridLayoutSpec)
|
||||
require.True(t, ok)
|
||||
require.Len(t, rootSpec.Items, 1)
|
||||
assert.Equal(t, "#/spec/panels/p-2", rootSpec.Items[0].Content.Ref)
|
||||
assert.Nil(t, rootSpec.Display)
|
||||
|
||||
sectionSpec, ok := layouts[1].Spec.(*dashboard.GridLayoutSpec)
|
||||
require.True(t, ok)
|
||||
require.NotNil(t, sectionSpec.Display)
|
||||
assert.Equal(t, "Latency", sectionSpec.Display.Title)
|
||||
require.NotNil(t, sectionSpec.Display.Collapse)
|
||||
assert.False(t, sectionSpec.Display.Collapse.Open, "collapsed=true → open=false")
|
||||
require.Len(t, sectionSpec.Items, 1)
|
||||
assert.Equal(t, "#/spec/panels/p-1", sectionSpec.Items[0].Content.Ref)
|
||||
}
|
||||
|
||||
func TestConvertV1LayoutsEmpty(t *testing.T) {
|
||||
assert.Nil(t, convertV1Layouts(StorableDashboardData{}))
|
||||
}
|
||||
|
||||
// ══════════════════════════════════════════════
|
||||
// Variables
|
||||
// ══════════════════════════════════════════════
|
||||
|
||||
func TestConvertV1VariablesAllTypes(t *testing.T) {
|
||||
raw := map[string]any{
|
||||
"u-1": map[string]any{
|
||||
"name": "service.name",
|
||||
"description": "the service",
|
||||
"type": "QUERY",
|
||||
"queryValue": "SELECT name FROM s",
|
||||
"multiSelect": true,
|
||||
"showALLOption": true,
|
||||
"sort": "ASC",
|
||||
"order": float64(1),
|
||||
},
|
||||
"u-2": map[string]any{
|
||||
"name": "env",
|
||||
"type": "CUSTOM",
|
||||
"customValue": "prod,staging,dev",
|
||||
"order": float64(2),
|
||||
"selectedValue": "prod",
|
||||
},
|
||||
"u-3": map[string]any{
|
||||
"name": "deployment.environment",
|
||||
"type": "DYNAMIC",
|
||||
"dynamicVariablesAttribute": "deployment.environment",
|
||||
"dynamicVariablesSource": "traces",
|
||||
"order": float64(0),
|
||||
},
|
||||
"u-4": map[string]any{
|
||||
"name": "freetext",
|
||||
"type": "TEXTBOX",
|
||||
"textboxValue": "hello",
|
||||
"order": float64(3),
|
||||
},
|
||||
}
|
||||
|
||||
vars := convertV1Variables(raw)
|
||||
require.Len(t, vars, 4)
|
||||
|
||||
// Ordered by `order` ascending: u-3 (0), u-1 (1), u-2 (2), u-4 (3)
|
||||
assert.Equal(t, variable.KindList, vars[0].Kind)
|
||||
dyn, ok := vars[0].Spec.(*ListVariableSpec)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, "deployment.environment", dyn.Name)
|
||||
assert.Equal(t, VariableKindDynamic, dyn.Plugin.Kind)
|
||||
|
||||
q, ok := vars[1].Spec.(*ListVariableSpec)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, "service.name", q.Name)
|
||||
assert.Equal(t, VariableKindQuery, q.Plugin.Kind)
|
||||
assert.True(t, q.AllowMultiple)
|
||||
assert.True(t, q.AllowAllValue)
|
||||
require.NotNil(t, q.Sort)
|
||||
assert.Equal(t, variable.SortAlphabeticalAsc, *q.Sort)
|
||||
|
||||
c, ok := vars[2].Spec.(*ListVariableSpec)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, "env", c.Name)
|
||||
assert.Equal(t, VariableKindCustom, c.Plugin.Kind)
|
||||
require.NotNil(t, c.DefaultValue)
|
||||
assert.Equal(t, "prod", c.DefaultValue.SingleValue)
|
||||
|
||||
assert.Equal(t, variable.KindText, vars[3].Kind)
|
||||
text, ok := vars[3].Spec.(*dashboard.TextVariableSpec)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, "freetext", text.Name)
|
||||
assert.Equal(t, "hello", text.Value)
|
||||
}
|
||||
|
||||
func TestConvertV1VariablesSkipsUnnamedAndUnknownTypes(t *testing.T) {
|
||||
raw := map[string]any{
|
||||
"u-1": map[string]any{"name": "", "type": "QUERY"},
|
||||
"u-2": map[string]any{"name": "ok", "type": "WHATEVER"},
|
||||
"u-3": map[string]any{"name": "good", "type": "CUSTOM", "customValue": "a"},
|
||||
}
|
||||
vars := convertV1Variables(raw)
|
||||
require.Len(t, vars, 1)
|
||||
spec := vars[0].Spec.(*ListVariableSpec)
|
||||
assert.Equal(t, "good", spec.Name)
|
||||
}
|
||||
|
||||
func TestConvertV1VariablesDefaultFromSelectedSlice(t *testing.T) {
|
||||
raw := map[string]any{
|
||||
"u-1": map[string]any{
|
||||
"name": "svc",
|
||||
"type": "QUERY",
|
||||
"queryValue": "SELECT 1",
|
||||
"selectedValue": []any{"foo", "", "bar"},
|
||||
},
|
||||
}
|
||||
vars := convertV1Variables(raw)
|
||||
require.Len(t, vars, 1)
|
||||
spec := vars[0].Spec.(*ListVariableSpec)
|
||||
require.NotNil(t, spec.DefaultValue)
|
||||
assert.Equal(t, []string{"foo", "bar"}, spec.DefaultValue.SliceValues)
|
||||
}
|
||||
170
pkg/types/dashboardtypes/perses_v1_to_v2_variables.go
Normal file
170
pkg/types/dashboardtypes/perses_v1_to_v2_variables.go
Normal file
@@ -0,0 +1,170 @@
|
||||
package dashboardtypes
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/perses/spec/go/dashboard"
|
||||
"github.com/perses/spec/go/dashboard/variable"
|
||||
)
|
||||
|
||||
// ══════════════════════════════════════════════
|
||||
// Variables
|
||||
// ══════════════════════════════════════════════
|
||||
|
||||
// convertV1Variables walks the v1 `variables` map (UUID-keyed) and produces an
|
||||
// ordered []Variable. Variables sort by `order` first, then by id for stable
|
||||
// output. v1 variable types map as follows:
|
||||
//
|
||||
// QUERY → ListVariable + signoz/QueryVariable
|
||||
// CUSTOM → ListVariable + signoz/CustomVariable
|
||||
// DYNAMIC → ListVariable + signoz/DynamicVariable
|
||||
// TEXTBOX → TextVariable
|
||||
func convertV1Variables(raw any) []Variable {
|
||||
rawMap, ok := raw.(map[string]any)
|
||||
if !ok || len(rawMap) == 0 {
|
||||
return nil
|
||||
}
|
||||
type ordered struct {
|
||||
key string
|
||||
val map[string]any
|
||||
ord float64
|
||||
}
|
||||
entries := make([]ordered, 0, len(rawMap))
|
||||
for key, value := range rawMap {
|
||||
m, ok := value.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
ord, _ := m["order"].(float64)
|
||||
entries = append(entries, ordered{key: key, val: m, ord: ord})
|
||||
}
|
||||
sort.SliceStable(entries, func(i, j int) bool {
|
||||
if entries[i].ord != entries[j].ord {
|
||||
return entries[i].ord < entries[j].ord
|
||||
}
|
||||
return entries[i].key < entries[j].key
|
||||
})
|
||||
|
||||
out := make([]Variable, 0, len(entries))
|
||||
for _, e := range entries {
|
||||
v, ok := convertV1Variable(e.val)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
out = append(out, v)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func convertV1Variable(v map[string]any) (Variable, bool) {
|
||||
name, _ := v["name"].(string)
|
||||
if name == "" {
|
||||
return Variable{}, false
|
||||
}
|
||||
description, _ := v["description"].(string)
|
||||
kind, _ := v["type"].(string)
|
||||
|
||||
switch kind {
|
||||
case "TEXTBOX":
|
||||
value, _ := v["textboxValue"].(string)
|
||||
spec := &dashboard.TextVariableSpec{
|
||||
TextSpec: variable.TextSpec{
|
||||
Display: &variable.Display{Name: name, Description: description},
|
||||
Value: value,
|
||||
},
|
||||
Name: name,
|
||||
}
|
||||
return Variable{Kind: variable.KindText, Spec: spec}, true
|
||||
|
||||
case "QUERY", "CUSTOM", "DYNAMIC":
|
||||
listSpec := &ListVariableSpec{
|
||||
Display: Display{Name: name, Description: description},
|
||||
AllowAllValue: valueAt[bool](v, "showALLOption"),
|
||||
AllowMultiple: valueAt[bool](v, "multiSelect"),
|
||||
CustomAllValue: valueAt[string](v, "customAllValue"),
|
||||
CapturingRegexp: valueAt[string](v, "capturingRegexp"),
|
||||
Sort: mapV1Sort(v["sort"]),
|
||||
Plugin: variablePluginFor(kind, v),
|
||||
Name: name,
|
||||
}
|
||||
if dv := mapV1VariableDefault(v); dv != nil {
|
||||
listSpec.DefaultValue = dv
|
||||
}
|
||||
return Variable{Kind: variable.KindList, Spec: listSpec}, true
|
||||
}
|
||||
|
||||
return Variable{}, false
|
||||
}
|
||||
|
||||
func variablePluginFor(kind string, v map[string]any) VariablePlugin {
|
||||
switch kind {
|
||||
case "QUERY":
|
||||
return VariablePlugin{
|
||||
Kind: VariableKindQuery,
|
||||
Spec: &QueryVariableSpec{QueryValue: valueAt[string](v, "queryValue")},
|
||||
}
|
||||
case "CUSTOM":
|
||||
return VariablePlugin{
|
||||
Kind: VariableKindCustom,
|
||||
Spec: &CustomVariableSpec{CustomValue: valueAt[string](v, "customValue")},
|
||||
}
|
||||
case "DYNAMIC":
|
||||
spec := &DynamicVariableSpec{Name: valueAt[string](v, "dynamicVariablesAttribute")}
|
||||
if signal := signalFromDataSource(v["dynamicVariablesSource"]); !signal.IsZero() {
|
||||
spec.Signal = signal
|
||||
}
|
||||
return VariablePlugin{Kind: VariableKindDynamic, Spec: spec}
|
||||
}
|
||||
return VariablePlugin{}
|
||||
}
|
||||
|
||||
func mapV1VariableDefault(v map[string]any) *variable.DefaultValue {
|
||||
if raw, ok := v["selectedValue"]; ok {
|
||||
return defaultValueFromAny(raw)
|
||||
}
|
||||
if raw, ok := v["defaultValue"]; ok {
|
||||
return defaultValueFromAny(raw)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func defaultValueFromAny(raw any) *variable.DefaultValue {
|
||||
switch v := raw.(type) {
|
||||
case string:
|
||||
if v == "" {
|
||||
return nil
|
||||
}
|
||||
return &variable.DefaultValue{SingleValue: v}
|
||||
case []any:
|
||||
if len(v) == 0 {
|
||||
return nil
|
||||
}
|
||||
values := make([]string, 0, len(v))
|
||||
for _, item := range v {
|
||||
if s, ok := item.(string); ok && s != "" {
|
||||
values = append(values, s)
|
||||
}
|
||||
}
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
return &variable.DefaultValue{SliceValues: values}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func mapV1Sort(raw any) *variable.Sort {
|
||||
s, _ := raw.(string)
|
||||
var sort variable.Sort
|
||||
switch s {
|
||||
case "ASC":
|
||||
sort = variable.SortAlphabeticalAsc
|
||||
case "DESC":
|
||||
sort = variable.SortAlphabeticalDesc
|
||||
case "DISABLED", "":
|
||||
return nil // SortNone is the implicit default
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
return &sort
|
||||
}
|
||||
@@ -51,17 +51,4 @@ type Store interface {
|
||||
DeletePreferencesForDashboard(ctx context.Context, orgID valuer.UUID, dashboardID valuer.UUID) error
|
||||
|
||||
DeletePreferencesForUser(ctx context.Context, orgID valuer.UUID, userID valuer.UUID) error
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════
|
||||
// Dashboard saved view methods
|
||||
// ════════════════════════════════════════════════════════════════════════
|
||||
CreateDashboardView(ctx context.Context, view *DashboardView) error
|
||||
|
||||
GetDashboardView(ctx context.Context, orgID valuer.UUID, id valuer.UUID) (*DashboardView, error)
|
||||
|
||||
ListDashboardViews(ctx context.Context, orgID valuer.UUID) ([]*DashboardView, error)
|
||||
|
||||
UpdateDashboardView(ctx context.Context, view *DashboardView) error
|
||||
|
||||
DeleteDashboardView(ctx context.Context, orgID valuer.UUID, id valuer.UUID) error
|
||||
}
|
||||
|
||||
66
pkg/types/migration.md
Normal file
66
pkg/types/migration.md
Normal file
@@ -0,0 +1,66 @@
|
||||
### Phases
|
||||
|
||||
1. **Pre-migration (dev)**: new tables `tag`, `tag_relations`, `pinned_dashboard`, `dashboard_view`
|
||||
2. **Validation**: run the migration script against a few prod snapshots locally. Verify counts match, spot-check shapes, time the run to estimate downtime.
|
||||
3. **Dry-run in cloud prod (cloud only).** Ship a build that runs the migration script in read-only
|
||||
mode against live prod data. Whenever the v1 get API is called for a dashboard, we dry-run the migration script for it in an async process. If there is a failure, schema mismatches, tag normalization rejections, etc, it is logged. Reach out to affected customers to fix their dashboards before the real migration. Re-run closer to migration day to confirm resolution.
|
||||
4. **Migration deploy**: script runs, FF flips on. Integration dashboards materialized in the `dashboard` table using an internal system account with `Locked = true`.
|
||||
5. **Post-migration**: v1 APIs deprecated but still respond.
|
||||
|
||||
#### **Rejected idea: dry run in a background job**
|
||||
|
||||
In the above plan, we only check the dashboards that the users access. However, that should be enough to cover enough dashboards to be able to find out possible issues. The extra effort of a background job doesn't have enough ROI.
|
||||
|
||||
### What gets migrated
|
||||
|
||||
Existing v1 dashboards → full v2 data shape (tags extracted from `data.tags` into `tag` and `tag_relations`; the field is removed from the blob). Integration dashboards → materialized rows. Pinned dashboards and saved views start empty.
|
||||
|
||||
### Tag normalization (v1 strings → v2 tag rows)
|
||||
|
||||
Each v1 dashboard `data.tags` is `[]string`. For every string `s`, derive `(key, value)`.
|
||||
|
||||
**Order of rules:**
|
||||
|
||||
1. **Trim** leading/trailing whitespace from `s`. If empty after trim → **skip silently** (log dashboard id + index, continue).
|
||||
2. **If `s` contains `:`** → split at the **first** `:`. Let `k` = left side, `v` = right side.
|
||||
- If `k` is empty (input was `:val`) → `key = "tag"`, `value = val`.
|
||||
- If `v` is empty (input was `key:`) → `key = "tag"`, `value = k` (the literal left side becomes the value).
|
||||
- Otherwise → `key = k`, `value = v`.
|
||||
- Other `:` are replaced with `_`.
|
||||
3. **Else if `s` contains `/`** → split at the **first** `/`. Let `k` = left side, `v` = right side.
|
||||
- Same empty-side handling: empty left → `key="tag", value=v`; empty right → `key="tag", value=k`. Otherwise → `key=k, value=v`.
|
||||
4. **Else** (no separator) → `key = "tag"`, `value = s`.
|
||||
5. Reserved-key collision. After steps 2–4, if the resulting key (case-insensitively) matches a reserved DSL key (name, description, created_at, updated_at, created_by, locked, public), prefix it with _ (e.g. name → _name). Silent — extremely unlikely in practice, but the rename keeps the dashboard alive without ambiguating the query DSL.
|
||||
6. **`/` scrub.** Output tags must never contain `/` (input validation forbids it). After the above steps, replace any remaining `/` in `key` and `value` with `_`:
|
||||
- `a/b/c` → step 3 splits at first `/` → `key="a", value="b/c"` → after scrub → `key="a", value="b_c"`
|
||||
- `team/eng:prod` → step 2 splits at `:` → `key="team/eng", value="prod"` → after scrub → `key="team_eng", value="prod"`
|
||||
- `team/eng:my/path` → step 2 → `key="team/eng", value="my/path"` → scrub → `key="team_eng", value="my_path"`
|
||||
|
||||
Trailing/leading whitespace within `key` and `value` after split is also trimmed; if either side becomes empty after that, apply the empty-side rules above. If both sides are effectively empty (e.g. input was `:` or `/`), skip silently.
|
||||
|
||||
**Case-collision dedup:**
|
||||
|
||||
Multiple v1 strings can normalize to the same `(LOWER(key), LOWER(value))` across an org (e.g. `Env:Prod` and `env:PROD`). The functional unique index ensures only one row exists. Display casing is taken from the variant on the dashboard with the **earliest `created_at`** (ties broken by `dashboard.id`) — same rule as the previous spec, just applied to `(key, value)` instead of `name`.
|
||||
|
||||
**Tag relations:**
|
||||
|
||||
After tag rows are upserted, build `tag_relations` from each (dashboard, tag-id-after-dedup) pair. `ON CONFLICT` clause in the query makes this idempotent.
|
||||
|
||||
### Script properties
|
||||
|
||||
- Per-dashboard transactional. One failure logs the dashboard id and continues.
|
||||
- Idempotent: `ON CONFLICT DO NOTHING` on tag and tag_relations upserts; dashboards already in v2 shape are skipped.
|
||||
- Progress logged every N dashboards; final summary includes totals and failure list.
|
||||
|
||||
### Rollback
|
||||
|
||||
Forward-only — no v2→v1 reverse script. The FF is the kill-switch pre-frontend-cutover. After cutover, rollback = another deploy with the fix.
|
||||
|
||||
### What about dashboards that fail to migrate after all this?
|
||||
|
||||
In Get API (v2) there will be a check on the dashboard fetched.
|
||||
|
||||
- `v2` → normal flow.
|
||||
- `v1` → return `422 Unprocessable Entity`.
|
||||
|
||||
The deprecated v1 APIs will still exist, so if any support ticket comes, we can check via the v1 API and see what’s wrong.
|
||||
@@ -232,66 +232,6 @@ def test_get_missing_dashboard_returns_not_found(
|
||||
assert response.status_code == HTTPStatus.NOT_FOUND
|
||||
|
||||
|
||||
def test_update_rejects_malformed_id(
|
||||
signoz: SigNoz,
|
||||
create_user_admin: Operation, # pylint: disable=unused-argument
|
||||
get_token: Callable[[str, str], str],
|
||||
):
|
||||
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
|
||||
|
||||
response = requests.put(
|
||||
signoz.self.host_configs["8080"].get(f"{BASE_URL}/not-a-uuid"),
|
||||
json={
|
||||
"schemaVersion": "v6",
|
||||
"name": "malformed-id",
|
||||
"spec": {"display": {"name": "Malformed Id"}},
|
||||
"tags": [],
|
||||
},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=5,
|
||||
)
|
||||
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST
|
||||
|
||||
|
||||
def test_update_missing_dashboard_returns_not_found(
|
||||
signoz: SigNoz,
|
||||
create_user_admin: Operation, # pylint: disable=unused-argument
|
||||
get_token: Callable[[str, str], str],
|
||||
):
|
||||
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
|
||||
|
||||
response = requests.put(
|
||||
signoz.self.host_configs["8080"].get(f"{BASE_URL}/{uuid.uuid4()}"),
|
||||
json={
|
||||
"schemaVersion": "v6",
|
||||
"name": "missing-dashboard",
|
||||
"spec": {"display": {"name": "Missing Dashboard"}},
|
||||
"tags": [],
|
||||
},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=5,
|
||||
)
|
||||
|
||||
assert response.status_code == HTTPStatus.NOT_FOUND
|
||||
|
||||
|
||||
def test_delete_rejects_malformed_id(
|
||||
signoz: SigNoz,
|
||||
create_user_admin: Operation, # pylint: disable=unused-argument
|
||||
get_token: Callable[[str, str], str],
|
||||
):
|
||||
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
|
||||
|
||||
response = requests.delete(
|
||||
signoz.self.host_configs["8080"].get(f"{BASE_URL}/not-a-uuid"),
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=5,
|
||||
)
|
||||
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST
|
||||
|
||||
|
||||
def test_delete_missing_dashboard_returns_not_found(
|
||||
signoz: SigNoz,
|
||||
create_user_admin: Operation, # pylint: disable=unused-argument
|
||||
|
||||
@@ -1,361 +0,0 @@
|
||||
import uuid
|
||||
from collections.abc import Callable
|
||||
from http import HTTPStatus
|
||||
|
||||
import pytest
|
||||
import requests
|
||||
|
||||
from fixtures.auth import USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD
|
||||
from fixtures.types import Operation, SigNoz
|
||||
|
||||
BASE_URL = "/api/v2/dashboard_views"
|
||||
|
||||
|
||||
# ─── failure cases (create no views) ─────────────────────────────────────────
|
||||
|
||||
|
||||
def test_create_rejects_missing_name(
|
||||
signoz: SigNoz,
|
||||
create_user_admin: Operation, # pylint: disable=unused-argument
|
||||
get_token: Callable[[str, str], str],
|
||||
):
|
||||
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
|
||||
|
||||
response = requests.post(
|
||||
signoz.self.host_configs["8080"].get(BASE_URL),
|
||||
json={"data": {"version": "v1"}},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=5,
|
||||
)
|
||||
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST
|
||||
body = response.json()
|
||||
assert body["status"] == "error"
|
||||
assert body["error"]["code"] == "dashboard_view_invalid_input"
|
||||
assert body["error"]["message"] == "name is required"
|
||||
|
||||
|
||||
def test_create_rejects_blank_name(
|
||||
signoz: SigNoz,
|
||||
create_user_admin: Operation, # pylint: disable=unused-argument
|
||||
get_token: Callable[[str, str], str],
|
||||
):
|
||||
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
|
||||
|
||||
response = requests.post(
|
||||
signoz.self.host_configs["8080"].get(BASE_URL),
|
||||
json={"name": " ", "data": {"version": "v1"}},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=5,
|
||||
)
|
||||
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST
|
||||
assert response.json()["error"]["code"] == "dashboard_view_invalid_input"
|
||||
assert response.json()["error"]["message"] == "name is required"
|
||||
|
||||
|
||||
def test_create_rejects_name_too_long(
|
||||
signoz: SigNoz,
|
||||
create_user_admin: Operation, # pylint: disable=unused-argument
|
||||
get_token: Callable[[str, str], str],
|
||||
):
|
||||
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
|
||||
|
||||
response = requests.post(
|
||||
signoz.self.host_configs["8080"].get(BASE_URL),
|
||||
json={"name": "x" * 33, "data": {"version": "v1"}},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=5,
|
||||
)
|
||||
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST
|
||||
assert response.json()["error"]["code"] == "dashboard_view_invalid_input"
|
||||
assert response.json()["error"]["message"] == "name must be at most 32 characters, got 33"
|
||||
|
||||
|
||||
def test_create_rejects_wrong_schema_version(
|
||||
signoz: SigNoz,
|
||||
create_user_admin: Operation, # pylint: disable=unused-argument
|
||||
get_token: Callable[[str, str], str],
|
||||
):
|
||||
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
|
||||
|
||||
response = requests.post(
|
||||
signoz.self.host_configs["8080"].get(BASE_URL),
|
||||
json={"name": "wrong-version", "data": {"version": "v2"}},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=5,
|
||||
)
|
||||
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST
|
||||
assert response.json()["error"]["code"] == "dashboard_view_invalid_input"
|
||||
assert response.json()["error"]["message"] == 'version must be "v1", got "v2"'
|
||||
|
||||
|
||||
def test_create_rejects_missing_version(
|
||||
signoz: SigNoz,
|
||||
create_user_admin: Operation, # pylint: disable=unused-argument
|
||||
get_token: Callable[[str, str], str],
|
||||
):
|
||||
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
|
||||
|
||||
response = requests.post(
|
||||
signoz.self.host_configs["8080"].get(BASE_URL),
|
||||
json={"name": "missing-version", "data": {}},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=5,
|
||||
)
|
||||
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST
|
||||
assert response.json()["error"]["code"] == "dashboard_view_invalid_input"
|
||||
assert response.json()["error"]["message"] == 'version must be "v1", got ""'
|
||||
|
||||
|
||||
def test_create_rejects_unknown_field(
|
||||
signoz: SigNoz,
|
||||
create_user_admin: Operation, # pylint: disable=unused-argument
|
||||
get_token: Callable[[str, str], str],
|
||||
):
|
||||
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
|
||||
|
||||
response = requests.post(
|
||||
signoz.self.host_configs["8080"].get(BASE_URL),
|
||||
json={"name": "rejects-unknown", "data": {"version": "v1"}, "unknownfield": "boom"},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=5,
|
||||
)
|
||||
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST
|
||||
assert response.json()["error"]["code"] == "dashboard_view_invalid_input"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"data",
|
||||
[
|
||||
{"version": "v1", "sort": "bogus"},
|
||||
{"version": "v1", "order": "bogus"},
|
||||
{"version": "v1", "query": "x" * 1025},
|
||||
],
|
||||
)
|
||||
def test_create_rejects_invalid_filter(
|
||||
signoz: SigNoz,
|
||||
create_user_admin: Operation, # pylint: disable=unused-argument
|
||||
get_token: Callable[[str, str], str],
|
||||
data: dict,
|
||||
):
|
||||
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
|
||||
|
||||
response = requests.post(
|
||||
signoz.self.host_configs["8080"].get(BASE_URL),
|
||||
json={"name": "invalid-filter", "data": data},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=5,
|
||||
)
|
||||
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST
|
||||
assert response.json()["error"]["code"] == "dashboard_list_invalid"
|
||||
|
||||
|
||||
def test_update_rejects_malformed_id(
|
||||
signoz: SigNoz,
|
||||
create_user_admin: Operation, # pylint: disable=unused-argument
|
||||
get_token: Callable[[str, str], str],
|
||||
):
|
||||
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
|
||||
|
||||
response = requests.put(
|
||||
signoz.self.host_configs["8080"].get(f"{BASE_URL}/not-a-uuid"),
|
||||
json={"name": "x", "data": {"version": "v1"}},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=5,
|
||||
)
|
||||
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST
|
||||
|
||||
|
||||
def test_update_missing_view_returns_not_found(
|
||||
signoz: SigNoz,
|
||||
create_user_admin: Operation, # pylint: disable=unused-argument
|
||||
get_token: Callable[[str, str], str],
|
||||
):
|
||||
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
|
||||
|
||||
response = requests.put(
|
||||
signoz.self.host_configs["8080"].get(f"{BASE_URL}/{uuid.uuid4()}"),
|
||||
json={"name": "x", "data": {"version": "v1"}},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=5,
|
||||
)
|
||||
|
||||
assert response.status_code == HTTPStatus.NOT_FOUND
|
||||
assert response.json()["error"]["code"] == "dashboard_view_not_found"
|
||||
|
||||
|
||||
def test_delete_rejects_malformed_id(
|
||||
signoz: SigNoz,
|
||||
create_user_admin: Operation, # pylint: disable=unused-argument
|
||||
get_token: Callable[[str, str], str],
|
||||
):
|
||||
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
|
||||
|
||||
response = requests.delete(
|
||||
signoz.self.host_configs["8080"].get(f"{BASE_URL}/not-a-uuid"),
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=5,
|
||||
)
|
||||
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST
|
||||
|
||||
|
||||
def test_delete_missing_view_returns_not_found(
|
||||
signoz: SigNoz,
|
||||
create_user_admin: Operation, # pylint: disable=unused-argument
|
||||
get_token: Callable[[str, str], str],
|
||||
):
|
||||
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
|
||||
|
||||
response = requests.delete(
|
||||
signoz.self.host_configs["8080"].get(f"{BASE_URL}/{uuid.uuid4()}"),
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=5,
|
||||
)
|
||||
|
||||
assert response.status_code == HTTPStatus.NOT_FOUND
|
||||
assert response.json()["error"]["code"] == "dashboard_view_not_found"
|
||||
|
||||
|
||||
# ─── lifecycle ───────────────────────────────────────────────────────────────
|
||||
# A single end-to-end flow through create → list → update → list → delete.
|
||||
# Saved views are shared org-wide and there is no get-by-id endpoint, so every
|
||||
# read goes through the list.
|
||||
|
||||
|
||||
def test_dashboard_view_lifecycle(
|
||||
signoz: SigNoz,
|
||||
create_user_admin: Operation, # pylint: disable=unused-argument
|
||||
get_token: Callable[[str, str], str],
|
||||
):
|
||||
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
|
||||
|
||||
# Saved views share this package's DB and it's reused across runs, so start
|
||||
# from a clean slate: delete every view. This test then owns the whole view
|
||||
# space and asserts on global counts.
|
||||
response = requests.get(
|
||||
signoz.self.host_configs["8080"].get(BASE_URL),
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=5,
|
||||
)
|
||||
assert response.status_code == HTTPStatus.OK, response.text
|
||||
for view in response.json()["data"]["views"]:
|
||||
requests.delete(
|
||||
signoz.self.host_configs["8080"].get(f"{BASE_URL}/{view['id']}"),
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=5,
|
||||
)
|
||||
|
||||
# ── stage 1: create and verify the round-tripped shape ───────────────────
|
||||
response = requests.post(
|
||||
signoz.self.host_configs["8080"].get(BASE_URL),
|
||||
json={
|
||||
"name": "Pulse Prod",
|
||||
"data": {
|
||||
"version": "v1",
|
||||
"query": "team = 'pulse' AND env = 'prod'",
|
||||
"sort": "name",
|
||||
"order": "asc",
|
||||
},
|
||||
},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=5,
|
||||
)
|
||||
assert response.status_code == HTTPStatus.CREATED, response.text
|
||||
created = response.json()["data"]
|
||||
view_id = created["id"]
|
||||
assert created["name"] == "Pulse Prod"
|
||||
assert created["data"]["version"] == "v1"
|
||||
assert created["data"]["query"] == "team = 'pulse' AND env = 'prod'"
|
||||
|
||||
# leading/trailing whitespace in the name is trimmed on create
|
||||
response = requests.post(
|
||||
signoz.self.host_configs["8080"].get(BASE_URL),
|
||||
json={"name": " Storage ", "data": {"version": "v1", "query": "team = 'storage'"}},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=5,
|
||||
)
|
||||
assert response.status_code == HTTPStatus.CREATED, response.text
|
||||
storage = response.json()["data"]
|
||||
assert storage["name"] == "Storage"
|
||||
assert storage["data"]["query"] == "team = 'storage'"
|
||||
assert storage["data"]["sort"] == ""
|
||||
assert storage["data"]["order"] == ""
|
||||
|
||||
# ── stage 2: list returns both views ─────────────────────────────────────
|
||||
response = requests.get(
|
||||
signoz.self.host_configs["8080"].get(BASE_URL),
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=5,
|
||||
)
|
||||
assert response.status_code == HTTPStatus.OK, response.text
|
||||
views = response.json()["data"]["views"]
|
||||
assert len(views) == 2
|
||||
assert {v["name"] for v in views} == {"Pulse Prod", "Storage"}
|
||||
|
||||
# ── stage 3: update replaces name and data ───────────────────────────────
|
||||
response = requests.put(
|
||||
signoz.self.host_configs["8080"].get(f"{BASE_URL}/{view_id}"),
|
||||
json={
|
||||
"name": "Pulse Staging",
|
||||
"data": {
|
||||
"version": "v1",
|
||||
"query": "team = 'pulse' AND env = 'staging'",
|
||||
"sort": "created_at",
|
||||
"order": "desc",
|
||||
},
|
||||
},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=5,
|
||||
)
|
||||
assert response.status_code == HTTPStatus.OK, response.text
|
||||
updated = response.json()["data"]
|
||||
assert updated["id"] == view_id
|
||||
assert updated["name"] == "Pulse Staging"
|
||||
assert updated["data"]["query"] == "team = 'pulse' AND env = 'staging'"
|
||||
assert updated["data"]["sort"] == "created_at"
|
||||
assert updated["data"]["order"] == "desc"
|
||||
|
||||
# ── stage 4: the update is reflected in the list ─────────────────────────
|
||||
response = requests.get(
|
||||
signoz.self.host_configs["8080"].get(BASE_URL),
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=5,
|
||||
)
|
||||
listed = {v["name"]: v for v in response.json()["data"]["views"]}
|
||||
assert set(listed) == {"Pulse Staging", "Storage"}
|
||||
assert listed["Pulse Staging"]["data"]["query"] == "team = 'pulse' AND env = 'staging'"
|
||||
|
||||
# ── stage 5: delete removes the view from the list ───────────────────────
|
||||
assert (
|
||||
requests.delete(
|
||||
signoz.self.host_configs["8080"].get(f"{BASE_URL}/{view_id}"),
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=5,
|
||||
).status_code
|
||||
== HTTPStatus.NO_CONTENT
|
||||
)
|
||||
response = requests.get(
|
||||
signoz.self.host_configs["8080"].get(BASE_URL),
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=5,
|
||||
)
|
||||
views = response.json()["data"]["views"]
|
||||
assert {v["name"] for v in views} == {"Storage"}
|
||||
|
||||
# deleting an already-deleted view is a 404
|
||||
assert (
|
||||
requests.delete(
|
||||
signoz.self.host_configs["8080"].get(f"{BASE_URL}/{view_id}"),
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=5,
|
||||
).status_code
|
||||
== HTTPStatus.NOT_FOUND
|
||||
)
|
||||
Reference in New Issue
Block a user