Skip to content

Commit

Permalink
feat: add title to strategy (#3510)
Browse files Browse the repository at this point in the history
<!-- Thanks for creating a PR! To make it easier for reviewers and
everyone else to understand what your changes relate to, please add some
relevant content to the headings below. Feel free to ignore or delete
sections that you don't think are relevant. Thank you! ❤️ -->
Adds title column to strategies, feature_strategies and features_view in
the db
Updates model/schemas
## About the changes
<!-- Describe the changes introduced. What are they and why are they
being introduced? Feel free to also add screenshots or steps to view the
changes if they're visual. -->

<!-- Does it close an issue? Multiple? -->
Closes #
[1-855](https://linear.app/unleash/issue/1-855/allow-for-title-on-strategy-backend)

<!-- (For internal contributors): Does it relate to an issue on public
roadmap? -->
<!--
Relates to [roadmap](https://github.com/orgs/Unleash/projects/10) item:
#
-->

### Important files
<!-- PRs can contain a lot of changes, but not all changes are equally
important. Where should a reviewer start looking to get an overview of
the changes? Are any files particularly important? -->


## Discussion points
<!-- Anything about the PR you'd like to discuss before it gets merged?
Got any questions or doubts? -->

---------

Signed-off-by: andreas-unleash <andreas@getunleash.ai>
  • Loading branch information
andreas-unleash committed Apr 18, 2023
1 parent 5940a81 commit 2da279b
Show file tree
Hide file tree
Showing 15 changed files with 354 additions and 36 deletions.
27 changes: 18 additions & 9 deletions src/lib/db/feature-strategy-store.ts
Expand Up @@ -10,27 +10,28 @@ import {
IConstraint,
IEnvironmentOverview,
IFeatureOverview,
IFeatureStrategiesStore,
IFeatureStrategy,
IFeatureToggleClient,
IFlagResolver,
IStrategyConfig,
ITag,
} from '../types/model';
import { IFeatureStrategiesStore } from '../types/stores/feature-strategies-store';
import { PartialDeep, PartialSome } from '../types/partial';
PartialDeep,
PartialSome,
} from '../types';
import FeatureToggleStore from './feature-toggle-store';
import { ensureStringValue } from '../util/ensureStringValue';
import { mapValues } from '../util/map-values';
import { IFlagResolver } from '../types/experimental';
import { ensureStringValue, mapValues } from '../util';
import { IFeatureProjectUserParams } from '../routes/admin-api/project/project-features';
import Raw = Knex.Raw;
import { Db } from './db';
import Raw = Knex.Raw;

const COLUMNS = [
'id',
'feature_name',
'project_name',
'environment',
'strategy_name',
'title',
'parameters',
'constraints',
'created_at',
Expand All @@ -55,6 +56,7 @@ interface IFeatureStrategiesTable {
feature_name: string;
project_name: string;
environment: string;
title?: string | null;
strategy_name: string;
parameters: object;
constraints: string;
Expand All @@ -76,6 +78,7 @@ function mapRow(row: IFeatureStrategiesTable): IFeatureStrategy {
projectId: row.project_name,
environment: row.environment,
strategyName: row.strategy_name,
title: row.title,
parameters: mapValues(row.parameters || {}, ensureStringValue),
constraints: (row.constraints as unknown as IConstraint[]) || [],
createdAt: row.created_at,
Expand All @@ -90,6 +93,7 @@ function mapInput(input: IFeatureStrategy): IFeatureStrategiesTable {
project_name: input.projectId,
environment: input.environment,
strategy_name: input.strategyName,
title: input.title,
parameters: input.parameters,
constraints: JSON.stringify(input.constraints || []),
created_at: input.createdAt,
Expand All @@ -101,6 +105,7 @@ interface StrategyUpdate {
strategy_name: string;
parameters: object;
constraints: string;
title?: string;
}

function mapStrategyUpdate(
Expand All @@ -113,6 +118,9 @@ function mapStrategyUpdate(
if (input.parameters !== null) {
update.parameters = input.parameters;
}
if (input.title !== null) {
update.title = input.title;
}
update.constraints = JSON.stringify(input.constraints || []);
return update;
}
Expand Down Expand Up @@ -376,8 +384,8 @@ class FeatureStrategiesStore implements IFeatureStrategiesStore {
feature: PartialDeep<IFeatureToggleClient>,
row: Record<string, any>,
) {
const strategy = feature.strategies.find(
(s) => s.id === row.strategy_id,
const strategy = feature.strategies?.find(
(s) => s?.id === row.strategy_id,
);
if (!strategy) {
return;
Expand Down Expand Up @@ -581,6 +589,7 @@ class FeatureStrategiesStore implements IFeatureStrategiesStore {
parameters: r.parameters,
sortOrder: r.sort_order,
id: r.strategy_id,
title: r.strategy_title || '',
};
if (!includeId) {
delete strategy.id;
Expand Down
6 changes: 6 additions & 0 deletions src/lib/db/strategy-store.ts
Expand Up @@ -11,6 +11,7 @@ import {
import { Db } from './db';

const STRATEGY_COLUMNS = [
'title',
'name',
'description',
'parameters',
Expand All @@ -21,6 +22,7 @@ const STRATEGY_COLUMNS = [
const TABLE = 'strategies';

interface IStrategyRow {
title: string;
name: string;
built_in: number;
description: string;
Expand Down Expand Up @@ -109,6 +111,7 @@ export default class StrategyStore implements IStrategyStore {
description: row.description,
parameters: row.parameters,
deprecated: row.deprecated,
title: row.title,
};
}

Expand All @@ -121,6 +124,7 @@ export default class StrategyStore implements IStrategyStore {
description: row.description,
parameters: row.parameters,
deprecated: row.deprecated,
title: row.title,
};
}

Expand All @@ -130,6 +134,7 @@ export default class StrategyStore implements IStrategyStore {
name: data.name,
description: data.description,
parameters: JSON.stringify(data.parameters),
title: data.title,
};
}

Expand Down Expand Up @@ -166,6 +171,7 @@ export default class StrategyStore implements IStrategyStore {
built_in: data.builtIn ? 1 : 0,
sort_order: data.sortOrder || 9999,
display_name: data.displayName,
title: data.title,
};
await this.db(TABLE).insert(rowData).onConflict(['name']).merge();
}
Expand Down
27 changes: 27 additions & 0 deletions src/lib/openapi/spec/create-feature-strategy-schema.ts
Expand Up @@ -9,22 +9,49 @@ export const createFeatureStrategySchema = {
properties: {
name: {
type: 'string',
description: 'The name or type of strategy',
example: 'flexibleRollout',
},
title: {
type: 'string',
nullable: true,
description: 'A descriptive title for the strategy',
example: 'Gradual Rollout 25-Prod',
},
sortOrder: {
type: 'number',
description: 'The order of the strategy in the list',
example: 9999,
},
constraints: {
type: 'array',
description: 'A list of the constraints attached to the strategy',
example: [
{
values: ['1', '2'],
inverted: false,
operator: 'IN',
contextName: 'appName',
caseInsensitive: false,
},
],
items: {
$ref: '#/components/schemas/constraintSchema',
},
},
parameters: {
description: 'An object containing the parameters for the strategy',
example: {
groupId: 'some_new',
rollout: '25',
stickiness: 'sessionId',
},
$ref: '#/components/schemas/parametersSchema',
},
segments: {
type: 'array',
description: 'Ids of segments to use for this strategy',
example: [1, 2],
items: {
type: 'number',
},
Expand Down
19 changes: 19 additions & 0 deletions src/lib/openapi/spec/feature-strategy-schema.ts
Expand Up @@ -4,30 +4,49 @@ import { parametersSchema } from './parameters-schema';

export const featureStrategySchema = {
$id: '#/components/schemas/featureStrategySchema',
description:
'A singles activation strategy configuration schema for a feature',
type: 'object',
additionalProperties: false,
required: ['name'],
properties: {
id: {
type: 'string',
description: 'A uuid for the feature strategy',
example: '6b5157cb-343a-41e7-bfa3-7b4ec3044840',
},
name: {
type: 'string',
description: 'The name or type of strategy',
example: 'flexibleRollout',
},
title: {
type: 'string',
description: 'A descriptive title for the strategy',
example: 'Gradual Rollout 25-Prod',
nullable: true,
},
featureName: {
type: 'string',
description: 'The name or feature the strategy is attached to',
example: 'myAwesomeFeature',
},
sortOrder: {
type: 'number',
description: 'The order of the strategy in the list',
example: 9999,
},
segments: {
type: 'array',
description: 'A list of segment ids attached to the strategy',
example: [1, 2],
items: {
type: 'number',
},
},
constraints: {
type: 'array',
description: 'A list of the constraints attached to the strategy',
items: {
$ref: '#/components/schemas/constraintSchema',
},
Expand Down
6 changes: 6 additions & 0 deletions src/lib/openapi/spec/strategy-schema.test.ts
Expand Up @@ -4,6 +4,7 @@ import { StrategySchema } from './strategy-schema';
test('strategySchema', () => {
const data: StrategySchema = {
description: '',
title: '',
name: '',
displayName: '',
editable: false,
Expand All @@ -25,4 +26,9 @@ test('strategySchema', () => {
expect(
validateSchema('#/components/schemas/strategySchema', {}),
).toMatchSnapshot();

const { title, ...noTitle } = { ...data };
expect(
validateSchema('#/components/schemas/strategySchema', noTitle),
).toBeUndefined();
});
23 changes: 23 additions & 0 deletions src/lib/openapi/spec/strategy-schema.ts
Expand Up @@ -2,6 +2,8 @@ import { FromSchema } from 'json-schema-to-ts';

export const strategySchema = {
$id: '#/components/schemas/strategySchema',
description:
'The [activation strategy](https://docs.getunleash.io/reference/activation-strategies) schema',
type: 'object',
additionalProperties: false,
required: [
Expand All @@ -13,39 +15,60 @@ export const strategySchema = {
'parameters',
],
properties: {
title: {
type: 'string',
nullable: true,
description: 'An optional title for the strategy',
example: 'GradualRollout - Prod25',
},
name: {
type: 'string',
description: 'The name or type of the strategy',
example: 'flexibleRollout',
},
displayName: {
type: 'string',
description: 'A human friendly name for the strategy',
example: 'Gradual Rollout',
nullable: true,
},
description: {
type: 'string',
description: 'A short description for the strategy',
example: 'Gradual rollout to logged in users',
},
editable: {
type: 'boolean',
description: 'Determines whether the strategy allows for editing',
example: true,
},
deprecated: {
type: 'boolean',
description: '',
example: true,
},
parameters: {
type: 'array',
description: 'A list of relevant parameters for each strategy',
items: {
type: 'object',
additionalProperties: false,
properties: {
name: {
type: 'string',
example: 'percentage',
},
type: {
type: 'string',
example: 'percentage',
},
description: {
type: 'string',
example: 'Gradual rollout to logged in users',
},
required: {
type: 'boolean',
example: true,
},
},
},
Expand Down

0 comments on commit 2da279b

Please sign in to comment.