1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00

feat: kept and discarded read model (#7045)

This commit is contained in:
Mateusz Kwasniewski 2024-05-13 14:24:31 +02:00 committed by GitHub
parent 4241e36819
commit dfc065500d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 51 additions and 8 deletions

View File

@ -43,7 +43,10 @@ export const populateCurrentStage = (
case 'completed':
return {
name: 'completed',
status: 'kept',
status:
feature.lifecycle.status === 'discarded'
? 'discarded'
: 'kept',
environments: getFilteredEnvironments(() => true),
enteredStageAt,
};

View File

@ -137,7 +137,10 @@ const FeatureOverviewMetaData = () => {
<span>{project}</span>
</SpacedBodyItem>
<ConditionallyRender
condition={featureLifecycleEnabled}
condition={
featureLifecycleEnabled &&
Boolean(feature.lifecycle)
}
show={
<SpacedBodyItem data-loading>
<StyledLabel>Lifecycle:</StyledLabel>

View File

@ -34,6 +34,7 @@ export type ILastSeenEnvironments = Pick<
export type Lifecycle = {
stage: 'initial' | 'pre-live' | 'live' | 'completed' | 'archived';
status?: string;
enteredStageAt: string;
};

View File

@ -27,6 +27,9 @@ export class FakeFeatureLifecycleStore implements IFeatureLifecycleStore {
...existingStages,
{
stage: featureLifecycleStage.stage,
...(featureLifecycleStage.status
? { status: featureLifecycleStage.status }
: {}),
enteredStageAt: new Date(),
},
];

View File

@ -6,6 +6,7 @@ import type { IFeatureLifecycleStage, StageName } from '../../types';
type DBType = {
feature: string;
stage: StageName;
status: string | null;
created_at: Date;
};
@ -23,8 +24,9 @@ export class FeatureLifecycleReadModel implements IFeatureLifecycleReadModel {
.where({ feature })
.orderBy('created_at', 'asc');
const stages = results.map(({ stage, created_at }: DBType) => ({
const stages = results.map(({ stage, status, created_at }: DBType) => ({
stage,
...(status ? { status } : {}),
enteredStageAt: created_at,
}));

View File

@ -10,8 +10,8 @@ import type { StageName } from '../../types';
type DBType = {
stage: StageName;
created_at: string;
status?: string;
status_value?: string;
status: string | null;
status_value: string | null;
};
type DBProjectType = DBType & {
@ -64,8 +64,9 @@ export class FeatureLifecycleStore implements IFeatureLifecycleStore {
.where({ feature })
.orderBy('created_at', 'asc');
return results.map(({ stage, created_at }: DBType) => ({
return results.map(({ stage, status, created_at }: DBType) => ({
stage,
...(status ? { status } : {}),
enteredStageAt: new Date(created_at),
}));
}

View File

@ -18,12 +18,15 @@ import {
STAGE_ENTERED,
} from './feature-lifecycle-service';
import type { FeatureLifecycleCompletedSchema } from '../../openapi';
import { FeatureLifecycleReadModel } from './feature-lifecycle-read-model';
import type { IFeatureLifecycleReadModel } from './feature-lifecycle-read-model-type';
let app: IUnleashTest;
let db: ITestDb;
let featureLifecycleService: FeatureLifecycleService;
let eventStore: IEventStore;
let eventBus: EventEmitter;
let featureLifecycleReadModel: IFeatureLifecycleReadModel;
beforeAll(async () => {
db = await dbInit('feature_lifecycle', getLogger);
@ -41,6 +44,7 @@ beforeAll(async () => {
eventStore = db.stores.eventStore;
eventBus = app.config.eventBus;
featureLifecycleService = app.services.featureLifecycleService;
featureLifecycleReadModel = new FeatureLifecycleReadModel(db.rawDatabase);
await app.request
.post(`/auth/demo/login`)
@ -62,6 +66,11 @@ const getFeatureLifecycle = async (featureName: string, expectedCode = 200) => {
.get(`/api/admin/projects/default/features/${featureName}/lifecycle`)
.expect(expectedCode);
};
const getCurrentStage = async (featureName: string) => {
return featureLifecycleReadModel.findCurrentStage(featureName);
};
const completeFeature = async (
featureName: string,
status: FeatureLifecycleCompletedSchema,
@ -166,6 +175,8 @@ test('should be able to toggle between completed/uncompleted', async () => {
status: 'kept',
statusValue: 'variant1',
});
const currentStage = await getCurrentStage('my_feature_b');
expect(currentStage).toMatchObject({ stage: 'completed', status: 'kept' });
await expectFeatureStage('my_feature_b', 'completed');

View File

@ -12,6 +12,7 @@ describe('getCurrentStage', () => {
},
{
stage: 'completed',
status: 'kept',
enteredStageAt: irrelevantDate,
},
{

View File

@ -79,6 +79,7 @@ class FeatureSearchStore implements IFeatureSearchStore {
.select(
'feature as stage_feature',
'stage as latest_stage',
'status as stage_status',
'created_at as entered_stage_at',
)
.distinctOn('stage_feature')
@ -416,6 +417,9 @@ class FeatureSearchStore implements IFeatureSearchStore {
entry.lifecycle = row.latest_stage
? {
stage: row.latest_stage,
...(row.stage_status
? { status: row.stage_status }
: {}),
enteredStageAt: row.entered_stage_at,
}
: undefined;

View File

@ -962,7 +962,7 @@ test('should return environment usage metrics and lifecycle', async () => {
{ feature: 'my_feature_b', stage: 'initial' },
]);
await stores.featureLifecycleStore.insert([
{ feature: 'my_feature_b', stage: 'pre-live' },
{ feature: 'my_feature_b', stage: 'completed', status: 'discarded' },
]);
const { body } = await searchFeatures({
@ -972,7 +972,7 @@ test('should return environment usage metrics and lifecycle', async () => {
features: [
{
name: 'my_feature_b',
lifecycle: { stage: 'pre-live' },
lifecycle: { stage: 'completed', status: 'discarded' },
environments: [
{
name: 'default',

View File

@ -16,6 +16,12 @@ export const featureLifecycleSchema = {
description:
'The name of the lifecycle stage that got recorded for a given feature',
},
status: {
type: 'string',
example: 'kept',
description:
'The name of the detailed status of a given stage. E.g. completed stage can be kept or discarded.',
},
enteredStageAt: {
type: 'string',
format: 'date-time',

View File

@ -161,6 +161,13 @@ export const featureSearchResponseSchema = {
],
example: 'initial',
},
status: {
type: 'string',
nullable: true,
example: 'kept',
description:
'The name of the detailed status of a given stage. E.g. completed stage can be kept or discarded.',
},
enteredStageAt: {
description: 'When the feature entered this stage',
type: 'string',

View File

@ -163,6 +163,7 @@ export type StageName =
export interface IFeatureLifecycleStage {
stage: StageName;
enteredStageAt: Date;
status?: string;
}
export type IProjectLifecycleStageDuration = {