* feat: alternate search index for nimbus
Create an alternate search index for Nimbus that filters out
feature-flagged pages (equivalent to setting all feature flags to
false).
Notes:
- Creates two new DB tables, `page_nimbus` and `page_section_nimbus`,
which are filtered versions of `page` and `page_section`
- Makes `nimbus` versions of all the DB search functions
- Refactored the embedding upload script. Changes to make it faster (got
annoyed by how slow it was when testing...), incorporate retries, and
produce better summary logs.
- Upload script, when run with the environment variable
ENABLED_FEATURES_OVERRIDE_DISABLE_ALL, produces and uploads the
alternate search index
- Changed all the search calls in frontend/API to check for
`isFeatureEnabled('search:fullIndex')` to determine whether to search
the full or alternate index
* ci: produce nimbus search indexes on merge
* fix: turn full search index on
70 lines
2.3 KiB
TypeScript
70 lines
2.3 KiB
TypeScript
import type { components } from 'api-types'
|
|
import enabledFeaturesRaw from './enabled-features.json' with { type: 'json' }
|
|
|
|
const enabledFeaturesStaticObj = enabledFeaturesRaw as Omit<typeof enabledFeaturesRaw, '$schema'>
|
|
|
|
type Profile = components['schemas']['ProfileResponse']
|
|
|
|
export type Feature = Profile['disabled_features'][number] | keyof typeof enabledFeaturesStaticObj
|
|
|
|
const disabledFeaturesStaticArray = Object.entries(enabledFeaturesStaticObj)
|
|
.filter(([_, value]) => !value)
|
|
.map(([key]) => key as Feature)
|
|
|
|
function checkFeature(feature: Feature, features: Set<Feature>) {
|
|
return !features.has(feature)
|
|
}
|
|
|
|
type SnakeToCamelCase<S extends string> = S extends `${infer First}_${infer Rest}`
|
|
? `${First}${SnakeToCamelCase<Capitalize<Rest>>}`
|
|
: S
|
|
|
|
type FeatureToCamelCase<S extends Feature> = S extends `${infer P}:${infer R}`
|
|
? `${SnakeToCamelCase<P>}${Capitalize<SnakeToCamelCase<R>>}`
|
|
: SnakeToCamelCase<S>
|
|
|
|
function featureToCamelCase(feature: Feature) {
|
|
return feature
|
|
.replace(/:/g, '_')
|
|
.split('_')
|
|
.map((word, index) => (index === 0 ? word : word[0].toUpperCase() + word.slice(1)))
|
|
.join('') as FeatureToCamelCase<typeof feature>
|
|
}
|
|
|
|
function isFeatureEnabled<T extends Feature[]>(
|
|
features: T,
|
|
runtimeDisabledFeatures?: Feature[]
|
|
): { [key in FeatureToCamelCase<T[number]>]: boolean }
|
|
function isFeatureEnabled(features: Feature, runtimeDisabledFeatures?: Feature[]): boolean
|
|
function isFeatureEnabled<T extends Feature | Feature[]>(
|
|
features: T,
|
|
runtimeDisabledFeatures?: Feature[]
|
|
) {
|
|
// Override is used to produce a filtered version of the docs search index
|
|
// using the same sync setup as our normal search index
|
|
if (process.env.ENABLED_FEATURES_OVERRIDE_DISABLE_ALL === 'true') {
|
|
if (Array.isArray(features)) {
|
|
return Object.fromEntries(features.map((feature) => [featureToCamelCase(feature), false]))
|
|
}
|
|
return false
|
|
}
|
|
|
|
const disabledFeatures = new Set([
|
|
...(runtimeDisabledFeatures ?? []),
|
|
...disabledFeaturesStaticArray,
|
|
])
|
|
|
|
if (Array.isArray(features)) {
|
|
return Object.fromEntries(
|
|
features.map((feature) => [
|
|
featureToCamelCase(feature),
|
|
checkFeature(feature, disabledFeatures),
|
|
])
|
|
)
|
|
}
|
|
|
|
return checkFeature(features, disabledFeatures)
|
|
}
|
|
|
|
export { isFeatureEnabled }
|