-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathinsightNavLogic.tsx
388 lines (361 loc) · 15.3 KB
/
insightNavLogic.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import { actions, afterMount, connect, kea, key, listeners, path, props, reducers, selectors } from 'kea'
import { FEATURE_FLAGS } from 'lib/constants'
import { LemonTag } from 'lib/lemon-ui/LemonTag/LemonTag'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { identifierToHuman } from 'lib/utils'
import { insightDataLogic } from 'scenes/insights/insightDataLogic'
import { keyForInsightLogicProps } from 'scenes/insights/sharedUtils'
import { filterTestAccountsDefaultsLogic } from 'scenes/settings/environment/filterTestAccountDefaultsLogic'
import { nodeKindToInsightType } from '~/queries/nodes/InsightQuery/utils/queryNodeToFilter'
import { getDefaultQuery } from '~/queries/nodes/InsightViz/utils'
import {
ActionsNode,
DataWarehouseNode,
EventsNode,
FunnelsFilter,
FunnelsQuery,
InsightQueryNode,
InsightVizNode,
LifecycleFilter,
LifecycleQuery,
PathsFilter,
PathsQuery,
RetentionFilter,
RetentionQuery,
StickinessFilter,
StickinessQuery,
TrendsFilter,
TrendsQuery,
} from '~/queries/schema'
import {
containsHogQLQuery,
filterKeyForQuery,
getDisplay,
getShowPercentStackView,
getShowValuesOnSeries,
isFunnelsQuery,
isHogQuery,
isInsightQueryWithBreakdown,
isInsightQueryWithSeries,
isInsightVizNode,
isLifecycleQuery,
isPathsQuery,
isRetentionQuery,
isStickinessQuery,
isTrendsQuery,
} from '~/queries/utils'
import { BaseMathType, InsightLogicProps, InsightType } from '~/types'
import { MathAvailability } from '../filters/ActionFilter/ActionFilterRow/ActionFilterRow'
import { insightSceneLogic } from '../insightSceneLogic'
import type { insightNavLogicType } from './insightNavLogicType'
export interface Tab {
label: string | JSX.Element
type: InsightType
dataAttr: string
}
type OmitConflictingProperties<T> = Omit<T, 'resultCustomizations'>
export interface CommonInsightFilter
extends Partial<OmitConflictingProperties<TrendsFilter>>,
Partial<OmitConflictingProperties<FunnelsFilter>>,
Partial<RetentionFilter>,
Partial<PathsFilter>,
Partial<StickinessFilter>,
Partial<LifecycleFilter> {}
export interface QueryPropertyCache
extends Omit<Partial<TrendsQuery>, 'kind' | 'response'>,
Omit<Partial<FunnelsQuery>, 'kind' | 'response'>,
Omit<Partial<RetentionQuery>, 'kind' | 'response'>,
Omit<Partial<PathsQuery>, 'kind' | 'response'>,
Omit<Partial<StickinessQuery>, 'kind' | 'response'>,
Omit<Partial<LifecycleQuery>, 'kind' | 'response'> {
commonFilter: CommonInsightFilter
}
const cleanSeriesEntityMath = (
entity: EventsNode | ActionsNode | DataWarehouseNode,
mathAvailability: MathAvailability
): EventsNode | ActionsNode | DataWarehouseNode => {
const { math, math_property, math_group_type_index, math_hogql, ...baseEntity } = entity
// TODO: This should be improved to keep a math that differs from the default.
// For this we need to know wether the math was actively changed e.g.
// On which insight type the math properties have been set.
if (mathAvailability === MathAvailability.All) {
// return entity with default all availability math set
return { ...baseEntity, math: BaseMathType.TotalCount }
} else if (mathAvailability === MathAvailability.ActorsOnly) {
// return entity with default actors only availability math set
return { ...baseEntity, math: BaseMathType.UniqueUsers }
}
// return entity without math properties for insights that don't support it
return baseEntity
}
const cleanSeriesMath = (
series: (EventsNode | ActionsNode | DataWarehouseNode)[],
mathAvailability: MathAvailability
): (EventsNode | ActionsNode | DataWarehouseNode)[] => {
return series.map((entity) => cleanSeriesEntityMath(entity, mathAvailability))
}
export const insightNavLogic = kea<insightNavLogicType>([
props({} as InsightLogicProps),
key(keyForInsightLogicProps('new')),
path((key) => ['scenes', 'insights', 'InsightNav', 'insightNavLogic', key]),
connect((props: InsightLogicProps) => ({
values: [
featureFlagLogic,
['featureFlags'],
insightDataLogic(props),
['query'],
filterTestAccountsDefaultsLogic,
['filterTestAccountsDefault'],
],
actions: [insightDataLogic(props), ['setQuery'], insightSceneLogic, ['setOpenedWithQuery']],
})),
actions({
setActiveView: (view: InsightType) => ({ view }),
updateQueryPropertyCache: (cache: QueryPropertyCache) => ({ cache }),
}),
reducers({
queryPropertyCache: [
null as QueryPropertyCache | null,
{
updateQueryPropertyCache: (state, { cache }) => ({
...state,
...cache,
}),
},
],
}),
selectors({
activeView: [
(s) => [s.query],
(query) => {
if (containsHogQLQuery(query)) {
return InsightType.SQL
} else if (isHogQuery(query)) {
return InsightType.HOG
} else if (isInsightVizNode(query)) {
return nodeKindToInsightType[query.source.kind] || InsightType.TRENDS
}
return InsightType.JSON
},
],
tabs: [
(s) => [s.activeView, s.query, s.featureFlags],
(activeView, query, featureFlags) => {
const tabs: Tab[] = [
{
label: 'Trends',
type: InsightType.TRENDS,
dataAttr: 'insight-trends-tab',
},
{
label: 'Funnels',
type: InsightType.FUNNELS,
dataAttr: 'insight-funnels-tab',
},
{
label: 'Retention',
type: InsightType.RETENTION,
dataAttr: 'insight-retention-tab',
},
{
label: 'User Paths',
type: InsightType.PATHS,
dataAttr: 'insight-path-tab',
},
{
label: 'Stickiness',
type: InsightType.STICKINESS,
dataAttr: 'insight-stickiness-tab',
},
{
label: 'Lifecycle',
type: InsightType.LIFECYCLE,
dataAttr: 'insight-lifecycle-tab',
},
{
label: 'SQL',
type: InsightType.SQL,
dataAttr: 'insight-sql-tab',
},
]
if (featureFlags[FEATURE_FLAGS.HOG] || activeView === InsightType.HOG) {
tabs.push({
label: <>Hog 🦔</>,
type: InsightType.HOG,
dataAttr: 'insight-hog-tab',
})
}
if (activeView === InsightType.JSON) {
// only display this tab when it is selected by the provided insight query
// don't display it otherwise... humans shouldn't be able to click to select this tab
// it only opens when you click the <OpenEditorButton/>
const humanFriendlyQueryKind: string | null =
typeof query?.kind === 'string'
? identifierToHuman(query.kind.replace(/(Node|Query)$/g, ''), 'title')
: null
tabs.push({
label: (
<>
{humanFriendlyQueryKind ?? 'Custom'}{' '}
<LemonTag type="warning" className="uppercase ml-2">
Beta
</LemonTag>
</>
),
type: InsightType.JSON,
dataAttr: 'insight-json-tab',
})
}
return tabs
},
],
}),
listeners(({ values, actions }) => ({
setActiveView: ({ view }) => {
const query = getDefaultQuery(view, values.filterTestAccountsDefault)
if (isInsightVizNode(query)) {
actions.setQuery({
...query,
source: values.queryPropertyCache
? mergeCachedProperties(query.source, values.queryPropertyCache)
: query.source,
} as InsightVizNode)
actions.setOpenedWithQuery(query)
} else {
actions.setQuery(query)
actions.setOpenedWithQuery(query)
}
},
setQuery: ({ query }) => {
if (isInsightVizNode(query)) {
actions.updateQueryPropertyCache(cachePropertiesFromQuery(query.source, values.queryPropertyCache))
}
},
})),
afterMount(({ values, actions }) => {
if (values.query && isInsightVizNode(values.query)) {
actions.updateQueryPropertyCache(cachePropertiesFromQuery(values.query.source, values.queryPropertyCache))
}
}),
])
const cachePropertiesFromQuery = (query: InsightQueryNode, cache: QueryPropertyCache | null): QueryPropertyCache => {
const newCache = JSON.parse(JSON.stringify(query)) as QueryPropertyCache
// // set series (first two entries) from retention target and returning entity
// if (isRetentionQuery(query)) {
// const { targetEntity, returningEntity } = query.retentionFilter || {}
// const series = actionsAndEventsToSeries({
// events: [
// ...(targetEntity?.type === 'events' ? [targetEntity as ActionFilter] : []),
// ...(returningEntity?.type === 'events' ? [returningEntity as ActionFilter] : []),
// ],
// actions: [
// ...(targetEntity?.type === 'actions' ? [targetEntity as ActionFilter] : []),
// ...(returningEntity?.type === 'actions' ? [returningEntity as ActionFilter] : []),
// ],
// })
// if (series.length > 0) {
// newCache.series = [...series, ...(cache?.series ? cache.series.slice(series.length) : [])]
// }
// }
if (isLifecycleQuery(query)) {
newCache.series = cache?.series
}
/** store the insight specific filter in commonFilter */
const filterKey = filterKeyForQuery(query)
// exclude properties that shouldn't be shared
const { resultCustomizations, ...commonProperties } = query[filterKey] || {}
newCache.commonFilter = { ...cache?.commonFilter, ...commonProperties }
return newCache
}
const mergeCachedProperties = (query: InsightQueryNode, cache: QueryPropertyCache): InsightQueryNode => {
const mergedQuery = {
...query,
...(cache.dateRange ? { dateRange: cache.dateRange } : {}),
...(cache.properties ? { properties: cache.properties } : {}),
...(cache.samplingFactor ? { samplingFactor: cache.samplingFactor } : {}),
}
// series
if (isInsightQueryWithSeries(mergedQuery)) {
if (cache.series) {
if (isLifecycleQuery(mergedQuery)) {
mergedQuery.series = cleanSeriesMath(cache.series.slice(0, 1), MathAvailability.None)
} else {
const mathAvailability = isTrendsQuery(mergedQuery)
? MathAvailability.All
: isStickinessQuery(mergedQuery)
? MathAvailability.ActorsOnly
: MathAvailability.None
mergedQuery.series = cleanSeriesMath(cache.series, mathAvailability)
}
}
// else if (cache.retentionFilter?.targetEntity || cache.retentionFilter?.returningEntity) {
// mergedQuery.series = [
// ...(cache.retentionFilter.targetEntity
// ? [cache.retentionFilter.targetEntity as EventsNode | ActionsNode]
// : []),
// ...(cache.retentionFilter.returningEntity
// ? [cache.retentionFilter.returningEntity as EventsNode | ActionsNode]
// : []),
// ]
// }
} else if (isRetentionQuery(mergedQuery) && cache.series) {
// mergedQuery.retentionFilter = {
// ...mergedQuery.retentionFilter,
// ...(cache.series.length > 0 ? { targetEntity: cache.series[0] } : {}),
// ...(cache.series.length > 1 ? { returningEntity: cache.series[1] } : {}),
// }
}
// interval
if (isInsightQueryWithSeries(mergedQuery) && cache.interval) {
// Only support real time queries on trends for now
if (!isTrendsQuery(mergedQuery) && cache.interval == 'minute') {
mergedQuery.interval = 'hour'
} else {
mergedQuery.interval = cache.interval
}
}
// breakdown filter
if (isInsightQueryWithBreakdown(mergedQuery) && cache.breakdownFilter) {
mergedQuery.breakdownFilter = cache.breakdownFilter
// If we've changed the query kind, convert multiple breakdowns to a single breakdown
if (isTrendsQuery(cache) && isFunnelsQuery(query)) {
if (cache.breakdownFilter.breakdowns?.length) {
const firstBreakdown = cache.breakdownFilter?.breakdowns?.[0]
mergedQuery.breakdownFilter = {
...cache.breakdownFilter,
breakdowns: undefined,
breakdown: firstBreakdown?.property,
breakdown_type: firstBreakdown?.type,
breakdown_histogram_bin_count: firstBreakdown?.histogram_bin_count,
breakdown_group_type_index: firstBreakdown?.group_type_index,
breakdown_normalize_url: firstBreakdown?.normalize_url,
}
} else {
mergedQuery.breakdownFilter = {
...cache.breakdownFilter,
breakdowns: undefined,
}
}
}
}
// funnel paths filter
if (isPathsQuery(mergedQuery) && cache.funnelPathsFilter) {
mergedQuery.funnelPathsFilter = cache.funnelPathsFilter
}
// insight specific filter
const filterKey = filterKeyForQuery(mergedQuery)
if (cache[filterKey] || cache.commonFilter) {
const node = { kind: mergedQuery.kind, [filterKey]: cache.commonFilter } as unknown as InsightQueryNode
mergedQuery[filterKey] = {
...query[filterKey],
...cache[filterKey],
// TODO: fix an issue where switching between trends and funnels with the option enabled would
// result in an error before uncommenting
// ...(getCompare(node) ? { compare: getCompare(node) } : {}),
...(getShowValuesOnSeries(node) ? { showValuesOnSeries: getShowValuesOnSeries(node) } : {}),
...(getShowPercentStackView(node) ? { showPercentStackView: getShowPercentStackView(node) } : {}),
...(getDisplay(node) ? { display: getDisplay(node) } : {}),
}
}
return mergedQuery
}