Skip to content

Commit

Permalink
[ML] Use useMemo() for DataLoader.
Browse files Browse the repository at this point in the history
  • Loading branch information
walterra committed Jul 14, 2020
1 parent b794e97 commit 3883477
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { useEffect } from 'react';
import { useEffect, useMemo } from 'react';

import { EuiDataGridColumn } from '@elastic/eui';

Expand Down Expand Up @@ -106,9 +106,12 @@ export const useIndexData = (
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [indexPattern.title, JSON.stringify([query, pagination, sortingColumns])]);

const dataLoader = useMemo(() => new DataLoader(indexPattern, toastNotifications), [
indexPattern,
]);

const fetchColumnChartsData = async function () {
try {
const dataLoader = new DataLoader(indexPattern, toastNotifications);
const columnChartsData = await dataLoader.loadFieldHistograms(
columns
.filter((cT) => dataGrid.visibleColumns.includes(cT.id))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { useEffect } from 'react';
import { useEffect, useMemo } from 'react';

import { EuiDataGridColumn } from '@elastic/eui';

Expand Down Expand Up @@ -73,10 +73,15 @@ export const useExplorationResults = (
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [jobConfig && jobConfig.id, dataGrid.pagination, searchQuery, dataGrid.sortingColumns]);

const dataLoader = useMemo(
() =>
indexPattern !== undefined ? new DataLoader(indexPattern, toastNotifications) : undefined,
[indexPattern]
);

const fetchColumnChartsData = async function () {
try {
if (jobConfig !== undefined && indexPattern !== undefined) {
const dataLoader = new DataLoader(indexPattern, toastNotifications);
if (jobConfig !== undefined && dataLoader !== undefined) {
const columnChartsData = await dataLoader.loadFieldHistograms(
columns
.filter((cT) => dataGrid.visibleColumns.includes(cT.id))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { useEffect } from 'react';
import { useEffect, useMemo } from 'react';

import { EuiDataGridColumn } from '@elastic/eui';

Expand Down Expand Up @@ -80,10 +80,17 @@ export const useOutlierData = (
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [jobConfig && jobConfig.id, dataGrid.pagination, searchQuery, dataGrid.sortingColumns]);

const dataLoader = useMemo(
() =>
indexPattern !== undefined
? new DataLoader(indexPattern, getToastNotifications())
: undefined,
[indexPattern]
);

const fetchColumnChartsData = async function () {
try {
if (jobConfig !== undefined && indexPattern !== undefined) {
const dataLoader = new DataLoader(indexPattern, getToastNotifications());
if (jobConfig !== undefined && dataLoader !== undefined) {
const columnChartsData = await dataLoader.loadFieldHistograms(
columns
.filter((cT) => dataGrid.visibleColumns.includes(cT.id))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React, { FC, Fragment, useEffect, useState } from 'react';
import React, { FC, Fragment, useEffect, useMemo, useState } from 'react';
import { merge } from 'rxjs';
import { i18n } from '@kbn/i18n';

Expand Down Expand Up @@ -108,7 +108,10 @@ export const Page: FC = () => {
autoRefreshSelector: true,
});

const dataLoader = new DataLoader(currentIndexPattern, getToastNotifications());
const dataLoader = useMemo(() => new DataLoader(currentIndexPattern, getToastNotifications()), [
currentIndexPattern,
]);

const [globalState, setGlobalState] = useUrlState('_g');
useEffect(() => {
if (globalState?.time !== undefined) {
Expand Down

0 comments on commit 3883477

Please sign in to comment.