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

feat: improve export #316

Merged
merged 8 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
90 changes: 63 additions & 27 deletions lib/modules/asset/AssetsController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { ControllerDefinition, HttpStream, KuzzleRequest } from "kuzzle";
import {
ControllerDefinition,
HttpStream,
KuzzleError,
KuzzleRequest,
} from "kuzzle";

import { MeasureExporter } from "../measure/";
import { DeviceManagerPlugin, InternalCollection } from "../plugin";
Expand Down Expand Up @@ -200,12 +205,14 @@ export class AssetsController {
const query = request.input.body?.query;
const sort = request.input.body?.sort;
const type = request.input.args.type;
const lang = request.getLangParam();

const { measures, total } = await this.measureExporter.search(
engineId,
{
endAt,
id,
lang,
query,
sort,
startAt,
Expand All @@ -227,20 +234,34 @@ export class AssetsController {
request.context.connection.protocol === "http" &&
request.context.connection.misc.verb === "GET"
) {
const exportId = request.getString("exportId");

const { id } = await this.measureExporter.getExport(engineId, exportId);

request.response.configure({
headers: {
"Content-Disposition": `attachment; filename="asset-${id}.csv"`,
"Content-Type": "text/csv",
},
});

const stream = await this.measureExporter.sendExport(engineId, exportId);

return new HttpStream(stream);
try {
const exportId = request.getString("exportId");

const { id } = await this.measureExporter.getExport(engineId, exportId);
const stream = await this.measureExporter.sendExport(
engineId,
exportId
);

request.response.configure({
headers: {
"Content-Disposition": `attachment; filename="asset-${id}.csv"`,
"Content-Type": "text/csv",
},
});

return new HttpStream(stream);
} catch (error) {
request.response.configure({
format: "raw",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why adding this instead of throwing a traditional error ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this endpoint is mostly called by browser and traditional error return a JSON that can be hard to understand for user.
Maybe the better solution could be to improve global error handler in the core

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment to explain this in the code :)

headers: {
"Content-Type": "text/plain",
},
status: (error as KuzzleError).status,
});

return error.message;
}
}

const id = request.getId();
Expand All @@ -251,13 +272,15 @@ export class AssetsController {
const query = request.input.body?.query;
const sort = request.input.body?.sort;
const type = request.input.args.type;
const lang = request.getLangParam();

const link = await this.measureExporter.prepareExport(
engineId,
request.getUser(),
{
endAt,
id,
lang,
query,
sort,
startAt,
Expand All @@ -275,27 +298,40 @@ export class AssetsController {
request.context.connection.protocol === "http" &&
request.context.connection.misc.verb === "GET"
) {
const exportId = request.getString("exportId");

request.response.configure({
headers: {
"Content-Disposition": `attachment; filename="${InternalCollection.ASSETS}.csv"`,
"Content-Type": "text/csv",
},
});

const stream = await this.exporter.sendExport(engineId, exportId);

return new HttpStream(stream);
try {
const exportId = request.getString("exportId");
const stream = await this.exporter.sendExport(engineId, exportId);

request.response.configure({
headers: {
"Content-Disposition": `attachment; filename="${InternalCollection.ASSETS}.csv"`,
"Content-Type": "text/csv",
},
});

return new HttpStream(stream);
} catch (error) {
request.response.configure({
format: "raw",
headers: {
"Content-Type": "text/plain",
},
status: (error as KuzzleError).status,
});

return error.message;
}
}

const query = request.input.body?.query;
const sort = request.input.body?.sort;
const lang = request.getLangParam();

const link = await this.exporter.prepareExport(
engineId,
request.getUser(),
{
lang,
query,
sort,
}
Expand Down
6 changes: 6 additions & 0 deletions lib/modules/asset/types/AssetApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ export interface ApiAssetGetMeasuresRequest extends AssetsControllerRequest {

type?: string;

lang?: "koncorde" | "elasticsearch";

body?: {
query?: JSONObject;
sort?: JSONObject;
Expand Down Expand Up @@ -114,6 +116,8 @@ export interface ApiAssetExportMeasuresRequest extends AssetsControllerRequest {

type?: string;

lang?: "koncorde" | "elasticsearch";

body?: {
query?: JSONObject;
sort?: JSONObject;
Expand All @@ -126,6 +130,8 @@ export type ApiAssetExportMeasuresResult = {
export interface ApiAssetExportRequest extends AssetsControllerRequest {
action: "export";

lang?: "koncorde" | "elasticsearch";

body?: {
query?: JSONObject;
sort?: JSONObject;
Expand Down
80 changes: 56 additions & 24 deletions lib/modules/device/DevicesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
BadRequestError,
ControllerDefinition,
HttpStream,
KuzzleError,
KuzzleRequest,
} from "kuzzle";
import _ from "lodash";
Expand Down Expand Up @@ -355,12 +356,14 @@ export class DevicesController {
const query = request.input.body?.query;
const sort = request.input.body?.sort;
const type = request.input.args.type;
const lang = request.getLangParam();

const { measures, total } = await this.measureExporter.search(
engineId,
{
endAt,
id,
lang,
query,
sort,
startAt,
Expand All @@ -379,20 +382,34 @@ export class DevicesController {
request.context.connection.protocol === "http" &&
request.context.connection.misc.verb === "GET"
) {
const exportId = request.getString("exportId");

const { id } = await this.measureExporter.getExport(engineId, exportId);

request.response.configure({
headers: {
"Content-Disposition": `attachment; filename="device-${id}.csv"`,
"Content-Type": "text/csv",
},
});
try {
const exportId = request.getString("exportId");

const stream = await this.measureExporter.sendExport(engineId, exportId);
const { id } = await this.measureExporter.getExport(engineId, exportId);
const stream = await this.measureExporter.sendExport(
engineId,
exportId
);

return new HttpStream(stream);
request.response.configure({
headers: {
"Content-Disposition": `attachment; filename="device-${id}.csv"`,
"Content-Type": "text/csv",
},
});

return new HttpStream(stream);
} catch (error) {
request.response.configure({
format: "raw",
headers: {
"Content-Type": "text/plain",
},
status: (error as KuzzleError).status,
});

return error.message;
}
}

const id = request.getId();
Expand All @@ -403,13 +420,15 @@ export class DevicesController {
const query = request.input.body?.query;
const sort = request.input.body?.sort;
const type = request.input.args.type;
const lang = request.getLangParam();

const link = await this.measureExporter.prepareExport(
engineId,
request.getUser(),
{
endAt,
id,
lang,
query,
sort,
startAt,
Expand Down Expand Up @@ -471,27 +490,40 @@ export class DevicesController {
request.context.connection.protocol === "http" &&
request.context.connection.misc.verb === "GET"
) {
const exportId = request.getString("exportId");

request.response.configure({
headers: {
"Content-Disposition": `attachment; filename="${InternalCollection.DEVICES}.csv"`,
"Content-Type": "text/csv",
},
});

const stream = await this.exporter.sendExport(engineId, exportId);

return new HttpStream(stream);
try {
const exportId = request.getString("exportId");
const stream = await this.exporter.sendExport(engineId, exportId);

request.response.configure({
headers: {
"Content-Disposition": `attachment; filename="${InternalCollection.DEVICES}.csv"`,
"Content-Type": "text/csv",
},
});

return new HttpStream(stream);
} catch (error) {
request.response.configure({
format: "raw",
headers: {
"Content-Type": "text/plain",
},
status: (error as KuzzleError).status,
});

return error.message;
}
}

const query = request.input.body?.query;
const sort = request.input.body?.sort;
const lang = request.getLangParam();

const link = await this.exporter.prepareExport(
engineId,
request.getUser(),
{
lang,
query,
sort,
}
Expand Down
6 changes: 6 additions & 0 deletions lib/modules/device/types/DeviceApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ export interface ApiDeviceGetMeasuresRequest extends DevicesControllerRequest {

type?: string;

lang?: "koncorde" | "elasticsearch";

body?: {
query?: JSONObject;
sort?: JSONObject;
Expand Down Expand Up @@ -195,6 +197,8 @@ export interface ApiDeviceExportMeasuresRequest

type?: string;

lang?: "koncorde" | "elasticsearch";

body?: {
query?: JSONObject;
sort?: JSONObject;
Expand Down Expand Up @@ -226,6 +230,8 @@ export type ApiDeviceReceiveMeasuresResult = void;
export interface ApiDeviceExportRequest extends DevicesControllerRequest {
action: "export";

lang?: "koncorde" | "elasticsearch";

body?: {
query?: JSONObject;
sort?: JSONObject;
Expand Down
Loading
Loading