Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

show served / not served feature flags

Blue Mouseef6b73196a07
diff --git a/backend/apps/cloud/src/feature-flag/feature-flag.controller.ts b/backend/apps/cloud/src/feature-flag/feature-flag.controller.ts
index f0aa9ff81..fb3050e5a 100644
--- a/backend/apps/cloud/src/feature-flag/feature-flag.controller.ts
+++ b/backend/apps/cloud/src/feature-flag/feature-flag.controller.ts
@@ -569,9 +569,10 @@ export class FeatureFlagController {
@Query('timezone') timezone?: string,
@Query('take', new ParseIntPipe({ optional: true })) take?: number,
@Query('skip', new ParseIntPipe({ optional: true })) skip?: number,
+ @Query('result') result?: 'true' | 'false',
) {
this.logger.log(
- { userId, id, period, from, to, take, skip },
+ { userId, id, period, from, to, take, skip, result },
'GET /feature-flag/:id/profiles',
)
@@ -598,6 +599,14 @@ export class FeatureFlagController {
const safeTake = Math.min(take || 15, 50)
const safeSkip = skip || 0
+ // Build HAVING clause for result filter
+ let resultHavingClause = ''
+ if (result === 'true') {
+ resultHavingClause = 'HAVING lastResult = 1'
+ } else if (result === 'false') {
+ resultHavingClause = 'HAVING lastResult = 0'
+ }
+
// Query to get profiles with their most recent evaluation
const profilesQuery = `
SELECT
@@ -611,17 +620,24 @@ export class FeatureFlagController {
AND flagId = {flagId:String}
AND created BETWEEN {groupFrom:String} AND {groupTo:String}
GROUP BY profileId
+ ${resultHavingClause}
ORDER BY lastEvaluated DESC
LIMIT {take:UInt32} OFFSET {skip:UInt32}
`
const countQuery = `
- SELECT uniqExact(profileId) as total
- FROM feature_flag_evaluations
- WHERE
- pid = {pid:FixedString(12)}
- AND flagId = {flagId:String}
- AND created BETWEEN {groupFrom:String} AND {groupTo:String}
+ SELECT count() as total FROM (
+ SELECT
+ profileId,
+ argMax(result, created) as lastResult
+ FROM feature_flag_evaluations
+ WHERE
+ pid = {pid:FixedString(12)}
+ AND flagId = {flagId:String}
+ AND created BETWEEN {groupFrom:String} AND {groupTo:String}
+ GROUP BY profileId
+ ${resultHavingClause}
+ )
`
const queryParams = {
diff --git a/web/app/api/index.ts b/web/app/api/index.ts
index 645f25425..6e2360558 100644
--- a/web/app/api/index.ts
+++ b/web/app/api/index.ts
@@ -1358,6 +1358,8 @@ export interface FeatureFlagProfile {
export const DEFAULT_FEATURE_FLAG_PROFILES_TAKE = 15
+export type FeatureFlagResultFilter = 'all' | 'true' | 'false'
+
export const getFeatureFlagProfiles = (
flagId: string,
period: string,
@@ -1366,10 +1368,19 @@ export const getFeatureFlagProfiles = (
timezone?: string,
take: number = DEFAULT_FEATURE_FLAG_PROFILES_TAKE,
skip: number = 0,
+ resultFilter: FeatureFlagResultFilter = 'all',
) =>
api
.get(`/feature-flag/${flagId}/profiles`, {
- params: { period, from, to, timezone, take, skip },
+ params: {
+ period,
+ from,
+ to,
+ timezone,
+ take,
+ skip,
+ result: resultFilter === 'all' ? undefined : resultFilter,
+ },
})
.then((response): { profiles: FeatureFlagProfile[]; total: number } => response.data)
.catch((error) => {
diff --git a/web/app/pages/Project/FeatureFlags/View/FeatureFlagsView.tsx b/web/app/pages/Project/FeatureFlags/View/FeatureFlagsView.tsx
index 98b94eca4..2728cb2ed 100644
--- a/web/app/pages/Project/FeatureFlags/View/FeatureFlagsView.tsx
+++ b/web/app/pages/Project/FeatureFlags/View/FeatureFlagsView.tsx
@@ -45,6 +45,7 @@ import Loader from '~/ui/Loader'
import LoadingBar from '~/ui/LoadingBar'
import Modal from '~/ui/Modal'
import Pagination from '~/ui/Pagination'
+import { Switch } from '~/ui/Switch'
import { Text } from '~/ui/Text'
import { nFormatter } from '~/utils/generic'
import { getProfileDisplayName, ProfileAvatar } from '~/utils/profileAvatars'
@@ -83,6 +84,7 @@ const FeatureFlagProfileRow = ({ profile, timeFormat }: FeatureFlagProfileRowPro
}, [profile.lastEvaluated, language, timeFormat])
const params = new URLSearchParams(location.search)
+ params.set('tab', 'profiles')
params.set('profileId', profile.profileId)
return (
@@ -135,11 +137,13 @@ interface FeatureFlagRowProps {
profiles: FeatureFlagProfile[]
profilesLoading: boolean
canLoadMoreProfiles: boolean
+ resultFilter: boolean
onDelete: (id: string) => void
onEdit: (id: string) => void
onToggle: (id: string, enabled: boolean) => void
onToggleExpand: (id: string) => void
onLoadMoreProfiles: (id: string) => void
+ onResultFilterChange: (id: string, filter: boolean) => void
timeFormat: '12-hour' | '24-hour'
}
@@ -151,11 +155,13 @@ const FeatureFlagRow = ({
profiles,
profilesLoading,
canLoadMoreProfiles,
+ resultFilter,
onDelete,
onEdit,
onToggle,
onToggleExpand,
onLoadMoreProfiles,
+ onResultFilterChange,
timeFormat,
}: FeatureFlagRowProps) => {
const { t } = useTranslation()
@@ -339,6 +345,12 @@ const FeatureFlagRow = ({
{/* Expanded profiles section */}
{isExpanded ? (
<div className='border-t border-gray-200 px-4 py-4 sm:px-6 dark:border-slate-700'>
+ {/* Result filter toggle */}
+ <div className='mb-3 flex items-center gap-2'>
+ <Switch checked={resultFilter} onChange={(checked) => onResultFilterChange(flag.id, checked)} />
+ <span className='text-xs text-gray-500 dark:text-gray-400'>{t('featureFlags.served')}</span>
+ </div>
+
{profilesLoading && profiles.length === 0 ? (
<div className='flex h-[120px] items-center justify-center'>
<Spin className='size-8' />
@@ -431,6 +443,7 @@ const FeatureFlagsView = ({ period, from = '', to = '', timezone }: FeatureFlags
const [flagProfiles, setFlagProfiles] = useState<Record<string, FeatureFlagProfile[]>>({})
const [flagProfilesTotal, setFlagProfilesTotal] = useState<Record<string, number>>({})
const [profilesLoading, setProfilesLoading] = useState<Record<string, boolean>>({})
+ const [flagResultFilters, setFlagResultFilters] = useState<Record<string, boolean>>({})
// Modal state
const [isModalOpen, setIsModalOpen] = useState(false)
@@ -499,9 +512,11 @@ const FeatureFlagsView = ({ period, from = '', to = '', timezone }: FeatureFlags
}
}
- const loadFlagProfiles = async (flagId: string, append = false) => {
- const currentProfiles = flagProfiles[flagId] || []
+ const loadFlagProfiles = async (flagId: string, append = false, resultFilter?: boolean) => {
+ const currentProfiles = append ? flagProfiles[flagId] || [] : []
const skip = append ? currentProfiles.length : 0
+ const filter = resultFilter ?? flagResultFilters[flagId] ?? true
+ const apiFilter = filter ? 'true' : 'false'
setProfilesLoading((prev) => ({ ...prev, [flagId]: true }))
try {
@@ -513,6 +528,7 @@ const FeatureFlagsView = ({ period, from = '', to = '', timezone }: FeatureFlags
timezone,
DEFAULT_FEATURE_FLAG_PROFILES_TAKE,
skip,
+ apiFilter,
)
if (isMountedRef.current) {
setFlagProfiles((prev) => ({
@@ -534,7 +550,7 @@ const FeatureFlagsView = ({ period, from = '', to = '', timezone }: FeatureFlags
}
}
- const handleToggleExpand = (flagId: string) => {
+ const handleToggleExpand = async (flagId: string) => {
if (expandedFlagId === flagId) {
// Collapse if already expanded
setExpandedFlagId(null)
@@ -542,7 +558,66 @@ const FeatureFlagsView = ({ period, from = '', to = '', timezone }: FeatureFlags
// Expand and load profiles if not already loaded
setExpandedFlagId(flagId)
if (!flagProfiles[flagId] && !profilesLoading[flagId]) {
- loadFlagProfiles(flagId)
+ // First try loading with true filter (served)
+ setProfilesLoading((prev) => ({ ...prev, [flagId]: true }))
+ try {
+ const trueResult = await getFeatureFlagProfiles(
+ flagId,
+ period,
+ from,
+ to,
+ timezone,
+ DEFAULT_FEATURE_FLAG_PROFILES_TAKE,
+ 0,
+ 'true',
+ )
+
+ if (trueResult.total > 0) {
+ // Has true results, use them
+ if (isMountedRef.current) {
+ setFlagProfiles((prev) => ({ ...prev, [flagId]: trueResult.profiles }))
+ setFlagProfilesTotal((prev) => ({ ...prev, [flagId]: trueResult.total }))
+ setFlagResultFilters((prev) => ({ ...prev, [flagId]: true }))
+ }
+ } else {
+ // No true results, try false
+ const falseResult = await getFeatureFlagProfiles(
+ flagId,
+ period,
+ from,
+ to,
+ timezone,
+ DEFAULT_FEATURE_FLAG_PROFILES_TAKE,
+ 0,
+ 'false',
+ )
+
+ if (isMountedRef.current) {
+ if (falseResult.total > 0) {
+ // Has false results, use them and set filter to false
+ setFlagProfiles((prev) => ({ ...prev, [flagId]: falseResult.profiles }))
+ setFlagProfilesTotal((prev) => ({ ...prev, [flagId]: falseResult.total }))
+ setFlagResultFilters((prev) => ({ ...prev, [flagId]: false }))
+ } else {
+ // No results at all, default to true filter with empty
+ setFlagProfiles((prev) => ({ ...prev, [flagId]: [] }))
+ setFlagProfilesTotal((prev) => ({ ...prev, [flagId]: 0 }))
+ setFlagResultFilters((prev) => ({ ...prev, [flagId]: true }))
+ }
+ }
+ }
+ } catch (err) {
+ console.error('Failed to load flag profiles:', err)
+ if (isMountedRef.current) {
+ setFlagProfiles((prev) => ({ ...prev, [flagId]: [] }))
+ setFlagProfilesTotal((prev) => ({ ...prev, [flagId]: 0 }))
+ setFlagResultFilters((prev) => ({ ...prev, [flagId]: true }))
+ }
+ } finally {
+ if (isMountedRef.current) {
+ setProfilesLoading((prev) => ({ ...prev, [flagId]: false }))
+ }
+ }
}
}
}
@@ -551,6 +626,13 @@ const FeatureFlagsView = ({ period, from = '', to = '', timezone }: FeatureFlags
loadFlagProfiles(flagId, true)
}
+ const handleResultFilterChange = (flagId: string, filter: boolean) => {
+ setFlagResultFilters((prev) => ({ ...prev, [flagId]: filter }))
+ // Reset profiles and reload with new filter
+ setFlagProfiles((prev) => ({ ...prev, [flagId]: [] }))
+ loadFlagProfiles(flagId, false, filter)
+ }
+
useEffect(() => {
loadFlags(DEFAULT_FEATURE_FLAGS_TAKE, (page - 1) * DEFAULT_FEATURE_FLAGS_TAKE)
// eslint-disable-next-line react-hooks/exhaustive-deps
@@ -733,11 +815,13 @@ const FeatureFlagsView = ({ period, from = '', to = '', timezone }: FeatureFlags
profiles={profiles}
profilesLoading={profilesLoading[flag.id] || false}
canLoadMoreProfiles={canLoadMore}
+ resultFilter={flagResultFilters[flag.id] ?? true}
onDelete={handleDeleteFlag}
onEdit={handleEditFlag}
onToggle={handleToggleFlag}
onToggleExpand={handleToggleExpand}
onLoadMoreProfiles={handleLoadMoreProfiles}
+ onResultFilterChange={handleResultFilterChange}
timeFormat={timeFormat}
/>
)
diff --git a/web/app/ui/Switch.tsx b/web/app/ui/Switch.tsx
new file mode 100644
index 000000000..6eac5206e
--- /dev/null
+++ b/web/app/ui/Switch.tsx
@@ -0,0 +1,63 @@
+import cx from 'clsx'
+
+interface SwitchProps {
+ checked: boolean
+ onChange: (checked: boolean) => void
+ disabled?: boolean
+ className?: string
+ label?: string
+ id?: string
+}
+
+export const Switch = ({ checked, onChange, disabled = false, className, label, id }: SwitchProps) => {
+ const switchId = id || `switch-${Math.random().toString(36).substr(2, 9)}`
+
+ return (
+ <div className={cx('inline-flex items-center', className)}>
+ <button
+ id={switchId}
+ type='button'
+ role='switch'
+ aria-checked={checked}
+ disabled={disabled}
+ onClick={(e) => {
+ e.preventDefault()
+ e.stopPropagation()
+ if (!disabled) {
+ onChange(!checked)
+ }
+ }}
+ className={cx(
+ 'relative inline-flex h-6 w-11 shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none focus-visible:ring-2 focus-visible:ring-white/75',
+ {
+ 'bg-slate-900 dark:bg-slate-500': checked,
+ 'bg-gray-300 dark:bg-slate-600': !checked,
+ 'cursor-not-allowed opacity-50': disabled,
+ },
+ )}
+ >
+ <span
+ aria-hidden='true'
+ className={cx(
+ 'pointer-events-none inline-block size-5 transform rounded-full bg-white shadow-lg ring-0 transition duration-200 ease-in-out',
+ {
+ 'translate-x-5': checked,
+ 'translate-x-0': !checked,
+ },
+ )}
+ />
+ </button>
+ {label ? (
+ <label
+ htmlFor={switchId}
+ className={cx('ml-2 text-sm text-gray-900 dark:text-gray-100', {
+ 'cursor-pointer': !disabled,
+ 'cursor-not-allowed opacity-50': disabled,
+ })}
+ >
+ {label}
+ </label>
+ ) : null}
+ </div>
+ )
+}
diff --git a/web/public/locales/en.json b/web/public/locales/en.json
index 5cb714b35..5a3272667 100644
--- a/web/public/locales/en.json
+++ b/web/public/locales/en.json
@@ -1052,7 +1052,8 @@
"noProfiles": "No profiles have evaluated this flag yet",
"loadMore": "Load more",
"xEvaluations": "{{count}} evaluation(s)",
- "lastEvaluated": "Last evaluated"
+ "lastEvaluated": "Last evaluated",
+ "served": "Served"
},
"error": {
"status": {