platform/dev/tool/src/cleanOrphan.ts
Andrey Sobolev 4e1169a21e
UBERF-6893: Move index build into workspace usage. (#5586)
Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
2024-05-14 13:40:14 +07:00

81 lines
3.1 KiB
TypeScript

import { dropWorkspaceFull, setWorkspaceDisabled, type Workspace } from '@hcengineering/account'
import core, { AccountRole, MeasureMetricsContext, SortingOrder, type MeasureContext } from '@hcengineering/core'
import contact from '@hcengineering/model-contact'
import { type StorageAdapter } from '@hcengineering/server-core'
import { connect } from '@hcengineering/server-tool'
import { type Db, type MongoClient } from 'mongodb'
export async function checkOrphanWorkspaces (
ctx: MeasureContext,
workspaces: Workspace[],
transactorUrl: string,
productId: string,
cmd: { remove: boolean, disable: boolean },
db: Db,
client: MongoClient,
storageAdapter: StorageAdapter,
excludes: string[]
): Promise<void> {
for (const ws of workspaces) {
if (excludes.includes(ws.workspace) || (ws.workspaceUrl != null && excludes.includes(ws.workspaceUrl))) {
continue
}
if ((ws.accounts ?? []).length === 0) {
// Potential orhpan workspace
// Let's connect and check activity.
const connection = await connect(transactorUrl, { name: ws.workspace, productId }, undefined, { admin: 'true' })
const accounts = await connection.findAll(contact.class.PersonAccount, {})
const employees = await connection.findAll(contact.mixin.Employee, {})
let activeOwners = 0
for (const person of employees) {
const account = accounts.find((it) => it.person === person._id)
if (account !== undefined) {
if (account.role === AccountRole.Owner && person.active) {
activeOwners++
}
// console.log('-----------', person.name, person.active, account.email, account.role)
}
}
// Find last transaction index:
const wspace = { name: ws.workspace, productId }
const hasBucket = await storageAdapter.exists(ctx, wspace)
const [lastTx] = await connection.findAll(
core.class.Tx,
{
objectSpace: { $ne: core.space.Model },
createdBy: { $nin: [core.account.System, core.account.ConfigUser] },
modifiedBy: { $ne: core.account.System }
},
{ limit: 1, sort: { modifiedOn: SortingOrder.Descending } }
)
await connection.close()
const lastTxHours = Math.floor((Date.now() - (lastTx?.modifiedOn ?? 0)) / 1000 / 60 / 60)
if (((activeOwners === 0 || lastTx == null) && lastTxHours > 1000) || !hasBucket) {
const createdOn = (ws.createdOn ?? 0) !== 0 ? new Date(ws.createdOn).toDateString() : ''
console.log(
'Found orhpan workspace',
`'${ws.workspaceName}' id: '${ws.workspace}' url:${ws.workspaceUrl} by: ${ws.createdBy ?? ''} on: '${createdOn}'`,
lastTxHours + ' hours without modifications',
hasBucket
)
if (cmd.disable) {
await setWorkspaceDisabled(db, ws._id, true)
}
if (cmd.remove) {
await dropWorkspaceFull(
new MeasureMetricsContext('tool', {}),
db,
client,
productId,
ws.workspace,
storageAdapter
)
}
}
}
}
}