Skip to content

Commit 75bdc61

Browse files
committed
Add option to use average of all payout items as final payout
1 parent 4fc7db1 commit 75bdc61

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

frontend/src/components/stages/payout_editor.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class PayoutEditor extends MobxLitElement {
5050
);
5151

5252
return html`
53-
${this.renderPayoutCurrency()}
53+
${this.renderPayoutCurrency()} ${this.renderPayoutAverage()}
5454
<pr-menu
5555
name="Add stage payout"
5656
?disabled=${!this.experimentEditor.canEditStages}
@@ -67,6 +67,31 @@ export class PayoutEditor extends MobxLitElement {
6767
`;
6868
}
6969

70+
renderPayoutAverage() {
71+
if (!this.stage) return nothing;
72+
73+
const updateAverage = () => {
74+
if (!this.stage) return;
75+
this.experimentEditor.updateStage({
76+
...this.stage,
77+
averageAllPayoutItems: !this.stage.averageAllPayoutItems,
78+
});
79+
};
80+
81+
return html`
82+
<div class="checkbox-wrapper">
83+
<md-checkbox
84+
touch-target="wrapper"
85+
?checked=${this.stage.averageAllPayoutItems}
86+
?disabled=${!this.experimentEditor.canEditStages}
87+
@click=${updateAverage}
88+
>
89+
</md-checkbox>
90+
<div>Use the average of all stage payouts as the final payout</div>
91+
</div>
92+
`;
93+
}
94+
7095
addPayout(stage: StageConfig) {
7196
if (!this.stage) return;
7297

frontend/src/components/stages/payout_summary_view.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ export class PayoutView extends MobxLitElement {
6969

7070
private renderTotalPayout(resultConfig: PayoutResultConfig) {
7171
const total = calculatePayoutTotal(resultConfig);
72+
const averageNote = resultConfig.averageAllPayoutItems ? ' (average)' : '';
7273

7374
return html`
7475
<div class="scoring-bundle row">
75-
<h2>Final payout</h2>
76+
<h2>Final payout${averageNote}</h2>
7677
<div class="chip primary">
7778
${this.renderCurrency(total, resultConfig.currency)}
7879
</div>

utils/src/stages/payout_stage.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export interface PayoutStageConfig extends BaseStageConfig {
2626
kind: StageKind.PAYOUT;
2727
currency: PayoutCurrency;
2828
payoutItems: PayoutItem[];
29+
averageAllPayoutItems: boolean;
2930
}
3031

3132
export enum PayoutCurrency {
@@ -93,6 +94,7 @@ export interface PayoutResultConfig {
9394
id: string; // stage ID of the original payout config
9495
currency: PayoutCurrency;
9596
results: PayoutItemResult[];
97+
averageAllPayoutItems: boolean;
9698
}
9799

98100
export type PayoutItemResult =
@@ -157,6 +159,7 @@ export function createPayoutStage(
157159
progress: config.progress ?? createStageProgressConfig(),
158160
currency: config.currency ?? PayoutCurrency.USD,
159161
payoutItems: config.payoutItems ?? [],
162+
averageAllPayoutItems: config.averageAllPayoutItems ?? false,
160163
};
161164
}
162165

@@ -292,6 +295,7 @@ export function calculatePayoutResult(
292295
id: payoutConfig.id,
293296
currency: payoutConfig.currency,
294297
results,
298+
averageAllPayoutItems: payoutConfig.averageAllPayoutItems,
295299
};
296300
return resultConfig;
297301
}
@@ -316,6 +320,12 @@ export function calculatePayoutTotal(resultConfig: PayoutResultConfig) {
316320
total += Math.max(0, chipTotal - initialChipTotal);
317321
}
318322
});
323+
324+
// If average all items, divide total by number of results
325+
if (resultConfig.averageAllPayoutItems) {
326+
return Math.ceil((total / resultConfig.results.length) * 100) / 100;
327+
}
328+
319329
return Math.ceil(total * 100) / 100;
320330
}
321331

utils/src/stages/payout_stage.validation.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export const PayoutStageConfigData = Type.Object(
8585
progress: StageProgressConfigSchema,
8686
currency: PayoutCurrencySchema,
8787
payoutItems: Type.Array(PayoutItemData),
88+
averageAllPayoutItems: Type.Boolean(),
8889
},
8990
strict,
9091
);

0 commit comments

Comments
 (0)