File size: 6,016 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
import { useMemo } from 'react';
import { useSelector } from 'react-redux';
import { useLocalizedMoment } from 'calypso/components/localized-moment';
import useRewindableActivityLogQuery from 'calypso/data/activity-log/use-rewindable-activity-log-query';
import {
	DELTA_ACTIVITIES,
	getDeltaActivitiesByType,
	isActivityBackup,
	isSuccessfulRealtimeBackup,
} from 'calypso/lib/jetpack/backup-utils';
import { applySiteOffset } from 'calypso/lib/site/timezone';
import getSiteGmtOffset from 'calypso/state/selectors/get-site-gmt-offset';
import getSiteTimezoneValue from 'calypso/state/selectors/get-site-timezone-value';
import { useFirstMatchingBackupAttempt, useMatchingBackupAttemptsInRange } from '../hooks';

const useLatestBackupAttempt = (
	siteId,
	{ before, after, successOnly = false } = {},
	queryOptions = {}
) => {
	return useFirstMatchingBackupAttempt(
		siteId,
		{
			before,
			after,
			successOnly,
			sortOrder: 'desc',
		},
		queryOptions
	);
};

const useBackupDeltas = ( siteId, { before, after, number = 1000 } = {}, enabled = true ) => {
	const filter = {
		name: DELTA_ACTIVITIES,
		before: before ? before.toISOString() : undefined,
		after: after ? after.toISOString() : undefined,
		number,
	};

	const isValidRequest = filter.before && filter.after;

	const { data, isInitialLoading } = useRewindableActivityLogQuery( siteId, filter, {
		enabled: isValidRequest && enabled,
		refetchOnWindowFocus: false,
	} );

	return {
		isInitialLoading,
		deltas: getDeltaActivitiesByType( data ?? [] ),
	};
};

// Get the dates where there are no successful backups in a range
export const useDatesWithNoSuccessfulBackups = ( siteId, startDate, endDate ) => {
	const moment = useLocalizedMoment();
	const timezone = useSelector( ( state ) =>
		siteId ? getSiteTimezoneValue( state, siteId ) : null
	);
	const gmtOffset = useSelector( ( state ) =>
		siteId ? getSiteGmtOffset( state, siteId ) : null
	);

	// Adapted from useDateWithOffsetHook to move dates based on the selected blog's GMT offset.
	const adjustDate = ( date ) =>
		date ? applySiteOffset( date, { timezone, gmtOffset, keepLocalTime: false } ) : undefined;

	// This will get a set of activity logs
	const { isLoading, backups } = useMatchingBackupAttemptsInRange( siteId, {
		after: moment( startDate ).startOf( 'day' ),
		before: moment( endDate ).endOf( 'day' ),
	} );

	const datesWithoutBackups = useMemo( () => {
		const endMoment = moment( endDate ).endOf( 'day' );
		const movingDate = moment( startDate ).startOf( 'day' );
		const dates = [];

		// Collect an array of all the dates in the range
		while ( movingDate < endMoment ) {
			dates.push( movingDate.format( 'MM-DD-YYYY' ) );
			movingDate.add( 1, 'day' );
		}

		if ( backups ) {
			backups.forEach( ( item ) => {
				// Remove dates from the dates array that have backups
				// This should leave only dates that have no backups in the array
				const backupDate = adjustDate( item.activityDate ).format( 'MM-DD-YYYY' );
				if ( dates.indexOf( backupDate ) > -1 && item.activityIsRewindable ) {
					dates.splice( dates.indexOf( backupDate ), 1 );
				}
			} );
		}

		return dates;
	}, [ startDate, endDate, backups ] );

	return {
		isLoading,
		dates: datesWithoutBackups,
	};
};

export const useDailyBackupStatus = ( siteId, selectedDate ) => {
	const moment = useLocalizedMoment();

	const isToday = useMemo(
		() => moment().isSame( moment( selectedDate ), 'day' ),
		[ moment, selectedDate ]
	);

	const lastBackupBeforeDate = useLatestBackupAttempt(
		siteId,
		{
			before: moment( selectedDate ).startOf( 'day' ),
			successOnly: true,
		},
		{ refetchOnWindowFocus: false }
	);
	const lastAttemptOnDate = useLatestBackupAttempt(
		siteId,
		{
			after: moment( selectedDate ).startOf( 'day' ),
			before: moment( selectedDate ).endOf( 'day' ),
		},
		{ refetchOnWindowFocus: isToday }
	);

	const hasPreviousBackup = ! lastBackupBeforeDate.isLoading && lastBackupBeforeDate.backupAttempt;
	const successfulLastAttempt =
		! lastAttemptOnDate.isLoading && lastAttemptOnDate.backupAttempt?.activityIsRewindable;

	const backupDeltas = useBackupDeltas(
		siteId,
		{
			after: moment( lastBackupBeforeDate.backupAttempt?.activityTs ),
			before: moment( lastAttemptOnDate.backupAttempt?.activityTs ),
		},
		!! ( hasPreviousBackup && successfulLastAttempt )
	);

	return {
		isLoading:
			lastBackupBeforeDate.isLoading ||
			lastAttemptOnDate.isLoading ||
			backupDeltas.isInitialLoading,
		lastBackupBeforeDate: lastBackupBeforeDate.backupAttempt,
		lastBackupAttemptOnDate: lastAttemptOnDate.backupAttempt,
		deltas: backupDeltas.deltas,
	};
};

export const useRealtimeBackupStatus = ( siteId, selectedDate ) => {
	const moment = useLocalizedMoment();

	const isToday = useMemo(
		() => moment().isSame( moment( selectedDate ), 'day' ),
		[ moment, selectedDate ]
	);

	// This is the last attempt irrespective of date
	const lastBackupAttempt = useLatestBackupAttempt( siteId );

	const lastBackupBeforeDate = useLatestBackupAttempt(
		siteId,
		{
			before: moment( selectedDate ).startOf( 'day' ),
			successOnly: true,
		},
		{ refetchOnWindowFocus: false }
	);

	const activityLog = useRewindableActivityLogQuery(
		siteId,
		{
			before: moment( selectedDate ).endOf( 'day' ).toISOString(),
			after: moment( selectedDate ).startOf( 'day' ).toISOString(),
		},
		{
			select: ( data ) =>
				data.filter( ( a ) => isActivityBackup( a ) || isSuccessfulRealtimeBackup( a ) ),
			refetchOnWindowFocus: isToday,
		}
	);

	const backupAttemptsOnDate = activityLog.data ?? [];
	const lastBackupAttemptOnDate = backupAttemptsOnDate[ 0 ];
	const lastSuccessfulBackupOnDate = backupAttemptsOnDate.find( isSuccessfulRealtimeBackup );

	return {
		isLoading: lastBackupBeforeDate.isLoading || activityLog.isLoading,
		lastBackupBeforeDate: lastBackupBeforeDate.backupAttempt,
		lastBackupAttempt: lastBackupAttempt.backupAttempt,
		lastBackupAttemptOnDate,
		lastSuccessfulBackupOnDate,
		backupAttemptsOnDate,
		refetch: activityLog.refetch,
	};
};