Skip to content

Commit

Permalink
[AMSA-33] extend sync log info
Browse files Browse the repository at this point in the history
  • Loading branch information
NaLLiFFuNT committed Apr 16, 2019
1 parent 5a68134 commit c840bb2
Show file tree
Hide file tree
Showing 23 changed files with 428 additions and 110 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"repository": "https://github.com/appodeal/admob-sync-app",
"description": "Appodeal AdMob Sync application",
"private": true,
"version": "0.1.5",
"version": "0.1.6",
"scripts": {
"start": "webpack --watch --progress --config=webpack/development.ts",
"test": "jest",
Expand Down
23 changes: 10 additions & 13 deletions src/core/logs-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ import {AppodealApi} from 'core/appdeal-api/appodeal-api.factory';
import {AdMobAccount} from 'core/appdeal-api/interfaces/admob-account.interface';
import {Connector} from 'core/connector';
import {Store} from 'core/store';
import {shell} from 'electron';
import {ActionTypes, LogAction} from 'lib/actions';
import {getLogContent, getLogsDirectory, LogFileInfo} from 'lib/sync-logs/logger';


const shell = require('electron').shell;
const path = require('path');
import {getLogContent, logFilePathName} from 'lib/sync-logs/logger';


export class LogsConnector extends Connector {
Expand All @@ -18,25 +15,25 @@ export class LogsConnector extends Connector {
async onAction ({type, payload}: LogAction) {
switch (type) {
case ActionTypes.openLogFile:
this.openLog(payload.account, payload.log);
this.openLog(payload.account, payload.syncId);
return payload;
case ActionTypes.submitLogToAppodeal:
return this.submitLog(payload.account, payload.log, payload.appodealAccountId);
return this.submitLog(payload.account, payload.syncId, payload.appodealAccountId);
}

}

openLog (account: AdMobAccount, log: LogFileInfo) {
console.info('openLog', log);
openLog (account: AdMobAccount, syncId: string) {
console.info('openLog', syncId);
try {
shell.openItem(path.join(getLogsDirectory(account), log.fileName));
shell.openItem(logFilePathName(account, syncId));
} catch (e) {
console.error(e);
}
}

async submitLog (account: AdMobAccount, log: LogFileInfo, appodealAccountId: string) {
const rawLog = await getLogContent(account, log.uuid);
return this.appodealApi.getFor(appodealAccountId).submitLog(account.id, log.uuid, rawLog);
async submitLog (account: AdMobAccount, syncId: string, appodealAccountId: string) {
const rawLog = await getLogContent(account, syncId);
return this.appodealApi.getFor(appodealAccountId).submitLog(account.id, syncId, rawLog);
}
}
40 changes: 8 additions & 32 deletions src/core/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {ActionTypes} from 'lib/actions';
import {AppPreferences, Preferences} from 'lib/app-preferences';
import {deepAssign} from 'lib/core';
import {onActionFromRenderer} from 'lib/messages';
import {getLogsList, LogFileInfo} from 'lib/sync-logs/logger';
import {openSettingsWindow} from 'lib/ui-windows';
import {confirmDialog, messageDialog, openWindow, waitForNavigation} from 'lib/window';
import {action, observable, observe, set} from 'mobx';
Expand All @@ -33,7 +32,6 @@ export interface SyncProgress {
export interface AppState {
selectedAccount: {
account: AdMobAccount;
logs: LogFileInfo[];
}
selectedAppodealAccount: AppodealAccount;
syncHistory: Record<AccountID, SyncHistoryInfo>;
Expand All @@ -50,8 +48,7 @@ export class Store {

@observable readonly state: AppState = {
selectedAccount: {
account: null,
logs: []
account: null
},
selectedAppodealAccount: null,
syncHistory: {},
Expand Down Expand Up @@ -87,13 +84,13 @@ export class Store {
this.onlineService.on('statusChange', isOnline => {
set<AppState>(this.state, 'online', isOnline);
});
this.onlineService.on('nextReconnect' , (time) => {
this.onlineService.on('nextReconnect', (time) => {
set<AppState>(this.state, 'nextReconnect', time);
});
}

public updateUserWhenOnline() {
this.onlineService.on('online',() => {
public updateUserWhenOnline () {
this.onlineService.on('online', () => {
this.fetchAllAppodealUsers();
});
}
Expand Down Expand Up @@ -205,15 +202,8 @@ export class Store {
}

@action
async pushLogs (account: AdMobAccount, logs?: Array<LogFileInfo>) {
logs = Array.isArray(logs) ? logs : await this.loadSelectedAdMobAccountLogs(account);
let selectedAccount = this.state.selectedAccount.account;
if (account && selectedAccount && account.id === selectedAccount.id) {
set(this.state, 'selectedAccount', {
logs,
account
});
}
async pushLogs (account: AdMobAccount) {
return this.updateAdMobAccountInfo(account);
}

@action
Expand Down Expand Up @@ -320,27 +310,13 @@ export class Store {

@action
selectAdMobAccount (newAccount: AdMobAccount) {
let {account} = this.state.selectedAccount;
set<AppState>(this.state, 'selectedAccount', {
account: newAccount,
logs: account && newAccount && account.id === newAccount.id ? this.state.selectedAccount.logs : []
account: newAccount
});
// we dont need to wait while logs are loading
// we want provide quick response to UI
// once log-list is loaded - we will show it
if (newAccount) {
setTimeout(() => this.pushLogs(newAccount));
}
}

loadSelectedAdMobAccountLogs (account: AdMobAccount): Promise<LogFileInfo[]> {
if (!account) {
return Promise.resolve([]);
}
return getLogsList(account).catch(e => {
console.error(e);
return [];
});
setTimeout(() => this.pushLogs(newAccount));
}

@action
Expand Down
16 changes: 13 additions & 3 deletions src/core/sync-apps/sync-history.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {AdMobAccount} from 'core/appdeal-api/interfaces/admob-account.interface';
import {Sync} from 'core/sync-apps/sync';
import {SyncInfo} from 'core/sync-apps/sync-stats';
import {ExtractedAdmobAccount} from 'interfaces/common.interfaces';
import {getJsonFile, saveJsonFile} from 'lib/json-storage';
import path from 'path';
Expand All @@ -8,7 +9,8 @@ import path from 'path';
export interface SyncHistoryInfo {
lastSync: number
lastSuccessfulSync: number;
admobAuthorizationRequired: boolean
admobAuthorizationRequired: boolean,
syncs: SyncInfo[];
}


Expand All @@ -26,10 +28,12 @@ export class SyncHistory {
private static async loadHistory (adMobAccount: AdmobAccount): Promise<SyncHistoryInfo> {
if (!SyncHistory.cache.has(adMobAccount.id)) {
const data = <SyncHistoryInfo>await getJsonFile(SyncHistory.fileName(adMobAccount));
data.syncs = data.syncs || [];
SyncHistory.cache.set(adMobAccount.id, data || {
lastSync: null,
lastSuccessfulSync: null,
admobAuthorizationRequired: true
admobAuthorizationRequired: true,
syncs: []
});
}

Expand All @@ -46,13 +50,19 @@ export class SyncHistory {
await SyncHistory.saveHistory(adMobAccount);
}

public static async logSyncEnd (sync: Sync) {
public static async saveSyncStats (sync: Sync) {
const history = await SyncHistory.loadHistory(sync.adMobAccount);
const time = Date.now();
history.lastSync = time;
if (!sync.hasErrors) {
history.lastSuccessfulSync = time;
}
const statIndex = history.syncs.findIndex(stats => stats.id === sync.id);
if (statIndex === -1) {
history.syncs.unshift(sync.stats.toPlainObject());
} else {
history.syncs[statIndex] = sync.stats.toPlainObject();
}
await SyncHistory.saveHistory(sync.adMobAccount);
}

Expand Down
8 changes: 4 additions & 4 deletions src/core/sync-apps/sync-scheduler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {OnlineService} from 'core/appdeal-api/online.service';
import {Store} from 'core/store';
import {SyncHistory} from 'core/sync-apps/sync-history';
import {SyncService} from 'core/sync-apps/sync.service';
import {SyncRunner, SyncService} from 'core/sync-apps/sync.service';
import {timeConversion} from 'lib/time';
import {observe} from 'mobx';

Expand Down Expand Up @@ -52,7 +52,7 @@ export class SyncScheduler {
unsubscribe = null;
this.store.state.selectedAppodealAccount.accounts.forEach(adMobAccount => {
this.log(`App started. Run sync for Admob Account [${adMobAccount.id} ${adMobAccount.email}]`);
this.syncService.runSync(this.store.state.selectedAppodealAccount.id, adMobAccount)
this.syncService.runSync(this.store.state.selectedAppodealAccount.id, adMobAccount, SyncRunner.SyncScheduler)
.catch(err => {

});
Expand All @@ -75,7 +75,7 @@ export class SyncScheduler {
if (!lastSync) {

this.log(`Admob Account [${adMobAccount.id} ${adMobAccount.email}] has never synced. Run sync.`);
return this.syncService.runSync(this.store.state.selectedAppodealAccount.id, adMobAccount)
return this.syncService.runSync(this.store.state.selectedAppodealAccount.id, adMobAccount, SyncRunner.SyncScheduler)
.catch(err => {

});
Expand All @@ -86,7 +86,7 @@ export class SyncScheduler {
timeConversion(scienceLastSync)
}. Run sync.`
);
return this.syncService.runSync(this.store.state.selectedAppodealAccount.id, adMobAccount)
return this.syncService.runSync(this.store.state.selectedAppodealAccount.id, adMobAccount, SyncRunner.SyncScheduler)
.catch(err => {

});
Expand Down
87 changes: 87 additions & 0 deletions src/core/sync-apps/sync-stats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import {AppodealApp} from 'core/appdeal-api/interfaces/appodeal-app.interface';
import {Sync} from 'core/sync-apps/sync';
import {SyncRunner} from 'core/sync-apps/sync.service';


export type AppInfo = Pick<AppodealApp, 'id'> & Pick<AppodealApp, 'name'>;


export interface SyncInfo {
readonly id: string;
readonly startTs: number
readonly endTs: number;
readonly terminated: boolean;
readonly hasErrors: boolean;
// if only some apps where synced
readonly partialSync: boolean;
affectedApps: {
created: AppInfo[];
updated: AppInfo[];
deleted: AppInfo[];
withErrors: AppInfo[];
},
runner: SyncRunner;
logSubmitted?: boolean;
}

export class SyncStats {

public readonly affectedApps = {
created: [],
updated: [],
deleted: [],
withErrors: []
};

public id: string;

constructor (private sync: Sync) {}

public startTs: number;
public endTs: number;

public terminated = false;
public partialSync = false;

start () {
this.startTs = Date.now();
}

end () {
this.endTs = Date.now();
}

appCreated (app: AppodealApp) {
this.affectedApps.created.push(app);
}

appUpdated (app: AppodealApp) {
if (this.affectedApps.created.some(v => v.id === app.id)) {
return;
}
this.affectedApps.updated = this.affectedApps.updated.filter(v => v.id !== app.id);
}

appDeleted (app: AppodealApp) {
this.affectedApps.deleted.push(app);
}

errorWhileSync (app: AppodealApp) {
this.affectedApps.withErrors.push(app);
}

toPlainObject (): SyncInfo {
return {
id: this.sync.id,
runner: this.sync.runner,
hasErrors: this.sync.hasErrors,
affectedApps: this.affectedApps,
startTs: this.startTs,
endTs: this.endTs,
terminated: this.terminated,
partialSync: this.partialSync

};
}

}
12 changes: 9 additions & 3 deletions src/core/sync-apps/sync.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import {createSyncLogger, getLogContent, LoggerInstance, rotateSyncLogs} from 'l
import uuid from 'uuid';


export enum SyncRunner {
User = 1,
SyncScheduler = 2
}

type FinishPromise = Promise<any>;

export class SyncService {
Expand All @@ -33,7 +38,7 @@ export class SyncService {
}


public async runSync (appodealAccountId: string, admobAccount: AdMobAccount) {
public async runSync (appodealAccountId: string, admobAccount: AdMobAccount, runner: SyncRunner) {
return new Promise(async (resolve, reject) => {
if (this.onlineService.isOffline()) {
console.log('[Sync Service] Can not run sync. No Internet Connection');
Expand Down Expand Up @@ -70,7 +75,8 @@ export class SyncService {
admobAccount,
appodealAccountId,
logger,
id
id,
runner
);

const waitToFinish = [];
Expand Down Expand Up @@ -143,7 +149,7 @@ export class SyncService {
console.error(e);
}
try {
await SyncHistory.logSyncEnd(sync);
await SyncHistory.saveSyncStats(sync);
} catch (e) {
console.error('Failed to save sync history');
this.reportError(sync, e);
Expand Down
Loading

0 comments on commit c840bb2

Please sign in to comment.