diff --git a/packages/server/src/models/utils/user.ts b/packages/server/src/models/utils/user.ts index 33d22a755..3b398d6b8 100644 --- a/packages/server/src/models/utils/user.ts +++ b/packages/server/src/models/utils/user.ts @@ -18,3 +18,16 @@ export function getMaxTotalItemSize(user: User): number { const account = accountByType(user.account_type); return user.max_total_item_size !== null ? user.max_total_item_size : account.max_total_item_size; } + +export function totalSizePercent(user: User): number { + const maxTotalSize = getMaxTotalItemSize(user); + if (!maxTotalSize) return 0; + return user.total_item_size / maxTotalSize; +} + +export function totalSizeClass(user: User) { + const d = totalSizePercent(user); + if (d >= 1) return 'is-danger'; + if (d >= .7) return 'is-warning'; + return ''; +} diff --git a/packages/server/src/routes/index/home.ts b/packages/server/src/routes/index/home.ts index 9958afa98..b4400312f 100644 --- a/packages/server/src/routes/index/home.ts +++ b/packages/server/src/routes/index/home.ts @@ -5,8 +5,9 @@ import { AppContext } from '../../utils/types'; import { contextSessionId } from '../../utils/requestUtils'; import { ErrorMethodNotAllowed } from '../../utils/errors'; import defaultView from '../../utils/defaultView'; -import { accountByType, accountTypeToString } from '../../models/UserModel'; -import { formatMaxItemSize, yesOrNo } from '../../utils/strings'; +import { accountTypeToString } from '../../models/UserModel'; +import { formatMaxItemSize, formatMaxTotalSize, formatTotalSize, formatTotalSizePercent, yesOrNo } from '../../utils/strings'; +import { getCanShareFolder, totalSizeClass } from '../../models/utils/user'; const router: Router = new Router(RouteType.Web); @@ -14,24 +15,35 @@ router.get('home', async (_path: SubPath, ctx: AppContext) => { contextSessionId(ctx); if (ctx.method === 'GET') { - const accountProps = accountByType(ctx.joplin.owner.account_type); + const user = ctx.joplin.owner; const view = defaultView('home', 'Home'); view.content = { userProps: [ { label: 'Account Type', - value: accountTypeToString(accountProps.account_type), + value: accountTypeToString(user.account_type), show: true, }, { label: 'Is Admin', - value: yesOrNo(ctx.joplin.owner.is_admin), - show: !!ctx.joplin.owner.is_admin, + value: yesOrNo(user.is_admin), + show: !!user.is_admin, }, { label: 'Max Item Size', - value: formatMaxItemSize(ctx.joplin.owner), + value: formatMaxItemSize(user), + show: true, + }, + { + label: 'Total Size', + classes: [totalSizeClass(user)], + value: `${formatTotalSize(user)} (${formatTotalSizePercent(user)})`, + show: true, + }, + { + label: 'Max Total Size', + value: formatMaxTotalSize(user), show: true, }, { @@ -41,7 +53,7 @@ router.get('home', async (_path: SubPath, ctx: AppContext) => { }, { label: 'Can Share Notebook', - value: yesOrNo(accountProps.can_share_folder), + value: yesOrNo(getCanShareFolder(user)), show: true, }, ], diff --git a/packages/server/src/routes/index/users.ts b/packages/server/src/routes/index/users.ts index 2a8e98520..f1a168cb4 100644 --- a/packages/server/src/routes/index/users.ts +++ b/packages/server/src/routes/index/users.ts @@ -12,8 +12,8 @@ import { AclAction } from '../../models/BaseModel'; import { NotificationKey } from '../../models/NotificationModel'; import { accountTypeOptions, accountTypeToString } from '../../models/UserModel'; import uuidgen from '../../utils/uuidgen'; -import { formatMaxItemSize, formatMaxTotalSize, formatTotalSize, yesOrNo } from '../../utils/strings'; -import { getCanShareFolder, getMaxTotalItemSize } from '../../models/utils/user'; +import { formatMaxItemSize, formatMaxTotalSize, formatTotalSize, formatTotalSizePercent, yesOrNo } from '../../utils/strings'; +import { getCanShareFolder, totalSizeClass } from '../../models/utils/user'; import { yesNoDefaultOptions } from '../../utils/views/select'; interface CheckPasswordInput { @@ -84,19 +84,6 @@ function userIsMe(path: SubPath): boolean { const router = new Router(RouteType.Web); -function totalSizePercent(user: User): number { - const maxTotalSize = getMaxTotalItemSize(user); - if (!maxTotalSize) return 0; - return user.total_item_size / maxTotalSize; -} - -function totalSizeClass(user: User) { - const d = totalSizePercent(user); - if (d >= 1) return 'is-danger'; - if (d >= .7) return 'is-warning'; - return ''; -} - router.get('users', async (_path: SubPath, ctx: AppContext) => { const userModel = ctx.joplin.models.user(); await userModel.checkIfAllowed(ctx.joplin.owner, AclAction.List); @@ -117,7 +104,7 @@ router.get('users', async (_path: SubPath, ctx: AppContext) => { formattedItemMaxSize: formatMaxItemSize(user), formattedTotalSize: formatTotalSize(user), formattedMaxTotalSize: formatMaxTotalSize(user), - formattedTotalSizePercent: `${Math.round(totalSizePercent(user) * 100)}%`, + formattedTotalSizePercent: formatTotalSizePercent(user), totalSizeClass: totalSizeClass(user), formattedAccountType: accountTypeToString(user.account_type), formattedCanShareFolder: yesOrNo(getCanShareFolder(user)), diff --git a/packages/server/src/services/CronService.ts b/packages/server/src/services/CronService.ts index c1d5ee602..8b63247a8 100644 --- a/packages/server/src/services/CronService.ts +++ b/packages/server/src/services/CronService.ts @@ -7,8 +7,6 @@ const logger = Logger.create('cron'); export default class CronService extends BaseService { public async runInBackground() { - await this.models.item().updateTotalSizes(); - cron.schedule('0 */6 * * *', async () => { const startTime = Date.now(); logger.info('Deleting expired tokens...'); diff --git a/packages/server/src/utils/strings.ts b/packages/server/src/utils/strings.ts index 0608ff182..13cf83db5 100644 --- a/packages/server/src/utils/strings.ts +++ b/packages/server/src/utils/strings.ts @@ -1,5 +1,5 @@ import { User } from '../db'; -import { getMaxItemSize, getMaxTotalItemSize } from '../models/utils/user'; +import { getMaxItemSize, getMaxTotalItemSize, totalSizePercent } from '../models/utils/user'; import { formatBytes } from './bytes'; export function yesOrNo(value: any): string { @@ -20,6 +20,10 @@ export function formatMaxTotalSize(user: User): string { return size ? formatBytes(size) : '∞'; } +export function formatTotalSizePercent(user: User): string { + return `${Math.round(totalSizePercent(user) * 100)}%`; +} + export function formatTotalSize(user: User): string { return formatBytes(user.total_item_size); } diff --git a/packages/server/src/views/index/home.mustache b/packages/server/src/views/index/home.mustache index 4312ca713..b8dc07333 100644 --- a/packages/server/src/views/index/home.mustache +++ b/packages/server/src/views/index/home.mustache @@ -6,10 +6,10 @@ {{#userProps}} {{#show}} - + {{label}} - + {{value}}