Skip to content

Commit

Permalink
fix: histogram breakdown label for the null and other values (#22987)
Browse files Browse the repository at this point in the history
  • Loading branch information
skoob13 authored Jun 16, 2024
1 parent d9cbb4d commit a0eaff1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
22 changes: 22 additions & 0 deletions frontend/src/scenes/insights/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,28 @@ describe('formatBreakdownLabel()', () => {
expect(formatBreakdownLabel('[124.8,124.8]', breakdownFilter, [], identity)).toEqual('124.8')
})

it('handles histogram breakdowns for "other" value', () => {
const breakdownFilter: BreakdownFilter = {
breakdown: '$browser_version',
breakdown_type: 'event',
breakdown_histogram_bin_count: 10,
}
expect(formatBreakdownLabel('$$_posthog_breakdown_other_$$', breakdownFilter, [], identity)).toEqual(
'Other (i.e. all remaining values)'
)
})

it('handles histogram breakdowns for "null" value', () => {
const breakdownFilter: BreakdownFilter = {
breakdown: '$browser_version',
breakdown_type: 'event',
breakdown_histogram_bin_count: 10,
}
expect(formatBreakdownLabel('$$_posthog_breakdown_null_$$', breakdownFilter, [], identity)).toEqual(
'None (i.e. no value)'
)
})

it('handles numeric breakdowns', () => {
const breakdownFilter: BreakdownFilter = {
breakdown: 'coolness_factor',
Expand Down
12 changes: 11 additions & 1 deletion frontend/src/scenes/insights/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,15 @@ export function isNullBreakdown(breakdown_value: string | number | null | undefi
)
}

function isValidJsonArray(maybeJson: string): boolean {
try {
const json = JSON.parse(maybeJson)
return Array.isArray(json)
} catch {
return false
}
}

export function formatBreakdownLabel(
breakdown_value: BreakdownKeyType | undefined,
breakdownFilter: BreakdownFilter | null | undefined,
Expand All @@ -215,7 +224,8 @@ export function formatBreakdownLabel(
if (
breakdownFilter?.breakdown_histogram_bin_count != null &&
typeof breakdown_value === 'string' &&
breakdown_value.length > 0
breakdown_value.length > 0 &&
isValidJsonArray(breakdown_value)
) {
// replace nan with null
const bucketValues = breakdown_value.replace(/\bnan\b/g, 'null')
Expand Down

0 comments on commit a0eaff1

Please sign in to comment.