-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdb-history.service.ts
126 lines (110 loc) · 3.62 KB
/
db-history.service.ts
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
import { SupabaseClient } from '@supabase/supabase-js';
import { catchError, from, map, Observable, of } from 'rxjs';
export class HistoryQueries {
constructor(
private supabase: SupabaseClient,
private handleError: (error: any) => Observable<never>,
) {}
getChangeHistory(domainName?: string, days: number = 7): Observable<any[]> {
let query = this.supabase
.from('domain_updates')
.select('change_type, date')
.gte('date', new Date(Date.now() - days * 24 * 60 * 60 * 1000).toISOString());
if (domainName) {
query = query.eq('domains.domain_name', domainName);
}
return from(query).pipe(
map(({ data, error }) => {
if (error) {
this.handleError(error);
throw error;
}
// Process data to group by date and change_type
const historyMap: Record<string, { added: number, removed: number, updated: number }> = {};
data.forEach((entry: { date: string, change_type: string }) => {
const date = new Date(entry.date).toISOString().split('T')[0]; // Extract day
if (!historyMap[date]) {
historyMap[date] = { added: 0, removed: 0, updated: 0 };
}
if (entry.change_type === 'added') {
historyMap[date].added += 1;
} else if (entry.change_type === 'removed') {
historyMap[date].removed += 1;
} else {
historyMap[date].updated += 1;
}
});
return Object.entries(historyMap).map(([date, counts]) => ({
date,
...counts,
}));
}),
catchError((error) => {
this.handleError({
message: 'Error fetching change history',
error,
location: 'HistoryQueries.getChangeHistory',
});
return of([]);
})
);
}
getTotalUpdateCount(domainName?: string): Observable<number> {
let query = this.supabase
.from('domain_updates')
.select('id', { count: 'exact' });
if (domainName) {
query = this.supabase
.from<any, any>('domain_updates')
.select('id, domains!inner(domain_name)', { count: 'exact' })
.eq('domains.domain_name', domainName);
}
return from(query.then(({ count, error }: { count: number | null; error: any }) => {
if (error) throw error;
return count || 0;
})).pipe(
catchError(error => {
this.handleError({
message: 'Error fetching total update count',
error,
location: 'HistoryQueries.getTotalUpdateCount',
});
return of(0);
})
);
}
getDomainUpdates(domainName?: string, start: number = 0, end: number = 24, category?: string, changeType?: string, filterDomain?: string): Observable<any[]> {
let query = this.supabase
.from('domain_updates')
.select(`
*,
domains!inner(domain_name)
`)
.order('date', { ascending: false })
.range(start, end);
if (domainName) {
query = query.eq('domains.domain_name', domainName);
}
if (category) {
query = query.eq('change', category);
}
if (changeType) {
query = query.eq('change_type', changeType);
}
if (filterDomain) {
query = query.ilike('domains.domain_name', `%${filterDomain}%`);
}
return from(query).pipe(
map(({ data, error }) => {
if (error) throw error;
return data;
}),
catchError((error) => {
this.handleError({
error, message: 'Error fetching domain updates', location: 'HistoryQueries.getDomainUpdates',
});
return of([]);
})
);
}
}