File size: 6,052 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import { UseQueryOptions, keepPreviousData, useQuery, useQueryClient } from '@tanstack/react-query';
import { useEffect, useState } from 'react';
import wpcom from 'calypso/lib/wp';

interface SiteLogsAPIResponse {
	message: string;
	data: {
		total_results: number | { value: number; relation: string };
		logs: ( PHPLogFromEndpoint | ServerLogFromEndpoint )[];
		scroll_id: string | null;
	};
}

interface SiteLogsData {
	total_results: number;
	logs: ( ServerLog | PHPLog )[];
	scroll_id: string | null;
	has_more: boolean;
}

interface ServerLogFromEndpoint {
	date: string;
	request_type: 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE';
	status: '200' | '301' | '302' | '400' | '401' | '403' | '404' | '429' | '500';
	request_url: string;
	body_bytes_sent: number;
	cached: string;
	http_host: string;
	http_referer: string;
	http2: string;
	http_user_agent: string;
	http_version: string;
	http_x_forwarded_for: string;
	renderer: string;
	request_completion: string;
	request_time: string;
	scheme: string;
	timestamp: number;
	type: string;
	user_ip: string;
}

export interface ServerLog extends ServerLogFromEndpoint {
	id: string;
}

interface PHPLogFromEndpoint {
	timestamp: string;
	severity: 'User' | 'Warning' | 'Deprecated' | 'Fatal error';
	message: string;
	kind: string;
	name: string;
	file: string;
	line: number;
	atomic_site_id: number;
}

export interface PHPLog extends Omit< PHPLogFromEndpoint, 'atomic_site_id' > {
	id: string;
}

export enum LogType {
	PHP = 'php',
	WEB = 'web',
}

export interface LogQueryParams {
	from: string;
	to: string;
	// PHP filters
	severity: string;
	// Web filters
	request_type: string;
	status: string;
	renderer: string;
	cached: string;
}

export interface FilterType {
	[ key: string ]: Array< string >;
}

interface SiteLogsParams {
	logType: LogType;
	start: number;
	end: number;
	filter: FilterType;
	sortOrder?: 'asc' | 'desc';
	pageSize?: number;
	pageIndex?: number;
}

export function useSiteLogsQuery(
	siteId: number | null | undefined,
	params: SiteLogsParams,
	queryOptions: Omit<
		UseQueryOptions< SiteLogsAPIResponse, unknown, SiteLogsData >,
		'queryKey'
	> = {}
) {
	const queryClient = useQueryClient();
	const [ scrollId, setScrollId ] = useState< string | undefined >();

	// The scroll ID represents the state of a particular set of filtering arguments. If any of
	// those filter arguments change we throw out the scroll ID so we can start over.
	const [ previousSiteId, setPreviousSiteId ] = useState( siteId );
	const [ previousParams, setPreviousParams ] = useState( params );
	if ( previousSiteId !== siteId || ! areRequestParamsEqual( previousParams, params ) ) {
		queryClient.removeQueries( {
			queryKey: buildPartialQueryKey( previousSiteId, previousParams ),
		} );

		// We're updating state directly in the render flow. This is preferable to using an effect.
		// https://react.dev/learn/you-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes
		setPreviousSiteId( siteId );
		setPreviousParams( params );
		setScrollId( undefined );
	}

	const queryResult = useQuery< SiteLogsAPIResponse, unknown, SiteLogsData >( {
		queryKey: buildQueryKey( siteId, params ),
		queryFn: () => {
			const logTypeFragment = params.logType === LogType.PHP ? 'error-logs' : 'logs';
			const path = `/sites/${ siteId }/hosting/${ logTypeFragment }`;
			return wpcom.req.get(
				{ path, apiNamespace: 'wpcom/v2' },
				{
					start: params.start,
					end: params.end,
					filter: params.filter,
					sort_order: params.sortOrder,
					page_size: params.pageSize,
					scroll_id: scrollId,
				}
			);
		},
		placeholderData: keepPreviousData,
		enabled: !! siteId && params.start <= params.end,
		staleTime: Infinity, // The logs within a specified time range never change.
		select( { data }: SiteLogsAPIResponse ): SiteLogsData {
			return {
				has_more: !! data.scroll_id,
				total_results:
					typeof data.total_results === 'number' ? data.total_results : data.total_results.value,
				logs: data.logs.map( ( { atomic_site_id, ...restLog }: any, key ) => {
					return {
						...restLog,
						id: String( key ),
					};
				} ),
				scroll_id: data.scroll_id,
			};
		},
		meta: {
			persist: false,
		},
		...queryOptions,
	} );

	const { data } = queryResult;

	useEffect( () => {
		if ( data?.has_more && scrollId !== data.scroll_id ) {
			setScrollId( data.scroll_id ?? undefined );
		}
	}, [ data, scrollId ] );

	// The state represented by scroll ID will have already advanced to the next page, so we
	// can't allow `refetch` to be used. Remember, the logs are fetched with a POST and the
	// requests are not idempotent.
	const { refetch, ...remainingQueryResults } = queryResult;

	return {
		...remainingQueryResults,
	};
}

function buildPartialQueryKey( siteId: number | null | undefined, params: SiteLogsParams ) {
	return [ params.logType === LogType.PHP ? 'site-logs-php' : 'site-logs-web', siteId ];
}

function buildQueryKey( siteId: number | null | undefined, params: SiteLogsParams ) {
	return [
		...buildPartialQueryKey( siteId, params ),
		params.start,
		params.end,
		params.filter,
		params.sortOrder,
		params.pageSize,
		params.pageIndex,
	];
}

// Request params are equal if every field is the same _except_ for the page index.
// The page index is not part of the request body, it is only part of the query key.
function areRequestParamsEqual( a: SiteLogsParams, b: SiteLogsParams ) {
	return (
		a.logType === b.logType &&
		a.start === b.start &&
		a.end === b.end &&
		a.sortOrder === b.sortOrder &&
		a.pageSize === b.pageSize &&
		areFilterParamsEqual( a.filter, b.filter )
	);
}

function areFilterParamsEqual( a: FilterType, b: FilterType ) {
	for ( const filter in a ) {
		if ( ! b.hasOwnProperty( filter ) ) {
			return false;
		}

		if ( a[ filter ].toString() !== b[ filter ].toString() ) {
			return false;
		}
	}

	for ( const filter in b ) {
		if ( ! a.hasOwnProperty( filter ) ) {
			return false;
		}

		if ( b[ filter ].toString() !== a[ filter ].toString() ) {
			return false;
		}
	}

	return true;
}