Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tool: add qms ranks #7713

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions dev/tool/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import path from 'path'

import { buildStorageFromConfig, createStorageFromConfig, storageConfigFromEnv } from '@hcengineering/server-storage'
import { program, type Command } from 'commander'
import { addControlledDocumentRank } from './qms'
import { clearTelegramHistory } from './telegram'
import { diffWorkspace, recreateElastic, updateField } from './workspace'

Expand Down Expand Up @@ -2101,6 +2102,55 @@ export function devTool (
})
})

program
.command('add-controlled-doc-rank-mongo')
.description('add rank to controlled documents')
.option('-w, --workspace <workspace>', 'Selected workspace only', '')
.action(async (cmd: { workspace: string }) => {
const { version } = prepareTools()

let workspaces: Workspace[] = []
await withAccountDatabase(async (db) => {
workspaces = await listWorkspacesPure(db)
workspaces = workspaces
.filter((p) => isActiveMode(p.mode))
.filter((p) => cmd.workspace === '' || p.workspace === cmd.workspace)
.sort((a, b) => b.lastVisit - a.lastVisit)
})

console.log('found workspaces', workspaces.length)

const mongodbUri = getMongoDBUrl()
const client = getMongoClient(mongodbUri)
const _client = await client.getClient()

try {
const count = workspaces.length
let index = 0
for (const workspace of workspaces) {
index++

toolCtx.info('processing workspace', {
workspace: workspace.workspace,
version: workspace.version,
index,
count
})

if (workspace.version === undefined || !deepEqual(workspace.version, version)) {
console.log(`upgrade to ${versionToString(version)} is required`)
continue
}
const workspaceId = getWorkspaceId(workspace.workspace)
const wsDb = getWorkspaceMongoDB(_client, { name: workspace.workspace })

await addControlledDocumentRank(toolCtx, wsDb, workspaceId)
}
} finally {
client.close()
}
})

extendProgram?.(program)

program.parse(process.argv)
Expand Down
55 changes: 55 additions & 0 deletions dev/tool/src/qms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// Copyright © 2024 Hardcore Engineering Inc.
//
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. You may
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
//

import { type MeasureContext, type Ref, type WorkspaceId } from '@hcengineering/core'
import documents, { type DocumentMeta, type ProjectMeta } from '@hcengineering/controlled-documents'
import { DOMAIN_DOCUMENTS } from '@hcengineering/model-controlled-documents'
import { type Db } from 'mongodb'
import { makeRank } from '@hcengineering/task'

export async function addControlledDocumentRank (ctx: MeasureContext, db: Db, workspaceId: WorkspaceId): Promise<void> {
const collections = await db.listCollections().toArray()
if (collections.find((it) => it.name === DOMAIN_DOCUMENTS) === undefined) {
ctx.error('skipping migration, no collection found', { workspace: workspaceId.name })
return
}

const prjMeta = await db
.collection<ProjectMeta>(DOMAIN_DOCUMENTS)
.find({ _class: documents.class.ProjectMeta })
.toArray()

const docMeta = await db
.collection<DocumentMeta>(DOMAIN_DOCUMENTS)
.find({ _class: documents.class.DocumentMeta })
.toArray()

const docMetaById = new Map<Ref<DocumentMeta>, DocumentMeta>()
for (const doc of docMeta) {
docMetaById.set(doc._id, doc)
}

prjMeta.sort((a, b) => {
const titleA = docMetaById.get(a.meta)?.title ?? ''
const titleB = docMetaById.get(b.meta)?.title ?? ''
return titleA.localeCompare(titleB, undefined, { numeric: true })
})

let rank = makeRank(undefined, undefined)
for (const doc of prjMeta) {
await db.collection(DOMAIN_DOCUMENTS).updateOne({ _id: doc._id }, { $set: { rank } })
rank = makeRank(rank, undefined)
}
}
Loading