2021-01-22 19:41:11 +02:00
import time from '../time' ;
import BaseItem from '../models/BaseItem' ;
import Alarm from '../models/Alarm' ;
import Folder from '../models/Folder' ;
import Note from '../models/Note' ;
import BaseModel from '../BaseModel' ;
import DecryptionWorker from './DecryptionWorker' ;
import ResourceFetcher from './ResourceFetcher' ;
import Resource from '../models/Resource' ;
import { _ } from '../locale' ;
2020-11-05 18:58:23 +02:00
const { toTitleCase } = require ( '../string-utils.js' ) ;
2017-07-13 20:09:47 +02:00
2021-05-15 20:56:49 +02:00
enum CanRetryType {
E2EE = 'e2ee' ,
ResourceDownload = 'resourceDownload' ,
ItemSync = 'itemSync' ,
}
enum ReportItemType {
OpenList = 'openList' ,
CloseList = 'closeList' ,
}
2024-02-26 12:16:23 +02:00
type ReportItemOrString = ReportItem | string ;
2021-05-15 20:56:49 +02:00
2023-11-03 18:01:13 +02:00
export type RetryAllHandler = ( ) = > void ;
2024-04-08 13:35:57 +02:00
export type RetryHandler = ( ) = > Promise < void > ;
export type IgnoreHandler = ( ) = > Promise < void > ;
2023-11-03 18:01:13 +02:00
export interface ReportSection {
2021-05-15 20:56:49 +02:00
title : string ;
2024-02-26 12:16:23 +02:00
body : ReportItemOrString [ ] ;
2021-05-15 20:56:49 +02:00
name? : string ;
canRetryAll? : boolean ;
2023-11-03 18:01:13 +02:00
retryAllHandler? : RetryAllHandler ;
2021-05-15 20:56:49 +02:00
}
2023-11-03 18:01:13 +02:00
export interface ReportItem {
2021-05-15 20:56:49 +02:00
type ? : ReportItemType ;
key? : string ;
text? : string ;
canRetry? : boolean ;
canRetryType? : CanRetryType ;
2024-04-08 13:35:57 +02:00
retryHandler? : RetryHandler ;
canIgnore? : boolean ;
ignoreHandler? : IgnoreHandler ;
2021-05-15 20:56:49 +02:00
}
2021-01-22 19:41:11 +02:00
export default class ReportService {
2023-03-06 16:22:01 +02:00
public csvEscapeCell ( cell : string ) {
2017-11-21 20:48:50 +02:00
cell = this . csvValueToString ( cell ) ;
2020-03-14 01:46:14 +02:00
const output = cell . replace ( /"/ , '""' ) ;
2017-11-21 20:48:50 +02:00
if ( this . csvCellRequiresQuotes ( cell , ',' ) ) {
2019-09-19 23:51:18 +02:00
return ` " ${ output } " ` ;
2017-11-21 20:48:50 +02:00
}
return output ;
}
2023-03-06 16:22:01 +02:00
public csvCellRequiresQuotes ( cell : string , delimiter : string ) {
2017-11-21 20:48:50 +02:00
if ( cell . indexOf ( '\n' ) >= 0 ) return true ;
if ( cell . indexOf ( '"' ) >= 0 ) return true ;
if ( cell . indexOf ( delimiter ) >= 0 ) return true ;
return false ;
}
2023-03-06 16:22:01 +02:00
public csvValueToString ( v : string ) {
2017-11-21 20:48:50 +02:00
if ( v === undefined || v === null ) return '' ;
return v . toString ( ) ;
}
2023-03-06 16:22:01 +02:00
public csvCreateLine ( row : string [ ] ) {
2017-11-21 20:48:50 +02:00
for ( let i = 0 ; i < row . length ; i ++ ) {
row [ i ] = this . csvEscapeCell ( row [ i ] ) ;
}
return row . join ( ',' ) ;
}
2024-04-05 13:16:49 +02:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
2023-03-06 16:22:01 +02:00
public csvCreate ( rows : any [ ] ) {
2020-03-14 01:46:14 +02:00
const output = [ ] ;
2017-11-21 20:48:50 +02:00
for ( let i = 0 ; i < rows . length ; i ++ ) {
output . push ( this . csvCreateLine ( rows [ i ] ) ) ;
}
return output . join ( '\n' ) ;
}
2024-04-05 13:16:49 +02:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
2023-03-06 16:22:01 +02:00
public async basicItemList ( option : any = null ) {
2017-11-21 20:48:50 +02:00
if ( ! option ) option = { } ;
if ( ! option . format ) option . format = 'array' ;
const itemTypes = BaseItem . syncItemTypes ( ) ;
2020-03-14 01:46:14 +02:00
const output = [ ] ;
2017-11-21 20:48:50 +02:00
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 ` ) ;
2017-11-21 20:48:50 +02:00
for ( let j = 0 ; j < items . length ; j ++ ) {
const item = items [ j ] ;
2020-03-14 01:46:14 +02:00
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 : '' ) ;
2017-11-21 20:48:50 +02:00
output . push ( row ) ;
}
}
return option . format === 'csv' ? this . csvCreate ( output ) : output ;
}
2023-03-06 16:22:01 +02:00
public async syncStatus ( syncTarget : number ) {
2024-04-05 13:16:49 +02:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
2021-01-22 19:41:11 +02:00
const output : any = {
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 ++ ) {
2020-03-14 01:46:14 +02:00
const d = BaseItem . syncItemDefinitions_ [ i ] ;
2023-04-09 13:54:48 +02:00
// ref: https://github.com/laurent22/joplin/issues/7940#issuecomment-1473709148
if ( d . className === 'MasterKey' ) continue ;
2020-03-14 01:46:14 +02:00
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 ;
}
2020-03-14 01:46:14 +02:00
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 ;
}
2021-06-08 22:36:10 +02:00
private addRetryAllHandler ( section : ReportSection ) : ReportSection {
2023-06-30 11:30:29 +02:00
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
2021-06-08 22:36:10 +02:00
const retryHandlers : Function [ ] = [ ] ;
for ( let i = 0 ; i < section . body . length ; i ++ ) {
2024-02-26 12:16:23 +02:00
const item : ReportItemOrString = section . body [ i ] ;
2021-06-08 22:36:10 +02:00
if ( typeof item !== 'string' && item . canRetry ) {
retryHandlers . push ( item . retryHandler ) ;
}
}
if ( retryHandlers . length ) {
section . canRetryAll = true ;
section . retryAllHandler = async ( ) = > {
for ( const retryHandler of retryHandlers ) {
await retryHandler ( ) ;
}
} ;
}
return section ;
}
2023-03-06 16:22:01 +02:00
public async status ( syncTarget : number ) : Promise < ReportSection [ ] > {
2020-03-14 01:46:14 +02:00
const r = await this . syncStatus ( syncTarget ) ;
2021-05-15 20:56:49 +02:00
const sections : ReportSection [ ] = [ ] ;
let section : ReportSection = null ;
2017-07-13 20:09:47 +02:00
2017-12-05 21:21:01 +02:00
const disabledItems = await BaseItem . syncDisabledItems ( syncTarget ) ;
if ( disabledItems . length ) {
section = { title : _ ( 'Items that cannot be synchronised' ) , body : [ ] } ;
2019-06-08 00:11:08 +02:00
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).' ) ) ;
2024-04-08 13:35:57 +02:00
const processRow = ( row : typeof disabledItems [ 0 ] ) = > {
2023-02-05 13:32:28 +02:00
let msg = '' ;
2019-05-12 02:15:52 +02:00
if ( row . location === BaseItem . SYNC_ITEM_LOCATION_LOCAL ) {
2021-05-15 20:56:49 +02:00
msg = _ ( '%s (%s) could not be uploaded: %s' , row . item . title , row . item . id , row . syncInfo . sync_disabled_reason ) ;
2019-05-12 02:15:52 +02:00
} else {
2021-05-15 20:56:49 +02:00
msg = _ ( 'Item "%s" could not be downloaded: %s' , row . syncInfo . item_id , row . syncInfo . sync_disabled_reason ) ;
2019-05-12 02:15:52 +02:00
}
2021-05-15 20:56:49 +02:00
2024-04-15 19:13:41 +02:00
// row.item may be undefined when location !== SYNC_ITEM_LOCATION_LOCAL
const item = { type_ : row.syncInfo.item_type , id : row.syncInfo.item_id } ;
2021-05-15 20:56:49 +02:00
section . body . push ( {
text : msg ,
canRetry : true ,
canRetryType : CanRetryType.ItemSync ,
retryHandler : async ( ) = > {
2024-04-15 19:13:41 +02:00
await BaseItem . saveSyncEnabled ( item . type_ , item . id ) ;
2021-05-15 20:56:49 +02:00
} ,
2024-04-08 13:35:57 +02:00
canIgnore : ! row . warning_ignored ,
ignoreHandler : async ( ) = > {
2024-04-15 19:13:41 +02:00
await BaseItem . ignoreItemSyncWarning ( syncTarget , item ) ;
2024-04-08 13:35:57 +02:00
} ,
2021-05-15 20:56:49 +02:00
} ) ;
2024-04-08 13:35:57 +02:00
} ;
section . body . push ( { type : ReportItemType . OpenList , key : 'disabledSyncItems' } ) ;
let hasIgnoredItems = false ;
let hasUnignoredItems = false ;
for ( const row of disabledItems ) {
if ( ! row . warning_ignored ) {
processRow ( row ) ;
hasUnignoredItems = true ;
} else {
hasIgnoredItems = true ;
}
2017-12-05 21:21:01 +02:00
}
2017-01-29 20:29:34 +02:00
2024-04-08 13:35:57 +02:00
if ( ! hasUnignoredItems ) {
section . body . push ( _ ( 'All item sync failures have been marked as "ignored".' ) ) ;
}
2021-05-15 20:56:49 +02:00
2024-04-08 13:35:57 +02:00
section . body . push ( { type : ReportItemType . CloseList } ) ;
2021-06-08 22:36:10 +02:00
section = this . addRetryAllHandler ( section ) ;
2019-06-08 00:11:08 +02:00
sections . push ( section ) ;
2024-04-08 13:35:57 +02:00
if ( hasIgnoredItems ) {
section = { title : _ ( 'Ignored items that cannot be synchronised' ) , body : [ ] } ;
section . body . push ( _ ( 'These items failed to sync, but have been marked as "ignored". They won\'t cause the sync warning to appear, but still aren\'t synced. To unignore, click "retry".' ) ) ;
section . body . push ( { type : ReportItemType . OpenList , key : 'ignoredDisabledItems' } ) ;
for ( const row of disabledItems ) {
if ( row . warning_ignored ) {
processRow ( row ) ;
}
}
section . body . push ( { type : ReportItemType . CloseList } ) ;
sections . push ( section ) ;
}
2019-06-08 00:11:08 +02:00
}
const decryptionDisabledItems = await DecryptionWorker . instance ( ) . decryptionDisabledItems ( ) ;
if ( decryptionDisabledItems . length ) {
2020-04-04 19:30:13 +02:00
section = { title : _ ( 'Items that cannot be decrypted' ) , body : [ ] , name : 'failedDecryption' , canRetryAll : false , retryAllHandler : null } ;
2019-06-08 00:11:08 +02:00
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
2019-06-08 00:11:08 +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 ,
2021-05-15 20:56:49 +02:00
canRetryType : CanRetryType.E2EE ,
2019-07-29 15:43:53 +02:00
retryHandler : async ( ) = > {
await DecryptionWorker . instance ( ) . clearDisabledItem ( row . type_ , row . id ) ;
2021-01-22 19:41:11 +02:00
void DecryptionWorker . instance ( ) . scheduleStart ( ) ;
2019-07-29 15:43:53 +02:00
} ,
} ) ;
2019-06-08 00:11:08 +02:00
}
2017-01-29 20:29:34 +02:00
2021-06-08 22:36:10 +02:00
section = this . addRetryAllHandler ( section ) ;
2020-04-04 19:30:13 +02:00
2017-12-05 21:21:01 +02:00
sections . push ( section ) ;
}
2019-12-28 21:23:38 +02:00
{
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 ) ;
2020-12-30 20:35:18 +02:00
const createdLocallyCount = await Resource . createdLocallyCount ( ) ;
2019-12-28 21:23:38 +02:00
section . body . push ( _ ( '%s: %d' , _ ( 'Downloaded and decrypted' ) , downloadedCount - downloadedButEncryptedBlobCount ) ) ;
section . body . push ( _ ( '%s: %d' , _ ( 'Downloaded and encrypted' ) , downloadedButEncryptedBlobCount ) ) ;
2020-12-30 20:35:18 +02:00
section . body . push ( _ ( '%s: %d' , _ ( 'Created locally' ) , createdLocallyCount ) ) ;
2019-12-28 21:23:38 +02:00
} 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 ,
2021-05-15 20:56:49 +02:00
canRetryType : CanRetryType.ResourceDownload ,
2019-12-28 21:23:38 +02:00
retryHandler : async ( ) = > {
2023-12-13 21:24:58 +02:00
await Resource . resetFetchErrorStatus ( row . resource_id ) ;
2021-01-22 19:41:11 +02:00
void ResourceFetcher . instance ( ) . autoAddResources ( ) ;
2019-12-28 21:23:38 +02:00
} ,
} ) ;
}
2023-11-03 18:01:13 +02:00
section = this . addRetryAllHandler ( section ) ;
2019-12-28 21:23:38 +02:00
sections . push ( section ) ;
}
2017-12-05 21:21:01 +02:00
section = { title : _ ( 'Sync status (synced items / total items)' ) , body : [ ] } ;
2017-07-13 20:09:47 +02:00
2020-03-14 01:46:14 +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 ) ;
2024-05-03 00:12:06 +02:00
section = { title : _ ( 'Notebooks' ) , body : [ ] } ;
2017-07-13 20:09:47 +02:00
2017-11-28 22:31:14 +02:00
const folders = await Folder . all ( {
2024-01-06 19:21:51 +02:00
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 ) ;
2017-11-28 22:31:14 +02:00
const alarms = await Alarm . allDue ( ) ;
2017-12-05 21:21:01 +02:00
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-11-28 22:31:14 +02:00
2017-07-13 20:09:47 +02:00
return sections ;
}
}