Skip to content

Commit

Permalink
feat(web-analytics): Disable world map when geoip app is not enabled (#…
Browse files Browse the repository at this point in the history
…18745)

Disable world map when geoip app is not enabled
  • Loading branch information
robbie-c authored Nov 20, 2023
1 parent 3cb88c0 commit 6a380b8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 47 deletions.
18 changes: 18 additions & 0 deletions frontend/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import { getCurrentExporterData } from '~/exporter/exporterViewLogic'
import { encodeParams } from 'kea-router'

export const ACTIVITY_PAGE_SIZE = 20
const PAGINATION_DEFAULT_MAX_PAGES = 10

export interface PaginatedResponse<T> {
results: T[]
Expand Down Expand Up @@ -1863,6 +1864,23 @@ const api = {
}
return response
},

async loadPaginatedResults(
url: string | null,
maxIterations: number = PAGINATION_DEFAULT_MAX_PAGES
): Promise<any[]> {
let results: any[] = []
for (let i = 0; i <= maxIterations; ++i) {
if (!url) {
break
}

const { results: partialResults, next } = await api.get(url)
results = results.concat(partialResults)
url = next
}
return results
},
}

function reportError(method: string, url: string, response: Response, startTime: number): void {
Expand Down
22 changes: 2 additions & 20 deletions frontend/src/scenes/pipeline/transformationsLogic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const pipelineTransformationsLogic = kea<pipelineTransformationsLogicType
{} as Record<number, PluginType>,
{
loadPlugins: async () => {
const results: PluginType[] = await loadPaginatedResults(
const results: PluginType[] = await api.loadPaginatedResults(
`api/organizations/@current/pipeline_transformations`
)
const plugins: Record<number, PluginType> = {}
Expand All @@ -60,7 +60,7 @@ export const pipelineTransformationsLogic = kea<pipelineTransformationsLogicType
{
loadPluginConfigs: async () => {
const pluginConfigs: Record<number, PluginConfigTypeNew> = {}
const results = await loadPaginatedResults(
const results = await api.loadPaginatedResults(
`api/projects/${values.currentTeamId}/pipeline_transformations_configs`
)

Expand Down Expand Up @@ -185,21 +185,3 @@ export const pipelineTransformationsLogic = kea<pipelineTransformationsLogicType
actions.loadPluginConfigs()
}),
])

const PAGINATION_DEFAULT_MAX_PAGES = 10
async function loadPaginatedResults(
url: string | null,
maxIterations: number = PAGINATION_DEFAULT_MAX_PAGES
): Promise<any[]> {
let results: any[] = []
for (let i = 0; i <= maxIterations; ++i) {
if (!url) {
break
}

const { results: partialResults, next } = await api.get(url)
results = results.concat(partialResults)
url = next
}
return results
}
23 changes: 2 additions & 21 deletions frontend/src/scenes/plugins/pluginsLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ export interface PluginSelectionType {
url?: string
}

const PAGINATION_DEFAULT_MAX_PAGES = 10

function capturePluginEvent(event: string, plugin: PluginType, type?: PluginInstallationType): void {
posthog.capture(event, {
plugin_name: plugin.name,
Expand All @@ -40,23 +38,6 @@ function capturePluginEvent(event: string, plugin: PluginType, type?: PluginInst
})
}

async function loadPaginatedResults(
url: string | null,
maxIterations: number = PAGINATION_DEFAULT_MAX_PAGES
): Promise<any[]> {
let results: any[] = []
for (let i = 0; i <= maxIterations; ++i) {
if (!url) {
break
}

const { results: partialResults, next } = await api.get(url)
results = results.concat(partialResults)
url = next
}
return results
}

export const pluginsLogic = kea<pluginsLogicType>([
path(['scenes', 'plugins', 'pluginsLogic']),
connect(frontendAppsLogic),
Expand Down Expand Up @@ -102,7 +83,7 @@ export const pluginsLogic = kea<pluginsLogicType>([
{} as Record<number, PluginType>,
{
loadPlugins: async () => {
const results: PluginType[] = await loadPaginatedResults('api/organizations/@current/plugins')
const results: PluginType[] = await api.loadPaginatedResults('api/organizations/@current/plugins')
const plugins: Record<string, PluginType> = {}
for (const plugin of results) {
plugins[plugin.id] = plugin
Expand Down Expand Up @@ -160,7 +141,7 @@ export const pluginsLogic = kea<pluginsLogicType>([
{
loadPluginConfigs: async () => {
const pluginConfigs: Record<string, PluginConfigType> = {}
const results: PluginConfigType[] = await loadPaginatedResults('api/plugin_config')
const results: PluginConfigType[] = await api.loadPaginatedResults('api/plugin_config')

for (const pluginConfig of results) {
pluginConfigs[pluginConfig.plugin] = { ...pluginConfig }
Expand Down
38 changes: 32 additions & 6 deletions frontend/src/scenes/web-analytics/webAnalyticsLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -661,12 +661,38 @@ export const webAnalyticsLogic = kea<webAnalyticsLogicType>([
shouldShowGeographyTile: {
_default: null as boolean | null,
loadShouldShowGeographyTile: async (): Promise<boolean> => {
const response = await api.propertyDefinitions.list({
event_names: ['$pageview'],
properties: ['$geoip_country_code'],
})
const countryCodeDefinition = response.results.find((r) => r.name === '$geoip_country_code')
return !!countryCodeDefinition && !isDefinitionStale(countryCodeDefinition)
const [propertiesResponse, pluginsResponse, pluginsConfigResponse] = await Promise.allSettled([
api.propertyDefinitions.list({
event_names: ['$pageview'],
properties: ['$geoip_country_code'],
}),
api.loadPaginatedResults('api/organizations/@current/plugins'),
api.loadPaginatedResults('api/plugin_config'),
])

const hasNonStaleCountryCodeDefinition =
propertiesResponse.status === 'fulfilled' &&
propertiesResponse.value.results.some(
(property) => property.name === '$geoip_country_code' && !isDefinitionStale(property)
)

if (!hasNonStaleCountryCodeDefinition) {
return false
}

const geoIpPlugin =
pluginsResponse.status === 'fulfilled' &&
pluginsResponse.value.find(
(plugin) => plugin.url === 'https://www.npmjs.com/package/@posthog/geoip-plugin'
)
const geoIpPluginId = geoIpPlugin ? geoIpPlugin.id : undefined

const geoIpPluginConfig =
isNotNil(geoIpPluginId) &&
pluginsConfigResponse.status === 'fulfilled' &&
pluginsConfigResponse.value.find((plugin) => plugin.id === geoIpPluginId)

return !!geoIpPluginConfig && geoIpPluginConfig.enabled
},
},
})),
Expand Down

0 comments on commit 6a380b8

Please sign in to comment.