Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref: Ladder class #83259

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 6 additions & 18 deletions static/app/components/charts/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {useMemo} from 'react';
import * as Sentry from '@sentry/react';
import type {LegendComponentOption, LineSeriesOption} from 'echarts';
import type {Location} from 'history';
import orderBy from 'lodash/orderBy';
import moment from 'moment-timezone';

import {DEFAULT_STATS_PERIOD} from 'sentry/constants';
Expand All @@ -17,6 +16,7 @@ import {defined, escape} from 'sentry/utils';
import {getFormattedDate} from 'sentry/utils/dates';
import type {TableDataWithTitle} from 'sentry/utils/discover/discoverQuery';
import {parsePeriodToHours} from 'sentry/utils/duration/parsePeriodToHours';
import {Ladder} from 'sentry/utils/number/ladder';
import oxfordizeArray from 'sentry/utils/oxfordizeArray';
import {decodeList} from 'sentry/utils/queryString';

Expand Down Expand Up @@ -67,18 +67,10 @@ export function useShortInterval(datetimeObj: DateTimeObject): boolean {
export type GranularityStep = [timeDiff: number, interval: string];

export class GranularityLadder {
steps: GranularityStep[];

constructor(steps: GranularityStep[]) {
if (
!steps.some(step => {
return step[0] === 0;
})
) {
throw new Error('At least one step in the ladder must start at 0');
}
ladder: Ladder<string>;

this.steps = orderBy(steps, step => step[0], 'desc');
constructor(steps: [GranularityStep, ...GranularityStep[]]) {
this.ladder = new Ladder<string>(steps);
}

getInterval(minutes: number): string {
Expand All @@ -91,14 +83,10 @@ export class GranularityLadder {
);
});

return (this.steps.at(-1) as GranularityStep)[1];
return this.ladder.min;
}

const step = this.steps.find(([threshold]) => {
return minutes >= threshold;
}) as GranularityStep;

return step[1];
return this.ladder.rung(minutes);
}
}

Expand Down
86 changes: 86 additions & 0 deletions static/app/utils/number/ladder.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import {Ladder} from './ladder';

describe('Ladder', () => {
test('it picks the correct rung', () => {
const ladder = new Ladder([
[0, 'first'],
[10, 'second'],
[20, 'third'],
]);

expect(ladder.rung(5)).toBe('first');
expect(ladder.rung(15)).toBe('second');
});

test('ladder must start at 0', () => {
expect(
() =>
new Ladder([
[10, 'first'],
[20, 'second'],
])
).toThrow();
});

test('ladders cannot have duplicate items', () => {
expect(
() =>
new Ladder([
[10, 'hello'],
[20, 'goodbye'],
[10, 'hi'],
])
).toThrow();
});

test('exact boundary match looks upward', () => {
const ladder = new Ladder([
[0, 'first'],
[10, 'second'],
]);

expect(ladder.rung(0)).toBe('first');
expect(ladder.rung(10)).toBe('second');
});

test('top of ladder is the maximum', () => {
const ladder = new Ladder([
[0, 'first'],
[10, 'second'],
]);

expect(ladder.rung(15)).toBe('second');
});

test('cannot check for values below 0', () => {
const ladder = new Ladder([
[0, 'first'],
[10, 'second'],
]);

expect(() => ladder.rung(-1)).toThrow();
});

test('provides the min and max value', () => {
const ladder = new Ladder([
[10, 'third'],
[5, 'second'],
[0, 'first'],
]);

expect(ladder.min).toBe('first');
expect(ladder.max).toBe('third');
});

test('enforces type', () => {
type Salutation = 'Hello' | 'Hi';

const ladder = new Ladder([
[0, 'Hello' as Salutation],
[10, 'Hi' as Salutation],
]);

const salutation = ladder.rung(0);
expect(salutation === 'Hi').toBeFalsy();
});
});
63 changes: 63 additions & 0 deletions static/app/utils/number/ladder.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import orderBy from 'lodash/orderBy';

type Rung<T> = [threshold: number, value: T];

/**
* Class that represents a value ladder. Given a ladder of increasing thresholds,
* the ladder can match an incoming value against the known intervals.
*
* @example
* ```javascript
* // Selecting a quota based on team size
* const ladder = new Ladder([
* [0, 10],
* [10, 100_000],
* [30, 1_000_000],
* ]);
*
* const quota = ladder.step(0); // Quota for small teams is 10
* const quota = ladder.step(12); // Quota for a 10-20 person team is 100,000
* const quota = ladder.step(120); // Quota for a 30 person or higher team is 1,000,000
* ```
*/
export class Ladder<T> {
rungs: Rung<T>[];

// At least one rung is required by the type
constructor(rungs: [Rung<T>, ...Rung<T>[]]) {
if (!rungs.some(rung => rung[0] === 0)) {
throw new Error('At least one rung in the ladder must start at 0');
}

const sortedRungs = orderBy(rungs, rung => rung[0], 'desc');

const rungThresholds = sortedRungs.map(rung => rung[0]);
const uniqueThresholds = new Set(rungThresholds);

if (uniqueThresholds.size !== rungThresholds.length) {
throw new Error('Rung thresholds are not unique');
}

this.rungs = sortedRungs;
}

rung(value: number) {
if (value < 0) {
throw new Error('Cannot check ladder for values below 0');
}

const rung = this.rungs.find(([threshold]) => {
return value >= threshold;
}) as Rung<T>;

return rung[1];
}

get min() {
return (this.rungs.at(-1) as Rung<T>)[1];
}

get max() {
return (this.rungs.at(0) as Rung<T>)[1];
}
}
Loading