number of sync changes to push next to "sync now" button

This commit is contained in:
azivner 2017-11-01 20:31:44 -04:00
parent 72905a9854
commit 962c078bbc
8 changed files with 58 additions and 38 deletions

4
app.js
View File

@ -20,7 +20,7 @@ const migrationRoute = require('./routes/migration');
const treeApiRoute = require('./routes/api/tree');
const notesApiRoute = require('./routes/api/notes');
const notesMoveApiRoute = require('./routes/api/notes_move');
const auditApiRoute = require('./routes/api/audit');
const statusApiRoute = require('./routes/api/status');
const noteHistoryApiRoute = require('./routes/api/note_history');
const recentChangesApiRoute = require('./routes/api/recent_changes');
const settingsApiRoute = require('./routes/api/settings');
@ -88,7 +88,7 @@ app.use('/migration', migrationRoute);
app.use('/api/tree', treeApiRoute);
app.use('/api/notes', notesApiRoute);
app.use('/api/notes', notesMoveApiRoute);
app.use('/api/audit', auditApiRoute);
app.use('/api/status', statusApiRoute);
app.use('/api/notes-history', noteHistoryApiRoute);
app.use('/api/recent-changes', recentChangesApiRoute);
app.use('/api/settings', settingsApiRoute);

View File

@ -0,0 +1,27 @@
function checkStatus() {
$.ajax({
url: baseApiUrl + 'status/' + globalFullLoadTime,
type: 'GET',
success: resp => {
if (resp.changed) {
window.location.reload(true);
}
else {
$("#changesToPushCount").html(resp.changesToPushCount);
}
},
statusCode: {
401: () => {
// if the user got logged out then we should display the page
// here we do that by reloading which will force the redirect if the user is really logged out
window.location.reload(true);
},
409: () => {
// 409 means we need to migrate database, reload will take care of it
window.location.reload(true);
}
}
});
}
setInterval(checkStatus, 10 * 1000);

View File

@ -4,7 +4,7 @@ function syncNow() {
type: 'POST',
success: result => {
if (result.success) {
checkAudit();
checkStatus();
message("Sync finished successfully");

View File

@ -88,31 +88,6 @@ let globalEncryptionSessionTimeout;
let globalEncryptedDataKey;
let globalFullLoadTime;
function checkAudit() {
$.ajax({
url: baseApiUrl + 'audit/' + globalFullLoadTime,
type: 'GET',
success: resp => {
if (resp.changed) {
window.location.reload(true);
}
},
statusCode: {
401: () => {
// if the user got logged out then we should display the page
// here we do that by reloading which will force the redirect if the user is really logged out
window.location.reload(true);
},
409: () => {
// 409 means we need to migrate database, reload will take care of it
window.location.reload(true);
}
}
});
}
setInterval(checkAudit, 10 * 1000);
$(() => {
$.get(baseApiUrl + 'tree').then(resp => {
const notes = resp.notes;

View File

@ -10,11 +10,15 @@ router.get('/:full_load_time', auth.checkApiAuth, async (req, res, next) => {
const browserId = req.get('x-browser-id');
const row = await sql.getSingleResult("SELECT COUNT(*) AS 'count' FROM audit_log WHERE (browser_id IS NULL OR browser_id != ?) " +
const rowCount = await sql.getSingleValue("SELECT COUNT(*) FROM audit_log WHERE (browser_id IS NULL OR browser_id != ?) " +
"AND date_modified >= ?", [browserId, fullLoadTime]);
const lastSyncedPush = await sql.getOption('last_synced_push');
const changesToPushCount = await sql.getSingleValue("SELECT COUNT(*) FROM sync WHERE id > ?", [lastSyncedPush]);
res.send({
'changed': row.count > 0
'changed': rowCount > 0,
'changesToPushCount': changesToPushCount
});
});

View File

@ -55,11 +55,11 @@ async function setOption(optName, optValue) {
}
async function getSingleResult(query, params = []) {
return await db.get(query, ...params);
return await wrap(async () => db.get(query, ...params));
}
async function getSingleResultOrNull(query, params = []) {
const all = await db.all(query, ...params);
const all = await wrap(async () => db.all(query, ...params));
return all.length > 0 ? all[0] : null;
}
@ -75,7 +75,7 @@ async function getSingleValue(query, params = []) {
}
async function getResults(query, params = []) {
return await db.all(query, ...params);
return await wrap(async () => db.all(query, ...params));
}
async function getFlattenedResults(key, query, params = []) {
@ -90,11 +90,11 @@ async function getFlattenedResults(key, query, params = []) {
}
async function execute(query, params = []) {
return await db.run(query, ...params);
return await wrap(async () => db.run(query, ...params));
}
async function executeScript(query) {
return await db.exec(query);
return await wrap(async () => db.exec(query));
}
async function remove(tableName, noteId) {
@ -145,6 +145,19 @@ async function addEntitySync(entityName, entityId, sourceId) {
});
}
async function wrap(func) {
const error = new Error();
try {
return await func();
}
catch (e) {
log.error("Error executing transaction, executing rollback. Inner exception: " + e.stack + error.stack);
throw e;
}
}
async function doInTransaction(func) {
const error = new Error(); // to capture correct stack trace in case of exception

View File

@ -178,9 +178,9 @@ async function sync() {
const cookieJar = await login(syncLog);
await pushSync(cookieJar, syncLog);
await pullSync(cookieJar, syncLog);
await pushSync(cookieJar, syncLog);
}
catch (e) {
logSync("sync failed: " + e.stack, syncLog);

View File

@ -21,7 +21,7 @@
<span id="top-message"></span>
<span id="error-message"></span>
<button class="btn btn-xs" onclick="syncNow();">Sync now</button>
<button class="btn btn-xs" onclick="syncNow();">Sync now (<span id="changesToPushCount">0</span>)</button>
<button class="btn btn-xs" onclick="displaySettings();">Settings</button>
@ -276,6 +276,7 @@
<script src="javascripts/note_history.js"></script>
<script src="javascripts/recent_changes.js"></script>
<script src="javascripts/status.js"></script>
<script src="javascripts/sync.js"></script>
<script src="javascripts/utils.js"></script>
</body>