Skip to content

Commit

Permalink
feat: render segments changes in feature strategy update event messag…
Browse files Browse the repository at this point in the history
…es (#4950)

## About the changes

Segment changes in predata and data columns were both showing the new
segments list

Adds formatting of what's changed with segments to feature strategy
update events, so when a user changes the strategy from using
constraints, to using segments instead, it's communicated in event
updates

results in: 

admin updated
[sample-toggle](http://localhost/projects/default/features/sample-toggle)
in project [default](http://localhost/projects/default) by updating
strategy Sample Strategy in development constraints from [userId is one
of (1,2,3)] to empty set of constraints; segments from empty set of
segments to (1)


Closes #
#4912 

### Important files

- `src/lib/services/feature-toggle-service.ts` - Segment changes in
preData and data
- `src/lib/addons/feature-event-formatter-md.ts` - Formatting segments

## Discussion points

This is an SR least effort PR - we should plan a task where we look at
how to render this list of segments in a more comprehensible way (it's
just rendering ids now)
  • Loading branch information
daveleek authored Oct 9, 2023
1 parent e0faa3e commit e065e2a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
44 changes: 39 additions & 5 deletions src/lib/addons/feature-event-formatter-md.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,11 @@ export class FeatureEventFormatterMd implements FeatureEventFormatter {
preData?.constraints,
data?.constraints,
);
const strategySpecificText = [usersText, constraintText]
const segmentsText = this.segmentsChangeText(
preData?.segments,
data?.segments,
);
const strategySpecificText = [usersText, constraintText, segmentsText]
.filter((x) => x.length)
.join(';');
return `by updating strategy *${this.getStrategyTitle(
Expand Down Expand Up @@ -453,11 +457,16 @@ export class FeatureEventFormatterMd implements FeatureEventFormatter {
preData?.constraints,
data?.constraints,
);
const segmentsText = this.segmentsChangeText(
preData?.segments,
data?.segments,
);
const strategySpecificText = [
stickinessText,
rolloutText,
groupIdText,
constraintText,
segmentsText,
]
.filter((txt) => txt.length)
.join(';');
Expand All @@ -468,12 +477,20 @@ export class FeatureEventFormatterMd implements FeatureEventFormatter {

private defaultStrategyChangeText(event: IEvent) {
const { preData, data, environment } = event;
return `by updating strategy *${this.getStrategyTitle(
event,
)}* in *${environment}*${this.constraintChangeText(
const constraintText = this.constraintChangeText(
preData?.constraints,
data?.constraints,
)}`;
);
const segmentsText = this.segmentsChangeText(
preData?.segments,
data?.segments,
);
const strategySpecificText = [constraintText, segmentsText]
.filter((txt) => txt.length)
.join(';');
return `by updating strategy *${this.getStrategyTitle(
event,
)}* in *${environment}*${strategySpecificText}`;
}

private constraintChangeText(
Expand Down Expand Up @@ -525,6 +542,23 @@ export class FeatureEventFormatterMd implements FeatureEventFormatter {
: ` constraints from ${oldConstraintText} to ${newConstraintText}`;
}

private segmentsChangeText(
oldSegments: string[] = [],
newSegments: string[] = [],
) {
const formatSegments = (segments: string[]) => {
return segments.length === 0
? 'empty set of segments'
: `(${segments.join(',')})`;
};
const oldSegmentsText = formatSegments(oldSegments);
const newSegmentsText = formatSegments(newSegments);

return oldSegmentsText === newSegmentsText
? ''
: ` segments from ${oldSegmentsText} to ${newSegmentsText}`;
}

format(event: IEvent): {
text: string;
url?: string;
Expand Down
8 changes: 6 additions & 2 deletions src/lib/services/feature-toggle-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,7 @@ class FeatureToggleService {
projectId,
updates.segments,
);
const existingSegments = await this.segmentService.getByStrategy(id);

if (existingStrategy.id === id) {
if (updates.constraints && updates.constraints.length > 0) {
Expand Down Expand Up @@ -752,7 +753,7 @@ class FeatureToggleService {
const data = this.featureStrategyToPublic(strategy, segments);
const preData = this.featureStrategyToPublic(
existingStrategy,
segments,
existingSegments,
);
await this.eventService.storeEvent(
new FeatureStrategyUpdateEvent({
Expand Down Expand Up @@ -789,6 +790,9 @@ class FeatureToggleService {

if (existingStrategy.id === id) {
existingStrategy.parameters[name] = String(value);
const existingSegments = await this.segmentService.getByStrategy(
id,
);
const strategy = await this.featureStrategiesStore.updateStrategy(
id,
existingStrategy,
Expand All @@ -799,7 +803,7 @@ class FeatureToggleService {
const data = this.featureStrategyToPublic(strategy, segments);
const preData = this.featureStrategyToPublic(
existingStrategy,
segments,
existingSegments,
);
await this.eventService.storeEvent(
new FeatureStrategyUpdateEvent({
Expand Down

0 comments on commit e065e2a

Please sign in to comment.