Skip to content

Commit

Permalink
remove sync store
Browse files Browse the repository at this point in the history
  • Loading branch information
JoanVicens committed Apr 15, 2024
1 parent bb05ea5 commit 2cf72ab
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 115 deletions.
17 changes: 11 additions & 6 deletions src/apps/main/database/adapters/base.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
export abstract class DatabaseCollectionAdapter<DatabaseItemType> {
export interface DatabaseCollectionAdapter<DatabaseItemType> {
/**
* Used to initialize the database adapter
*/
abstract connect(): Promise<{ success: boolean }>;
connect(): Promise<{ success: boolean }>;

/**
* Gets an item from the database
*/
abstract get(
get(
itemId: string
): Promise<{ success: boolean; result: DatabaseItemType | null }>;

/**
* Updates an item in the database
*/
abstract update(
update(
itemId: string,
updatePayload: Partial<DatabaseItemType>
): Promise<{
Expand All @@ -25,15 +25,20 @@ export abstract class DatabaseCollectionAdapter<DatabaseItemType> {
/**
* Creates an item in the database
*/
abstract create(creationPayload: DatabaseItemType): Promise<{
create(creationPayload: DatabaseItemType): Promise<{
success: boolean;
result: DatabaseItemType | null;
}>;

/**
* Removes an item from the database
*/
abstract remove(itemId: string): Promise<{
remove(itemId: string): Promise<{
success: boolean;
}>;

getLastUpdated(): Promise<{
success: boolean;
result: DatabaseItemType | null;
}>;
}
31 changes: 31 additions & 0 deletions src/apps/main/database/collections/DriveFileCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { DatabaseCollectionAdapter } from '../adapters/base';
import { AppDataSource } from '../data-source';
import { DriveFile } from '../entities/DriveFile';
import { Repository } from 'typeorm';
import * as Sentry from '@sentry/electron/main';
import Logger from 'electron-log';

export class DriveFilesCollection
implements DatabaseCollectionAdapter<DriveFile>
{
Expand Down Expand Up @@ -67,4 +70,32 @@ export class DriveFilesCollection
success: result.affected ? true : false,
};
}

async getLastUpdated(): Promise<{
success: boolean;
result: DriveFile | null;
}> {
try {
const queryResult = await this.repository
.createQueryBuilder('drive_file')
.orderBy(
// eslint-disable-next-line quotes
"STR_TO_DATE(drive_file.updatedAt, '%Y-%m-%dT%H:%i:%sZ')",
'DESC'
)
.getOne();

return {
success: true,
result: queryResult,
};
} catch (error) {
Sentry.captureException(error);
Logger.error('Error fetching newest drive file:', error);
return {
success: false,
result: null,
};
}
}
}
31 changes: 31 additions & 0 deletions src/apps/main/database/collections/DriveFolderCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { DatabaseCollectionAdapter } from '../adapters/base';
import { AppDataSource } from '../data-source';
import { DriveFolder } from '../entities/DriveFolder';
import { Repository } from 'typeorm';
import * as Sentry from '@sentry/electron/main';
import Logger from 'electron-log';

export class DriveFoldersCollection
implements DatabaseCollectionAdapter<DriveFolder>
{
Expand Down Expand Up @@ -66,4 +69,32 @@ export class DriveFoldersCollection
success: result.affected ? true : false,
};
}

async getLastUpdated(): Promise<{
success: boolean;
result: DriveFolder | null;
}> {
try {
const queryResult = await this.repository
.createQueryBuilder('drive_folder')
.orderBy(
// eslint-disable-next-line quotes
"STR_TO_DATE(drive_folder.updatedAt, '%Y-%m-%dT%H:%i:%sZ')",
'DESC'
)
.getOne();

return {
success: true,
result: queryResult,
};
} catch (error) {
Sentry.captureException(error);
Logger.error('Error fetching newest drive folder:', error);
return {
success: false,
result: null,
};
}
}
}
75 changes: 38 additions & 37 deletions src/apps/main/remote-sync/RemoteSyncManager.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import Logger from 'electron-log';
import * as helpers from './helpers';
import {
RemoteSyncStatus,
RemoteSyncedFolder,
RemoteSyncedFile,
SyncConfig,
SYNC_OFFSET_MS,
WAITING_AFTER_SYNCING,
SIX_HOURS_IN_MILLISECONDS,
rewind,
} from './helpers';
import { reportError } from '../bug-report/service';

import { DatabaseCollectionAdapter } from '../database/adapters/base';
import { Axios } from 'axios';
import { DriveFolder } from '../database/entities/DriveFolder';
import { DriveFile } from '../database/entities/DriveFile';
import { Nullable } from '../../shared/types/Nullable';

export class RemoteSyncManager {
private foldersSyncStatus: RemoteSyncStatus = 'IDLE';
Expand All @@ -38,7 +39,7 @@ export class RemoteSyncManager {
fetchFoldersLimitPerRequest: number;
syncFiles: boolean;
syncFolders: boolean;
} // , // private chekers: { // fileCheker: FileCheckerStatusInRoot; // }
}
) {}

set placeholderStatus(status: RemoteSyncStatus) {
Expand Down Expand Up @@ -131,27 +132,8 @@ export class RemoteSyncManager {
this.changeStatus('SYNC_FAILED');
reportError(error as Error);
} finally {
// const totalDuration = Date.now() - start;

// Logger.info('-----------------');
// Logger.info('REMOTE SYNC STATS\n');
Logger.info('Total synced files: ', this.totalFilesSynced);
Logger.info('Total synced folders: ', this.totalFoldersSynced);

// Logger.info(
// `Files sync speed: ${
// this.totalFilesSynced / (totalDuration / 1000)
// } files/second`
// );

// Logger.info('Total synced folders: ', this.totalFoldersSynced);
// Logger.info(
// `Folders sync speed: ${
// this.totalFoldersSynced / (totalDuration / 1000)
// } folders/second`
// );
// Logger.info(`Total remote to local sync time: ${totalDuration}ms`);
// Logger.info('-----------------');
}
}

Expand Down Expand Up @@ -234,31 +216,41 @@ export class RemoteSyncManager {
}
}

private async getFileCheckpoint(): Promise<Nullable<Date>> {
const { success, result } = await this.db.files.getLastUpdated();

if (!success) return undefined;

if (!result) return undefined;

const updatedAt = new Date(result.updatedAt);

return rewind(updatedAt, SIX_HOURS_IN_MILLISECONDS);
}

/**
* Syncs all the remote files and saves them into the local db
* @param syncConfig Config to execute the sync with
* @returns
*/
private async syncRemoteFiles(syncConfig: SyncConfig, from?: Date) {
const lastFilesSyncAt = from ?? helpers.getLastFilesSyncAt();
const fileCheckpoint = from ?? (await this.getFileCheckpoint());
try {
Logger.info(
`Syncing files updated from ${
lastFilesSyncAt ?? '(no last date provided)'
fileCheckpoint ?? '(no last date provided)'
}`
);
const { hasMore, result } = await this.fetchFilesFromRemote(
lastFilesSyncAt
fileCheckpoint
);

let lastFileSynced = null;

for (const remoteFile of result) {
// eslint-disable-next-line no-await-in-loop
await this.createOrUpdateSyncedFileEntry(remoteFile);
const fileUpdatedAt = new Date(remoteFile.updatedAt);

helpers.saveLastFilesSyncAt(fileUpdatedAt, SYNC_OFFSET_MS);
this.totalFilesSynced++;
lastFileSynced = remoteFile;
}
Expand All @@ -281,8 +273,8 @@ export class RemoteSyncManager {
Logger.error('Remote files sync failed with error: ', error);

reportError(error as Error, {
lastFilesSyncAt: lastFilesSyncAt
? lastFilesSyncAt.toISOString()
lastFilesSyncAt: fileCheckpoint
? fileCheckpoint.toISOString()
: 'INITIAL_FILES_SYNC',
});
if (syncConfig.retry >= syncConfig.maxRetries) {
Expand All @@ -299,32 +291,41 @@ export class RemoteSyncManager {
}
}

private async getLastFolderSyncAt(): Promise<Nullable<Date>> {
const { success, result } = await this.db.folders.getLastUpdated();

if (!success) return undefined;

if (!result) return undefined;

const updatedAt = new Date(result.updatedAt);

return rewind(updatedAt, SIX_HOURS_IN_MILLISECONDS);
}

/**
* Syncs all the remote folders and saves them into the local db
* @param syncConfig Config to execute the sync with
* @returns
*/
private async syncRemoteFolders(syncConfig: SyncConfig, from?: Date) {
const lastFoldersSyncAt = from ?? helpers.getLastFoldersSyncAt();
const lastFolderSyncAt = from ?? (await this.getLastFolderSyncAt());
try {
Logger.info(
`Syncing folders updated from ${
lastFoldersSyncAt ?? '(no last date provided)'
lastFolderSyncAt ?? '(no last date provided)'
}`
);
const { hasMore, result } = await this.fetchFoldersFromRemote(
lastFoldersSyncAt
lastFolderSyncAt
);

let lastFolderSynced = null;

for (const remoteFolder of result) {
// eslint-disable-next-line no-await-in-loop
await this.createOrUpdateSyncedFolderEntry(remoteFolder);
const foldersUpdatedAt = new Date(remoteFolder.updatedAt);

Logger.info(`Saving folders updatedAt ${foldersUpdatedAt}`);
helpers.saveLastFoldersSyncAt(foldersUpdatedAt, SYNC_OFFSET_MS);
this.totalFoldersSynced++;
lastFolderSynced = remoteFolder;
}
Expand All @@ -346,8 +347,8 @@ export class RemoteSyncManager {
} catch (error) {
Logger.error('Remote folders sync failed with error: ', error);
reportError(error as Error, {
lastFoldersSyncAt: lastFoldersSyncAt
? lastFoldersSyncAt.toISOString()
lastFoldersSyncAt: lastFolderSyncAt
? lastFolderSyncAt.toISOString()
: 'INITIAL_FOLDERS_SYNC',
});
if (syncConfig.retry >= syncConfig.maxRetries) {
Expand Down
3 changes: 1 addition & 2 deletions src/apps/main/remote-sync/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import eventBus from '../event-bus';
import { RemoteSyncManager } from './RemoteSyncManager';
import { DriveFilesCollection } from '../database/collections/DriveFileCollection';
import { DriveFoldersCollection } from '../database/collections/DriveFolderCollection';
import { clearRemoteSyncStore, RemoteSyncStatus } from './helpers';
import { RemoteSyncStatus } from './helpers';
import { getNewTokenClient } from '../../shared/HttpClient/main-process-client';
import Logger from 'electron-log';
import { ipcMain } from 'electron';
Expand Down Expand Up @@ -99,7 +99,6 @@ eventBus.on('USER_LOGGED_IN', async () => {
eventBus.on('USER_LOGGED_OUT', () => {
initialSyncReady = false;
remoteSyncManager.resetRemoteSync();
clearRemoteSyncStore();
});

ipcMain.on('CHECK_SYNC', (event) => {
Expand Down
Loading

0 comments on commit 2cf72ab

Please sign in to comment.