Skip to content

Commit

Permalink
Unify display strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Twixes committed Mar 28, 2024
1 parent 5ad3ded commit c6eb28e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 13 deletions.
12 changes: 7 additions & 5 deletions frontend/src/scenes/insights/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,13 @@ export function formatAggregationValue(
return Array.isArray(formattedValue) ? formattedValue[0] : formattedValue
}

// NB! Sync this with breakdown.py
// NB! Sync this with breakdown_values.py
export const BREAKDOWN_OTHER_STRING_LABEL = '$$_posthog_breakdown_other_$$'
export const BREAKDOWN_OTHER_NUMERIC_LABEL = 9007199254740991 // pow(2, 53) - 1
export const BREAKDOWN_OTHER_DISPLAY = 'Other (i.e. all remaining values)'
export const BREAKDOWN_NULL_STRING_LABEL = '$$_posthog_breakdown_null_$$'
export const BREAKDOWN_NULL_NUMERIC_LABEL = 9007199254740990 // pow(2, 53) - 2
export const BREAKDOWN_NULL_DISPLAY = 'None (i.e. no value)'

export function isOtherBreakdown(breakdown_value: string | number | null | undefined | ReactNode): boolean {
return (
Expand Down Expand Up @@ -277,17 +279,17 @@ export function formatBreakdownLabel(
return cohorts?.filter((c) => c.id == breakdown_value)[0]?.name ?? (breakdown_value || '').toString()
} else if (typeof breakdown_value == 'number') {
return isOtherBreakdown(breakdown_value)
? 'Other (i.e. all remaining values)'
? BREAKDOWN_OTHER_DISPLAY
: isNullBreakdown(breakdown_value)
? 'None (i.e. no value)'
? BREAKDOWN_NULL_DISPLAY
: formatPropertyValueForDisplay
? formatPropertyValueForDisplay(breakdown, breakdown_value)?.toString() ?? 'None'
: String(breakdown_value)
} else if (typeof breakdown_value == 'string') {
return isOtherBreakdown(breakdown_value) || breakdown_value === 'nan'
? 'Other (i.e. all remaining values)'
? BREAKDOWN_OTHER_DISPLAY
: isNullBreakdown(breakdown_value) || breakdown_value === ''
? 'None (i.e. no value)'
? BREAKDOWN_NULL_DISPLAY
: breakdown_value
} else if (Array.isArray(breakdown_value)) {
return breakdown_value
Expand Down
12 changes: 9 additions & 3 deletions frontend/src/scenes/trends/viz/ActionsHorizontalBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { useEffect, useState } from 'react'
import { insightLogic } from 'scenes/insights/insightLogic'
import { insightVizDataLogic } from 'scenes/insights/insightVizDataLogic'
import { formatBreakdownLabel, isNullBreakdown, isOtherBreakdown } from 'scenes/insights/utils'
import {
BREAKDOWN_NULL_DISPLAY,
BREAKDOWN_OTHER_DISPLAY,
formatBreakdownLabel,
isNullBreakdown,
isOtherBreakdown,
} from 'scenes/insights/utils'

import { cohortsModel } from '~/models/cohortsModel'
import { propertyDefinitionsModel } from '~/models/propertyDefinitionsModel'
Expand Down Expand Up @@ -43,9 +49,9 @@ export function ActionsHorizontalBar({ showPersonsModal = true }: ChartParams):
{
labels: _data.map((item) =>
isOtherBreakdown(item.label)
? 'Other (i.e. all remaining values)'
? BREAKDOWN_OTHER_DISPLAY
: isNullBreakdown(item.label)
? 'None (i.e. no value)'
? BREAKDOWN_NULL_DISPLAY
: item.label
),
data: _data.map((item) => item.aggregated_value),
Expand Down
2 changes: 2 additions & 0 deletions posthog/hogql_queries/insights/trends/breakdown_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@

BREAKDOWN_OTHER_STRING_LABEL = "$$_posthog_breakdown_other_$$"
BREAKDOWN_OTHER_NUMERIC_LABEL = 9007199254740991 # pow(2, 53) - 1, for JS compatibility
BREAKDOWN_OTHER_DISPLAY = "Other (i.e. all remaining values)"
BREAKDOWN_NULL_STRING_LABEL = "$$_posthog_breakdown_null_$$"
BREAKDOWN_NULL_NUMERIC_LABEL = 9007199254740990 # pow(2, 53) - 2, for JS compatibility
BREAKDOWN_NULL_DISPLAY = "None (i.e. no value)"


class BreakdownValues:
Expand Down
6 changes: 4 additions & 2 deletions posthog/hogql_queries/insights/trends/trends_query_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
from posthog.hogql.query import execute_hogql_query
from posthog.hogql.timings import HogQLTimings
from posthog.hogql_queries.insights.trends.breakdown_values import (
BREAKDOWN_NULL_DISPLAY,
BREAKDOWN_NULL_STRING_LABEL,
BREAKDOWN_OTHER_DISPLAY,
BREAKDOWN_OTHER_STRING_LABEL,
)
from posthog.hogql_queries.insights.trends.display import TrendsDisplay
Expand Down Expand Up @@ -245,9 +247,9 @@ def to_actors_query_options(self) -> InsightActorsQueryOptionsResponse:
label = cohort_name
value = value
elif value == BREAKDOWN_OTHER_STRING_LABEL:
label = "Other (i.e. all remaining values)"
label = BREAKDOWN_OTHER_DISPLAY
elif value == BREAKDOWN_NULL_STRING_LABEL:
label = "None (i.e. no value)"
label = BREAKDOWN_NULL_DISPLAY
elif is_boolean_breakdown:
label = self._convert_boolean(value)
else:
Expand Down
7 changes: 4 additions & 3 deletions posthog/queries/trends/breakdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
PropertyOperatorType,
TREND_FILTER_TYPE_EVENTS,
)
from posthog.hogql_queries.insights.trends.breakdown_values import BREAKDOWN_NULL_DISPLAY, BREAKDOWN_OTHER_DISPLAY
from posthog.models.action.util import format_action_filter
from posthog.models.entity import Entity
from posthog.models.event.sql import EVENT_JOIN_PERSON_SQL
Expand Down Expand Up @@ -740,11 +741,11 @@ def _determine_breakdown_label(
if breakdown_type == "cohort":
return get_breakdown_cohort_name(breakdown_value)
elif str(value) == BREAKDOWN_OTHER_STRING_LABEL or value == BREAKDOWN_OTHER_NUMERIC_LABEL:
return "Other (i.e. all remaining values)"
return BREAKDOWN_OTHER_DISPLAY
elif str(value) == BREAKDOWN_NULL_STRING_LABEL or value == BREAKDOWN_NULL_NUMERIC_LABEL:
return "None (i.e. no value)"
return BREAKDOWN_NULL_DISPLAY
else:
return str(value) or "None (i.e. no value)"
return str(value) or BREAKDOWN_NULL_DISPLAY

def _person_join_condition(self) -> Tuple[str, Dict]:
if self.person_on_events_mode == PersonOnEventsMode.V1_ENABLED:
Expand Down

0 comments on commit c6eb28e

Please sign in to comment.