mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-24 10:31:12 -05:00
Implement monitor-rate-limits edge function to compare metrics against alert configurations, trigger notifications, and record alerts; update config and groundwork for admin UI integration.
174 lines
4.5 KiB
TypeScript
174 lines
4.5 KiB
TypeScript
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
|
import { supabase } from '@/integrations/supabase/client';
|
|
import { toast } from 'sonner';
|
|
|
|
export interface AlertConfig {
|
|
id: string;
|
|
metric_type: 'block_rate' | 'total_requests' | 'unique_ips' | 'function_specific';
|
|
threshold_value: number;
|
|
time_window_ms: number;
|
|
function_name?: string;
|
|
enabled: boolean;
|
|
created_at: string;
|
|
updated_at: string;
|
|
}
|
|
|
|
export interface Alert {
|
|
id: string;
|
|
config_id: string;
|
|
metric_type: string;
|
|
metric_value: number;
|
|
threshold_value: number;
|
|
time_window_ms: number;
|
|
function_name?: string;
|
|
alert_message: string;
|
|
resolved_at?: string;
|
|
created_at: string;
|
|
}
|
|
|
|
export function useAlertConfigs() {
|
|
return useQuery({
|
|
queryKey: ['rateLimitAlertConfigs'],
|
|
queryFn: async () => {
|
|
const { data, error } = await supabase
|
|
.from('rate_limit_alert_config')
|
|
.select('*')
|
|
.order('metric_type');
|
|
|
|
if (error) throw error;
|
|
return data as AlertConfig[];
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useAlertHistory(limit: number = 50) {
|
|
return useQuery({
|
|
queryKey: ['rateLimitAlerts', limit],
|
|
queryFn: async () => {
|
|
const { data, error } = await supabase
|
|
.from('rate_limit_alerts')
|
|
.select('*')
|
|
.order('created_at', { ascending: false })
|
|
.limit(limit);
|
|
|
|
if (error) throw error;
|
|
return data as Alert[];
|
|
},
|
|
refetchInterval: 30000, // Refetch every 30 seconds
|
|
});
|
|
}
|
|
|
|
export function useUnresolvedAlerts() {
|
|
return useQuery({
|
|
queryKey: ['rateLimitAlertsUnresolved'],
|
|
queryFn: async () => {
|
|
const { data, error } = await supabase
|
|
.from('rate_limit_alerts')
|
|
.select('*')
|
|
.is('resolved_at', null)
|
|
.order('created_at', { ascending: false });
|
|
|
|
if (error) throw error;
|
|
return data as Alert[];
|
|
},
|
|
refetchInterval: 15000, // Refetch every 15 seconds
|
|
});
|
|
}
|
|
|
|
export function useUpdateAlertConfig() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: async ({ id, updates }: { id: string; updates: Partial<AlertConfig> }) => {
|
|
const { data, error } = await supabase
|
|
.from('rate_limit_alert_config')
|
|
.update(updates)
|
|
.eq('id', id)
|
|
.select()
|
|
.single();
|
|
|
|
if (error) throw error;
|
|
return data;
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['rateLimitAlertConfigs'] });
|
|
toast.success('Alert configuration updated');
|
|
},
|
|
onError: (error) => {
|
|
toast.error(`Failed to update alert config: ${error.message}`);
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useCreateAlertConfig() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: async (config: Omit<AlertConfig, 'id' | 'created_at' | 'updated_at'>) => {
|
|
const { data, error } = await supabase
|
|
.from('rate_limit_alert_config')
|
|
.insert(config)
|
|
.select()
|
|
.single();
|
|
|
|
if (error) throw error;
|
|
return data;
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['rateLimitAlertConfigs'] });
|
|
toast.success('Alert configuration created');
|
|
},
|
|
onError: (error) => {
|
|
toast.error(`Failed to create alert config: ${error.message}`);
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useDeleteAlertConfig() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: async (id: string) => {
|
|
const { error } = await supabase
|
|
.from('rate_limit_alert_config')
|
|
.delete()
|
|
.eq('id', id);
|
|
|
|
if (error) throw error;
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['rateLimitAlertConfigs'] });
|
|
toast.success('Alert configuration deleted');
|
|
},
|
|
onError: (error) => {
|
|
toast.error(`Failed to delete alert config: ${error.message}`);
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useResolveAlert() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: async (id: string) => {
|
|
const { data, error } = await supabase
|
|
.from('rate_limit_alerts')
|
|
.update({ resolved_at: new Date().toISOString() })
|
|
.eq('id', id)
|
|
.select()
|
|
.single();
|
|
|
|
if (error) throw error;
|
|
return data;
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['rateLimitAlerts'] });
|
|
queryClient.invalidateQueries({ queryKey: ['rateLimitAlertsUnresolved'] });
|
|
toast.success('Alert resolved');
|
|
},
|
|
onError: (error) => {
|
|
toast.error(`Failed to resolve alert: ${error.message}`);
|
|
},
|
|
});
|
|
}
|