File size: 3,737 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
export const INDEX_FORMAT = 'YYYYMMDD';

export const DELTA_ACTIVITIES = [
	'attachment__uploaded',
	// 'attachment__updated',
	'attachment__deleted',
	'post__published',
	'post__trashed',
	// 'post__updated',
	'plugin__installed',
	'plugin__deleted',
	'theme__installed',
	'theme__deleted',
	'user__invite_accepted',
];

export const getDeltaActivitiesByType = ( logs ) => {
	return {
		mediaCreated: logs.filter( ( event ) => 'attachment__uploaded' === event.activityName ),
		mediaDeleted: logs.filter( ( event ) => 'attachment__deleted' === event.activityName ),
		posts: logs.filter(
			( event ) =>
				'post__published' === event.activityName || 'post__trashed' === event.activityName
		),
		postsCreated: logs.filter( ( event ) => 'post__published' === event.activityName ),
		postsDeleted: logs.filter( ( event ) => 'post__trashed' === event.activityName ),
		plugins: logs.filter(
			( event ) =>
				'plugin__installed' === event.activityName || 'plugin__deleted' === event.activityName
		),
		themes: logs.filter(
			( event ) =>
				'theme__installed' === event.activityName || 'theme__deleted' === event.activityName
		),
		users: logs.filter( ( event ) => 'user__invite_accepted' === event.activityName ),
	};
};

export const SUCCESSFUL_BACKUP_ACTIVITIES = [
	'rewind__backup_complete_full',
	'rewind__backup_complete_initial',
	'rewind__backup_only_complete_full',
	'rewind__backup_only_complete_initial',
];

export const BACKUP_ATTEMPT_ACTIVITIES = [
	...SUCCESSFUL_BACKUP_ACTIVITIES,
	'rewind__backup_error',
	'rewind__backup_only_error',
];

/**
 * Check if the activity is the type of backup
 * @param activity {object} Activity to check
 */
export const isActivityBackup = ( activity ) => {
	return BACKUP_ATTEMPT_ACTIVITIES.includes( activity.activityName );
};

/**
 * Retrieve the backup error code from activity object.
 * @typedef {import('calypso/state/data-layer/wpcom/sites/activity/from-api.js').processItem} ProcessItem
 * @param {Object} activity Activity to get the error code from.
 * @returns {'BAD_CREDENTIALS'|'NOT_ACCESSIBLE'} The error code as set in @see {ProcessItem}
 */
export const getBackupErrorCode = ( activity ) => {
	return activity?.activityMeta?.errorCode?.toUpperCase?.();
};

/**
 * Retrieve any warnings from a backup activity object.
 * @param backup {object} Backup to check
 */
export const getBackupWarnings = ( backup ) => {
	if ( ! backup || ! backup.activityWarnings ) {
		return {};
	}
	const warnings = {};

	Object.keys( backup.activityWarnings ).forEach( function ( itemType ) {
		const typeWarnings = backup.activityWarnings[ itemType ];
		typeWarnings.forEach( ( typeWarning ) => {
			if ( ! warnings.hasOwnProperty( typeWarning.name ) ) {
				warnings[ typeWarning.name ] = {
					category: typeWarning.category,
					items: [],
				};
			}

			const warningItem = {
				code: typeWarning.code,
				item: typeWarning.itemName,
			};

			warnings[ typeWarning.name ].items.push( warningItem );
		} );
	} );

	return warnings;
};

/**
 * Check if the backup is completed
 * @param backup {object} Backup to check
 */
export const isSuccessfulDailyBackup = ( backup ) => {
	return SUCCESSFUL_BACKUP_ACTIVITIES.includes( backup.activityName );
};

/**
 * Check if a Realtime backup backup is completed
 * @param backup {object} Backup to check
 */
export const isSuccessfulRealtimeBackup = ( backup ) => {
	const hasRestorableStreams =
		backup.streams && !! backup.streams.filter( ( stream ) => stream.activityIsRewindable ).length;
	return hasRestorableStreams || backup.activityIsRewindable;
};

export const isStorageOrRetentionReached = ( backup ) => {
	return (
		SUCCESSFUL_BACKUP_ACTIVITIES.includes( backup.activityName ) && ! backup.activityIsRewindable
	);
};