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

refactor(frontend): remove eventDefintionsModel #11467

Merged
merged 12 commits into from
Aug 25, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { expectLogic } from 'kea-test-utils'
import { urls } from 'scenes/urls'
import { actionsModel } from '~/models/actionsModel'
import { ActionType, CohortType, PersonProperty, PropertyDefinition } from '~/types'
import { eventDefinitionsModel } from '~/models/eventDefinitionsModel'
import { propertyDefinitionsModel } from '~/models/propertyDefinitionsModel'
import { cohortsModel } from '~/models/cohortsModel'
import { useMocks } from '~/mocks/jest'
Expand Down Expand Up @@ -52,7 +51,6 @@ describe('definitionPopupLogic', () => {

initKeaTests()
actionsModel.mount()
eventDefinitionsModel.mount()
propertyDefinitionsModel.mount()
cohortsModel.mount()
})
Expand Down Expand Up @@ -130,13 +128,13 @@ describe('definitionPopupLogic', () => {
type: TaxonomicFilterGroupType.CustomEvents,
definition: mockEventDefinitions[0],
url: `api/projects/@current/event_definitions/${mockEventDefinitions[0].id}`,
dispatchActions: [eventDefinitionsModel, ['updateEventDefinition']],
dispatchActions: [],
},
{
type: TaxonomicFilterGroupType.Events,
definition: mockEventDefinitions[1],
url: `api/projects/@current/event_definitions/${mockEventDefinitions[1].id}`,
dispatchActions: [eventDefinitionsModel, ['updateEventDefinition']],
dispatchActions: [],
},
{
type: TaxonomicFilterGroupType.PersonProperties,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { getSingularType } from 'lib/components/DefinitionPopup/utils'
import { ActionType, AvailableFeature, CohortType, EventDefinition, PropertyDefinition } from '~/types'
import { urls } from 'scenes/urls'
import api from 'lib/api'
import { eventDefinitionsModel } from '~/models/eventDefinitionsModel'
import { actionsModel } from '~/models/actionsModel'
import { updatePropertyDefinitions } from '~/models/propertyDefinitionsModel'
import { cohortsModel } from '~/models/cohortsModel'
Expand Down Expand Up @@ -94,9 +93,6 @@ export const definitionPopupLogic = kea<definitionPopupLogicType>({
owner: _event.owner?.id ?? null,
verified: !!_event.verified,
})
eventDefinitionsModel
.findMounted()
?.actions.updateEventDefinition(definition as EventDefinition)
} else if (
values.type === TaxonomicFilterGroupType.EventProperties ||
values.type === TaxonomicFilterGroupType.EventFeatureFlags
Expand Down
85 changes: 0 additions & 85 deletions frontend/src/models/eventDefinitionsModel.ts
Original file line number Diff line number Diff line change
@@ -1,85 +0,0 @@
import { kea } from 'kea'
import api from 'lib/api'
import { EventDefinition } from '~/types'
import type { eventDefinitionsModelType } from './eventDefinitionsModelType'
import { updatePropertyDefinitions } from './propertyDefinitionsModel'
import { teamLogic } from 'scenes/teamLogic'

export interface EventDefinitionStorage {
count: number
next: null | string
results: EventDefinition[]
}

export const eventDefinitionsModel = kea<eventDefinitionsModelType>({
path: ['models', 'eventDefinitionsModel'],
actions: () => ({
updateDescription: (id: string, description: string | null, type: string) => ({ id, description, type }),
updateEventDefinition: (eventDefinition: EventDefinition) => ({ eventDefinition }),
}),
loaders: ({ values }) => ({
eventStorage: [
{ results: [], next: null, count: 0 } as EventDefinitionStorage,
{
loadEventDefinitions: async (initial?: boolean) => {
const url = initial
? `api/projects/${teamLogic.values.currentTeamId}/event_definitions/?limit=5000&event_type=event`
: values.eventStorage.next
if (!url) {
throw new Error('Incorrect call to eventDefinitionsModel.loadEventDefinitions')
}
const eventStorage = await api.get(url)
return {
count: eventStorage.count,
results: [...values.eventStorage.results, ...eventStorage.results],
next: eventStorage.next,
}
},
},
],
}),
reducers: () => ({
eventStorage: [
{ results: [], next: null, count: 0 } as EventDefinitionStorage,
{
updateEventDefinition: (state, { eventDefinition }) => ({
count: state.count,
results: state.results.map((p) => (eventDefinition.id === p.id ? eventDefinition : p)),
next: state.next,
}),
},
],
}),
listeners: ({ actions }) => ({
loadEventDefinitionsSuccess: ({ eventStorage }) => {
if (eventStorage.next) {
actions.loadEventDefinitions()
}
},
updateDescription: async ({ id, description, type }) => {
const response = await api.update(`api/projects/@current/${type}_definitions/${id}`, { description })
if (type === 'event') {
actions.updateEventDefinition(response)
} else {
updatePropertyDefinitions([response])
}
},
}),
events: ({ actions }) => ({
afterMount: () => {
actions.loadEventDefinitions(true)
},
}),
selectors: {
loaded: [
// Whether *all* the event definitions are fully loaded
(s) => [s.eventStorage, s.eventStorageLoading],
(eventStorage, eventStorageLoading): boolean => !eventStorageLoading && !eventStorage.next,
],
eventDefinitions: [(s) => [s.eventStorage], (eventStorage): EventDefinition[] => eventStorage.results],
eventNames: [
(s) => [s.eventDefinitions],
(eventDefinitions): string[] => eventDefinitions.map((definition) => definition.name),
],
},
})
2 changes: 0 additions & 2 deletions frontend/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { actionsModel } from './actionsModel'
import { annotationsModel } from './annotationsModel'
import { cohortsModel } from './cohortsModel'
import { dashboardsModel } from './dashboardsModel'
import { eventDefinitionsModel } from './eventDefinitionsModel'
import { personPropertiesModel } from './personPropertiesModel'
import { propertyDefinitionsModel } from './propertyDefinitionsModel'

Expand All @@ -17,7 +16,6 @@ export const models = kea<modelsType>({
annotationsModel,
cohortsModel,
dashboardsModel,
eventDefinitionsModel,
personPropertiesModel,
propertyDefinitionsModel,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { mockEventDefinitions, mockEventPropertyDefinition } from '~/test/mocks'
import { initKeaTests } from '~/test/init'
import { definitionEditLogic } from 'scenes/data-management/definition/definitionEditLogic'
import { expectLogic } from 'kea-test-utils'
import { eventDefinitionsModel } from '~/models/eventDefinitionsModel'
import { eventDefinitionsTableLogic } from 'scenes/data-management/events/eventDefinitionsTableLogic'
import { eventPropertyDefinitionsTableLogic } from 'scenes/data-management/event-properties/eventPropertyDefinitionsTableLogic'
import { router } from 'kea-router'
Expand Down Expand Up @@ -34,7 +33,6 @@ describe('definitionEditLogic', () => {
})
initKeaTests()
await expectLogic(definitionLogic({ id: '1' })).toFinishAllListeners()
eventDefinitionsModel.mount()
eventDefinitionsTableLogic.mount()
eventPropertyDefinitionsTableLogic.mount()
logic = definitionEditLogic({ id: '1', definition: mockEventDefinitions[0] })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { loaders } from 'kea-loaders'
import api from 'lib/api'
import { lemonToast } from 'lib/components/lemonToast'
import { updatePropertyDefinitions } from '~/models/propertyDefinitionsModel'
import { eventDefinitionsModel } from '~/models/eventDefinitionsModel'
import {
definitionLogic,
DefinitionLogicProps,
Expand Down Expand Up @@ -65,9 +64,6 @@ export const definitionEditLogic = kea<definitionEditLogicType>([
verified: !!_event.verified,
},
})
eventDefinitionsModel
.findMounted()
?.actions.updateEventDefinition(definition as EventDefinition)
} else {
// Event Property Definition
const _eventProperty = definition as PropertyDefinition
Expand All @@ -83,7 +79,6 @@ export const definitionEditLogic = kea<definitionEditLogicType>([
}

lemonToast.success(`${capitalizeFirstLetter(values.singular)} saved`)
eventDefinitionsModel.actions.loadEventDefinitions(true) // reload definitions so they are immediately available
// Update table values
if (values.isEvent) {
actions.setLocalEventDefinition(definition)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { actions, afterMount, kea, key, props, path, selectors, reducers, connect } from 'kea'
import { AvailableFeature, Breadcrumb, Definition, EventDefinition, PropertyDefinition } from '~/types'
import { AvailableFeature, Breadcrumb, Definition, PropertyDefinition } from '~/types'
import { loaders } from 'kea-loaders'
import api from 'lib/api'
import { eventDefinitionsModel } from '~/models/eventDefinitionsModel'
import { updatePropertyDefinitions } from '~/models/propertyDefinitionsModel'
import { router } from 'kea-router'
import { urls } from 'scenes/urls'
Expand Down Expand Up @@ -64,9 +63,6 @@ export const definitionLogic = kea<definitionLogicType>([
definition = await api.eventDefinitions.get({
eventDefinitionId: id,
})
eventDefinitionsModel
.findMounted()
?.actions.updateEventDefinition(definition as EventDefinition)
} else {
// Event Property Definition
definition = await api.propertyDefinitions.get({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ const eventTypeOptions: LemonSelectOptions<CombinedEventType> = [
icon: <UnverifiedEvent />,
'data-attr': 'event-type-option-event',
},
{
value: CombinedEventType.EventCustom,
label: 'Events / Custom',
icon: <UnverifiedEvent />,
'data-attr': 'event-type-option-event-custom',
},
{
value: CombinedEventType.EventPostHog,
label: 'Events / PostHog',
icon: <UnverifiedEvent />,
'data-attr': 'event-type-option-event-posthog',
},
]

export const scene: SceneExport = {
Expand Down
24 changes: 0 additions & 24 deletions frontend/src/scenes/insights/EmptyStates/timeout-state.json
Original file line number Diff line number Diff line change
Expand Up @@ -1342,30 +1342,6 @@
"lastDashboardId": 3,
"diveSourceId": null
},
"eventDefinitionsModel": {
"eventStorage": {
"count": 29,
"results": [
{
"id": "017d15d8-126d-0001-508f-78eb18a4c6d5",
"name": "$autocapture",
"owner": {
"distinct_id": "",
"first_name": "",
"email": ""
},
"description": null,
"tags": null,
"volume_30_day": null,
"query_usage_30_day": null,
"updated_at": null,
"updated_by": null
}
],
"next": null
},
"eventStorageLoading": false
},
"personPropertiesModel": {
"personProperties": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from '~/types'
import { PropertyFilters } from 'lib/components/PropertyFilters/PropertyFilters'
import { DownOutlined } from '@ant-design/icons'
import { BareEntity, entityFilterLogic } from '../entityFilterLogic'
import { entityFilterLogic } from '../entityFilterLogic'
import { getEventNamesForAction } from 'lib/utils'
import { SeriesGlyph, SeriesLetter } from 'lib/components/SeriesGlyph'
import './ActionFilterRow.scss'
Expand Down Expand Up @@ -109,7 +109,7 @@ export function ActionFilterRow({
readOnly = false,
renderRow,
}: ActionFilterRowProps): JSX.Element {
const { selectedFilter, entities, entityFilterVisible } = useValues(logic)
const { selectedFilter, entityFilterVisible } = useValues(logic)
const {
updateFilter,
selectFilter,
Expand All @@ -124,7 +124,7 @@ export function ActionFilterRow({

const propertyFiltersVisible = typeof filter.order === 'number' ? entityFilterVisible[filter.order] : false

let entity: BareEntity, name: string | null | undefined, value: PropertyFilterValue
let name: string | null | undefined, value: PropertyFilterValue
const { math, math_property: mathProperty, math_group_type_index: mathGroupTypeIndex } = filter

const onClose = (): void => {
Expand Down Expand Up @@ -161,10 +161,13 @@ export function ActionFilterRow({
if (filter.type === EntityTypes.NEW_ENTITY) {
name = null
value = null
} else if (filter.type === EntityTypes.ACTIONS) {
const action = actions.find((action) => action.id === filter.id)
name = action?.name || filter.name
value = action?.id || filter.id
} else {
entity = (entities[filter.type] as BareEntity[])?.filter((action) => action.id === filter.id)[0] || {}
name = entity.name || filter.name
value = entity.id || filter.id
name = filter.name || String(filter.id)
value = filter.name || filter.id
}

const orLabel = <div className="stateful-badge or width-locked">OR</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { initKeaTests } from '~/test/init'
import filtersJson from './__mocks__/filters.json'
import eventDefinitionsJson from './__mocks__/event_definitions.json'
import { FilterType } from '~/types'
import { actionsModel } from '~/models/actionsModel'
import { useMocks } from '~/mocks/jest'

describe('entityFilterLogic', () => {
Expand All @@ -29,10 +28,6 @@ describe('entityFilterLogic', () => {
})

describe('core assumptions', () => {
it('mounts other logics', async () => {
await expectLogic(logic).toMount([actionsModel])
})

it('localFilters', async () => {
await expectLogic(logic).toMatchValues({
localFilters: toLocalFilters(filtersJson as FilterType),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { kea } from 'kea'
import { actionsModel } from '~/models/actionsModel'
import { EntityTypes, FilterType, Entity, EntityType, ActionFilter, EntityFilter, AnyPropertyFilter } from '~/types'
import type { entityFilterLogicType } from './entityFilterLogicType'
import { eventDefinitionsModel } from '~/models/eventDefinitionsModel'
import { eventUsageLogic, GraphSeriesAddedSource } from 'lib/utils/eventUsageLogic'
import { convertPropertyGroupToProperties } from 'lib/utils'

Expand Down Expand Up @@ -56,7 +54,6 @@ export const entityFilterLogic = kea<entityFilterLogicType>({
path: (key) => ['scenes', 'insights', 'ActionFilter', 'entityFilterLogic', key],
connect: {
logic: [eventUsageLogic],
values: [actionsModel, ['actions']],
},
actions: () => ({
selectFilter: (filter: EntityFilter | ActionFilter | null) => ({ filter }),
Expand Down Expand Up @@ -142,18 +139,6 @@ export const entityFilterLogic = kea<entityFilterLogicType>({
}),

selectors: {
entities: [
(s) => [eventDefinitionsModel.selectors.eventNames, s.actions],
(
events,
actions
): {
[x: string]: ActionFilter[] | BareEntity[]
} => ({
[EntityTypes.ACTIONS]: actions.map((action) => ({ ...action, name: action.name || '' })),
[EntityTypes.EVENTS]: events.map((event) => ({ id: event, name: event })),
}),
],
filters: [(s) => [s.localFilters], (localFilters): FilterType => toFilters(localFilters)],
},

Expand Down