Skip to content

Commit

Permalink
fix(insights): do not remove empty series from converted queries (#17879
Browse files Browse the repository at this point in the history
)
  • Loading branch information
thmsobrmlr authored Oct 10, 2023
1 parent a914bf4 commit b09c7a8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 32 deletions.
53 changes: 29 additions & 24 deletions frontend/src/lib/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -432,31 +432,36 @@ export function objectClean<T extends Record<string | number | symbol, unknown>>
})
return response
}
export function objectCleanWithEmpty<T extends Record<string | number | symbol, unknown>>(obj: T): T {
export function objectCleanWithEmpty<T extends Record<string | number | symbol, unknown>>(
obj: T,
ignoredKeys: string[] = []
): T {
const response = { ...obj }
Object.keys(response).forEach((key) => {
// remove undefined values
if (response[key] === undefined) {
delete response[key]
}
// remove empty arrays i.e. []
if (
typeof response[key] === 'object' &&
Array.isArray(response[key]) &&
(response[key] as unknown[]).length === 0
) {
delete response[key]
}
// remove empty objects i.e. {}
if (
typeof response[key] === 'object' &&
!Array.isArray(response[key]) &&
response[key] !== null &&
Object.keys(response[key] as Record<string | number | symbol, unknown>).length === 0
) {
delete response[key]
}
})
Object.keys(response)
.filter((key) => !ignoredKeys.includes(key))
.forEach((key) => {
// remove undefined values
if (response[key] === undefined) {
delete response[key]
}
// remove empty arrays i.e. []
if (
typeof response[key] === 'object' &&
Array.isArray(response[key]) &&
(response[key] as unknown[]).length === 0
) {
delete response[key]
}
// remove empty objects i.e. {}
if (
typeof response[key] === 'object' &&
!Array.isArray(response[key]) &&
response[key] !== null &&
Object.keys(response[key] as Record<string | number | symbol, unknown>).length === 0
) {
delete response[key]
}
})
return response
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,10 @@ describe('filtersToQueryNode', () => {

const result = filtersToQueryNode(filters)

const query: Partial<TrendsQuery> = {
const query: TrendsQuery = {
kind: NodeKind.TrendsQuery,
interval: 'day',
series: [],
}
expect(result).toEqual(query)
})
Expand All @@ -308,7 +309,7 @@ describe('filtersToQueryNode', () => {

const result = filtersToQueryNode(filters)

const query: Partial<TrendsQuery> = {
const query: TrendsQuery = {
kind: NodeKind.TrendsQuery,
trendsFilter: {
smoothing_intervals: 1,
Expand All @@ -324,6 +325,7 @@ describe('filtersToQueryNode', () => {
breakdown: {
breakdown_histogram_bin_count: 1,
},
series: [],
}
expect(result).toEqual(query)
})
Expand Down Expand Up @@ -362,7 +364,7 @@ describe('filtersToQueryNode', () => {

const result = filtersToQueryNode(filters)

const query: Partial<FunnelsQuery> = {
const query: FunnelsQuery = {
kind: NodeKind.FunnelsQuery,
funnelsFilter: {
funnel_viz_type: FunnelVizType.Steps,
Expand All @@ -384,6 +386,7 @@ describe('filtersToQueryNode', () => {
layout: FunnelLayout.horizontal,
hidden_legend_breakdowns: ['Chrome', 'Safari'],
},
series: [],
}
expect(result).toEqual(query)
})
Expand All @@ -403,7 +406,7 @@ describe('filtersToQueryNode', () => {

const result = filtersToQueryNode(filters)

const query: Partial<RetentionQuery> = {
const query: RetentionQuery = {
kind: NodeKind.RetentionQuery,
retentionFilter: {
retention_type: 'retention_first_time',
Expand Down Expand Up @@ -443,7 +446,7 @@ describe('filtersToQueryNode', () => {

const result = filtersToQueryNode(filters)

const query: Partial<PathsQuery> = {
const query: PathsQuery = {
kind: NodeKind.PathsQuery,
pathsFilter: {
path_type: PathType.Screen,
Expand Down Expand Up @@ -479,14 +482,15 @@ describe('filtersToQueryNode', () => {

const result = filtersToQueryNode(filters)

const query: Partial<StickinessQuery> = {
const query: StickinessQuery = {
kind: NodeKind.StickinessQuery,
stickinessFilter: {
compare: true,
show_legend: true,
hidden_legend_indexes: [0, 10],
display: ChartDisplayType.ActionsLineGraph,
},
series: [],
}
expect(result).toEqual(query)
})
Expand All @@ -502,11 +506,12 @@ describe('filtersToQueryNode', () => {

const result = filtersToQueryNode(filters)

const query: Partial<LifecycleQuery> = {
const query: LifecycleQuery = {
kind: NodeKind.LifecycleQuery,
lifecycleFilter: {
toggledLifecycles: ['new', 'dormant'],
},
series: [],
}
expect(result).toEqual(query)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,5 +257,5 @@ export const filtersToQueryNode = (filters: Partial<FilterType>): InsightQueryNo
}

// remove undefined and empty array/objects and return
return objectCleanWithEmpty(query as Record<string, any>) as InsightQueryNode
return objectCleanWithEmpty(query as Record<string, any>, ['series']) as InsightQueryNode
}

0 comments on commit b09c7a8

Please sign in to comment.