-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Matthew Elwell <[email protected]>
- Loading branch information
1 parent
4348c3e
commit 1a7155b
Showing
10 changed files
with
511 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Res } from 'common/types/responses' | ||
import { Req } from 'common/types/requests' | ||
import { service } from 'common/service' | ||
import Utils from 'common/utils/utils' | ||
|
||
export const conversionEventService = service | ||
.enhanceEndpoints({ addTagTypes: ['ConversionEvent'] }) | ||
.injectEndpoints({ | ||
endpoints: (builder) => ({ | ||
getConversionEvents: builder.query< | ||
Res['conversionEvents'], | ||
Req['getConversionEvents'] | ||
>({ | ||
providesTags: [{ id: 'LIST', type: 'ConversionEvent' }], | ||
query: (query) => { | ||
return { | ||
url: `conversion-event-types/?${Utils.toParam(query)}`, | ||
} | ||
}, | ||
}), | ||
// END OF ENDPOINTS | ||
}), | ||
}) | ||
|
||
export async function getConversionEvents( | ||
store: any, | ||
data: Req['getConversionEvents'], | ||
options?: Parameters< | ||
typeof conversionEventService.endpoints.getConversionEvents.initiate | ||
>[1], | ||
) { | ||
return store.dispatch( | ||
conversionEventService.endpoints.getConversionEvents.initiate( | ||
data, | ||
options, | ||
), | ||
) | ||
} | ||
// END OF FUNCTION_EXPORTS | ||
|
||
export const { | ||
useGetConversionEventsQuery, | ||
// END OF EXPORTS | ||
} = conversionEventService | ||
|
||
/* Usage examples: | ||
const { data, isLoading } = useGetConversionEventsQuery({ id: 2 }, {}) //get hook | ||
const [createConversionEvents, { isLoading, data, isSuccess }] = useCreateConversionEventsMutation() //create hook | ||
conversionEventService.endpoints.getConversionEvents.select({id: 2})(store.getState()) //access data from any function | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { | ||
PagedResponse, | ||
PConfidence, | ||
Res, | ||
ServersideSplitTestResult, | ||
SplitTestResult, | ||
} from 'common/types/responses' | ||
import { Req } from 'common/types/requests' | ||
import { service } from 'common/service' | ||
import Utils from 'common/utils/utils' | ||
import { groupBy, sortBy } from 'lodash' | ||
|
||
export const splitTestService = service | ||
.enhanceEndpoints({ addTagTypes: ['SplitTest'] }) | ||
.injectEndpoints({ | ||
endpoints: (builder) => ({ | ||
getSplitTest: builder.query<Res['splitTest'], Req['getSplitTest']>({ | ||
providesTags: (res, _, q) => [ | ||
{ id: q?.conversion_event_type_id, type: 'SplitTest' }, | ||
], | ||
query: (query: Req['getSplitTest']) => ({ | ||
url: `split-testing/?${Utils.toParam(query)}`, | ||
}), | ||
transformResponse: (res: PagedResponse<ServersideSplitTestResult>) => { | ||
const groupedFeatures = groupBy( | ||
res.results, | ||
(item) => item.feature.id, | ||
) | ||
|
||
const results: SplitTestResult[] = Object.keys(groupedFeatures).map( | ||
(group) => { | ||
const features = groupedFeatures[group] | ||
let minP = Number.MAX_SAFE_INTEGER | ||
let maxP = Number.MIN_SAFE_INTEGER | ||
let maxConversionCount = Number.MIN_SAFE_INTEGER | ||
let maxConversionPercentage = Number.MIN_SAFE_INTEGER | ||
let minConversion = Number.MAX_SAFE_INTEGER | ||
let maxConversionPValue = 0 | ||
const results = sortBy( | ||
features.map((v) => { | ||
if (v.pvalue < minP) { | ||
minP = v.pvalue | ||
} | ||
if (v.pvalue > maxP) { | ||
maxP = v.pvalue | ||
} | ||
const conversion = v.conversion_count | ||
? Math.round( | ||
(v.conversion_count / v.evaluation_count) * 100, | ||
) | ||
: 0 | ||
if (conversion > maxConversionPercentage) { | ||
maxConversionCount = v.conversion_count | ||
maxConversionPercentage = conversion | ||
maxConversionPValue = v.pvalue | ||
} | ||
if (conversion < minConversion) { | ||
minConversion = conversion | ||
} | ||
|
||
return { | ||
confidence: Utils.convertToPConfidence(v.pvalue), | ||
conversion_count: v.conversion_count, | ||
conversion_percentage: conversion, | ||
evaluation_count: v.evaluation_count, | ||
pvalue: v.pvalue, | ||
value_data: v.value_data, | ||
} as SplitTestResult['results'][number] | ||
}), | ||
'conversion_count', | ||
) | ||
return { | ||
conversion_variance: maxConversionPercentage - minConversion, | ||
feature: features[0].feature, | ||
max_conversion_count: maxConversionCount, | ||
max_conversion_percentage: maxConversionPercentage, | ||
max_conversion_pvalue: maxConversionPValue, | ||
results: sortBy(results, (v) => -v.conversion_count), | ||
} | ||
}, | ||
) | ||
return { | ||
...res, | ||
results, | ||
} | ||
}, | ||
}), | ||
// END OF ENDPOINTS | ||
}), | ||
}) | ||
|
||
export async function getSplitTest( | ||
store: any, | ||
data: Req['getSplitTest'], | ||
options?: Parameters< | ||
typeof splitTestService.endpoints.getSplitTest.initiate | ||
>[1], | ||
) { | ||
return store.dispatch( | ||
splitTestService.endpoints.getSplitTest.initiate(data, options), | ||
) | ||
} | ||
// END OF FUNCTION_EXPORTS | ||
|
||
export const { | ||
useGetSplitTestQuery, | ||
// END OF EXPORTS | ||
} = splitTestService | ||
|
||
/* Usage examples: | ||
const { data, isLoading } = useGetSplitTestQuery({ id: 2 }, {}) //get hook | ||
const [createSplitTest, { isLoading, data, isSuccess }] = useCreateSplitTestMutation() //create hook | ||
splitTestService.endpoints.getSplitTest.select({id: 2})(store.getState()) //access data from any function | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { FC } from 'react' | ||
import cn from 'classnames' | ||
import Utils from 'common/utils/utils' | ||
import Format from 'common/utils/format' | ||
|
||
type ConfidenceType = { | ||
pValue: number | ||
} | ||
|
||
const Confidence: FC<ConfidenceType> = ({ pValue }) => { | ||
const confidence = Utils.convertToPConfidence(pValue) | ||
const confidenceDisplay = Format.enumeration.get(confidence) | ||
|
||
const confidenceClass = cn({ | ||
'text-danger': confidence === 'VERY_LOW' || confidence === 'LOW', | ||
'text-muted': !['VERY_LOW', 'LOW', 'HIGH', 'VERY_HIGH'].includes( | ||
confidence, | ||
), | ||
'text-success': confidence === 'HIGH' || confidence === 'VERY_HIGH', | ||
}) | ||
|
||
return <div className={confidenceClass}>{confidenceDisplay}</div> | ||
} | ||
|
||
export default Confidence |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import React, { FC, useEffect, useState } from 'react' | ||
import { useGetConversionEventsQuery } from 'common/services/useConversionEvent' | ||
import useSearchThrottle from 'common/useSearchThrottle' | ||
import { ConversionEvent } from 'common/types/responses' | ||
import ProjectStore from 'common/stores/project-store' | ||
|
||
type ConversionEventSelectType = { | ||
onChange: (v: number) => void | ||
environmentId: string | ||
} | ||
|
||
const ConversionEventSelect: FC<ConversionEventSelectType> = ({ | ||
environmentId, | ||
onChange, | ||
}) => { | ||
const { search, searchInput, setSearchInput } = useSearchThrottle('') | ||
const { data } = useGetConversionEventsQuery({ | ||
environment_id: ProjectStore.getEnvironmentIdFromKey(environmentId), | ||
q: `${search}`, | ||
}) | ||
const [selected, setSelected] = useState<ConversionEvent | null>(null) | ||
|
||
return ( | ||
<div> | ||
<Select | ||
value={ | ||
selected | ||
? { label: selected.name, value: selected.id } | ||
: { | ||
label: 'Select an Event', | ||
value: '', | ||
} | ||
} | ||
inputValue={searchInput} | ||
onInputChange={setSearchInput} | ||
options={data?.results?.map((result) => ({ | ||
...result, | ||
label: result.name, | ||
value: result.id, | ||
}))} | ||
onChange={(value: ConversionEvent) => { | ||
setSelected(value) | ||
onChange(value.id) | ||
}} | ||
/> | ||
</div> | ||
) | ||
} | ||
|
||
export default ConversionEventSelect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.