Skip to content

Commit

Permalink
Add other queries and share code between query runners
Browse files Browse the repository at this point in the history
  • Loading branch information
robbie-c committed Sep 27, 2023
1 parent accaf3d commit 789c19d
Show file tree
Hide file tree
Showing 18 changed files with 621 additions and 124 deletions.
17 changes: 12 additions & 5 deletions frontend/src/queries/nodes/DataTable/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import {
isHogQLQuery,
isPersonsNode,
isWebTopSourcesQuery,
isWebTopClicksQuery,
isWebTopPagesQuery,
taxonomicFilterToHogQl,
} from '~/queries/utils'
import { PersonPropertyFilters } from '~/queries/nodes/PersonsNode/PersonPropertyFilters'
Expand Down Expand Up @@ -123,7 +125,10 @@ export function DataTable({ uniqueKey, query, setQuery, context, cachedResults }

const actionsColumnShown = showActions && isEventsQuery(query.source) && columnsInResponse?.includes('*')
const columnsInLemonTable =
isHogQLQuery(query.source) || isWebTopSourcesQuery(query.source)
isHogQLQuery(query.source) ||
isWebTopSourcesQuery(query.source) ||
isWebTopClicksQuery(query.source) ||
isWebTopPagesQuery(query.source)
? columnsInResponse ?? columnsInQuery
: columnsInQuery

Expand All @@ -145,7 +150,9 @@ export function DataTable({ uniqueKey, query, setQuery, context, cachedResults }
if (
isEventsQuery(query.source) ||
isHogQLQuery(query.source) ||
isWebTopSourcesQuery(query.source)
isWebTopSourcesQuery(query.source) ||
isWebTopClicksQuery(query.source) ||
isWebTopPagesQuery(query.source)
) {
return renderColumn(key, result[index], result, query, setQuery, context)
}
Expand Down Expand Up @@ -506,9 +513,9 @@ export function DataTable({ uniqueKey, query, setQuery, context, cachedResults }
}
footer={
canLoadNextData &&
((response as any).results.length > 0 || !responseLoading) && (
<LoadNext query={query.source} />
)
((response as any).results.length > 0 ||
(response as any).result.length > 0 ||
!responseLoading) && <LoadNext query={query.source} />
}
/>
)}
Expand Down
10 changes: 8 additions & 2 deletions frontend/src/queries/nodes/DataTable/dataTableLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,15 @@ export const dataTableLogic = kea<dataTableLogicType>([
}))
}

return response && 'results' in response && Array.isArray(response.results)
? response.results.map((result: any) => ({ result })) ?? null
const results = !response
? null
: 'results' in response && Array.isArray(response.results)
? response.results
: 'result' in response && Array.isArray(response.result)
? response.result
: null

return results ? results.map((result: any) => ({ result })) ?? null : null
},
],
queryWithDefaults: [
Expand Down
155 changes: 153 additions & 2 deletions frontend/src/queries/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@
},
{
"$ref": "#/definitions/WebTopSourcesQuery"
},
{
"$ref": "#/definitions/WebTopClicksQuery"
},
{
"$ref": "#/definitions/WebTopPagesQuery"
}
]
},
Expand Down Expand Up @@ -395,6 +401,12 @@
},
{
"$ref": "#/definitions/WebTopSourcesQuery"
},
{
"$ref": "#/definitions/WebTopClicksQuery"
},
{
"$ref": "#/definitions/WebTopPagesQuery"
}
],
"description": "Source of the events"
Expand Down Expand Up @@ -2276,8 +2288,113 @@
"required": ["result"],
"type": "object"
},
"WebAnalyticsFilters": {
"WebAnalyticsFilters": {},
"WebTopClicksQuery": {
"additionalProperties": false,
"properties": {
"dateRange": {
"$ref": "#/definitions/DateRange"
},
"filters": {
"$ref": "#/definitions/WebAnalyticsFilters"
},
"kind": {
"const": "WebTopClicksQuery",
"type": "string"
},
"response": {
"$ref": "#/definitions/WebTopClicksQueryResponse"
}
},
"required": ["kind", "filters"],
"type": "object"
},
"WebTopClicksQueryResponse": {
"additionalProperties": false,
"properties": {
"columns": {
"items": {},
"type": "array"
},
"is_cached": {
"type": "boolean"
},
"last_refresh": {
"type": "string"
},
"next_allowed_client_refresh": {
"type": "string"
},
"result": {
"items": {},
"type": "array"
},
"timings": {
"items": {
"$ref": "#/definitions/QueryTiming"
},
"type": "array"
},
"types": {
"items": {},
"type": "array"
}
},
"required": ["result"],
"type": "object"
},
"WebTopPagesQuery": {
"additionalProperties": false,
"properties": {
"dateRange": {
"$ref": "#/definitions/DateRange"
},
"filters": {
"$ref": "#/definitions/WebAnalyticsFilters"
},
"kind": {
"const": "WebTopPagesQuery",
"type": "string"
},
"response": {
"$ref": "#/definitions/WebTopPagesQueryResponse"
}
},
"required": ["kind", "filters"],
"type": "object"
},
"WebTopPagesQueryResponse": {
"additionalProperties": false,
"properties": {
"columns": {
"items": {},
"type": "array"
},
"is_cached": {
"type": "boolean"
},
"last_refresh": {
"type": "string"
},
"next_allowed_client_refresh": {
"type": "string"
},
"result": {
"items": {},
"type": "array"
},
"timings": {
"items": {
"$ref": "#/definitions/QueryTiming"
},
"type": "array"
},
"types": {
"items": {},
"type": "array"
}
},
"required": ["result"],
"type": "object"
},
"WebTopSourcesQuery": {
Expand All @@ -2294,11 +2411,45 @@
"type": "string"
},
"response": {
"$ref": "#/definitions/HogQLQueryResponse"
"$ref": "#/definitions/WebTopSourcesQueryResponse"
}
},
"required": ["kind", "filters"],
"type": "object"
},
"WebTopSourcesQueryResponse": {
"additionalProperties": false,
"properties": {
"columns": {
"items": {},
"type": "array"
},
"is_cached": {
"type": "boolean"
},
"last_refresh": {
"type": "string"
},
"next_allowed_client_refresh": {
"type": "string"
},
"result": {
"items": {},
"type": "array"
},
"timings": {
"items": {
"$ref": "#/definitions/QueryTiming"
},
"type": "array"
},
"types": {
"items": {},
"type": "array"
}
},
"required": ["result"],
"type": "object"
}
}
}
46 changes: 42 additions & 4 deletions frontend/src/queries/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export enum NodeKind {

// Web analytics queries
WebTopSourcesQuery = 'WebTopSourcesQuery',
WebTopPagesQuery = 'WebTopPagesQuery',
WebTopClicksQuery = 'WebTopClicksQuery',

// Time to see data
TimeToSeeDataSessionsQuery = 'TimeToSeeDataSessionsQuery',
Expand All @@ -79,6 +81,8 @@ export type AnyDataNode =
| HogQLMetadata
| TimeToSeeDataSessionsQuery
| WebTopSourcesQuery
| WebTopClicksQuery
| WebTopPagesQuery

export type QuerySchema =
// Data nodes (see utils.ts)
Expand Down Expand Up @@ -281,7 +285,15 @@ export type HasPropertiesNode = EventsNode | EventsQuery | PersonsNode
export interface DataTableNode extends Node, DataTableNodeViewProps {
kind: NodeKind.DataTableNode
/** Source of the events */
source: EventsNode | EventsQuery | PersonsNode | HogQLQuery | TimeToSeeDataSessionsQuery | WebTopSourcesQuery
source:
| EventsNode
| EventsQuery
| PersonsNode
| HogQLQuery
| TimeToSeeDataSessionsQuery
| WebTopSourcesQuery
| WebTopClicksQuery
| WebTopPagesQuery

/** Columns shown in the table, unless the `source` provides them. */
columns?: HogQLExpression[]
Expand Down Expand Up @@ -487,7 +499,7 @@ export interface LifecycleQuery extends InsightsQueryBase {
response?: LifecycleQueryResponse
}

export interface WebAnalyticsFilters {}
export type WebAnalyticsFilters = any

export interface WebAnalyticsQueryBase {
dateRange?: DateRange
Expand All @@ -496,9 +508,35 @@ export interface WebAnalyticsQueryBase {
export interface WebTopSourcesQuery extends WebAnalyticsQueryBase {
kind: NodeKind.WebTopSourcesQuery
filters: WebAnalyticsFilters
response?: HogQLQueryResponse
response?: WebTopSourcesQueryResponse
}
export interface WebTopSourcesQueryResponse extends QueryResponse {
result: unknown[]
types?: unknown[]
columns?: unknown[]
}

export interface WebTopClicksQuery extends WebAnalyticsQueryBase {
kind: NodeKind.WebTopClicksQuery
filters: WebAnalyticsFilters
response?: WebTopClicksQueryResponse
}
export interface WebTopClicksQueryResponse extends QueryResponse {
result: unknown[]
types?: unknown[]
columns?: unknown[]
}

export interface WebTopPagesQuery extends WebAnalyticsQueryBase {
kind: NodeKind.WebTopPagesQuery
filters: WebAnalyticsFilters
response?: WebTopPagesQueryResponse
}
export interface WebTopPagesQueryResponse extends QueryResponse {
result: unknown[]
types?: unknown[]
columns?: unknown[]
}
export interface WebTopSourcesQueryResponse extends QueryResponse {}

export type InsightQueryNode =
| TrendsQuery
Expand Down
10 changes: 10 additions & 0 deletions frontend/src/queries/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import {
DatabaseSchemaQuery,
SavedInsightNode,
WebTopSourcesQuery,
WebTopClicksQuery,
WebTopPagesQuery,
} from '~/queries/schema'
import { TaxonomicFilterGroupType, TaxonomicFilterValue } from 'lib/components/TaxonomicFilter/types'
import { dayjs } from 'lib/dayjs'
Expand Down Expand Up @@ -95,6 +97,14 @@ export function isWebTopSourcesQuery(node?: Node | null): node is WebTopSourcesQ
return node?.kind === NodeKind.WebTopSourcesQuery
}

export function isWebTopClicksQuery(node?: Node | null): node is WebTopClicksQuery {
return node?.kind === NodeKind.WebTopClicksQuery
}

export function isWebTopPagesQuery(node?: Node | null): node is WebTopPagesQuery {
return node?.kind === NodeKind.WebTopPagesQuery
}

export function containsHogQLQuery(node?: Node | null): boolean {
if (!node) {
return false
Expand Down
12 changes: 12 additions & 0 deletions frontend/src/scenes/saved-insights/SavedInsights.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,18 @@ export const QUERY_TYPES_METADATA: Record<NodeKind, InsightTypeMetadata> = {
icon: InsightsTrendsIcon,
inMenu: true,
},
[NodeKind.WebTopPagesQuery]: {
name: 'Top Pages',
description: 'View top pages for a website',
icon: InsightsTrendsIcon,
inMenu: true,
},
[NodeKind.WebTopClicksQuery]: {
name: 'Top Clicks',
description: 'View top clicks for a website',
icon: InsightsTrendsIcon,
inMenu: true,
},
}

export const INSIGHT_TYPE_OPTIONS: LemonSelectOptions<string> = [
Expand Down
Loading

0 comments on commit 789c19d

Please sign in to comment.