|
| 1 | +import type { NetworkLog } from '../types'; |
| 2 | + |
| 3 | +export type StatusFilterKey = |
| 4 | + | 'all' |
| 5 | + | 'success' |
| 6 | + | 'redirect' |
| 7 | + | 'clientError' |
| 8 | + | 'serverError' |
| 9 | + | 'error'; |
| 10 | + |
| 11 | +export interface FilterState { |
| 12 | + searchTerm: string; |
| 13 | + statusFilter: StatusFilterKey; |
| 14 | + methodFilter: string | null; |
| 15 | + apiOnly: boolean; |
| 16 | +} |
| 17 | + |
| 18 | +export const defaultFilterState: FilterState = { |
| 19 | + searchTerm: '', |
| 20 | + statusFilter: 'all', |
| 21 | + methodFilter: null, |
| 22 | + apiOnly: false, |
| 23 | +}; |
| 24 | + |
| 25 | +export const matchesSearchTerm = ( |
| 26 | + log: NetworkLog, |
| 27 | + searchTerm: string |
| 28 | +): boolean => { |
| 29 | + if (!searchTerm) return true; |
| 30 | + const lowerSearch = searchTerm.toLowerCase(); |
| 31 | + return ( |
| 32 | + log.url.toLowerCase().includes(lowerSearch) || |
| 33 | + log.method.toLowerCase().includes(lowerSearch) || |
| 34 | + (log.response?.body?.toLowerCase().includes(lowerSearch) ?? false) || |
| 35 | + (log.body?.toLowerCase().includes(lowerSearch) ?? false) |
| 36 | + ); |
| 37 | +}; |
| 38 | + |
| 39 | +export const matchesStatusFilter = ( |
| 40 | + log: NetworkLog, |
| 41 | + statusFilter: StatusFilterKey |
| 42 | +): boolean => { |
| 43 | + const status = log.response?.status; |
| 44 | + const hasError = !!log.error; |
| 45 | + |
| 46 | + switch (statusFilter) { |
| 47 | + case 'all': |
| 48 | + return true; |
| 49 | + case 'success': |
| 50 | + return status !== undefined && status >= 200 && status < 300; |
| 51 | + case 'redirect': |
| 52 | + return status !== undefined && status >= 300 && status < 400; |
| 53 | + case 'clientError': |
| 54 | + return status !== undefined && status >= 400 && status < 500; |
| 55 | + case 'serverError': |
| 56 | + return status !== undefined && status >= 500; |
| 57 | + case 'error': |
| 58 | + return hasError || (status !== undefined && status >= 400); |
| 59 | + default: |
| 60 | + return true; |
| 61 | + } |
| 62 | +}; |
| 63 | + |
| 64 | +export const matchesMethodFilter = ( |
| 65 | + log: NetworkLog, |
| 66 | + methodFilter: string | null |
| 67 | +): boolean => { |
| 68 | + if (!methodFilter) return true; |
| 69 | + return log.method.toUpperCase() === methodFilter.toUpperCase(); |
| 70 | +}; |
| 71 | + |
| 72 | +export const matchesApiFilter = ( |
| 73 | + log: NetworkLog, |
| 74 | + apiOnly: boolean |
| 75 | +): boolean => { |
| 76 | + if (!apiOnly) return true; |
| 77 | + return log.url.toLowerCase().includes('api'); |
| 78 | +}; |
| 79 | + |
| 80 | +export const filterLogs = ( |
| 81 | + logs: NetworkLog[], |
| 82 | + filters: FilterState |
| 83 | +): NetworkLog[] => { |
| 84 | + return logs.filter((log) => { |
| 85 | + return ( |
| 86 | + matchesSearchTerm(log, filters.searchTerm) && |
| 87 | + matchesStatusFilter(log, filters.statusFilter) && |
| 88 | + matchesMethodFilter(log, filters.methodFilter) && |
| 89 | + matchesApiFilter(log, filters.apiOnly) |
| 90 | + ); |
| 91 | + }); |
| 92 | +}; |
| 93 | + |
| 94 | +export const shouldIgnoreUrl = (url: string, patterns: string[]): boolean => { |
| 95 | + if (patterns.length === 0) return false; |
| 96 | + return patterns.some((pattern) => { |
| 97 | + if (pattern.includes('*')) { |
| 98 | + const regex = new RegExp(pattern.replace(/\*/g, '.*'), 'i'); |
| 99 | + return regex.test(url); |
| 100 | + } |
| 101 | + return url.toLowerCase().includes(pattern.toLowerCase()); |
| 102 | + }); |
| 103 | +}; |
| 104 | + |
| 105 | +export const shouldIgnoreDomain = (url: string, domains: string[]): boolean => { |
| 106 | + if (domains.length === 0) return false; |
| 107 | + try { |
| 108 | + const urlObj = new URL(url); |
| 109 | + return domains.some( |
| 110 | + (domain) => urlObj.hostname.toLowerCase() === domain.toLowerCase() |
| 111 | + ); |
| 112 | + } catch { |
| 113 | + return false; |
| 114 | + } |
| 115 | +}; |
| 116 | + |
| 117 | +export const getLogStats = ( |
| 118 | + logs: NetworkLog[] |
| 119 | +): { |
| 120 | + total: number; |
| 121 | + success: number; |
| 122 | + errors: number; |
| 123 | + pending: number; |
| 124 | + avgDuration: number; |
| 125 | +} => { |
| 126 | + const stats = { |
| 127 | + total: logs.length, |
| 128 | + success: 0, |
| 129 | + errors: 0, |
| 130 | + pending: 0, |
| 131 | + avgDuration: 0, |
| 132 | + }; |
| 133 | + |
| 134 | + let totalDuration = 0; |
| 135 | + let durationCount = 0; |
| 136 | + |
| 137 | + logs.forEach((log) => { |
| 138 | + const status = log.response?.status; |
| 139 | + const duration = log.response?.duration || log.duration; |
| 140 | + |
| 141 | + if (log.error || (status !== undefined && status >= 400)) { |
| 142 | + stats.errors++; |
| 143 | + } else if (status !== undefined && status >= 200 && status < 400) { |
| 144 | + stats.success++; |
| 145 | + } else { |
| 146 | + stats.pending++; |
| 147 | + } |
| 148 | + |
| 149 | + if (duration) { |
| 150 | + totalDuration += duration; |
| 151 | + durationCount++; |
| 152 | + } |
| 153 | + }); |
| 154 | + |
| 155 | + stats.avgDuration = |
| 156 | + durationCount > 0 ? Math.round(totalDuration / durationCount) : 0; |
| 157 | + |
| 158 | + return stats; |
| 159 | +}; |
0 commit comments