Include leading zero in months when accessing usage and quota data (#1528)

Closes #1527 

Improves front-end types & ensures the data being accessed matches the
data sent by the back-end.

Tested by hand by using the returned data from the `/orgs/${orgId}`
endpoint in prod where this is happening in dev
This commit is contained in:
Emma Segal-Grossman 2024-02-12 19:27:42 -05:00 committed by GitHub
parent 4bc8152640
commit d88a6eb07f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 23 deletions

View File

@ -7,7 +7,7 @@ import type { SlSelectEvent } from "@shoelace-style/shoelace";
import LiteElement, { html } from "@/utils/LiteElement"; import LiteElement, { html } from "@/utils/LiteElement";
import type { AuthState } from "@/utils/AuthService"; import type { AuthState } from "@/utils/AuthService";
import type { OrgData } from "@/utils/orgs"; import type { OrgData, YearMonth } from "@/utils/orgs";
import type { SelectNewDialogEvent } from "./index"; import type { SelectNewDialogEvent } from "./index";
import { humanizeExecutionSeconds } from "@/utils/executionTimeFormatter"; import { humanizeExecutionSeconds } from "@/utils/executionTimeFormatter";
@ -374,13 +374,13 @@ export class Dashboard extends LiteElement {
} }
const now = new Date(); const now = new Date();
const currentYear = now.getFullYear();
const currentMonth = String(now.getUTCMonth() + 1).padStart(2, "0");
const currentPeriod = `${currentYear}-${currentMonth}` as YearMonth;
let usageSeconds = 0; let usageSeconds = 0;
if (this.org!.monthlyExecSeconds) { if (this.org!.monthlyExecSeconds) {
const actualUsage = const actualUsage = this.org!.monthlyExecSeconds[currentPeriod];
this.org!.monthlyExecSeconds[
`${now.getFullYear()}-${now.getUTCMonth() + 1}`
];
if (actualUsage) { if (actualUsage) {
usageSeconds = actualUsage; usageSeconds = actualUsage;
} }
@ -392,10 +392,7 @@ export class Dashboard extends LiteElement {
let usageSecondsAllTypes = 0; let usageSecondsAllTypes = 0;
if (this.org!.crawlExecSeconds) { if (this.org!.crawlExecSeconds) {
const actualUsage = const actualUsage = this.org!.crawlExecSeconds[currentPeriod];
this.org!.crawlExecSeconds[
`${now.getFullYear()}-${now.getUTCMonth() + 1}`
];
if (actualUsage) { if (actualUsage) {
usageSecondsAllTypes = actualUsage; usageSecondsAllTypes = actualUsage;
} }
@ -403,10 +400,7 @@ export class Dashboard extends LiteElement {
let usageSecondsExtra = 0; let usageSecondsExtra = 0;
if (this.org!.extraExecSeconds) { if (this.org!.extraExecSeconds) {
const actualUsageExtra = const actualUsageExtra = this.org!.extraExecSeconds[currentPeriod];
this.org!.extraExecSeconds[
`${now.getFullYear()}-${now.getUTCMonth() + 1}`
];
if (actualUsageExtra) { if (actualUsageExtra) {
usageSecondsExtra = actualUsageExtra; usageSecondsExtra = actualUsageExtra;
} }
@ -424,10 +418,7 @@ export class Dashboard extends LiteElement {
let usageSecondsGifted = 0; let usageSecondsGifted = 0;
if (this.org!.giftedExecSeconds) { if (this.org!.giftedExecSeconds) {
const actualUsageGifted = const actualUsageGifted = this.org!.giftedExecSeconds[currentPeriod];
this.org!.giftedExecSeconds[
`${now.getFullYear()}-${now.getUTCMonth() + 1}`
];
if (actualUsageGifted) { if (actualUsageGifted) {
usageSecondsGifted = actualUsageGifted; usageSecondsGifted = actualUsageGifted;
} }
@ -756,7 +747,7 @@ export class Dashboard extends LiteElement {
); );
} }
const rows = Object.entries(this.org.usage || {}) const rows = (Object.entries(this.org.usage || {}) as [YearMonth, number][])
// Sort latest // Sort latest
.reverse() .reverse()
.map(([mY, crawlTime]) => { .map(([mY, crawlTime]) => {

View File

@ -1,3 +1,5 @@
import type { Range } from "./utils";
// From UserRole in backend // From UserRole in backend
export type UserRole = "viewer" | "crawler" | "owner" | "superadmin"; export type UserRole = "viewer" | "crawler" | "owner" | "superadmin";
@ -8,6 +10,8 @@ export const AccessCode: Record<UserRole, number> = {
owner: 40, owner: 40,
} as const; } as const;
export type YearMonth = `${number}-${Range<0, 2>}${Range<0, 10>}`;
export type OrgData = { export type OrgData = {
id: string; id: string;
name: string; name: string;
@ -16,23 +20,23 @@ export type OrgData = {
bytesStored: number; bytesStored: number;
usage: { usage: {
// Keyed by {4-digit year}-{2-digit month} // Keyed by {4-digit year}-{2-digit month}
[key: string]: number; [key: YearMonth]: number;
} | null; } | null;
crawlExecSeconds?: { crawlExecSeconds?: {
// Keyed by {4-digit year}-{2-digit month} // Keyed by {4-digit year}-{2-digit month}
[key: string]: number; [key: YearMonth]: number;
}; };
monthlyExecSeconds?: { monthlyExecSeconds?: {
// Keyed by {4-digit year}-{2-digit month} // Keyed by {4-digit year}-{2-digit month}
[key: string]: number; [key: YearMonth]: number;
}; };
extraExecSeconds?: { extraExecSeconds?: {
// Keyed by {4-digit year}-{2-digit month} // Keyed by {4-digit year}-{2-digit month}
[key: string]: number; [key: YearMonth]: number;
}; };
giftedExecSeconds?: { giftedExecSeconds?: {
// Keyed by {4-digit year}-{2-digit month} // Keyed by {4-digit year}-{2-digit month}
[key: string]: number; [key: YearMonth]: number;
}; };
extraExecSecondsAvailable: number; extraExecSecondsAvailable: number;
giftedExecSecondsAvailable: number; giftedExecSecondsAvailable: number;

View File

@ -8,3 +8,15 @@ import type { DebouncedFunc } from "lodash";
* <sl-input @sl-input={this.onInput as UnderlyingFunction<typeof this.onInput>} > * <sl-input @sl-input={this.onInput as UnderlyingFunction<typeof this.onInput>} >
*/ */
export type UnderlyingFunction<T> = T extends DebouncedFunc<infer F> ? F : T; export type UnderlyingFunction<T> = T extends DebouncedFunc<infer F> ? F : T;
type Enumerate<
N extends number,
Acc extends number[] = [],
> = Acc["length"] extends N
? Acc[number]
: Enumerate<N, [...Acc, Acc["length"]]>;
export type Range<F extends number, T extends number> = Exclude<
Enumerate<T>,
Enumerate<F>
>;