diff --git a/packages/presentation/src/pipeline.ts b/packages/presentation/src/pipeline.ts index 2530f926d7..02b02ef752 100644 --- a/packages/presentation/src/pipeline.ts +++ b/packages/presentation/src/pipeline.ts @@ -304,6 +304,29 @@ export class OptimizeQueryMiddleware extends BasePresentationMiddleware implemen console.error('_class must be specified in query', query) return toFindResult([], 0) } - return await this.provideFindAll(_class, query, options) + const fQuery = { ...query } + const fOptions = { ...options } + this.optimizeQuery(fQuery, fOptions) + return await this.provideFindAll(_class, fQuery, fOptions) + } + + private optimizeQuery(fQuery: DocumentQuery, fOptions: FindOptions): void { + if (typeof fQuery._id === 'string' && fOptions.sort !== undefined) { + delete fOptions.sort + } + if (fOptions.lookup !== undefined && Object.keys(fOptions.lookup).length === 0) { + delete fOptions.lookup + } + } + + async findOne( + _class: Ref>, + query: DocumentQuery, + options?: FindOptions | undefined + ): Promise | undefined> { + const fQuery = { ...query } + const fOptions = { ...options } + this.optimizeQuery(fQuery, fOptions) + return await this.provideFindOne(_class, fQuery, fOptions) } } diff --git a/packages/presentation/src/utils.ts b/packages/presentation/src/utils.ts index ae0f1bedf7..8279558734 100644 --- a/packages/presentation/src/utils.ts +++ b/packages/presentation/src/utils.ts @@ -137,7 +137,6 @@ class UIClient extends TxOperations implements Client, MeasureClient { this.measureOp = undefined if (this.afterMeasure.length > 0) { const txes = this.afterMeasure - console.log('after measture', txes) this.afterMeasure = [] for (const tx of txes) { await this.doNotify(tx) diff --git a/packages/query/src/index.ts b/packages/query/src/index.ts index 33a0d17a8e..f702c238b7 100644 --- a/packages/query/src/index.ts +++ b/packages/query/src/index.ts @@ -34,6 +34,7 @@ import core, { SearchQuery, SearchResult, SortingQuery, + Space, Tx, TxCollectionCUD, TxCreateDoc, @@ -200,6 +201,11 @@ export class LiveQuery extends TxProcessor implements Client { modifiedOn: 1 } } + if (options === undefined) { + options = {} + } + options.limit = 1 + const q = this.findQuery(_class, query, options) ?? this.createDumpQuery(_class, query, options) if (q.result instanceof Promise) { q.result = await q.result @@ -393,8 +399,8 @@ export class LiveQuery extends TxProcessor implements Client { return false } - private async getCurrentDoc (q: Query, _id: Ref): Promise { - const current = await this.client.findOne(q._class, { _id }, q.options) + private async getCurrentDoc (q: Query, _id: Ref, space: Ref): Promise { + const current = await this.client.findOne(q._class, { _id, space }, q.options) if (q.result instanceof Promise) { q.result = await q.result } @@ -480,7 +486,7 @@ export class LiveQuery extends TxProcessor implements Client { continue } } else { - const currentRefresh = await this.getCurrentDoc(q, updatedDoc._id) + const currentRefresh = await this.getCurrentDoc(q, updatedDoc._id, updatedDoc.space) if (currentRefresh) { continue } @@ -568,7 +574,7 @@ export class LiveQuery extends TxProcessor implements Client { const updateRefresh = await this.checkUpdatedDocMatch(q, updatedDoc) if (updateRefresh) return } else { - const currentRefresh = await this.getCurrentDoc(q, updatedDoc._id) + const currentRefresh = await this.getCurrentDoc(q, updatedDoc._id, updatedDoc.space) if (currentRefresh) return } } diff --git a/plugins/devmodel-resources/src/index.ts b/plugins/devmodel-resources/src/index.ts index 4f1aa58467..4567252914 100644 --- a/plugins/devmodel-resources/src/index.ts +++ b/plugins/devmodel-resources/src/index.ts @@ -54,8 +54,6 @@ export interface QueryWithResult { findOne: boolean } -export const transactions: TxWitHResult[] = [] - class ModelClient implements AccountClient { notifyEnabled = true constructor (readonly client: AccountClient) { @@ -92,6 +90,7 @@ class ModelClient implements AccountClient { query: DocumentQuery, options?: FindOptions ): Promise | undefined> { + const startTime = Date.now() const result = await this.client.findOne(_class, query, options) if (this.notifyEnabled) { console.debug( @@ -103,7 +102,8 @@ class ModelClient implements AccountClient { result, ' =>model', this.client.getModel(), - getMetadata(devmodel.metadata.DevModel) + getMetadata(devmodel.metadata.DevModel), + Date.now() - startTime ) } return result @@ -114,6 +114,7 @@ class ModelClient implements AccountClient { query: DocumentQuery, options?: FindOptions ): Promise> { + const startTime = Date.now() const result = await this.client.findAll(_class, query, options) if (this.notifyEnabled) { console.debug( @@ -125,7 +126,8 @@ class ModelClient implements AccountClient { result, ' =>model', this.client.getModel(), - getMetadata(devmodel.metadata.DevModel) + getMetadata(devmodel.metadata.DevModel), + Date.now() - startTime ) } return result @@ -140,13 +142,10 @@ class ModelClient implements AccountClient { } async tx (tx: Tx): Promise { + const startTime = Date.now() const result = await this.client.tx(tx) if (this.notifyEnabled) { - console.debug('devmodel# tx=>', tx, result, getMetadata(devmodel.metadata.DevModel)) - } - transactions.push({ tx, result }) - if (transactions.length > 100) { - transactions.shift() + console.debug('devmodel# tx=>', tx, result, getMetadata(devmodel.metadata.DevModel), Date.now() - startTime) } return result } diff --git a/plugins/tracker-resources/src/components/CreateIssue.svelte b/plugins/tracker-resources/src/components/CreateIssue.svelte index 94485ed3e6..3cd8b1ef2b 100644 --- a/plugins/tracker-resources/src/components/CreateIssue.svelte +++ b/plugins/tracker-resources/src/components/CreateIssue.svelte @@ -359,7 +359,11 @@ try { const operations = client.apply(_id) - const lastOne = await client.findOne(tracker.class.Issue, {}, { sort: { rank: SortingOrder.Descending } }) + const lastOne = await client.findOne( + tracker.class.Issue, + { space: _space }, + { sort: { rank: SortingOrder.Descending } } + ) const incResult = await client.updateDoc( tracker.class.Project, core.space.Space, @@ -449,7 +453,10 @@ draftController.remove() descriptionBox?.removeDraft(false) isAssigneeTouched = false - console.log('createIssue measure', doneOp()) + const d1 = Date.now() + void doneOp().then((res) => { + console.log('createIssue measure', res, Date.now() - d1) + }) } catch (err: any) { console.error(err) await doneOp() // Complete in case of error