1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00
joplin/ReactNativeClient/lib/services/report.js

268 lines
8.2 KiB
JavaScript
Raw Normal View History

const { time } = require('lib/time-utils');
2017-12-14 20:12:14 +02:00
const BaseItem = require('lib/models/BaseItem.js');
const Alarm = require('lib/models/Alarm');
2017-12-14 20:12:14 +02:00
const Folder = require('lib/models/Folder.js');
const Note = require('lib/models/Note.js');
const BaseModel = require('lib/BaseModel.js');
const DecryptionWorker = require('lib/services/DecryptionWorker');
const ResourceFetcher = require('lib/services/ResourceFetcher');
const Resource = require('lib/models/Resource');
const { _ } = require('lib/locale.js');
const { toTitleCase } = require('lib/string-utils.js');
2017-07-13 20:09:47 +02:00
class ReportService {
csvEscapeCell(cell) {
cell = this.csvValueToString(cell);
const output = cell.replace(/"/, '""');
if (this.csvCellRequiresQuotes(cell, ',')) {
2019-09-19 23:51:18 +02:00
return `"${output}"`;
}
return output;
}
csvCellRequiresQuotes(cell, delimiter) {
if (cell.indexOf('\n') >= 0) return true;
if (cell.indexOf('"') >= 0) return true;
if (cell.indexOf(delimiter) >= 0) return true;
return false;
}
csvValueToString(v) {
if (v === undefined || v === null) return '';
return v.toString();
}
csvCreateLine(row) {
for (let i = 0; i < row.length; i++) {
row[i] = this.csvEscapeCell(row[i]);
}
return row.join(',');
}
csvCreate(rows) {
const output = [];
for (let i = 0; i < rows.length; i++) {
output.push(this.csvCreateLine(rows[i]));
}
return output.join('\n');
}
async basicItemList(option = null) {
if (!option) option = {};
if (!option.format) option.format = 'array';
const itemTypes = BaseItem.syncItemTypes();
const output = [];
output.push(['type', 'id', 'updated_time', 'sync_time', 'is_conflict']);
for (let i = 0; i < itemTypes.length; i++) {
const itemType = itemTypes[i];
const ItemClass = BaseItem.getClassByItemType(itemType);
2019-09-19 23:51:18 +02:00
const items = await ItemClass.modelSelectAll(`SELECT items.id, items.updated_time, sync_items.sync_time FROM ${ItemClass.tableName()} items JOIN sync_items ON sync_items.item_id = items.id`);
for (let j = 0; j < items.length; j++) {
const item = items[j];
const row = [itemType, item.id, item.updated_time, item.sync_time];
2019-07-29 15:43:53 +02:00
row.push('is_conflict' in item ? item.is_conflict : '');
output.push(row);
}
}
return option.format === 'csv' ? this.csvCreate(output) : output;
}
2017-07-16 18:06:05 +02:00
async syncStatus(syncTarget) {
const output = {
2017-07-13 20:09:47 +02:00
items: {},
total: {},
};
let itemCount = 0;
let syncedCount = 0;
for (let i = 0; i < BaseItem.syncItemDefinitions_.length; i++) {
const d = BaseItem.syncItemDefinitions_[i];
const ItemClass = BaseItem.getClass(d.className);
const o = {
2017-07-13 20:09:47 +02:00
total: await ItemClass.count(),
2017-07-16 18:06:05 +02:00
synced: await ItemClass.syncedCount(syncTarget),
2017-07-13 20:09:47 +02:00
};
output.items[d.className] = o;
itemCount += o.total;
syncedCount += o.synced;
}
const conflictedCount = await Note.conflictedCount();
2017-07-16 00:47:11 +02:00
2017-07-13 20:09:47 +02:00
output.total = {
2017-07-16 00:47:11 +02:00
total: itemCount - conflictedCount,
2017-07-13 20:09:47 +02:00
synced: syncedCount,
};
output.toDelete = {
2017-07-19 21:15:55 +02:00
total: await BaseItem.deletedItemCount(syncTarget),
2017-07-13 20:09:47 +02:00
};
2017-07-15 17:35:40 +02:00
output.conflicted = {
total: await Note.conflictedCount(),
};
output.items['Note'].total -= output.conflicted.total;
2017-07-13 20:09:47 +02:00
return output;
}
2017-07-16 18:06:05 +02:00
async status(syncTarget) {
const r = await this.syncStatus(syncTarget);
const sections = [];
let section = null;
2017-07-13 20:09:47 +02:00
const disabledItems = await BaseItem.syncDisabledItems(syncTarget);
if (disabledItems.length) {
section = { title: _('Items that cannot be synchronised'), body: [] };
section.body.push(_('These items will remain on the device but will not be uploaded to the sync target. In order to find these items, either search for the title or the ID (which is displayed in brackets above).'));
section.body.push('');
for (let i = 0; i < disabledItems.length; i++) {
const row = disabledItems[i];
if (row.location === BaseItem.SYNC_ITEM_LOCATION_LOCAL) {
section.body.push(_('%s (%s) could not be uploaded: %s', row.item.title, row.item.id, row.syncInfo.sync_disabled_reason));
} else {
section.body.push(_('Item "%s" could not be downloaded: %s', row.syncInfo.item_id, row.syncInfo.sync_disabled_reason));
}
}
2017-01-29 20:29:34 +02:00
sections.push(section);
}
const decryptionDisabledItems = await DecryptionWorker.instance().decryptionDisabledItems();
if (decryptionDisabledItems.length) {
Desktop: Resolves #2688: Add "Retry All" option to synchronisation page Based on pull request #2712 by fhfuih commit 7d3815e51dbf682ecc1ed3544a15e51b79d5d12d Author: Laurent Cozic <laurent@cozic.net> Date: Sat Apr 4 18:26:01 2020 +0100 Fix issues commit dad1d2c4d4eb37fdd23096a3344cf4f14498e335 Merge: 16cf0a3058 0a1ba511ea Author: Laurent Cozic <laurent@cozic.net> Date: Sat Apr 4 18:09:46 2020 +0100 Merge branch 'master' of https://github.com/fhfuih/joplin into fhfuih-master commit 0a1ba511ea4cb04a20a1334dbc45a9ad6ad15300 Merge: 6207b42044 bdd760f343 Author: fhfuih <fhfuih@outlook.com> Date: Sat Mar 14 20:30:27 2020 -0400 Merge branch 'master' into master commit 6207b42044127b49fdd879217fc080fa11fb686b Author: fhfuih <sam.zyhuang@outlook.com> Date: Sat Mar 14 20:13:28 2020 -0400 Decryption Retry All RN frontend commit 6f46d1985f580ce4b1428c0a2ce16800b696ce1d Author: fhfuih <sam.zyhuang@outlook.com> Date: Sat Mar 14 20:10:55 2020 -0400 Decryption Retry All backend & desktop commit 4c3d37b311991dcbb76e984c299d5c5517bd2437 Author: fhfuih <sam.zyhuang@outlook.com> Date: Fri Mar 13 13:21:21 2020 -0400 Revert "Add "retry all" option to synchronisation page" This reverts commit 15daaa16fdb961003af78d54a083a3a35c79d7b3. commit cffc919791f86dd980bf326c913c4931c3261eca Author: fhfuih <sam.zyhuang@outlook.com> Date: Fri Mar 13 13:19:51 2020 -0400 Revert "Refactor "retry all" renderer to avoid hacks" This reverts commit 51ba6ea65521c8bfa953d90f8078684d8293122b. commit 51ba6ea65521c8bfa953d90f8078684d8293122b Author: fhfuih <sam.zyhuang@outlook.com> Date: Tue Mar 10 18:27:22 2020 -0400 Refactor "retry all" renderer to avoid hacks commit 57d71712e1718d9bdb18bc2b5fdaea94211a01e8 Merge: 15daaa16fd b367955e56 Author: fhfuih <sam.zyhuang@outlook.com> Date: Mon Mar 9 20:42:02 2020 -0400 Merge branch 'master' of https://github.com/fhfuih/joplin commit 15daaa16fdb961003af78d54a083a3a35c79d7b3 Author: fhfuih <sam.zyhuang@outlook.com> Date: Mon Mar 9 20:36:09 2020 -0400 Add "retry all" option to synchronisation page
2020-04-04 19:30:13 +02:00
section = { title: _('Items that cannot be decrypted'), body: [], name: 'failedDecryption', canRetryAll: false, retryAllHandler: null };
section.body.push(_('Joplin failed to decrypt these items multiple times, possibly because they are corrupted or too large. These items will remain on the device but Joplin will no longer attempt to decrypt them.'));
2017-01-29 20:29:34 +02:00
section.body.push('');
2019-07-29 15:43:53 +02:00
for (let i = 0; i < decryptionDisabledItems.length; i++) {
const row = decryptionDisabledItems[i];
2019-07-29 15:43:53 +02:00
section.body.push({
text: _('%s: %s', toTitleCase(BaseModel.modelTypeToName(row.type_)), row.id),
canRetry: true,
canRetryType: 'e2ee',
2019-07-29 15:43:53 +02:00
retryHandler: async () => {
await DecryptionWorker.instance().clearDisabledItem(row.type_, row.id);
DecryptionWorker.instance().scheduleStart();
},
});
}
2017-01-29 20:29:34 +02:00
Desktop: Resolves #2688: Add "Retry All" option to synchronisation page Based on pull request #2712 by fhfuih commit 7d3815e51dbf682ecc1ed3544a15e51b79d5d12d Author: Laurent Cozic <laurent@cozic.net> Date: Sat Apr 4 18:26:01 2020 +0100 Fix issues commit dad1d2c4d4eb37fdd23096a3344cf4f14498e335 Merge: 16cf0a3058 0a1ba511ea Author: Laurent Cozic <laurent@cozic.net> Date: Sat Apr 4 18:09:46 2020 +0100 Merge branch 'master' of https://github.com/fhfuih/joplin into fhfuih-master commit 0a1ba511ea4cb04a20a1334dbc45a9ad6ad15300 Merge: 6207b42044 bdd760f343 Author: fhfuih <fhfuih@outlook.com> Date: Sat Mar 14 20:30:27 2020 -0400 Merge branch 'master' into master commit 6207b42044127b49fdd879217fc080fa11fb686b Author: fhfuih <sam.zyhuang@outlook.com> Date: Sat Mar 14 20:13:28 2020 -0400 Decryption Retry All RN frontend commit 6f46d1985f580ce4b1428c0a2ce16800b696ce1d Author: fhfuih <sam.zyhuang@outlook.com> Date: Sat Mar 14 20:10:55 2020 -0400 Decryption Retry All backend & desktop commit 4c3d37b311991dcbb76e984c299d5c5517bd2437 Author: fhfuih <sam.zyhuang@outlook.com> Date: Fri Mar 13 13:21:21 2020 -0400 Revert "Add "retry all" option to synchronisation page" This reverts commit 15daaa16fdb961003af78d54a083a3a35c79d7b3. commit cffc919791f86dd980bf326c913c4931c3261eca Author: fhfuih <sam.zyhuang@outlook.com> Date: Fri Mar 13 13:19:51 2020 -0400 Revert "Refactor "retry all" renderer to avoid hacks" This reverts commit 51ba6ea65521c8bfa953d90f8078684d8293122b. commit 51ba6ea65521c8bfa953d90f8078684d8293122b Author: fhfuih <sam.zyhuang@outlook.com> Date: Tue Mar 10 18:27:22 2020 -0400 Refactor "retry all" renderer to avoid hacks commit 57d71712e1718d9bdb18bc2b5fdaea94211a01e8 Merge: 15daaa16fd b367955e56 Author: fhfuih <sam.zyhuang@outlook.com> Date: Mon Mar 9 20:42:02 2020 -0400 Merge branch 'master' of https://github.com/fhfuih/joplin commit 15daaa16fdb961003af78d54a083a3a35c79d7b3 Author: fhfuih <sam.zyhuang@outlook.com> Date: Mon Mar 9 20:36:09 2020 -0400 Add "retry all" option to synchronisation page
2020-04-04 19:30:13 +02:00
const retryHandlers = [];
for (let i = 0; i < section.body.length; i++) {
if (section.body[i].canRetry) {
retryHandlers.push(section.body[i].retryHandler);
}
}
if (retryHandlers.length > 1) {
section.canRetryAll = true;
section.retryAllHandler = async () => {
for (const retryHandler of retryHandlers) {
await retryHandler();
}
};
}
sections.push(section);
}
{
section = { title: _('Attachments'), body: [], name: 'resources' };
const statuses = [Resource.FETCH_STATUS_IDLE, Resource.FETCH_STATUS_STARTED, Resource.FETCH_STATUS_DONE, Resource.FETCH_STATUS_ERROR];
for (const status of statuses) {
if (status === Resource.FETCH_STATUS_DONE) {
const downloadedButEncryptedBlobCount = await Resource.downloadedButEncryptedBlobCount();
const downloadedCount = await Resource.downloadStatusCounts(Resource.FETCH_STATUS_DONE);
section.body.push(_('%s: %d', _('Downloaded and decrypted'), downloadedCount - downloadedButEncryptedBlobCount));
section.body.push(_('%s: %d', _('Downloaded and encrypted'), downloadedButEncryptedBlobCount));
} else {
const count = await Resource.downloadStatusCounts(status);
section.body.push(_('%s: %d', Resource.fetchStatusToLabel(status), count));
}
}
sections.push(section);
}
const resourceErrorFetchStatuses = await Resource.errorFetchStatuses();
if (resourceErrorFetchStatuses.length) {
section = { title: _('Attachments that could not be downloaded'), body: [], name: 'failedResourceDownload' };
for (let i = 0; i < resourceErrorFetchStatuses.length; i++) {
const row = resourceErrorFetchStatuses[i];
section.body.push({
text: _('%s (%s): %s', row.resource_title, row.resource_id, row.fetch_error),
canRetry: true,
canRetryType: 'resourceDownload',
retryHandler: async () => {
await Resource.resetErrorStatus(row.resource_id);
ResourceFetcher.instance().autoAddResources();
},
});
}
sections.push(section);
}
section = { title: _('Sync status (synced items / total items)'), body: [] };
2017-07-13 20:09:47 +02:00
for (const n in r.items) {
2017-07-13 20:09:47 +02:00
if (!r.items.hasOwnProperty(n)) continue;
section.body.push(_('%s: %d/%d', n, r.items[n].synced, r.items[n].total));
}
2017-07-15 17:35:40 +02:00
section.body.push(_('Total: %d/%d', r.total.synced, r.total.total));
section.body.push('');
section.body.push(_('Conflicted: %d', r.conflicted.total));
section.body.push(_('To delete: %d', r.toDelete.total));
2017-07-13 20:09:47 +02:00
sections.push(section);
section = { title: _('Folders'), body: [] };
2017-07-13 20:09:47 +02:00
const folders = await Folder.all({
order: { by: 'title', dir: 'ASC' },
2017-07-14 20:02:45 +02:00
caseInsensitive: true,
});
2017-07-13 20:09:47 +02:00
for (let i = 0; i < folders.length; i++) {
section.body.push(_('%s: %d notes', folders[i].title, await Folder.noteCount(folders[i].id)));
}
sections.push(section);
const alarms = await Alarm.allDue();
if (alarms.length) {
section = { title: _('Coming alarms'), body: [] };
for (let i = 0; i < alarms.length; i++) {
const alarm = alarms[i];
const note = await Note.load(alarm.note_id);
section.body.push(_('On %s: %s', time.formatMsToLocal(alarm.trigger_time), note.title));
}
sections.push(section);
}
2017-07-13 20:09:47 +02:00
return sections;
}
}
2019-07-29 15:43:53 +02:00
module.exports = { ReportService };