Skip to content
This repository has been archived by the owner on Aug 9, 2022. It is now read-only.

CSV APIs endpoints for data reports. #50

Merged
merged 7 commits into from
Aug 21, 2020

Conversation

fabioued
Copy link
Contributor

Issue #49

Added Data report route to generate and save the metadata of a report.
Added another route to tech the data from previously saved metadata with a limit of 150k rows.

@fabioued fabioued changed the title APIs endpoints for data reports. #49 APIs endpoints for data reports. Jul 27, 2020
@zhongnansu zhongnansu self-requested a review July 28, 2020 02:18
Comment on lines +19 to +22
saved_search_id: schema.string(),
start: schema.string(),
end: schema.string(),
report_format: schema.oneOf([schema.literal('csv'), schema.literal('xlsx')]),
Copy link
Member

Choose a reason for hiding this comment

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

Please keep the same 2-space indentation

Copy link
Member

Choose a reason for hiding this comment

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

We use prettier to format the code base. There is also a .prettoerrc config file in the root path of this project.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

got it

Comment on lines 17 to 18
pdf = 'pdf',
png = 'png',
Copy link
Member

Choose a reason for hiding this comment

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

Please keep the same 2-space indentation

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

let resp: any = {};
const ssExist = await axios({
method: 'GET',
proxy: { host: '127.0.0.1', port: 5601 },
Copy link
Member

Choose a reason for hiding this comment

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

Think about removing this "check if exists" logic. And handle the non-existing saved search in the failure response of the actual es query via client.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it. I handle it now in the parseEsErrorResponse function

const report = await context.core.elasticsearch.adminClient.callAsInternalUser(
'index',
{
index: 'report',
Copy link
Member

Choose a reason for hiding this comment

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

Use another index name. Because the reporting landing page needs to query all documents from "report" index to render the reports table view.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok I changed to datareport. You can also change it later on

Comment on lines +52 to +53
// is_count is set to 1 if we building the count query but 0 if we building the fetch data query
export const buildQuery = (report, is_count) => {
Copy link
Member

Choose a reason for hiding this comment

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

a simple boolean value will make is more readable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes it's a boolean

Copy link
Member

Choose a reason for hiding this comment

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

I mean the variable "is_count". You are using 0/1 instead of true/false. It's up to you, but I think for a statement "is_count = 1", the readability is not very good.

Comment on lines 158 to 163
for (let valueRes of arrayHits) {
for (let data of valueRes.hits) {
const fields = data.fields;
//get all the fields of type date and fromat them to excel format
for (let dateType of report._source.dateFields) {
if (data._source[dateType]) {
Copy link
Member

Choose a reason for hiding this comment

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

Is there a way to optimize this 3-layer for loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am using 3-layer for loop because of the the Elasticsearch data response structure.

Comment on lines +19 to +21
const esb = require('elastic-builder');
const moment = require('moment');
const converter = require('json-2-csv');
Copy link
Member

Choose a reason for hiding this comment

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

Add dependencies to package.json, use import .. as .. from.. syntax that typescript can recognize

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@zhongnansu
Copy link
Member

zhongnansu commented Aug 11, 2020

Can you briefly describe why the client side needs two api calls to get an on-demand csv report? Is it possible to do this through only one api call, by wrapping the save metadata part in the generate data report API?

@@ -0,0 +1,353 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above- 2 space indentation

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The two APIs calls are not only for on-demand reports but also for the scheduled reports. Once the user decides to schedule a report, only the metadata will be created. Then when he needs to download, the CSV will be generated from the metadata then return it to the client without saving it.

Copy link
Member

@zhongnansu zhongnansu left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution

rows = nbRows;
nbScroll = Math.floor(nbRows / default_max_size);
}
// let data = {
Copy link
Contributor

Choose a reason for hiding this comment

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

please remove comments

registerReportRoute(router);
registerReportDefinitionRoute(router);
registerDataReportMetadata(router);
registerDataReport(router);
Copy link
Contributor

Choose a reason for hiding this comment

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

2 space indentation

@davidcui1225 davidcui1225 merged commit 9c9df14 into opendistro-for-elasticsearch:dev Aug 21, 2020
@anirudha anirudha changed the title APIs endpoints for data reports. CSV APIs endpoints for data reports. Jun 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants