File size: 11,160 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import page from '@automattic/calypso-router';
import {
	Button,
	__experimentalToggleGroupControl as ToggleGroupControl,
	__experimentalToggleGroupControlOption as ToggleGroupControlOption,
	ToggleControl,
} from '@wordpress/components';
import { DataViews } from '@wordpress/dataviews';
import { download } from '@wordpress/icons';
import { useTranslate } from 'i18n-calypso';
import { useCallback, useState, useMemo } from 'react';
import QuerySiteSettings from 'calypso/components/data/query-site-settings';
import DateControl from 'calypso/components/date-control';
import { getShortcuts } from 'calypso/components/date-range/use-shortcuts';
import InlineSupportLink from 'calypso/components/inline-support-link';
import { useLocalizedMoment } from 'calypso/components/localized-moment';
import NavigationHeader from 'calypso/components/navigation-header';
import {
	LogType,
	LogQueryParams,
	PHPLog,
	ServerLog,
} from 'calypso/data/hosting/use-site-logs-query';
import { useInterval } from 'calypso/lib/interval';
import { navigate } from 'calypso/lib/navigate';
import { useDispatch, useSelector } from 'calypso/state';
import { recordTracksEvent } from 'calypso/state/analytics/actions';
import { getSelectedSiteId } from 'calypso/state/ui/selectors';
import useActions from '../hooks/use-actions';
import useData from '../hooks/use-data';
import useFields from '../hooks/use-fields';
import { useSiteLogsDownloader } from '../hooks/use-site-logs-downloader';
import {
	default as useView,
	toFilterParams,
	getSortField,
	getVisibleFields,
	getFilterValue,
} from '../hooks/use-view';
import DetailsModal from './details-modal';
import type { View, Action, Filter } from '@wordpress/dataviews';
import type { Moment } from 'moment';
import './style.scss';

export const SiteLogsDataViews = ( {
	logType,
	query,
}: {
	logType: LogType;
	query: LogQueryParams;
} ) => {
	const translate = useTranslate();
	const moment = useLocalizedMoment();
	const siteId = useSelector( getSelectedSiteId );
	const dispatch = useDispatch();

	const getMomentFromTimestamp = useCallback(
		( value: string, fallback?: string ) => {
			const fromValue = parseInt( value || '', 10 );
			if ( ! isNaN( fromValue ) ) {
				return moment.unix( fromValue );
			}

			if ( fallback === '7-days-ago' ) {
				return moment().subtract( 7, 'd' );
			}

			return moment();
		},
		[ moment ]
	);

	const getTimestampFor7DaysAgo = useCallback(
		() => moment().subtract( 7, 'd' ).unix().toString( 10 ),
		[ moment ]
	);
	const getTimestampForNow = useCallback( () => moment().unix().toString( 10 ), [ moment ] );

	const startTime = useMemo(
		() => getMomentFromTimestamp( query.from, '7-days-ago' ),
		[ query.from, getMomentFromTimestamp ]
	);
	const endTime = useMemo(
		() => getMomentFromTimestamp( query.to ),
		[ query.to, getMomentFromTimestamp ]
	);
	const dateRange = useMemo( () => {
		const daysInRange = endTime.diff( startTime, 'days' );
		return {
			chartStart: startTime.format( 'YYYY-MM-DD' ),
			chartEnd: endTime.format( 'YYYY-MM-DD' ),
			daysInRange,
		};
	}, [ startTime, endTime ] );

	const { supportedShortcutList } = useSelector( ( state ) =>
		getShortcuts( state, dateRange, translate )
	);

	const [ autoRefresh, setAutoRefresh ] = useState( false );
	const autoRefreshCallback = useCallback( () => {
		const url = new URL( window.location.href );
		url.searchParams.set( 'from', getTimestampFor7DaysAgo() );
		url.searchParams.set( 'to', getTimestampForNow() );
		page.replace( url.pathname + url.search );
	}, [ getTimestampFor7DaysAgo, getTimestampForNow ] );
	useInterval( autoRefreshCallback, autoRefresh && 10 * 1000 );
	const handleAutoRefreshClick = ( isChecked: boolean ) => {
		if ( isChecked ) {
			const url = new URL( window.location.href );
			url.searchParams.delete( 'from' );
			url.searchParams.delete( 'to' );
			page.replace( url.pathname + url.search );
		} else {
			const url = new URL( window.location.href );
			url.searchParams.set( 'from', getTimestampFor7DaysAgo() );
			url.searchParams.set( 'to', getTimestampForNow() );
			page.replace( url.pathname + url.search );
		}

		dispatch( recordTracksEvent( 'calypso_site_logs_auto_refresh', { enabled: isChecked } ) );
		setAutoRefresh( isChecked );
	};

	const handleTimeChange = useCallback( ( updatedStartTime: Moment, updatedEndTime: Moment ) => {
		setAutoRefresh( false );

		const url = new URL( window.location.href );

		if ( ! updatedStartTime.isValid() ) {
			url.searchParams.delete( 'from' );
		} else {
			url.searchParams.set( 'from', updatedStartTime.unix().toString( 10 ) );
		}

		if ( ! updatedEndTime.isValid() ) {
			url.searchParams.delete( 'to' );
		} else {
			url.searchParams.set( 'to', updatedEndTime.unix().toString( 10 ) );
		}

		page.replace( url.pathname + url.search );
	}, [] );

	const [ view, setView ] = useView( { logType, query } );
	const oldSeverity = getFilterValue( view, 'severity' )?.sort().toString() || '';
	const setViewWithSideEffects = useCallback(
		( newView: View ) => {
			const severity = getFilterValue( newView, 'severity' )?.sort().toString() || '';
			if ( severity !== oldSeverity ) {
				dispatch(
					recordTracksEvent( 'calypso_site_logs_severity_filter', {
						severity,
						severity_user: severity.includes( 'User' ),
						severity_warning: severity.includes( 'Warning' ),
						severity_deprecated: severity.includes( 'Deprecated' ),
						severity_fatal: severity.includes( 'Fatal' ),
					} )
				);
			}

			// Disable auto-refresh if the user navigates to a different page.
			if ( autoRefresh === true && newView.page !== view.page ) {
				setAutoRefresh( false );
			}

			setView( newView );

			// Handle URL changes.
			const url = new URL( window.location.href );
			const isEmpty = ( filter: Filter | undefined ) =>
				! filter || ! filter?.value || filter?.value.length === 0;
			[ 'severity', 'request_type', 'status', 'renderer', 'cached' ].forEach( ( filterField ) => {
				const filter = newView.filters?.find( ( f ) => f.field === filterField );

				// Use cases to cover:
				//
				// 1. URL doesn't have the filter and the filter is empty. Do nothing.
				// 2. URL doesn't have the filter and the filter is not empty. Update URL param.
				// 3. URL has the filter and the filter is the same as before. Do nothing.
				// 4. URL has the filter and the filter is different from before. Update URL param.
				// 5. URL has the filter and the filter is empty. Remove URL param.

				if ( ! url.searchParams.has( filterField ) && isEmpty( filter ) ) {
					return;
				}

				if ( ! url.searchParams.has( filterField ) && ! isEmpty( filter ) ) {
					url.searchParams.set( filterField, filter?.value.sort().toString() );
				}

				if (
					url.searchParams.has( filterField ) &&
					url.searchParams.get( filterField ) === filter?.value.sort().toString()
				) {
					return;
				}

				if (
					url.searchParams.has( filterField ) &&
					url.searchParams.get( filterField ) !== filter?.value.sort().toString()
				) {
					url.searchParams.set( filterField, filter?.value.sort().toString() );
				}

				if ( url.searchParams.has( filterField ) && isEmpty( filter ) ) {
					url.searchParams.delete( filterField );
				}
			} );

			page.replace( url.pathname + url.search );
		},
		[ autoRefresh, setAutoRefresh, oldSeverity, view.page, setView, dispatch ]
	);

	const fields = useFields( { logType } );
	const { data, paginationInfo, isLoading } = useData( {
		view,
		logType,
		startTime,
		endTime,
	} );
	const actions: Action< PHPLog | ServerLog >[] = useActions( { logType, isLoading } );

	const { downloadLogs, state } = useSiteLogsDownloader( { roundDateRangeToWholeDays: false } );
	const isDownloading = state.status === 'downloading';
	const onDownloadLogs = useCallback( () => {
		downloadLogs( {
			logType,
			startDateTime: startTime,
			endDateTime: endTime,
			filter: toFilterParams( { view, logType } ),
		} );
	}, [ downloadLogs, logType, startTime, endTime, view ] );

	const [ itemDetailsModal, setItemDetailsModal ] = useState< PHPLog | ServerLog | null >( null );
	const onOpenDetailsModal = useCallback( ( item: PHPLog | ServerLog ) => {
		setItemDetailsModal( item );
	}, [] );
	const onCloseDetailsModal = useCallback( () => {
		setItemDetailsModal( null );
	}, [] );

	return (
		<>
			{ siteId && <QuerySiteSettings siteId={ siteId } /> }
			{ !! itemDetailsModal && (
				<DetailsModal
					item={ itemDetailsModal }
					logType={ logType }
					onClose={ onCloseDetailsModal }
				/>
			) }
			<div className="site-logs-header">
				<NavigationHeader
					title={ translate( 'Logs' ) }
					subtitle={ translate(
						'View and download various server logs. {{link}}Learn more{{/link}}',
						{
							components: {
								link: (
									<InlineSupportLink supportContext="site-monitoring-logs" showIcon={ false } />
								),
							},
						}
					) }
				/>
				<ToggleGroupControl
					className="site-logs-toolbar__toggle"
					hideLabelFromVision
					isBlock
					label="Filter"
					onChange={ ( value ) => {
						if ( value === LogType.PHP || value === LogType.WEB ) {
							navigate( window.location.pathname.replace( /\/[^/]+$/, '/' + value ) );
							setView( ( view: View ) => ( {
								...view,
								filters: [],
								sort: {
									field: getSortField( value ),
									direction: view?.sort?.direction || 'desc',
								},
								titleField: getSortField( value ),
								fields: getVisibleFields( value ),
							} ) );
						}
					} }
					value={ logType }
					__nextHasNoMarginBottom
					__next40pxDefaultSize
				>
					<ToggleGroupControlOption
						className="site-logs-toolbar__toggle-option"
						label={ translate( 'PHP error', {
							textOnly: true,
						} ) }
						value="php"
					/>
					<ToggleGroupControlOption
						className="site-logs-toolbar__toggle-option"
						label={ translate( 'Web server', {
							textOnly: true,
						} ) }
						value="web"
					/>
				</ToggleGroupControl>
				<DateControl
					dateRange={ dateRange }
					onApplyButtonClick={ handleTimeChange }
					shortcutList={ supportedShortcutList }
					onShortcutClick={ ( shortcut, closePopoverAndCommit ) => {
						/* Time change is handled by onApplyButtonClick */
						closePopoverAndCommit();
					} }
					tooltip={ translate( 'Select a date range' ) }
				/>
			</div>
			<DataViews< PHPLog | ServerLog >
				data={ data }
				isLoading={ isLoading }
				paginationInfo={ paginationInfo }
				fields={ fields }
				view={ view }
				onChangeView={ setViewWithSideEffects }
				onClickItem={ onOpenDetailsModal }
				actions={ actions }
				search={ false }
				defaultLayouts={ { table: {} } }
				header={
					<>
						<Button
							size="compact"
							icon={ download }
							label="Download logs"
							onClick={ onDownloadLogs }
							isBusy={ isDownloading }
						/>
						<ToggleControl
							__nextHasNoMarginBottom
							className="site-logs__auto-refresh site-logs__auto-refresh_desktop"
							label={ translate( 'Auto-refresh', { textOnly: true } ) }
							checked={ autoRefresh }
							onChange={ handleAutoRefreshClick }
						/>
					</>
				}
			/>
		</>
	);
};