diff --git a/server/migrations/20241127151705-attachment-tracking.js b/server/migrations/20241127151705-attachment-tracking.js new file mode 100644 index 0000000000..2ada3411b7 --- /dev/null +++ b/server/migrations/20241127151705-attachment-tracking.js @@ -0,0 +1,21 @@ +"use strict"; + +/** @type {import('sequelize-cli').Migration} */ +module.exports = { + async up(queryInterface, Sequelize) { + await queryInterface.sequelize.transaction(async transaction => { + await queryInterface.addColumn("teams", "approximateTotalAttachmentsSize", { + type: Sequelize.BIGINT, + defaultValue: 0, + }, { transaction }); + await queryInterface.addIndex("attachments", ["createdAt"], { transaction }); + }); + }, + + async down(queryInterface, Sequelize) { + await queryInterface.sequelize.transaction(async transaction => { + await queryInterface.removeIndex("attachments", ["createdAt"], { transaction }); + await queryInterface.removeColumn("teams", "approximateTotalAttachmentsSize", { transaction }); + }); + }, +}; diff --git a/server/models/Team.ts b/server/models/Team.ts index f54882dd02..a8c844a9a1 100644 --- a/server/models/Team.ts +++ b/server/models/Team.ts @@ -25,6 +25,7 @@ import { AfterUpdate, BeforeUpdate, BeforeCreate, + IsNumeric, } from "sequelize-typescript"; import { TeamPreferenceDefaults } from "@shared/constants"; import { TeamPreference, TeamPreferences, UserRole } from "@shared/types"; @@ -151,6 +152,11 @@ class Team extends ParanoidModel< @Column(DataType.STRING) defaultUserRole: UserRole; + /** Approximate size in bytes of all attachments in the team. */ + @IsNumeric + @Column(DataType.BIGINT) + approximateTotalAttachmentsSize: number; + @AllowNull @Column(DataType.JSONB) preferences: TeamPreferences | null; diff --git a/server/queues/tasks/UpdateTeamAttachmentsSizeTask.ts b/server/queues/tasks/UpdateTeamAttachmentsSizeTask.ts new file mode 100644 index 0000000000..49c5f2c1e7 --- /dev/null +++ b/server/queues/tasks/UpdateTeamAttachmentsSizeTask.ts @@ -0,0 +1,31 @@ +import { Attachment, Team } from "@server/models"; +import BaseTask, { TaskPriority } from "./BaseTask"; + +type Props = { + /** The teamId to operate on */ + teamId: string; +}; + +/** + * A task that updates the team stats. + */ +export default class UpdateTeamAttachmentsSizeTask extends BaseTask { + public async perform({ teamId }: Props) { + const sizeInBytes = await Attachment.getTotalSizeForTeam(teamId); + if (!sizeInBytes) { + return; + } + + await Team.update( + { approximateTotalAttachmentsSize: sizeInBytes }, + { where: { id: teamId } } + ); + } + + public get options() { + return { + attempts: 1, + priority: TaskPriority.Background, + }; + } +} diff --git a/server/queues/tasks/UpdateTeamsAttachmentsSizeTask.ts b/server/queues/tasks/UpdateTeamsAttachmentsSizeTask.ts new file mode 100644 index 0000000000..433a0835a7 --- /dev/null +++ b/server/queues/tasks/UpdateTeamsAttachmentsSizeTask.ts @@ -0,0 +1,53 @@ +import { subDays } from "date-fns"; +import { Op } from "sequelize"; +import Logger from "@server/logging/Logger"; +import { Attachment } from "@server/models"; +import { sequelize } from "@server/storage/database"; +import BaseTask, { TaskPriority, TaskSchedule } from "./BaseTask"; +import UpdateTeamAttachmentsSizeTask from "./UpdateTeamAttachmentsSizeTask"; + +type Props = { + limit: number; +}; + +export default class UpdateTeamsAttachmentsSizeTask extends BaseTask { + static cron = TaskSchedule.Daily; + + public async perform({ limit }: Props) { + Logger.info( + "task", + `Recalculating attachment sizes for upto ${limit} teams…` + ); + + // Find unique attachment teamIds created in the last day, update only + // those teams' approximate attachment sizes + await Attachment.findAllInBatches( + { + attributes: [ + [sequelize.fn("DISTINCT", sequelize.col("teamId")), "teamId"], + ], + where: { + createdAt: { + [Op.gt]: subDays(new Date(), 1), + }, + }, + batchLimit: 100, + raw: true, + }, + async (rows) => { + const teamIds = rows.map((row) => row.teamId); + + for (const teamId of teamIds) { + await UpdateTeamAttachmentsSizeTask.schedule({ teamId }); + } + } + ); + } + + public get options() { + return { + attempts: 1, + priority: TaskPriority.Background, + }; + } +}