Skip to content

Commit a5cd2e1

Browse files
fix(measureexporter): use scroll instead of from/size + remove sort by _id
1 parent 8ec2f3e commit a5cd2e1

File tree

3 files changed

+12
-25
lines changed

3 files changed

+12
-25
lines changed

lib/modules/asset/AssetsGroupsController.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,17 @@ export class AssetsGroupsController {
176176
async checkGroupName(
177177
engineId: string,
178178
name: AssetsGroupsBodyRequest["name"],
179-
assetId?: string,
179+
groupId?: string,
180180
) {
181181
if (typeof name !== "string") {
182182
return;
183183
}
184184

185-
const groupsCount = await this.sdk.document.count(
185+
const groupsCount = await this.sdk.document.search(
186186
engineId,
187187
InternalCollection.ASSETS_GROUPS,
188188
{
189+
_source: false,
189190
query: {
190191
bool: {
191192
must: [
@@ -198,19 +199,15 @@ export class AssetsGroupsController {
198199
},
199200
},
200201
],
201-
must_not: [
202-
{
203-
terms: {
204-
_id: typeof assetId === "string" ? [assetId] : [],
205-
},
206-
},
207-
],
208202
},
209203
},
210204
},
211205
);
212206

213-
if (groupsCount > 0) {
207+
if (groupsCount.total > 0) {
208+
if (groupsCount.total === 1 && groupsCount.hits[0]._id === groupId) {
209+
return;
210+
}
214211
throw new BadRequestError(`A group with name "${name}" already exist`);
215212
}
216213
}
@@ -279,7 +276,9 @@ export class AssetsGroupsController {
279276
if (model !== null) {
280277
const groupModel = await ask<AskModelGroupGet>(
281278
"ask:device-manager:model:group:get",
282-
{ model },
279+
{
280+
model,
281+
},
283282
);
284283
for (const metadataName of Object.keys(
285284
groupModel.group.metadataMappings,

lib/modules/measure/MeasureExporter.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,6 @@ export class MeasureExporter extends AbstractExporter<MeasureExportParams> {
107107
query: searchQuery,
108108
sort: params.sort ?? { measuredAt: "desc" },
109109
};
110-
111-
/**
112-
* ? Prevent error duplicate result on search with sort.
113-
* "Unable to retrieve all results from search: the sort combination must identify one item only. Add document "_id" to the sort."
114-
*/
115-
if (exportParams.sort._id === undefined) {
116-
exportParams.sort._id = "asc";
117-
}
118-
119110
return super.prepareExport(engineId, user, exportParams);
120111
}
121112

@@ -154,14 +145,12 @@ export class MeasureExporter extends AbstractExporter<MeasureExportParams> {
154145
model,
155146
lang = "elasticsearch",
156147
} = await this.getExport(engineId, exportId);
157-
158148
const result = await this.sdk.document.search<MeasureContent>(
159149
engineId,
160150
InternalCollection.MEASURES,
161151
{ query, sort },
162-
{ lang, size: 200 },
152+
{ lang, scroll: "20s", size: 200 },
163153
);
164-
165154
const targetModel =
166155
this.target === InternalCollection.ASSETS ? "asset" : "device";
167156
const engine = await this.getEngine(engineId);

lib/modules/shared/services/AbstractExporter.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,7 @@ export abstract class AbstractExporter<P extends ExportParams = ExportParams> {
152152
for (const hit of result.hits) {
153153
stream.write(stringify([this.formatHit(columns, hit)]));
154154
}
155-
156-
result = result.fetched < 10000 ? await result.next() : null;
155+
result = await result.next();
157156
}
158157
} catch (error) {
159158
stream.write(error.message);

0 commit comments

Comments
 (0)