forked from mtusi2026/mtusi2026lab6
feat(lab6): add observability and SLO burn-rate alerts
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
const timeFormatter = new Intl.DateTimeFormat("ru-RU", {
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
});
|
||||
|
||||
const dayFormatter = new Intl.DateTimeFormat("ru-RU", {
|
||||
day: "numeric",
|
||||
month: "long",
|
||||
});
|
||||
|
||||
const fullDateFormatter = new Intl.DateTimeFormat("ru-RU", {
|
||||
day: "numeric",
|
||||
month: "long",
|
||||
year: "numeric",
|
||||
});
|
||||
|
||||
export function formatMessageTime(value: string): string {
|
||||
return timeFormatter.format(new Date(value));
|
||||
}
|
||||
|
||||
export function formatDay(value: string): string {
|
||||
const date = new Date(value);
|
||||
const now = new Date();
|
||||
const sameYear = date.getFullYear() === now.getFullYear();
|
||||
return (sameYear ? dayFormatter : fullDateFormatter).format(date);
|
||||
}
|
||||
|
||||
export function dayKey(value: string): string {
|
||||
const date = new Date(value);
|
||||
return [date.getFullYear(), date.getMonth(), date.getDate()].join("-");
|
||||
}
|
||||
|
||||
export function formatMemberCount(count: number): string {
|
||||
const mod10 = count % 10;
|
||||
const mod100 = count % 100;
|
||||
if (mod10 === 1 && mod100 !== 11) {
|
||||
return `${count} участник`;
|
||||
}
|
||||
if (mod10 >= 2 && mod10 <= 4 && (mod100 < 12 || mod100 > 14)) {
|
||||
return `${count} участника`;
|
||||
}
|
||||
return `${count} участников`;
|
||||
}
|
||||
|
||||
export function shortID(id: string): string {
|
||||
return id.length > 12 ? `${id.slice(0, 8)}…` : id;
|
||||
}
|
||||
|
||||
export function initials(value: string): string {
|
||||
const parts = value.trim().split(/\s+/).filter(Boolean);
|
||||
if (parts.length === 0) {
|
||||
return "?";
|
||||
}
|
||||
return parts
|
||||
.slice(0, 2)
|
||||
.map((part) => Array.from(part)[0]?.toUpperCase() ?? "")
|
||||
.join("");
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { createIdempotencyKey } from "./idempotency";
|
||||
|
||||
describe("createIdempotencyKey", () => {
|
||||
it("creates an API-compatible unique key", () => {
|
||||
vi.stubGlobal("crypto", {
|
||||
randomUUID: () => "12345678-1234-1234-1234-123456789abc",
|
||||
});
|
||||
|
||||
const key = createIdempotencyKey();
|
||||
|
||||
expect(key).toMatch(/^[A-Za-z0-9._:-]{1,128}$/);
|
||||
expect(key).toContain("12345678123412341234123456789abc");
|
||||
expect(key.length).toBeLessThanOrEqual(128);
|
||||
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,4 @@
|
||||
export function createIdempotencyKey(): string {
|
||||
const random = crypto.randomUUID().replaceAll("-", "");
|
||||
return `web-${Date.now().toString(36)}-${random}`;
|
||||
}
|
||||
Reference in New Issue
Block a user