event_log - entity and logging sync info

This commit is contained in:
azivner 2017-11-03 22:08:27 -04:00
parent bd71547258
commit e35c2dd4ad
10 changed files with 90 additions and 76 deletions

View file

@ -0,0 +1,10 @@
CREATE TABLE `event_log` (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`note_id` TEXT,
`comment` TEXT,
`date_added` INTEGER NOT NULL
);
CREATE INDEX `IDX_event_log_date_added` ON `event_log` (
`date_added`
);

View file

@ -108,7 +108,7 @@ $("#encryption-password-form").submit(() => {
.catch(reason => {
console.log(reason);
alert(reason);
error(reason);
});
return false;
@ -257,7 +257,7 @@ async function changeEncryptionOnNoteHistory(noteId, encrypt) {
const result = await $.ajax({
url: baseApiUrl + 'notes-history/' + noteId + "?encryption=" + (encrypt ? 0 : 1),
type: 'GET',
error: () => alert("Error getting note history.")
error: () => error("Error getting note history.")
});
for (const row of result) {
@ -277,7 +277,7 @@ async function changeEncryptionOnNoteHistory(noteId, encrypt) {
type: 'PUT',
contentType: 'application/json',
data: JSON.stringify(row),
error: () => alert("Error de/encrypting note history.")
error: () => error("Error de/encrypting note history.")
});
console.log('Note history ' + row.note_history_id + ' de/encrypted');
@ -335,7 +335,7 @@ async function encryptSubTree(noteId) {
}
});
alert("Encryption finished.");
message("Encryption finished.");
}
async function decryptSubTree(noteId) {
@ -362,7 +362,7 @@ async function decryptSubTree(noteId) {
}
});
alert("Decryption finished.");
message("Decryption finished.");
}
function updateSubTreeRecursively(noteId, updateCallback, successCallback) {

View file

@ -38,6 +38,6 @@ $("#run-migration").click(() => {
$("#migration-table").append(row);
}
},
error: () => alert("Migration failed with unknown error")
error: () => error("Migration failed with unknown error")
});
});

View file

@ -37,7 +37,7 @@ function showNoteHistoryDialog(noteId, noteHistoryId) {
$("#note-history-list").val(noteHistoryId).trigger('change');
}
},
error: () => alert("Error getting note history.")
error: () => error("Error getting note history.")
});
}

View file

@ -70,7 +70,7 @@ function showRecentChanges() {
$("#recent-changes-dialog").append(dayEl);
}
},
error: () => alert("Error getting recent changes.")
error: () => error("Error getting recent changes.")
});
}

View file

@ -6,17 +6,12 @@ function syncNow() {
if (result.success) {
checkStatus();
message("Sync finished successfully");
for (const l of result.log)
{
console.log(l);
}
message("Sync triggered.");
}
else {
alert("Sync failed");
error("Sync failed");
}
},
error: () => alert("Sync failed")
error: () => error("Sync failed")
});
}

View file

@ -8,11 +8,10 @@ const sql = require('../../services/sql');
const options = require('../../services/options');
router.post('/now', auth.checkApiAuth, async (req, res, next) => {
const log = await sync.sync();
await sync.sync();
res.send({
success: true,
log: log
success: true
});
});

19
services/event_log.js Normal file
View file

@ -0,0 +1,19 @@
const sql = require('./sql');
const utils = require('./utils');
async function addEvent(comment) {
await addNoteEvent(null, comment);
}
async function addNoteEvent(noteId, comment) {
await sql.insert('event_log', {
note_id : noteId,
comment: comment,
date_added: utils.nowTimestamp()
});
}
module.exports = {
addEvent,
addNoteEvent
};

View file

@ -4,7 +4,7 @@ const options = require('./options');
const fs = require('fs-extra');
const log = require('./log');
const APP_DB_VERSION = 24;
const APP_DB_VERSION = 25;
const MIGRATIONS_DIR = "./migrations";
async function migrate() {

View file

@ -9,6 +9,7 @@ const utils = require('./utils');
const config = require('./config');
const SOURCE_ID = require('./source_id');
const audit_category = require('./audit_category');
const eventLog = require('./event_log');
const SYNC_SERVER = config['Sync']['syncServerHost'];
const isSyncSetup = !!SYNC_SERVER;
@ -16,7 +17,7 @@ const isSyncSetup = !!SYNC_SERVER;
let syncInProgress = false;
async function pullSync(syncContext, syncLog) {
async function pullSync(syncContext) {
const lastSyncedPull = parseInt(await options.getOption('last_synced_pull'));
let syncRows;
@ -34,7 +35,7 @@ async function pullSync(syncContext, syncLog) {
logSync("Pulled " + syncRows.length + " changes");
}
catch (e) {
logSyncError("Can't pull changes, inner exception: ", e, syncLog);
logSyncError("Can't pull changes, inner exception: ", e);
}
for (const sync of syncRows) {
@ -48,26 +49,26 @@ async function pullSync(syncContext, syncLog) {
});
}
catch (e) {
logSyncError("Can't pull " + sync.entity_name + " " + sync.entity_id, e, syncLog);
logSyncError("Can't pull " + sync.entity_name + " " + sync.entity_id, e);
}
if (sync.entity_name === 'notes') {
await updateNote(resp.entity, resp.links, syncContext.sourceId, syncLog);
await updateNote(resp.entity, resp.links, syncContext.sourceId);
}
else if (sync.entity_name === 'notes_tree') {
await updateNoteTree(resp, syncContext.sourceId, syncLog);
await updateNoteTree(resp, syncContext.sourceId);
}
else if (sync.entity_name === 'notes_history') {
await updateNoteHistory(resp, syncContext.sourceId, syncLog);
await updateNoteHistory(resp, syncContext.sourceId);
}
else if (sync.entity_name === 'notes_reordering') {
await updateNoteReordering(resp, syncContext.sourceId, syncLog);
await updateNoteReordering(resp, syncContext.sourceId);
}
else if (sync.entity_name === 'options') {
await updateOptions(resp, syncContext.sourceId, syncLog);
await updateOptions(resp, syncContext.sourceId);
}
else {
logSyncError("Unrecognized entity type " + sync.entity_name, e, syncLog);
logSyncError("Unrecognized entity type " + sync.entity_name, e);
}
await options.setOption('last_synced_pull', sync.id);
@ -76,7 +77,7 @@ async function pullSync(syncContext, syncLog) {
logSync("Finished pull");
}
async function sendEntity(entity, entityName, cookieJar, syncLog) {
async function sendEntity(entity, entityName, cookieJar) {
try {
const payload = {
sourceId: SOURCE_ID,
@ -97,11 +98,11 @@ async function sendEntity(entity, entityName, cookieJar, syncLog) {
});
}
catch (e) {
logSyncError("Failed sending update for entity " + entityName, e, syncLog);
logSyncError("Failed sending update for entity " + entityName, e);
}
}
async function readAndPushEntity(sync, syncLog, syncContext) {
async function readAndPushEntity(sync, syncContext) {
let entity;
if (sync.entity_name === 'notes') {
@ -123,15 +124,15 @@ async function readAndPushEntity(sync, syncLog, syncContext) {
entity = await sql.getSingleResult('SELECT * FROM options WHERE opt_name = ?', [sync.entity_id]);
}
else {
logSyncError("Unrecognized entity type " + sync.entity_name, null, syncLog);
logSyncError("Unrecognized entity type " + sync.entity_name, null);
}
logSync("Pushing changes in " + sync.entity_name + " " + sync.entity_id);
await sendEntity(entity, sync.entity_name, syncContext.cookieJar, syncLog);
await sendEntity(entity, sync.entity_name, syncContext.cookieJar);
}
async function pushSync(syncContext, syncLog) {
async function pushSync(syncContext) {
let lastSyncedPush = parseInt(await options.getOption('last_synced_push'));
while (true) {
@ -140,16 +141,16 @@ async function pushSync(syncContext, syncLog) {
if (sync === null) {
// nothing to sync
logSync("Nothing to push", syncLog);
logSync("Nothing to push");
break;
}
if (sync.source_id === syncContext.sourceId) {
logSync("Skipping sync " + sync.entity_name + " " + sync.entity_id + " because it originates from sync target", syncLog);
logSync("Skipping sync " + sync.entity_name + " " + sync.entity_id + " because it originates from sync target");
}
else {
await readAndPushEntity(sync, syncLog, syncContext);
await readAndPushEntity(sync, syncContext);
}
lastSyncedPush = sync.id;
@ -158,7 +159,7 @@ async function pushSync(syncContext, syncLog) {
}
}
async function login(syncLog) {
async function login() {
const timestamp = utils.nowTimestamp();
const documentSecret = await options.getOption('document_secret');
@ -186,55 +187,47 @@ async function login(syncLog) {
};
}
catch (e) {
logSyncError("Can't login to API for sync, inner exception: ", e, syncLog);
logSyncError("Can't login to API for sync, inner exception: ", e);
}
}
async function sync() {
const syncLog = [];
if (syncInProgress) {
syncLog.push("Sync already in progress");
logSyncError("Sync already in progress");
return syncLog;
return;
}
syncInProgress = true;
try {
if (!await migration.isDbUpToDate()) {
syncLog.push("DB not up to date");
logSyncError("DB not up to date");
return syncLog;
return;
}
const syncContext = await login(syncLog);
const syncContext = await login();
await pushSync(syncContext, syncLog);
await pushSync(syncContext);
await pullSync(syncContext, syncLog);
await pullSync(syncContext);
await pushSync(syncContext, syncLog);
await pushSync(syncContext);
}
catch (e) {
logSync("sync failed: " + e.stack, syncLog);
logSync("sync failed: " + e.stack);
}
finally {
syncInProgress = false;
}
return syncLog;
}
function logSync(message, syncLog) {
function logSync(message) {
log.info(message);
if (syncLog) {
syncLog.push(message);
}
}
function logSyncError(message, e, syncLog) {
function logSyncError(message, e) {
let completeMessage = message;
if (e) {
@ -243,14 +236,10 @@ function logSyncError(message, e, syncLog) {
log.info(completeMessage);
if (syncLog) {
syncLog.push(completeMessage);
}
throw new Error(completeMessage);
}
async function updateNote(entity, links, sourceId, syncLog) {
async function updateNote(entity, links, sourceId) {
const origNote = await sql.getSingleResult("select * from notes where note_id = ?", [entity.note_id]);
if (!origNote || origNote.date_modified <= entity.date_modified) {
@ -270,16 +259,18 @@ async function updateNote(entity, links, sourceId, syncLog) {
// we don't distinguish between those for now
await sql.addSyncAudit(audit_category.UPDATE_CONTENT, sourceId, entity.note_id);
await sql.addSyncAudit(audit_category.UPDATE_TITLE, sourceId, entity.note_id);
await eventLog.addNoteEvent(entity.note_id, "Synced note <note>");
});
logSync("Update/sync note " + entity.note_id, syncLog);
logSync("Update/sync note " + entity.note_id);
}
else {
logSync("Sync conflict in note " + entity.note_id, syncLog);
await eventLog.addNoteEvent(entity.note_id, "Sync conflict in note <note>");
}
}
async function updateNoteTree(entity, sourceId, syncLog) {
async function updateNoteTree(entity, sourceId) {
const orig = await sql.getSingleResultOrNull("select * from notes_tree where note_id = ?", [entity.note_id]);
if (orig === null || orig.date_modified < entity.date_modified) {
@ -291,14 +282,14 @@ async function updateNoteTree(entity, sourceId, syncLog) {
await sql.addSyncAudit(audit_category.UPDATE_TITLE, sourceId, entity.note_id);
});
logSync("Update/sync note tree " + entity.note_id, syncLog);
logSync("Update/sync note tree " + entity.note_id);
}
else {
logSync("Sync conflict in note tree " + entity.note_id, syncLog);
await eventLog.addNoteEvent(entity.note_id, "Sync conflict in note tree <note>");
}
}
async function updateNoteHistory(entity, sourceId, syncLog) {
async function updateNoteHistory(entity, sourceId) {
const orig = await sql.getSingleResultOrNull("select * from notes_history where note_history_id = ?", [entity.note_history_id]);
if (orig === null || orig.date_modified_to < entity.date_modified_to) {
@ -308,14 +299,14 @@ async function updateNoteHistory(entity, sourceId, syncLog) {
await sql.addNoteHistorySync(entity.note_history_id, sourceId);
});
logSync("Update/sync note history " + entity.note_history_id, syncLog);
logSync("Update/sync note history " + entity.note_history_id);
}
else {
logSync("Sync conflict in note history for " + entity.note_id + ", from=" + entity.date_modified_from + ", to=" + entity.date_modified_to, syncLog);
await eventLog.addNoteEvent(entity.note_id, "Sync conflict in note history for <note>");
}
}
async function updateNoteReordering(entity, sourceId, syncLog) {
async function updateNoteReordering(entity, sourceId) {
await sql.doInTransaction(async () => {
Object.keys(entity.ordering).forEach(async key => {
await sql.execute("UPDATE notes_tree SET note_pos = ? WHERE note_id = ?", [entity.ordering[key], key]);
@ -326,7 +317,7 @@ async function updateNoteReordering(entity, sourceId, syncLog) {
});
}
async function updateOptions(entity, sourceId, syncLog) {
async function updateOptions(entity, sourceId) {
if (!options.SYNCED_OPTIONS.includes(entity.opt_name)) {
return;
}
@ -340,10 +331,10 @@ async function updateOptions(entity, sourceId, syncLog) {
await sql.addOptionsSync(entity.opt_name, sourceId);
});
logSync("Update/sync options " + entity.opt_name, syncLog);
await eventLog.addEvent("Synced option " + entity.opt_name);
}
else {
logSync("Sync conflict in options for " + entity.opt_name + ", date_modified=" + entity.date_modified, syncLog);
await eventLog.addEvent("Sync conflict in options for " + entity.opt_name);
}
}