-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_state.js
More file actions
328 lines (278 loc) · 10.5 KB
/
sync_state.js
File metadata and controls
328 lines (278 loc) · 10.5 KB
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
/**
* @fileoverview Sync State Management Module - Incremental Audit Support
* Maintains sync metadata to enable progressive data synchronization
* Tracks last sync timestamp, record counts, and sync modes for each service
*
* @version 1.0
* @date January 2026
*/
// =================================================================
// SYNC STATE STORAGE CONSTANTS
// =================================================================
const SYNC_STATE_PREFIX = 'ADDOCU_SYNC_STATE_';
const SYNC_STATE_HISTORY_PREFIX = 'ADDOCU_SYNC_HISTORY_';
/**
* Records the sync state for a service/resource type combination.
* Enables incremental audits on subsequent runs.
*
* @param {string} service - Service name (e.g., 'GA4', 'GTM', 'BigQuery')
* @param {string} resourceType - Resource type (e.g., 'Properties', 'Containers', 'Datasets')
* @param {number} recordCount - Number of records processed in this sync
* @param {string} status - Sync status ('SUCCESS', 'PARTIAL', 'ERROR')
* @param {string} syncMode - Sync mode ('FULL', 'DELTA', 'INCREMENTAL')
* @returns {Object} The recorded sync state
*/
function recordSyncState(service, resourceType, recordCount, status = 'SUCCESS', syncMode = 'DELTA') {
try {
// Validate inputs
if (!service || !resourceType) {
throw new Error('Service and resourceType are required parameters');
}
const userProperties = PropertiesService.getUserProperties();
const stateKey = `${SYNC_STATE_PREFIX}${service.toUpperCase()}_${resourceType.toUpperCase()}`;
const historyKey = `${SYNC_STATE_HISTORY_PREFIX}${service.toUpperCase()}_${resourceType.toUpperCase()}`;
// Create sync state object
const now = new Date();
const newState = {
service: service.toUpperCase(),
resourceType: resourceType.toUpperCase(),
lastSyncTimestamp: now.toISOString(),
lastSyncCount: parseInt(recordCount) || 0,
lastSyncStatus: status,
syncMode: syncMode,
syncDurationMs: 0 // Will be calculated by caller if needed
};
// Store current state
userProperties.setProperty(stateKey, JSON.stringify(newState));
// Keep history of last 10 syncs (optional, for analytics)
storeHistoricalSync(userProperties, historyKey, newState);
// Log the sync state recording
logEvent('SYNC_STATE', `${service}/${resourceType}: ${status} | ${recordCount} records | Mode: ${syncMode}`);
return newState;
} catch (error) {
logError('SYNC_STATE', `Failed to record sync state for ${service}/${resourceType}: ${error.message}`);
return null;
}
}
/**
* Retrieves the sync state for a service/resource type combination.
* Used to determine if subsequent sync should be full or incremental.
*
* @param {string} service - Service name (e.g., 'GA4', 'GTM')
* @param {string} resourceType - Resource type (e.g., 'Properties', 'Containers')
* @returns {Object|null} Sync state object or null if not found
*/
function getSyncState(service, resourceType) {
try {
if (!service || !resourceType) {
return null;
}
const userProperties = PropertiesService.getUserProperties();
const stateKey = `${SYNC_STATE_PREFIX}${service.toUpperCase()}_${resourceType.toUpperCase()}`;
const stored = userProperties.getProperty(stateKey);
if (!stored) {
return null;
}
const parsed = JSON.parse(stored);
// Validate timestamp format
if (parsed.lastSyncTimestamp) {
parsed.lastSyncDate = new Date(parsed.lastSyncTimestamp);
}
return parsed;
} catch (error) {
logWarning('SYNC_STATE', `Failed to retrieve sync state for ${service}/${resourceType}: ${error.message}`);
return null;
}
}
/**
* Determines if this is the first sync for a service/resource type.
* First syncs should use full audit mode, subsequent should use incremental.
*
* @param {string} service - Service name
* @param {string} resourceType - Resource type
* @returns {boolean} True if no previous sync exists
*/
function isFirstSync(service, resourceType) {
const state = getSyncState(service, resourceType);
return !state || !state.lastSyncTimestamp;
}
/**
* Gets the last sync timestamp for a service/resource type.
* Used as filter cutoff for incremental syncs.
*
* @param {string} service - Service name
* @param {string} resourceType - Resource type
* @returns {Date|null} Last sync timestamp or null if not found
*/
function getLastSyncTimestamp(service, resourceType) {
const state = getSyncState(service, resourceType);
if (!state || !state.lastSyncTimestamp) {
return null;
}
return new Date(state.lastSyncTimestamp);
}
/**
* Clears sync state for a service (forces full re-audit on next sync).
* Useful for troubleshooting or resetting incremental sync.
*
* @param {string} service - Service name
* @param {string} resourceType - Resource type (optional; if omitted, clears all for service)
* @returns {boolean} True if successfully cleared
*/
function clearSyncState(service, resourceType = null) {
try {
if (!service) {
throw new Error('Service name is required');
}
const userProperties = PropertiesService.getUserProperties();
const prefix = `${SYNC_STATE_PREFIX}${service.toUpperCase()}`;
if (resourceType) {
// Clear specific resource type
const stateKey = `${prefix}_${resourceType.toUpperCase()}`;
userProperties.deleteProperty(stateKey);
logEvent('SYNC_STATE', `Cleared: ${service}/${resourceType} (will force FULL sync on next run)`);
} else {
// Clear all states for this service
const allProperties = userProperties.getKeys();
allProperties.forEach(key => {
if (key.startsWith(prefix)) {
userProperties.deleteProperty(key);
}
});
logEvent('SYNC_STATE', `Cleared all sync states for ${service} (will force FULL sync on next run)`);
}
return true;
} catch (error) {
logError('SYNC_STATE', `Failed to clear sync state: ${error.message}`);
return false;
}
}
/**
* Gets a summary of all sync states for a service (for dashboard/reporting).
*
* @param {string} service - Service name (optional; if omitted, returns all)
* @returns {Object} Map of resource types to their sync states
*/
function getAllSyncStates(service = null) {
try {
const userProperties = PropertiesService.getUserProperties();
const allProperties = userProperties.getKeys();
const states = {};
allProperties.forEach(key => {
if (key.startsWith(SYNC_STATE_PREFIX)) {
const serviceMatch = key.match(/ADDOCU_SYNC_STATE_(\w+)_(\w+)/);
if (serviceMatch) {
const [, srv, rtype] = serviceMatch;
// Filter by service if specified
if (service && srv.toUpperCase() !== service.toUpperCase()) {
return;
}
const stored = userProperties.getProperty(key);
if (stored) {
const state = JSON.parse(stored);
if (!states[srv]) states[srv] = {};
states[srv][rtype] = state;
}
}
}
});
return states;
} catch (error) {
logError('SYNC_STATE', `Failed to retrieve all sync states: ${error.message}`);
return {};
}
}
/**
* Stores historical sync data for analytics and debugging.
* Keeps last 10 syncs per service/resource type.
*
* @private
* @param {Properties} userProperties - User properties service
* @param {string} historyKey - History key for this service/resource
* @param {Object} newState - Current sync state
*/
function storeHistoricalSync(userProperties, historyKey, newState) {
try {
const stored = userProperties.getProperty(historyKey);
let history = stored ? JSON.parse(stored) : [];
// Keep history array as array (not exceeding 10 entries)
if (!Array.isArray(history)) {
history = [];
}
history.push(newState);
// Keep only last 10 syncs
if (history.length > 10) {
history = history.slice(-10);
}
userProperties.setProperty(historyKey, JSON.stringify(history));
} catch (error) {
logWarning('SYNC_STATE', `Could not store historical sync: ${error.message}`);
// Don't fail if history storage fails - it's optional
}
}
/**
* Gets sync state summary for metadata sheet.
* Returns data formatted for writing to _AUDIT_METADATA sheet.
*
* @returns {Array<Array>} Array of formatted sync state rows
*/
function getSyncStateForSheet() {
try {
const allStates = getAllSyncStates();
const rows = [];
// Header row (if needed by caller)
// Column order: Service, Resource Type, Last Sync Timestamp, Record Count, Status, Sync Mode
for (const [service, resources] of Object.entries(allStates)) {
for (const [resourceType, state] of Object.entries(resources)) {
rows.push([
service, // Service
resourceType, // Resource Type
state.lastSyncTimestamp || '', // Last Sync Timestamp
state.lastSyncCount || 0, // Record Count
state.lastSyncStatus || '', // Status
state.syncMode || 'DELTA' // Sync Mode
]);
}
}
return rows;
} catch (error) {
logError('SYNC_STATE', `Failed to format sync state for sheet: ${error.message}`);
return [];
}
}
/**
* Updates the _AUDIT_METADATA sheet with current sync states.
* Creates sheet if it doesn't exist.
*/
function updateAuditMetadataSheet() {
try {
const ss = SpreadsheetApp.getActiveSpreadsheet();
let sheet = ss.getSheetByName('_AUDIT_METADATA');
// Create sheet if it doesn't exist
if (!sheet) {
sheet = ss.insertSheet('_AUDIT_METADATA', 0);
logEvent('AUDIT_METADATA', 'Created _AUDIT_METADATA sheet');
}
// Clear existing content
sheet.clearContents();
// Add headers
const headers = ['Service', 'Resource Type', 'Last Sync Timestamp', 'Record Count', 'Status', 'Sync Mode'];
sheet.getRange(1, 1, 1, headers.length).setValues([headers]);
// Get and write sync states
const rows = getSyncStateForSheet();
if (rows.length > 0) {
sheet.getRange(2, 1, rows.length, rows[0].length).setValues(rows);
}
// Format headers (bold)
const headerRange = sheet.getRange(1, 1, 1, headers.length);
headerRange.setFontWeight('bold');
headerRange.setBackground('#f3f3f3');
// Auto-resize columns
for (let i = 1; i <= headers.length; i++) {
sheet.autoResizeColumn(i);
}
logEvent('AUDIT_METADATA', `Updated _AUDIT_METADATA sheet with ${rows.length} service states`);
} catch (error) {
logError('AUDIT_METADATA', `Failed to update metadata sheet: ${error.message}`);
}
}