This commit is contained in:
Death-PC 2025-12-28 17:55:22 +05:30
parent c2d728c3e2
commit 1f78331099
6 changed files with 258 additions and 1557 deletions

View file

@ -4,7 +4,7 @@ WORKDIR /app
COPY package*.json ./
RUN npm ci
RUN npm install
ARG DATABASE_URL
ARG DATABASE_URL_DOCKER

1653
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,13 +1,25 @@
import { getRegistry } from '../../utils/metrics';
import { getRegistry, initializeAllMetrics } from '../../utils/metrics';
import { scopedLogger } from '../../utils/logger';
const log = scopedLogger('metrics-daily-endpoint');
let isInitialized = false;
async function ensureMetricsInitialized() {
if (!isInitialized) {
log.info('Initializing metrics from daily endpoint...', { evt: 'init_start' });
await initializeAllMetrics();
isInitialized = true;
log.info('Metrics initialized from daily endpoint', { evt: 'init_complete' });
}
}
export default defineEventHandler(async event => {
try {
await ensureMetricsInitialized();
// Get the daily registry
const dailyRegistry = getRegistry('daily');
const metrics = await dailyRegistry.metrics();
event.node.res.setHeader('Content-Type', dailyRegistry.contentType);
return metrics;

View file

@ -1,13 +1,25 @@
import { getRegistry } from '../../utils/metrics';
import { getRegistry, initializeAllMetrics } from '../../utils/metrics';
import { scopedLogger } from '../../utils/logger';
const log = scopedLogger('metrics-monthly-endpoint');
let isInitialized = false;
async function ensureMetricsInitialized() {
if (!isInitialized) {
log.info('Initializing metrics from monthly endpoint...', { evt: 'init_start' });
await initializeAllMetrics();
isInitialized = true;
log.info('Metrics initialized from monthly endpoint', { evt: 'init_complete' });
}
}
export default defineEventHandler(async event => {
try {
await ensureMetricsInitialized();
// Get the monthly registry
const monthlyRegistry = getRegistry('monthly');
const metrics = await monthlyRegistry.metrics();
event.node.res.setHeader('Content-Type', monthlyRegistry.contentType);
return metrics;

View file

@ -1,13 +1,25 @@
import { getRegistry } from '../../utils/metrics';
import { getRegistry, initializeAllMetrics } from '../../utils/metrics';
import { scopedLogger } from '../../utils/logger';
const log = scopedLogger('metrics-weekly-endpoint');
let isInitialized = false;
async function ensureMetricsInitialized() {
if (!isInitialized) {
log.info('Initializing metrics from weekly endpoint...', { evt: 'init_start' });
await initializeAllMetrics();
isInitialized = true;
log.info('Metrics initialized from weekly endpoint', { evt: 'init_complete' });
}
}
export default defineEventHandler(async event => {
try {
await ensureMetricsInitialized();
// Get the weekly registry
const weeklyRegistry = getRegistry('weekly');
const metrics = await weeklyRegistry.metrics();
event.node.res.setHeader('Content-Type', weeklyRegistry.contentType);
return metrics;

View file

@ -76,118 +76,6 @@ export default defineEventHandler(async event => {
}));
}
if (event.path.includes('/watch-history/')) {
const segments = event.path.split('/');
const tmdbId = segments[segments.length - 1];
if (method === 'PUT') {
const body = await readBody(event);
const validatedBody = watchHistoryItemSchema.parse(body);
const watchedAt = defaultAndCoerceDateTime(validatedBody.watchedAt);
const now = new Date();
const existingItem = await prisma.watch_history.findUnique({
where: {
tmdb_id_user_id_season_id_episode_id: {
tmdb_id: tmdbId,
user_id: userId,
season_id: validatedBody.seasonId || null,
episode_id: validatedBody.episodeId || null,
},
},
});
let watchHistoryItem;
if (existingItem) {
watchHistoryItem = await prisma.watch_history.update({
where: {
id: existingItem.id,
},
data: {
duration: BigInt(validatedBody.duration),
watched: BigInt(validatedBody.watched),
watched_at: watchedAt,
completed: validatedBody.completed,
meta: validatedBody.meta,
updated_at: now,
},
});
} else {
watchHistoryItem = await prisma.watch_history.create({
data: {
id: randomUUID(),
tmdb_id: tmdbId,
user_id: userId,
season_id: validatedBody.seasonId || null,
episode_id: validatedBody.episodeId || null,
season_number: validatedBody.seasonNumber || null,
episode_number: validatedBody.episodeNumber || null,
duration: BigInt(validatedBody.duration),
watched: BigInt(validatedBody.watched),
watched_at: watchedAt,
completed: validatedBody.completed,
meta: validatedBody.meta,
updated_at: now,
},
});
}
return {
id: watchHistoryItem.id,
tmdbId: watchHistoryItem.tmdb_id,
userId: watchHistoryItem.user_id,
seasonId: watchHistoryItem.season_id,
episodeId: watchHistoryItem.episode_id,
seasonNumber: watchHistoryItem.season_number,
episodeNumber: watchHistoryItem.episode_number,
meta: watchHistoryItem.meta,
duration: Number(watchHistoryItem.duration),
watched: Number(watchHistoryItem.watched),
watchedAt: watchHistoryItem.watched_at.toISOString(),
completed: watchHistoryItem.completed,
updatedAt: watchHistoryItem.updated_at.toISOString(),
};
}
if (method === 'DELETE') {
const body = await readBody(event).catch(() => ({}));
const whereClause: any = {
user_id: userId,
tmdb_id: tmdbId,
};
if (body.seasonId) whereClause.season_id = body.seasonId;
if (body.episodeId) whereClause.episode_id = body.episodeId;
const itemsToDelete = await prisma.watch_history.findMany({
where: whereClause,
});
if (itemsToDelete.length === 0) {
return {
count: 0,
tmdbId,
episodeId: body.episodeId,
seasonId: body.seasonId,
};
}
await prisma.watch_history.deleteMany({
where: whereClause,
});
return {
count: itemsToDelete.length,
tmdbId,
episodeId: body.episodeId,
seasonId: body.seasonId,
};
}
}
throw createError({
statusCode: 405,
message: 'Method not allowed',