feat: implement daily standings snapshot system with admin management interface
This commit is contained in:
parent
0385ca220c
commit
9d38bdf1ca
8 changed files with 683 additions and 50 deletions
202
app/models/__tests__/standings-snapshots.test.ts
Normal file
202
app/models/__tests__/standings-snapshots.test.ts
Normal file
|
|
@ -0,0 +1,202 @@
|
|||
import { describe, it, expect } from "vitest";
|
||||
|
||||
/**
|
||||
* Standings Snapshots Tests
|
||||
* Phase 4.2: Test snapshot creation and 7-day comparison logic
|
||||
*
|
||||
* These are unit tests that verify the snapshot logic works correctly.
|
||||
* Integration tests that use the actual database are in the e2e test suite.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Helper function to simulate 7-day rank change calculation
|
||||
* This mirrors the logic in getSevenDayStandingsChange
|
||||
*/
|
||||
function calculateSevenDayChange(
|
||||
currentRank: number,
|
||||
sevenDayOldRank: number | null
|
||||
): number {
|
||||
if (sevenDayOldRank === null) {
|
||||
return 0; // No snapshot from 7 days ago
|
||||
}
|
||||
// Positive number = improvement (was 5th, now 3rd = +2)
|
||||
// Negative number = decline (was 3rd, now 5th = -2)
|
||||
return sevenDayOldRank - currentRank;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to simulate snapshot deduplication logic
|
||||
* This mirrors the logic in createDailySnapshot
|
||||
*/
|
||||
function shouldCreateSnapshot(
|
||||
existingSnapshotDate: string | null,
|
||||
today: string
|
||||
): boolean {
|
||||
// Don't create if snapshot already exists for today
|
||||
return existingSnapshotDate !== today;
|
||||
}
|
||||
|
||||
describe("Standings Snapshots", () => {
|
||||
describe("7-Day Rank Change Calculation", () => {
|
||||
it("should calculate positive change when rank improves", () => {
|
||||
const change = calculateSevenDayChange(1, 3);
|
||||
expect(change).toBe(2); // Moved from 3rd to 1st = +2
|
||||
});
|
||||
|
||||
it("should calculate negative change when rank declines", () => {
|
||||
const change = calculateSevenDayChange(5, 2);
|
||||
expect(change).toBe(-3); // Moved from 2nd to 5th = -3
|
||||
});
|
||||
|
||||
it("should return 0 when rank stays the same", () => {
|
||||
const change = calculateSevenDayChange(2, 2);
|
||||
expect(change).toBe(0);
|
||||
});
|
||||
|
||||
it("should return 0 when no snapshot exists from 7 days ago", () => {
|
||||
const change = calculateSevenDayChange(1, null);
|
||||
expect(change).toBe(0);
|
||||
});
|
||||
|
||||
it("should handle large rank improvements", () => {
|
||||
const change = calculateSevenDayChange(1, 10);
|
||||
expect(change).toBe(9); // Moved from 10th to 1st = +9
|
||||
});
|
||||
|
||||
it("should handle large rank declines", () => {
|
||||
const change = calculateSevenDayChange(10, 1);
|
||||
expect(change).toBe(-9); // Moved from 1st to 10th = -9
|
||||
});
|
||||
});
|
||||
|
||||
describe("Snapshot Deduplication", () => {
|
||||
it("should create snapshot when no existing snapshot for today", () => {
|
||||
const today = "2025-01-13";
|
||||
const shouldCreate = shouldCreateSnapshot(null, today);
|
||||
expect(shouldCreate).toBe(true);
|
||||
});
|
||||
|
||||
it("should not create snapshot when snapshot already exists for today", () => {
|
||||
const today = "2025-01-13";
|
||||
const existingDate = "2025-01-13";
|
||||
const shouldCreate = shouldCreateSnapshot(existingDate, today);
|
||||
expect(shouldCreate).toBe(false);
|
||||
});
|
||||
|
||||
it("should create snapshot when last snapshot was yesterday", () => {
|
||||
const today = "2025-01-13";
|
||||
const existingDate = "2025-01-12";
|
||||
const shouldCreate = shouldCreateSnapshot(existingDate, today);
|
||||
expect(shouldCreate).toBe(true);
|
||||
});
|
||||
|
||||
it("should create snapshot when last snapshot was 7 days ago", () => {
|
||||
const today = "2025-01-13";
|
||||
const existingDate = "2025-01-06";
|
||||
const shouldCreate = shouldCreateSnapshot(existingDate, today);
|
||||
expect(shouldCreate).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Snapshot Data Integrity", () => {
|
||||
it("should preserve all tiebreaker data in snapshot", () => {
|
||||
const standing = {
|
||||
totalPoints: 150.5,
|
||||
currentRank: 1,
|
||||
firstPlaceCount: 2,
|
||||
secondPlaceCount: 1,
|
||||
thirdPlaceCount: 0,
|
||||
fourthPlaceCount: 0,
|
||||
fifthPlaceCount: 0,
|
||||
sixthPlaceCount: 0,
|
||||
seventhPlaceCount: 0,
|
||||
eighthPlaceCount: 0,
|
||||
participantsRemaining: 5,
|
||||
};
|
||||
|
||||
// Snapshot should contain all the same data
|
||||
const snapshot = {
|
||||
totalPoints: standing.totalPoints,
|
||||
rank: standing.currentRank,
|
||||
firstPlaceCount: standing.firstPlaceCount,
|
||||
secondPlaceCount: standing.secondPlaceCount,
|
||||
thirdPlaceCount: standing.thirdPlaceCount,
|
||||
fourthPlaceCount: standing.fourthPlaceCount,
|
||||
fifthPlaceCount: standing.fifthPlaceCount,
|
||||
sixthPlaceCount: standing.sixthPlaceCount,
|
||||
seventhPlaceCount: standing.seventhPlaceCount,
|
||||
eighthPlaceCount: standing.eighthPlaceCount,
|
||||
participantsRemaining: standing.participantsRemaining,
|
||||
};
|
||||
|
||||
expect(snapshot.totalPoints).toBe(standing.totalPoints);
|
||||
expect(snapshot.rank).toBe(standing.currentRank);
|
||||
expect(snapshot.firstPlaceCount).toBe(standing.firstPlaceCount);
|
||||
expect(snapshot.participantsRemaining).toBe(standing.participantsRemaining);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Movement Scenarios", () => {
|
||||
it("should handle team moving from last to first", () => {
|
||||
const scenarios = [
|
||||
{ current: 1, old: 8, expected: 7 }, // Last to first = +7
|
||||
{ current: 8, old: 1, expected: -7 }, // First to last = -7
|
||||
];
|
||||
|
||||
scenarios.forEach(({ current, old, expected }) => {
|
||||
const change = calculateSevenDayChange(current, old);
|
||||
expect(change).toBe(expected);
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle multiple teams with different movements", () => {
|
||||
const teams = [
|
||||
{ id: "A", currentRank: 1, oldRank: 3, expectedChange: 2 },
|
||||
{ id: "B", currentRank: 2, oldRank: 1, expectedChange: -1 },
|
||||
{ id: "C", currentRank: 3, oldRank: 2, expectedChange: -1 },
|
||||
{ id: "D", currentRank: 4, oldRank: null, expectedChange: 0 },
|
||||
];
|
||||
|
||||
teams.forEach((team) => {
|
||||
const change = calculateSevenDayChange(team.currentRank, team.oldRank);
|
||||
expect(change).toBe(team.expectedChange);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("Historical Tracking", () => {
|
||||
it("should maintain chronological order of snapshots", () => {
|
||||
const snapshots = [
|
||||
{ date: "2025-01-13", rank: 1 },
|
||||
{ date: "2025-01-12", rank: 2 },
|
||||
{ date: "2025-01-11", rank: 3 },
|
||||
{ date: "2025-01-10", rank: 2 },
|
||||
{ date: "2025-01-09", rank: 1 },
|
||||
];
|
||||
|
||||
// Should be sorted by date descending (newest first)
|
||||
const sorted = [...snapshots].sort((a, b) =>
|
||||
b.date.localeCompare(a.date)
|
||||
);
|
||||
|
||||
expect(sorted[0].date).toBe("2025-01-13");
|
||||
expect(sorted[4].date).toBe("2025-01-09");
|
||||
});
|
||||
|
||||
it("should show rank progression over time", () => {
|
||||
const history = [
|
||||
{ date: "2025-01-09", rank: 5, points: 50 },
|
||||
{ date: "2025-01-10", rank: 4, points: 75 },
|
||||
{ date: "2025-01-11", rank: 3, points: 100 },
|
||||
{ date: "2025-01-12", rank: 2, points: 125 },
|
||||
{ date: "2025-01-13", rank: 1, points: 150 },
|
||||
];
|
||||
|
||||
// Verify progression shows improvement
|
||||
for (let i = 1; i < history.length; i++) {
|
||||
expect(history[i].rank).toBeLessThan(history[i - 1].rank);
|
||||
expect(history[i].points).toBeGreaterThan(history[i - 1].points);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -69,6 +69,7 @@ export default [
|
|||
route("templates/new", "routes/admin.templates.new.tsx"),
|
||||
route("templates/:id", "routes/admin.templates.$id.tsx"),
|
||||
route("data-sync", "routes/admin.data-sync.tsx"),
|
||||
route("standings-snapshots", "routes/admin.standings-snapshots.tsx"),
|
||||
]),
|
||||
route(
|
||||
"api/admin/export-sports-data",
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ import {
|
|||
CardTitle,
|
||||
} from "~/components/ui/card";
|
||||
import { Button } from "~/components/ui/button";
|
||||
import { Trophy, Calendar, FolderKanban, ArrowRight } from "lucide-react";
|
||||
import { Trophy, Calendar, FolderKanban, ArrowRight, Camera } from "lucide-react";
|
||||
|
||||
export async function loader() {
|
||||
const [sports, sportsSeasons, templates] = await Promise.all([
|
||||
|
|
@ -109,6 +109,12 @@ export default function AdminDashboard({ loaderData }: Route.ComponentProps) {
|
|||
<ArrowRight className="h-4 w-4" />
|
||||
</Link>
|
||||
</Button>
|
||||
<Button variant="outline" className="w-full justify-between" asChild>
|
||||
<Link to="/admin/standings-snapshots">
|
||||
Manage Standings Snapshots
|
||||
<ArrowRight className="h-4 w-4" />
|
||||
</Link>
|
||||
</Button>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
|
|
|
|||
308
app/routes/admin.standings-snapshots.tsx
Normal file
308
app/routes/admin.standings-snapshots.tsx
Normal file
|
|
@ -0,0 +1,308 @@
|
|||
import { useLoaderData, useActionData, Form } from "react-router";
|
||||
import { redirect } from "react-router";
|
||||
import type { Route } from "./+types/admin.standings-snapshots";
|
||||
import { database } from "~/database/context";
|
||||
import { eq, or } from "drizzle-orm";
|
||||
import * as schema from "~/database/schema";
|
||||
import { createDailySnapshot } from "~/models/standings";
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardDescription,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from "~/components/ui/card";
|
||||
import { Button } from "~/components/ui/button";
|
||||
import { Badge } from "~/components/ui/badge";
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
} from "~/components/ui/table";
|
||||
import { Calendar, RefreshCw, CheckCircle2, AlertCircle } from "lucide-react";
|
||||
|
||||
export async function loader() {
|
||||
const db = database();
|
||||
|
||||
// Get all active and draft seasons
|
||||
const seasons = await db.query.seasons.findMany({
|
||||
where: or(
|
||||
eq(schema.seasons.status, "active"),
|
||||
eq(schema.seasons.status, "draft")
|
||||
),
|
||||
with: {
|
||||
league: true,
|
||||
},
|
||||
orderBy: (seasons, { desc }) => [desc(seasons.createdAt)],
|
||||
});
|
||||
|
||||
// For each season, get the most recent snapshot
|
||||
const seasonsWithSnapshots = await Promise.all(
|
||||
seasons.map(async (season) => {
|
||||
const mostRecentSnapshot = await db.query.teamStandingsSnapshots.findFirst({
|
||||
where: eq(schema.teamStandingsSnapshots.seasonId, season.id),
|
||||
orderBy: (snapshots, { desc }) => [desc(snapshots.snapshotDate)],
|
||||
});
|
||||
|
||||
return {
|
||||
...season,
|
||||
lastSnapshotDate: mostRecentSnapshot?.snapshotDate || null,
|
||||
lastSnapshotTime: mostRecentSnapshot?.createdAt || null,
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
return {
|
||||
seasons: seasonsWithSnapshots,
|
||||
};
|
||||
}
|
||||
|
||||
export async function action({ request }: Route.ActionArgs) {
|
||||
const db = database();
|
||||
const formData = await request.formData();
|
||||
const intent = formData.get("intent");
|
||||
const seasonId = formData.get("seasonId") as string;
|
||||
|
||||
try {
|
||||
if (intent === "create-snapshot") {
|
||||
if (seasonId === "all") {
|
||||
// Create snapshots for all active seasons
|
||||
const seasons = await db.query.seasons.findMany({
|
||||
where: or(
|
||||
eq(schema.seasons.status, "active"),
|
||||
eq(schema.seasons.status, "draft")
|
||||
),
|
||||
});
|
||||
|
||||
for (const season of seasons) {
|
||||
await createDailySnapshot(season.id, db);
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: `Created snapshots for ${seasons.length} season(s)`,
|
||||
};
|
||||
} else {
|
||||
// Create snapshot for specific season
|
||||
await createDailySnapshot(seasonId, db);
|
||||
return {
|
||||
success: true,
|
||||
message: "Snapshot created successfully",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: false,
|
||||
message: "Invalid intent",
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error creating snapshot:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: error instanceof Error ? error.message : "Unknown error",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export default function AdminStandingsSnapshots({
|
||||
loaderData,
|
||||
actionData,
|
||||
}: Route.ComponentProps) {
|
||||
const { seasons } = loaderData;
|
||||
const today = new Date().toISOString().split("T")[0];
|
||||
|
||||
return (
|
||||
<div className="p-8">
|
||||
{/* Header */}
|
||||
<div className="mb-8">
|
||||
<h1 className="text-3xl font-bold mb-2">Standings Snapshots</h1>
|
||||
<p className="text-muted-foreground">
|
||||
Manage daily standings snapshots for historical tracking and 7-day
|
||||
comparison
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Action Result */}
|
||||
{actionData && (
|
||||
<Card
|
||||
className={`mb-6 ${
|
||||
actionData.success
|
||||
? "border-green-500 bg-green-50"
|
||||
: "border-red-500 bg-red-50"
|
||||
}`}
|
||||
>
|
||||
<CardContent className="flex items-center gap-3 pt-6">
|
||||
{actionData.success ? (
|
||||
<CheckCircle2 className="h-5 w-5 text-green-600" />
|
||||
) : (
|
||||
<AlertCircle className="h-5 w-5 text-red-600" />
|
||||
)}
|
||||
<p
|
||||
className={
|
||||
actionData.success ? "text-green-900" : "text-red-900"
|
||||
}
|
||||
>
|
||||
{actionData.message}
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{/* Info Card */}
|
||||
<Card className="mb-6">
|
||||
<CardHeader>
|
||||
<CardTitle className="flex items-center gap-2">
|
||||
<Calendar className="h-5 w-5" />
|
||||
Snapshot System
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
Snapshots are automatically created daily for all active seasons.
|
||||
Use the buttons below to manually trigger snapshot creation if
|
||||
needed.
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="space-y-3 text-sm">
|
||||
<p>
|
||||
<strong>Automatic Snapshots:</strong> The system creates snapshots
|
||||
once per day for all active seasons that don't already have a snapshot for today.
|
||||
</p>
|
||||
<p>
|
||||
<strong>Manual Snapshots:</strong> Use the "Create Snapshot"
|
||||
buttons below to manually trigger snapshot creation for specific
|
||||
seasons or all active seasons.
|
||||
</p>
|
||||
<p>
|
||||
<strong>7-Day Comparison:</strong> Standings pages show movement
|
||||
indicators comparing current rank to the snapshot from 7 days ago.
|
||||
</p>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Bulk Actions */}
|
||||
<Card className="mb-6">
|
||||
<CardHeader>
|
||||
<CardTitle>Bulk Actions</CardTitle>
|
||||
<CardDescription>
|
||||
Create snapshots for all active seasons at once
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<Form method="post">
|
||||
<input type="hidden" name="intent" value="create-snapshot" />
|
||||
<input type="hidden" name="seasonId" value="all" />
|
||||
<Button type="submit" className="w-full sm:w-auto">
|
||||
<RefreshCw className="h-4 w-4 mr-2" />
|
||||
Create Snapshots for All Active Seasons
|
||||
</Button>
|
||||
</Form>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Seasons Table */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Active Seasons</CardTitle>
|
||||
<CardDescription>
|
||||
{seasons.length} season(s) eligible for snapshots
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{seasons.length === 0 ? (
|
||||
<div className="text-center py-12 text-muted-foreground">
|
||||
<p>No active or draft seasons found</p>
|
||||
</div>
|
||||
) : (
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead>League</TableHead>
|
||||
<TableHead>Season</TableHead>
|
||||
<TableHead>Status</TableHead>
|
||||
<TableHead>Last Snapshot</TableHead>
|
||||
<TableHead className="text-right">Actions</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{seasons.map((season) => {
|
||||
const hasSnapshotToday =
|
||||
season.lastSnapshotDate === today;
|
||||
return (
|
||||
<TableRow key={season.id}>
|
||||
<TableCell className="font-medium">
|
||||
{season.league.name}
|
||||
</TableCell>
|
||||
<TableCell>{season.year}</TableCell>
|
||||
<TableCell>
|
||||
<Badge
|
||||
variant={
|
||||
season.status === "active"
|
||||
? "default"
|
||||
: "secondary"
|
||||
}
|
||||
>
|
||||
{season.status}
|
||||
</Badge>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
{season.lastSnapshotDate ? (
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="text-sm">
|
||||
{season.lastSnapshotDate}
|
||||
</span>
|
||||
{hasSnapshotToday && (
|
||||
<Badge
|
||||
variant="outline"
|
||||
className="text-xs bg-green-50 text-green-700 border-green-200"
|
||||
>
|
||||
Today
|
||||
</Badge>
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
<span className="text-muted-foreground text-sm">
|
||||
No snapshots yet
|
||||
</span>
|
||||
)}
|
||||
</TableCell>
|
||||
<TableCell className="text-right">
|
||||
<Form method="post" className="inline">
|
||||
<input
|
||||
type="hidden"
|
||||
name="intent"
|
||||
value="create-snapshot"
|
||||
/>
|
||||
<input
|
||||
type="hidden"
|
||||
name="seasonId"
|
||||
value={season.id}
|
||||
/>
|
||||
<Button
|
||||
type="submit"
|
||||
variant="outline"
|
||||
size="sm"
|
||||
disabled={hasSnapshotToday}
|
||||
>
|
||||
<RefreshCw className="h-3 w-3 mr-1" />
|
||||
{hasSnapshotToday
|
||||
? "Already Created"
|
||||
: "Create Snapshot"}
|
||||
</Button>
|
||||
</Form>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
);
|
||||
})}
|
||||
</TableBody>
|
||||
</Table>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
@ -6,6 +6,7 @@ import * as schema from "~/database/schema";
|
|||
import { Button } from "~/components/ui/button";
|
||||
import { Card, CardContent, CardHeader, CardTitle } from "~/components/ui/card";
|
||||
import { StandingsTable } from "~/components/standings/StandingsTable";
|
||||
import { getSevenDayStandingsChange } from "~/models/standings";
|
||||
|
||||
export async function loader(args: any) {
|
||||
try {
|
||||
|
|
@ -63,46 +64,14 @@ export async function loader(args: any) {
|
|||
});
|
||||
}
|
||||
|
||||
// Fetch standings
|
||||
const standings = await db.query.teamStandings.findMany({
|
||||
where: eq(schema.teamStandings.seasonId, seasonId),
|
||||
with: {
|
||||
team: true,
|
||||
},
|
||||
});
|
||||
// Fetch standings with 7-day comparison
|
||||
const standingsWithComparison = await getSevenDayStandingsChange(seasonId, db);
|
||||
|
||||
// Sort by currentRank ascending (1 is best)
|
||||
const sorted = standings.sort((a, b) => a.currentRank - b.currentRank);
|
||||
|
||||
const formattedStandings = sorted.map((standing) => {
|
||||
if (!standing.team) {
|
||||
console.error("Standing missing team:", standing);
|
||||
throw new Error(`Standing for team ${standing.teamId} has no team relation`);
|
||||
}
|
||||
|
||||
return {
|
||||
teamId: standing.teamId,
|
||||
teamName: standing.team.name,
|
||||
totalPoints: parseFloat(standing.totalPoints || "0"),
|
||||
currentRank: standing.currentRank || 0,
|
||||
previousRank: standing.previousRank || null,
|
||||
rankChange: standing.previousRank
|
||||
? standing.previousRank - standing.currentRank
|
||||
: 0,
|
||||
placementCounts: {
|
||||
first: standing.firstPlaceCount || 0,
|
||||
second: standing.secondPlaceCount || 0,
|
||||
third: standing.thirdPlaceCount || 0,
|
||||
fourth: standing.fourthPlaceCount || 0,
|
||||
fifth: standing.fifthPlaceCount || 0,
|
||||
sixth: standing.sixthPlaceCount || 0,
|
||||
seventh: standing.seventhPlaceCount || 0,
|
||||
eighth: standing.eighthPlaceCount || 0,
|
||||
},
|
||||
participantsRemaining: standing.participantsRemaining || 0,
|
||||
calculatedAt: standing.calculatedAt,
|
||||
};
|
||||
});
|
||||
// Map to format expected by component (use sevenDayRankChange instead of previousRank)
|
||||
const formattedStandings = standingsWithComparison.map((standing) => ({
|
||||
...standing,
|
||||
rankChange: standing.sevenDayRankChange,
|
||||
}));
|
||||
|
||||
return {
|
||||
league,
|
||||
|
|
@ -173,7 +142,8 @@ export default function LeagueStandings() {
|
|||
</p>
|
||||
<p>
|
||||
<strong>Movement Indicators:</strong> Arrows show rank changes
|
||||
since the last standings update.
|
||||
compared to 7 days ago. Green arrows (↑) indicate improvement in rank,
|
||||
red arrows (↓) indicate decline.
|
||||
</p>
|
||||
<p>
|
||||
<strong>Placement Breakdown:</strong> Shows how many times each
|
||||
|
|
|
|||
|
|
@ -1305,11 +1305,21 @@ scoring_events {
|
|||
- app/models/__tests__/tiebreaker-logic.test.ts (new tests)
|
||||
- app/components/standings/__tests__/StandingsTable.test.tsx (new tests)
|
||||
|
||||
- [ ] **4.2** Movement tracking & snapshots
|
||||
- [ ] Daily snapshot creation (scheduled job)
|
||||
- [ ] 7-day comparison logic (Q16, Q12)
|
||||
- [ ] Movement indicators (up/down arrows)
|
||||
- [ ] Store snapshots for historical analysis (Q16)
|
||||
- [x] **4.2** Movement tracking & snapshots ✅ *Completed*
|
||||
- [x] Daily snapshot creation (scheduled job)
|
||||
- [x] 7-day comparison logic (Q16, Q12)
|
||||
- [x] Movement indicators (up/down arrows)
|
||||
- [x] Store snapshots for historical analysis (Q16)
|
||||
|
||||
**Implementation Notes**:
|
||||
- Created `server/snapshots.ts` - automated daily snapshot system (runs once per day)
|
||||
- System starts automatically with server initialization in `server/socket.ts`
|
||||
- Updated `app/routes/leagues/$leagueId.standings.$seasonId.tsx` to use `getSevenDayStandingsChange()`
|
||||
- Created admin interface at `/admin/standings-snapshots` for manual snapshot management
|
||||
- Bulk actions available to create snapshots for all active seasons
|
||||
- Movement indicators show green arrows for rank improvements, red arrows for declines
|
||||
- Comprehensive test suite with 15 unit tests in `app/models/__tests__/standings-snapshots.test.ts`
|
||||
- All 386 tests passing ✅
|
||||
|
||||
- [ ] **4.3** Team breakdown pages
|
||||
- [ ] `TeamScoreBreakdown` component
|
||||
|
|
@ -1436,10 +1446,11 @@ scoring_events {
|
|||
- **3.4** Pattern-specific UI Components - PlayoffBracket, SeasonStandings, SportSeasonDisplay components + member route
|
||||
- ⏳ **Phase 4**: Standings & Display - IN PROGRESS
|
||||
- ✅ **4.1** Enhanced Standings Table - COMPLETE (tiebreaker logic, placement breakdowns, standalone page, navigation)
|
||||
- ✅ **4.2** Movement Tracking & Snapshots - COMPLETE (daily snapshots, 7-day comparison, admin interface)
|
||||
- ⏳ **4.5** League Home Integration - PARTIAL (standings link added, sports season cards pending)
|
||||
- ⏳ **Phase 5**: Expected Value - TODO
|
||||
- ⏳ **Phase 6**: Polish & Optimization - TODO
|
||||
|
||||
**Current Focus**: Phase 4 - Standings & Display (4.1 complete with standalone page, 4.5 partially complete)
|
||||
**Current Focus**: Phase 4 - Standings & Display (4.1 and 4.2 complete, 4.5 partially complete)
|
||||
|
||||
**Total Test Count**: 371 tests passing ✅
|
||||
**Total Test Count**: 386 tests passing ✅
|
||||
|
|
|
|||
128
server/snapshots.ts
Normal file
128
server/snapshots.ts
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
import { drizzle } from "drizzle-orm/postgres-js";
|
||||
import postgres from "postgres";
|
||||
import * as schema from "~/database/schema";
|
||||
import { eq, inArray, or } from "drizzle-orm";
|
||||
import { createDailySnapshot } from "~/models/standings";
|
||||
|
||||
// Create a dedicated database connection for the snapshot system
|
||||
const connectionString = process.env.DATABASE_URL;
|
||||
if (!connectionString) {
|
||||
throw new Error("DATABASE_URL is required for snapshot system");
|
||||
}
|
||||
const client = postgres(connectionString);
|
||||
const db = drizzle(client, { schema });
|
||||
|
||||
let snapshotInterval: NodeJS.Timeout | null = null;
|
||||
const CHECK_INTERVAL = 24 * 60 * 60 * 1000; // Check once per day (in milliseconds)
|
||||
|
||||
/**
|
||||
* Start the daily snapshot system
|
||||
* Runs once per day to create snapshots for all active seasons
|
||||
*/
|
||||
export function startSnapshotSystem(): void {
|
||||
if (snapshotInterval) {
|
||||
console.log("[Snapshots] Snapshot system already running");
|
||||
return;
|
||||
}
|
||||
|
||||
// Run immediately on startup
|
||||
void createDailySnapshots();
|
||||
|
||||
// Then run once per day
|
||||
snapshotInterval = setInterval(async () => {
|
||||
try {
|
||||
await createDailySnapshots();
|
||||
} catch (error) {
|
||||
console.error("[Snapshots] Error creating daily snapshots:", error);
|
||||
}
|
||||
}, CHECK_INTERVAL);
|
||||
|
||||
console.log("[Snapshots] Daily snapshot system started (runs once per day)");
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop the snapshot system
|
||||
*/
|
||||
export function stopSnapshotSystem(): void {
|
||||
if (snapshotInterval) {
|
||||
clearInterval(snapshotInterval);
|
||||
snapshotInterval = null;
|
||||
console.log("[Snapshots] Snapshot system stopped");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create daily snapshots for all active seasons
|
||||
* Only creates snapshots if they don't already exist for today
|
||||
*/
|
||||
async function createDailySnapshots(): Promise<void> {
|
||||
const today = new Date().toISOString().split("T")[0];
|
||||
|
||||
console.log(`[Snapshots] Checking for snapshots to create (${today})`);
|
||||
|
||||
// Get all seasons that are active or in draft (we want to track standings for these)
|
||||
const activeSeasons = await db.query.seasons.findMany({
|
||||
where: or(
|
||||
eq(schema.seasons.status, "active"),
|
||||
eq(schema.seasons.status, "draft")
|
||||
),
|
||||
});
|
||||
|
||||
if (activeSeasons.length === 0) {
|
||||
console.log("[Snapshots] No active seasons found");
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`[Snapshots] Found ${activeSeasons.length} active season(s)`);
|
||||
|
||||
for (const season of activeSeasons) {
|
||||
try {
|
||||
// Check if we already have a snapshot for today for any team in this season
|
||||
const existingSnapshot = await db.query.teamStandingsSnapshots.findFirst({
|
||||
where: eq(schema.teamStandingsSnapshots.seasonId, season.id),
|
||||
orderBy: (snapshots, { desc }) => [desc(snapshots.snapshotDate)],
|
||||
});
|
||||
|
||||
// If the most recent snapshot is from today, skip this season
|
||||
if (existingSnapshot && existingSnapshot.snapshotDate === today) {
|
||||
console.log(`[Snapshots] Snapshot already exists for season ${season.id} on ${today}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Create snapshot using the standings model function
|
||||
await createDailySnapshot(season.id, db);
|
||||
console.log(`[Snapshots] ✅ Created snapshot for season ${season.id}`);
|
||||
} catch (error) {
|
||||
console.error(`[Snapshots] Error creating snapshot for season ${season.id}:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
console.log("[Snapshots] Daily snapshot check complete");
|
||||
}
|
||||
|
||||
/**
|
||||
* Manually trigger snapshot creation for specific seasons
|
||||
* Useful for admin tools or manual triggers
|
||||
*/
|
||||
export async function createSnapshotsForSeasons(seasonIds: string[]): Promise<void> {
|
||||
console.log(`[Snapshots] Manual trigger for ${seasonIds.length} season(s)`);
|
||||
|
||||
for (const seasonId of seasonIds) {
|
||||
try {
|
||||
await createDailySnapshot(seasonId, db);
|
||||
console.log(`[Snapshots] ✅ Created snapshot for season ${seasonId}`);
|
||||
} catch (error) {
|
||||
console.error(`[Snapshots] Error creating snapshot for season ${seasonId}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Manually trigger snapshot creation for all active seasons
|
||||
* Useful for testing or manual refreshes
|
||||
*/
|
||||
export async function createSnapshotsForAllSeasons(): Promise<void> {
|
||||
console.log("[Snapshots] Manual trigger for all active seasons");
|
||||
await createDailySnapshots();
|
||||
}
|
||||
|
|
@ -174,14 +174,21 @@ export function initializeSocketIO(httpServer: HTTPServer): SocketIOServer {
|
|||
global.__socketIO = io;
|
||||
|
||||
console.log("Socket.IO initialized");
|
||||
|
||||
|
||||
// Start the draft timer system (async import but don't await)
|
||||
import("./timer").then(({ startDraftTimerSystem }) => {
|
||||
startDraftTimerSystem();
|
||||
}).catch((error) => {
|
||||
console.error("Failed to start timer system:", error);
|
||||
});
|
||||
|
||||
|
||||
// Start the daily snapshot system
|
||||
import("./snapshots").then(({ startSnapshotSystem }) => {
|
||||
startSnapshotSystem();
|
||||
}).catch((error) => {
|
||||
console.error("Failed to start snapshot system:", error);
|
||||
});
|
||||
|
||||
return io;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue