Skip to content

Commit c3a5b88

Browse files
authored
Fix mongo indexes (#6122)
Signed-off-by: Andrey Sobolev <[email protected]>
1 parent cfe99b6 commit c3a5b88

File tree

34 files changed

+791
-434
lines changed

34 files changed

+791
-434
lines changed

Diff for: dev/tool/src/index.ts

+33-8
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import {
4646
createStorageBackupStorage,
4747
restore
4848
} from '@hcengineering/server-backup'
49-
import serverClientPlugin, { BlobClient, getTransactorEndpoint } from '@hcengineering/server-client'
49+
import serverClientPlugin, { BlobClient, createClient, getTransactorEndpoint } from '@hcengineering/server-client'
5050
import serverToken, { decodeToken, generateToken } from '@hcengineering/server-token'
5151
import toolPlugin from '@hcengineering/server-tool'
5252

@@ -244,13 +244,29 @@ export function devTool (
244244
const { mongodbUri } = prepareTools()
245245
await withDatabase(mongodbUri, async (db, client) => {
246246
console.log(`assigning user ${email} to ${workspace}...`)
247-
const workspaceInfo = await getWorkspaceById(db, productId, workspace)
248-
if (workspaceInfo === null) {
249-
throw new Error(`workspace ${workspace} not found`)
250-
}
251-
console.log('assigning to workspace', workspaceInfo)
252247
try {
253-
await assignWorkspace(toolCtx, db, productId, null, email, workspaceInfo.workspace, AccountRole.User)
248+
const workspaceInfo = await getWorkspaceById(db, productId, workspace)
249+
if (workspaceInfo === null) {
250+
throw new Error(`workspace ${workspace} not found`)
251+
}
252+
const token = generateToken(systemAccountEmail, { name: workspaceInfo.workspace, productId })
253+
const endpoint = await getTransactorEndpoint(token, 'external')
254+
console.log('assigning to workspace', workspaceInfo, endpoint)
255+
const client = await createClient(endpoint, token)
256+
console.log('assigning to workspace connected', workspaceInfo, endpoint)
257+
await assignWorkspace(
258+
toolCtx,
259+
db,
260+
productId,
261+
null,
262+
email,
263+
workspaceInfo.workspace,
264+
AccountRole.User,
265+
undefined,
266+
undefined,
267+
client
268+
)
269+
await client.close()
254270
} catch (err: any) {
255271
console.error(err)
256272
}
@@ -328,7 +344,16 @@ export function devTool (
328344
const { mongodbUri } = prepareTools()
329345
console.log(`set user ${email} role for ${workspace}...`)
330346
await withDatabase(mongodbUri, async (db) => {
331-
await setRole(toolCtx, db, email, workspace, productId, role)
347+
const workspaceInfo = await getWorkspaceById(db, productId, workspace)
348+
if (workspaceInfo === null) {
349+
throw new Error(`workspace ${workspace} not found`)
350+
}
351+
console.log('assigning to workspace', workspaceInfo)
352+
const token = generateToken(systemAccountEmail, { name: workspaceInfo.workspace, productId })
353+
const endpoint = await getTransactorEndpoint(token, 'external')
354+
const client = await createClient(endpoint, token)
355+
await setRole(toolCtx, db, email, workspace, productId, role, client)
356+
await client.close()
332357
})
333358
})
334359

Diff for: models/activity/src/index.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,7 @@ export function createModel (builder: Builder): void {
362362

363363
builder.createDoc(core.class.DomainIndexConfiguration, core.space.Model, {
364364
domain: DOMAIN_ACTIVITY,
365-
indexes: [
366-
{ attachedTo: 1, createdOn: 1 },
367-
{ attachedTo: 1, createdOn: -1 }
368-
],
365+
indexes: [{ keys: { attachedTo: 1, createdOn: 1 } }, { keys: { attachedTo: 1, createdOn: -1 } }],
369366
disabled: [
370367
{ modifiedOn: 1 },
371368
{ createdOn: -1 },

Diff for: models/core/src/core.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {
3636
type DomainIndexConfiguration,
3737
type Enum,
3838
type EnumOf,
39-
type FieldIndex,
39+
type FieldIndexConfig,
4040
type FullTextSearchContext,
4141
type IndexStageState,
4242
type IndexingConfiguration,
@@ -134,7 +134,7 @@ export class TAttachedDoc extends TDoc implements AttachedDoc {
134134
export class TBlob extends TDoc implements Blob {
135135
@Prop(TypeString(), core.string.Blob)
136136
@ReadOnly()
137-
@Index(IndexKind.Indexed)
137+
// @Index(IndexKind.Indexed)
138138
provider!: string
139139

140140
@Prop(TypeString(), core.string.BlobContentType)
@@ -340,7 +340,6 @@ export class TDocIndexState extends TDoc implements DocIndexState {
340340
stages!: Record<string, boolean | string>
341341

342342
@Prop(TypeString(), getEmbeddedLabel('Generation'))
343-
@Index(IndexKind.Indexed)
344343
@Hidden()
345344
generationId?: string
346345
}
@@ -371,7 +370,7 @@ export class TConfiguration extends TDoc implements Configuration {
371370

372371
@MMixin(core.mixin.IndexConfiguration, core.class.Class)
373372
export class TIndexConfiguration<T extends Doc = Doc> extends TClass implements IndexingConfiguration<T> {
374-
indexes!: FieldIndex<T>[]
373+
indexes!: (string | FieldIndexConfig<T>)[]
375374
searchDisabled!: boolean
376375
}
377376

Diff for: models/core/src/index.ts

+28-29
Original file line numberDiff line numberDiff line change
@@ -194,26 +194,32 @@ export function createModel (builder: Builder): void {
194194
core.class.Class,
195195
core.mixin.IndexConfiguration,
196196
{
197-
indexes: [
198-
'tx.objectId',
199-
'tx.operations.attachedTo',
200-
'space',
201-
{
197+
indexes: ['tx.objectId', 'tx.operations.attachedTo']
198+
}
199+
)
200+
builder.createDoc(core.class.DomainIndexConfiguration, core.space.Model, {
201+
domain: DOMAIN_TX,
202+
disabled: [
203+
{ space: 1 },
204+
{ objectClass: 1 },
205+
{ createdBy: 1 },
206+
{ createdBy: -1 },
207+
{ createdOn: -1 },
208+
{ modifiedBy: 1 },
209+
{ objectSpace: 1 }
210+
],
211+
indexes: [
212+
{
213+
keys: {
202214
objectSpace: 1,
203215
_id: 1,
204216
modifiedOn: 1
205217
},
206-
{
207-
objectSpace: 1,
208-
modifiedBy: 1,
209-
objectClass: 1
218+
filter: {
219+
objectSpace: core.space.Model
210220
}
211-
]
212-
}
213-
)
214-
builder.createDoc(core.class.DomainIndexConfiguration, core.space.Model, {
215-
domain: DOMAIN_TX,
216-
disabled: [{ space: 1 }, { objectClass: 1 }, { createdBy: 1 }, { createdBy: -1 }, { createdOn: -1 }]
221+
}
222+
]
217223
})
218224

219225
builder.createDoc(core.class.DomainIndexConfiguration, core.space.Model, {
@@ -299,20 +305,13 @@ export function createModel (builder: Builder): void {
299305
{
300306
indexes: [
301307
{
302-
_class: 1,
303-
stages: 1,
304-
_id: 1,
305-
modifiedOn: 1
306-
},
307-
{
308-
_class: 1,
309-
_id: 1,
310-
modifiedOn: 1
311-
},
312-
{
313-
_class: 1,
314-
_id: 1,
315-
objectClass: 1
308+
keys: {
309+
_class: 1,
310+
stages: 1,
311+
_id: 1,
312+
modifiedOn: 1
313+
},
314+
sparse: true
316315
}
317316
]
318317
}

Diff for: models/core/src/tx.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import { TDoc } from './core'
4646
@Model(core.class.Tx, core.class.Doc, DOMAIN_TX)
4747
export class TTx extends TDoc implements Tx {
4848
@Prop(TypeRef(core.class.Space), core.string.Space)
49-
@Index(IndexKind.Indexed)
49+
// @Index(IndexKind.Indexed)
5050
@Hidden()
5151
objectSpace!: Ref<Space>
5252
}
@@ -62,7 +62,7 @@ export class TTxCUD<T extends Doc> extends TTx implements TxCUD<T> {
6262
objectId!: Ref<T>
6363

6464
@Prop(TypeRef(core.class.Class), core.string.ClassLabel)
65-
@Index(IndexKind.Indexed)
65+
// @Index(IndexKind.Indexed)
6666
@Hidden()
6767
objectClass!: Ref<Class<T>>
6868
}

Diff for: models/notification/src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -619,9 +619,10 @@ export function createModel (builder: Builder): void {
619619
},
620620
presenter: notification.component.ReactionNotificationPresenter
621621
})
622+
622623
builder.createDoc(core.class.DomainIndexConfiguration, core.space.Model, {
623624
domain: DOMAIN_NOTIFICATION,
624-
indexes: [{ user: 1, archived: 1 }],
625+
indexes: [{ keys: { user: 1, archived: 1 } }],
625626
disabled: [{ modifiedOn: 1 }, { modifiedBy: 1 }, { createdBy: 1 }, { isViewed: 1 }, { hidden: 1 }]
626627
})
627628

Diff for: packages/core/src/classes.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//
1616

1717
import type { Asset, IntlString, Plugin } from '@hcengineering/platform'
18+
import type { DocumentQuery } from './storage'
1819

1920
/**
2021
* @public
@@ -122,6 +123,7 @@ export enum IndexKind {
122123
* Also mean to include into Elastic search.
123124
*/
124125
Indexed,
126+
125127
// Same as indexed but for descending
126128
IndexedDsc
127129
}
@@ -623,14 +625,20 @@ export type FieldIndex<T extends Doc> = {
623625
[P in keyof T]?: IndexOrder
624626
} & Record<string, IndexOrder>
625627

628+
export interface FieldIndexConfig<T extends Doc> {
629+
sparse?: boolean
630+
filter?: Omit<DocumentQuery<T>, '$search'>
631+
keys: FieldIndex<T> | string
632+
}
633+
626634
/**
627635
* @public
628636
*
629637
* Mixin for extra indexing fields.
630638
*/
631639
export interface IndexingConfiguration<T extends Doc> extends Class<Doc> {
632640
// Define a list of extra index definitions.
633-
indexes: (FieldIndex<T> | string)[]
641+
indexes: (string | FieldIndexConfig<T>)[]
634642
searchDisabled?: boolean
635643
}
636644

@@ -643,7 +651,7 @@ export interface DomainIndexConfiguration extends Doc {
643651
disabled?: (FieldIndex<Doc> | string)[]
644652

645653
// Additional indexes we could like to enabled
646-
indexes?: (FieldIndex<Doc> | string)[]
654+
indexes?: (FieldIndexConfig<Doc> | string)[]
647655

648656
skip?: string[]
649657
}

Diff for: packages/core/src/measurements/context.ts

+25-7
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ export class MeasureMetricsContext implements MeasureContext {
1111
private readonly params: ParamsType
1212
logger: MeasureLogger
1313
metrics: Metrics
14-
private readonly done: (value?: number) => void
14+
private readonly done: (value?: number, override?: boolean) => void
1515

1616
constructor (
1717
name: string,
1818
params: ParamsType,
19-
fullParams: FullParamsType = {},
19+
fullParams: FullParamsType | (() => FullParamsType) = {},
2020
metrics: Metrics = newMetrics(),
2121
logger?: MeasureLogger,
2222
readonly parent?: MeasureContext,
@@ -25,8 +25,21 @@ export class MeasureMetricsContext implements MeasureContext {
2525
this.name = name
2626
this.params = params
2727
this.metrics = metrics
28+
this.metrics.namedParams = this.metrics.namedParams ?? {}
29+
for (const [k, v] of Object.entries(params)) {
30+
if (this.metrics.namedParams[k] !== v) {
31+
this.metrics.namedParams[k] = v
32+
} else {
33+
this.metrics.namedParams[k] = '*'
34+
}
35+
}
2836
this.done = measure(metrics, params, fullParams, (spend) => {
29-
this.logger.logOperation(this.name, spend, { ...params, ...fullParams, ...(this.logParams ?? {}) })
37+
this.logger.logOperation(this.name, spend, {
38+
...params,
39+
...(typeof fullParams === 'function' ? fullParams() : fullParams),
40+
...fullParams,
41+
...(this.logParams ?? {})
42+
})
3043
})
3144

3245
const errorPrinter = ({ message, stack, ...rest }: Error): object => ({
@@ -63,12 +76,17 @@ export class MeasureMetricsContext implements MeasureContext {
6376
}
6477
}
6578

66-
measure (name: string, value: number): void {
79+
measure (name: string, value: number, override?: boolean): void {
6780
const c = new MeasureMetricsContext('#' + name, {}, {}, childMetrics(this.metrics, ['#' + name]), this.logger, this)
68-
c.done(value)
81+
c.done(value, override)
6982
}
7083

71-
newChild (name: string, params: ParamsType, fullParams?: FullParamsType, logger?: MeasureLogger): MeasureContext {
84+
newChild (
85+
name: string,
86+
params: ParamsType,
87+
fullParams?: FullParamsType | (() => FullParamsType),
88+
logger?: MeasureLogger
89+
): MeasureContext {
7290
return new MeasureMetricsContext(
7391
name,
7492
params,
@@ -84,7 +102,7 @@ export class MeasureMetricsContext implements MeasureContext {
84102
name: string,
85103
params: ParamsType,
86104
op: (ctx: MeasureContext) => T | Promise<T>,
87-
fullParams?: ParamsType
105+
fullParams?: ParamsType | (() => FullParamsType)
88106
): Promise<T> {
89107
const c = this.newChild(name, params, fullParams, this.logger)
90108
try {

0 commit comments

Comments
 (0)