Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { prisma } from "./prisma.js";
/** Агрегаты по каталогу — открытые данные (без пользовательской приватной статистики). */
export interface CatalogStats {
tracksCount: number;
totalDistanceKm: number;
totalElevationM: number;
likesTotal: number;
favoritesTotal: number;
byType: { label: string; count: number }[];
byLayout: { label: string; count: number }[];
topLiked: { id: number; name: string; count: number }[];
topFavorited: { id: number; name: string; count: number }[];
}
/** Считает публичные агрегаты каталога. Переиспользуется публичным /stats и админским /admin/stats. */
export async function computeCatalogStats(): Promise<CatalogStats> {
const [
tracksAgg,
byTypeRaw,
byLayoutRaw,
likesTotal,
favoritesTotal,
types,
layouts,
topLikedRows,
topFavRows,
] = await Promise.all([
prisma.gpxTrack.aggregate({
where: { deletedAt: null },
_count: { _all: true },
_sum: { distanceKm: true, elevationM: true },
}),
prisma.gpxTrack.groupBy({
by: ["trackTypeId"],
where: { deletedAt: null },
_count: { _all: true },
}),
prisma.gpxTrack.groupBy({
by: ["routeLayoutId"],
where: { deletedAt: null },
_count: { _all: true },
}),
prisma.trackLike.count(),
prisma.trackFavorite.count(),
prisma.trackType.findMany({ select: { id: true, label: true } }),
prisma.routeLayout.findMany({ select: { id: true, label: true } }),
prisma.gpxTrack.findMany({
where: { deletedAt: null },
select: { id: true, name: true, _count: { select: { likes: true } } },
orderBy: { likes: { _count: "desc" } },
take: 5,
}),
prisma.gpxTrack.findMany({
where: { deletedAt: null },
select: { id: true, name: true, _count: { select: { favorites: true } } },
orderBy: { favorites: { _count: "desc" } },
take: 5,
}),
]);
const typeLabel = new Map(types.map((t) => [t.id, t.label]));
const layoutLabel = new Map(layouts.map((l) => [l.id, l.label]));
return {
tracksCount: tracksAgg._count._all,
totalDistanceKm: Math.round((tracksAgg._sum.distanceKm ?? 0) * 10) / 10,
totalElevationM: Math.round(tracksAgg._sum.elevationM ?? 0),
likesTotal,
favoritesTotal,
byType: byTypeRaw
.map((r) => ({ label: typeLabel.get(r.trackTypeId) ?? "?", count: r._count._all }))
.sort((a, b) => b.count - a.count),
byLayout: byLayoutRaw
.map((r) => ({ label: layoutLabel.get(r.routeLayoutId) ?? "?", count: r._count._all }))
.sort((a, b) => b.count - a.count),
topLiked: topLikedRows
.filter((t) => t._count.likes > 0)
.map((t) => ({ id: t.id, name: t.name, count: t._count.likes })),
topFavorited: topFavRows
.filter((t) => t._count.favorites > 0)
.map((t) => ({ id: t.id, name: t.name, count: t._count.favorites })),
};
}
/** JSON-схема публичных полей статистики (для ответов Fastify). */
export const catalogStatsProperties = {
tracksCount: { type: "integer" },
totalDistanceKm: { type: "number" },
totalElevationM: { type: "integer" },
likesTotal: { type: "integer" },
favoritesTotal: { type: "integer" },
byType: {
type: "array",
items: {
type: "object",
required: ["label", "count"],
additionalProperties: false,
properties: { label: { type: "string" }, count: { type: "integer" } },
},
},
byLayout: {
type: "array",
items: {
type: "object",
required: ["label", "count"],
additionalProperties: false,
properties: { label: { type: "string" }, count: { type: "integer" } },
},
},
topLiked: {
type: "array",
items: {
type: "object",
required: ["id", "name", "count"],
additionalProperties: false,
properties: { id: { type: "integer" }, name: { type: "string" }, count: { type: "integer" } },
},
},
topFavorited: {
type: "array",
items: {
type: "object",
required: ["id", "name", "count"],
additionalProperties: false,
properties: { id: { type: "integer" }, name: { type: "string" }, count: { type: "integer" } },
},
},
} as const;
/** Имена публичных полей статистики — required в схемах. */
export const catalogStatsRequired = [
"tracksCount",
"totalDistanceKm",
"totalElevationM",
"likesTotal",
"favoritesTotal",
"byType",
"byLayout",
"topLiked",
"topFavorited",
] as const;
|