Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(search): summarize insight when missing name #18809

Merged
merged 8 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified frontend/__snapshots__/components-command-bar--search.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 21 additions & 1 deletion frontend/src/lib/components/CommandBar/SearchResult.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import { resultTypeToName } from './constants'
import { searchBarLogic, urlForResult } from './searchBarLogic'
import { SearchResult as SearchResultType } from './types'
import { LemonSkeleton } from '@posthog/lemon-ui'
import { summarizeInsight } from 'scenes/insights/summarizeInsight'
import { groupsModel } from '~/models/groupsModel'
import { cohortsModel } from '~/models/cohortsModel'
import { mathsLogic } from 'scenes/trends/mathsLogic'
import { Node } from '~/queries/schema'
import { FilterType } from '~/types'

type SearchResultProps = {
result: SearchResultType
Expand Down Expand Up @@ -86,9 +92,23 @@ type ResultNameProps = {
}

export const ResultName = ({ result }: ResultNameProps): JSX.Element | null => {
const { aggregationLabel } = useValues(groupsModel)
const { cohortsById } = useValues(cohortsModel)
const { mathDefinitions } = useValues(mathsLogic)

const { type, extra_fields } = result
if (type === 'insight') {
return extra_fields.name ? <span>{extra_fields.name}</span> : <i>{extra_fields.derived_name}</i>
return extra_fields.name ? (
<span>{extra_fields.name}</span>
) : (
<i>
{summarizeInsight(extra_fields.query as Node | null, extra_fields.filters as Partial<FilterType>, {
aggregationLabel,
cohortsById,
mathDefinitions,
})}
</i>
)
Comment on lines +101 to +111
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem right, because there shouldn't be insights that have neither name nor derived_name. Facilitating searchability is a major reason for derived_name being saved, so we shouldn't paper over this. Should we revert this and fix the root cause, or am I missing something here? @thmsobrmlr

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I've reimplemented what we do for saved insights as derived_name isn't set for insights in production. Happy to look into it, but would appreciate pointers on how/where we set it. And wouldn't revert this change for now, as we do the same thing in other places and it works for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Digging a bit it seems that we've at one point set the derived_name frontend side when saving an insight, but don't do so now. We obviously can't get the names back this way (and this wouldn't work for insights generated via API), so I'll probably have to duplicate the javascript function in python.

} else if (type === 'feature_flag') {
return <span>{extra_fields.key}</span>
} else if (type === 'notebook') {
Expand Down
4 changes: 2 additions & 2 deletions posthog/api/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
ENTITY_MAP = {
"insight": {
"klass": Insight,
"search_fields": {"name": "A", "derived_name": "B", "description": "C"},
"extra_fields": ["name", "derived_name", "description"],
"search_fields": {"name": "A", "description": "C"},
"extra_fields": ["name", "description", "filters", "query"],
},
"dashboard": {
"klass": Dashboard,
Expand Down
2 changes: 1 addition & 1 deletion posthog/api/test/__snapshots__/test_feature_flag.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -1759,7 +1759,7 @@
"posthog_user"."toolbar_mode",
"posthog_user"."events_column_config"
FROM "posthog_featureflag"
INNER JOIN "posthog_user" ON ("posthog_featureflag"."created_by_id" = "posthog_user"."id")
LEFT OUTER JOIN "posthog_user" ON ("posthog_featureflag"."created_by_id" = "posthog_user"."id")
WHERE ("posthog_featureflag"."team_id" = 2
AND "posthog_featureflag"."id" = 2)
LIMIT 21 /*controller='project_feature_flags-create-static-cohort-for-flag',route='api/projects/%28%3FP%3Cparent_lookup_team_id%3E%5B%5E/.%5D%2B%29/feature_flags/%28%3FP%3Cpk%3E%5B%5E/.%5D%2B%29/create_static_cohort_for_flag/%3F%24'*/
Expand Down
8 changes: 2 additions & 6 deletions posthog/api/test/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,7 @@ def test_response_format_and_ids(self):
"rank": response.json()["results"][1]["rank"],
"type": "insight",
"result_id": self.insight_1.short_id,
"extra_fields": {
"derived_name": None,
"name": "second insight",
"description": None,
},
"extra_fields": {"name": "second insight", "description": None, "filters": {}, "query": None},
},
)

Expand All @@ -88,7 +84,7 @@ def test_extra_fields(self):
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.json()["results"][0]["extra_fields"],
{"derived_name": "derived name", "description": None, "name": None},
{"name": None, "description": None, "filters": {}, "query": None},
)

def test_search_with_fully_invalid_query(self):
Expand Down
Loading