Skip to content

Commit

Permalink
Move log rate result hooks into the pages dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
weltenwort committed Nov 19, 2019
1 parent a2d319e commit 3344f32
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export type GetLogEntryRateRequestPayload = rt.TypeOf<typeof getLogEntryRateRequ
* response
*/

export const logEntryRateAnomaly = rt.type({
export const logEntryRateAnomalyRT = rt.type({
actualLogEntryRate: rt.number,
anomalyScore: rt.number,
duration: rt.number,
Expand All @@ -39,22 +39,26 @@ export const logEntryRateAnomaly = rt.type({

export const logEntryRatePartitionRT = rt.type({
analysisBucketCount: rt.number,
anomalies: rt.array(logEntryRateAnomaly),
anomalies: rt.array(logEntryRateAnomalyRT),
averageActualLogEntryRate: rt.number,
maximumAnomalyScore: rt.number,
numberOfLogEntries: rt.number,
partitionId: rt.string,
});

export const logEntryRateHistogramBucket = rt.type({
export type LogEntryRatePartition = rt.TypeOf<typeof logEntryRatePartitionRT>;

export const logEntryRateHistogramBucketRT = rt.type({
partitions: rt.array(logEntryRatePartitionRT),
startTime: rt.number,
});

export type LogEntryRateHistogramBucket = rt.TypeOf<typeof logEntryRateHistogramBucketRT>;

export const getLogEntryRateSuccessReponsePayloadRT = rt.type({
data: rt.type({
bucketDuration: rt.number,
histogramBuckets: rt.array(logEntryRateHistogramBucket),
histogramBuckets: rt.array(logEntryRateHistogramBucketRT),
totalNumberOfLogEntries: rt.number,
}),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@
export * from './log_analysis_capabilities';
export * from './log_analysis_cleanup';
export * from './log_analysis_jobs';
export * from './log_analysis_results';
export * from './log_analysis_results_url_state';
export * from './log_analysis_status_state';

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@ import {
import numeral from '@elastic/numeral';
import { FormattedMessage } from '@kbn/i18n/react';
import moment from 'moment';
import React, { useCallback, useMemo, useState } from 'react';
import React, { useCallback, useMemo, useState, useEffect } from 'react';

import euiStyled from '../../../../../../common/eui_styled_components';
import { TimeRange } from '../../../../common/http_api/shared/time_range';
import { bucketSpan } from '../../../../common/log_analysis';
import { LoadingOverlayWrapper } from '../../../components/loading_overlay_wrapper';
import {
StringTimeRange,
useLogAnalysisResults,
useLogAnalysisResultsUrlState,
} from '../../../containers/logs/log_analysis';
import { useInterval } from '../../../hooks/use_interval';
import { useTrackPageview } from '../../../hooks/use_track_metric';
import { useKibanaUiSetting } from '../../../utils/use_kibana_ui_setting';
import { FirstUseCallout } from './first_use';
import { AnomaliesResults } from './sections/anomalies';
import { LogRateResults } from './sections/log_rate';
import { useLogEntryRateJobsContext } from './use_log_entry_rate_jobs';
import { useLogEntryRateResults } from './use_log_entry_rate_results';
import {
StringTimeRange,
useLogAnalysisResultsUrlState,
} from './use_log_entry_rate_results_url_state';

const JOB_STATUS_POLLING_INTERVAL = 30000;

Expand Down Expand Up @@ -65,32 +65,20 @@ export const LogEntryRateResultsContent = ({
lastChangedTime: Date.now(),
}));

const bucketDuration = useMemo(() => {
// This function takes the current time range in ms,
// works out the bucket interval we'd need to always
// display 100 data points, and then takes that new
// value and works out the nearest multiple of
// 900000 (15 minutes) to it, so that we don't end up with
// jaggy bucket boundaries between the ML buckets and our
// aggregation buckets.
const msRange = moment(queryTimeRange.value.endTime).diff(
moment(queryTimeRange.value.startTime)
);
const bucketIntervalInMs = msRange / 100;
const result = bucketSpan * Math.round(bucketIntervalInMs / bucketSpan);
const roundedResult = parseInt(Number(result).toFixed(0), 10);
return roundedResult < bucketSpan ? bucketSpan : roundedResult;
}, [queryTimeRange.value.startTime, queryTimeRange.value.endTime]);
const bucketDuration = useMemo(
() => getBucketDuration(queryTimeRange.value.startTime, queryTimeRange.value.endTime),
[queryTimeRange.value.endTime, queryTimeRange.value.startTime]
);

const { isLoading, logRateResults } = useLogAnalysisResults({
const { getLogEntryRate, isLoading, logEntryRate } = useLogEntryRateResults({
sourceId,
startTime: queryTimeRange.value.startTime,
endTime: queryTimeRange.value.endTime,
bucketDuration,
lastRequestTime: queryTimeRange.lastChangedTime,
});
const hasResults = useMemo(() => logRateResults && logRateResults.histogramBuckets.length > 0, [
logRateResults,

const hasResults = useMemo(() => (logEntryRate?.histogramBuckets?.length ?? 0) > 0, [
logEntryRate,
]);

const handleQueryTimeRangeChange = useCallback(
Expand Down Expand Up @@ -147,6 +135,10 @@ export const LogEntryRateResultsContent = ({
jobIds,
} = useLogEntryRateJobsContext();

useEffect(() => {
getLogEntryRate();
}, [getLogEntryRate, queryTimeRange.lastChangedTime]);

useInterval(() => {
fetchJobStatus();
}, JOB_STATUS_POLLING_INTERVAL);
Expand All @@ -168,7 +160,7 @@ export const LogEntryRateResultsContent = ({
<EuiPanel paddingSize="l">
<EuiFlexGroup justifyContent="spaceBetween" alignItems="center">
<EuiFlexItem grow={false}>
{logRateResults ? (
{logEntryRate ? (
<LoadingOverlayWrapper isLoading={isLoading}>
<EuiText size="s">
<FormattedMessage
Expand All @@ -178,7 +170,7 @@ export const LogEntryRateResultsContent = ({
numberOfLogs: (
<EuiBadge color="primary">
<EuiText size="s" color="ghost">
{numeral(logRateResults.totalNumberOfLogEntries).format('0.00a')}
{numeral(logEntryRate.totalNumberOfLogEntries).format('0.00a')}
</EuiText>
</EuiBadge>
),
Expand Down Expand Up @@ -210,7 +202,7 @@ export const LogEntryRateResultsContent = ({
{isFirstUse && !hasResults ? <FirstUseCallout /> : null}
<LogRateResults
isLoading={isLoading}
results={logRateResults}
results={logEntryRate}
setTimeRange={handleChartTimeRangeChange}
timeRange={queryTimeRange.value}
/>
Expand All @@ -223,7 +215,7 @@ export const LogEntryRateResultsContent = ({
jobStatus={jobStatus['log-entry-rate']}
viewSetupForReconfiguration={viewSetupForReconfiguration}
viewSetupForUpdate={viewSetupForUpdate}
results={logRateResults}
results={logEntryRate}
setTimeRange={handleChartTimeRangeChange}
setupStatus={setupStatus}
timeRange={queryTimeRange.value}
Expand All @@ -250,6 +242,23 @@ const stringToNumericTimeRange = (timeRange: StringTimeRange): TimeRange => ({
).valueOf(),
});

/**
* This function takes the current time range in ms,
* works out the bucket interval we'd need to always
* display 100 data points, and then takes that new
* value and works out the nearest multiple of
* 900000 (15 minutes) to it, so that we don't end up with
* jaggy bucket boundaries between the ML buckets and our
* aggregation buckets.
*/
const getBucketDuration = (startTime: number, endTime: number) => {
const msRange = moment(endTime).diff(moment(startTime));
const bucketIntervalInMs = msRange / 100;
const result = bucketSpan * Math.round(bucketIntervalInMs / bucketSpan);
const roundedResult = parseInt(Number(result).toFixed(0), 10);
return roundedResult < bucketSpan ? bucketSpan : roundedResult;
};

// This is needed due to the flex-basis: 100% !important; rule that
// kicks in on small screens via media queries breaking when using direction="column"
export const ResultsContentPage = euiStyled(EuiPage)`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { i18n } from '@kbn/i18n';
import numeral from '@elastic/numeral';
import { EuiFlexGroup, EuiFlexItem, EuiStat, EuiSpacer } from '@elastic/eui';
import { AnomaliesChart } from './chart';
import { LogRateResults } from '../../../../../containers/logs/log_analysis/log_analysis_results';
import { LogEntryRateResults } from '../../use_log_entry_rate_results';
import { TimeRange } from '../../../../../../common/http_api/shared/time_range';
import {
getLogEntryRateSeriesForPartition,
Expand All @@ -21,7 +21,7 @@ import { AnalyzeInMlButton } from '../analyze_in_ml_button';
export const AnomaliesTableExpandedRow: React.FunctionComponent<{
partitionId: string;
topAnomalyScore: number;
results: LogRateResults;
results: LogEntryRateResults;
setTimeRange: (timeRange: TimeRange) => void;
timeRange: TimeRange;
jobId: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { i18n } from '@kbn/i18n';
import React, { useMemo } from 'react';

import euiStyled from '../../../../../../../../common/eui_styled_components';
import { LogRateResults } from '../../../../../containers/logs/log_analysis/log_analysis_results';
import { LogEntryRateResults } from '../../use_log_entry_rate_results';
import { TimeRange } from '../../../../../../common/http_api/shared/time_range';
import { JobStatus, SetupStatus } from '../../../../../../common/log_analysis';
import {
Expand All @@ -36,7 +36,7 @@ import { LoadingOverlayWrapper } from '../../../../../components/loading_overlay
export const AnomaliesResults: React.FunctionComponent<{
isLoading: boolean;
jobStatus: JobStatus;
results: LogRateResults | null;
results: LogEntryRateResults | null;
setTimeRange: (timeRange: TimeRange) => void;
setupStatus: SetupStatus;
timeRange: TimeRange;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { EuiBasicTable, EuiButtonIcon } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { RIGHT_ALIGNMENT } from '@elastic/eui/lib/services';
import { TimeRange } from '../../../../../../common/http_api/shared/time_range';
import { LogRateResults } from '../../../../../containers/logs/log_analysis/log_analysis_results';
import { LogEntryRateResults } from '../../use_log_entry_rate_results';
import { AnomaliesTableExpandedRow } from './expanded_row';
import { formatAnomalyScore, getFriendlyNameForPartitionId } from '../helpers/data_formatters';
import euiStyled from '../../../../../../../../common/eui_styled_components';
Expand Down Expand Up @@ -50,7 +50,7 @@ const maxAnomalyScoreColumnName = i18n.translate(
);

export const AnomaliesTable: React.FunctionComponent<{
results: LogRateResults;
results: LogEntryRateResults;
setTimeRange: (timeRange: TimeRange) => void;
timeRange: TimeRange;
jobId: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@

import { RectAnnotationDatum } from '@elastic/charts';
import { i18n } from '@kbn/i18n';
import { LogRateResults } from '../../../../../containers/logs/log_analysis/log_analysis_results';

export type MLSeverityScoreCategories = 'warning' | 'minor' | 'major' | 'critical';
type MLSeverityScores = Record<MLSeverityScoreCategories, number>;
const ML_SEVERITY_SCORES: MLSeverityScores = {
import { LogEntryRateResults } from '../../use_log_entry_rate_results';

const ML_SEVERITY_SCORES = {
warning: 3,
minor: 25,
major: 50,
critical: 75,
};

export const getLogEntryRatePartitionedSeries = (results: LogRateResults) => {
export type MLSeverityScoreCategories = keyof typeof ML_SEVERITY_SCORES;

export const getLogEntryRatePartitionedSeries = (results: LogEntryRateResults) => {
return results.histogramBuckets.reduce<Array<{ group: string; time: number; value: number }>>(
(buckets, bucket) => {
return [
Expand All @@ -33,7 +34,7 @@ export const getLogEntryRatePartitionedSeries = (results: LogRateResults) => {
);
};

export const getLogEntryRateCombinedSeries = (results: LogRateResults) => {
export const getLogEntryRateCombinedSeries = (results: LogEntryRateResults) => {
return results.histogramBuckets.reduce<Array<{ time: number; value: number }>>(
(buckets, bucket) => {
return [
Expand All @@ -50,7 +51,10 @@ export const getLogEntryRateCombinedSeries = (results: LogRateResults) => {
);
};

export const getLogEntryRateSeriesForPartition = (results: LogRateResults, partitionId: string) => {
export const getLogEntryRateSeriesForPartition = (
results: LogEntryRateResults,
partitionId: string
) => {
return results.partitionBuckets[partitionId].buckets.reduce<
Array<{ time: number; value: number }>
>((buckets, bucket) => {
Expand All @@ -64,7 +68,7 @@ export const getLogEntryRateSeriesForPartition = (results: LogRateResults, parti
}, []);
};

export const getAnnotationsForPartition = (results: LogRateResults, partitionId: string) => {
export const getAnnotationsForPartition = (results: LogEntryRateResults, partitionId: string) => {
return results.partitionBuckets[partitionId].buckets.reduce<
Record<MLSeverityScoreCategories, RectAnnotationDatum[]>
>(
Expand Down Expand Up @@ -106,13 +110,13 @@ export const getAnnotationsForPartition = (results: LogRateResults, partitionId:
};

export const getTotalNumberOfLogEntriesForPartition = (
results: LogRateResults,
results: LogEntryRateResults,
partitionId: string
) => {
return results.partitionBuckets[partitionId].totalNumberOfLogEntries;
};

export const getAnnotationsForAll = (results: LogRateResults) => {
export const getAnnotationsForAll = (results: LogEntryRateResults) => {
return results.histogramBuckets.reduce<Record<MLSeverityScoreCategories, RectAnnotationDatum[]>>(
(annotatedBucketsBySeverity, bucket) => {
const maxAnomalyScoresByPartition = bucket.partitions.reduce<
Expand Down Expand Up @@ -169,7 +173,7 @@ export const getAnnotationsForAll = (results: LogRateResults) => {
);
};

export const getTopAnomalyScoreAcrossAllPartitions = (results: LogRateResults) => {
export const getTopAnomalyScoreAcrossAllPartitions = (results: LogEntryRateResults) => {
const allTopScores = Object.values(results.partitionBuckets).reduce(
(scores: number[], partition) => {
return [...scores, partition.topAnomalyScore];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { EuiEmptyPrompt, EuiLoadingSpinner, EuiSpacer, EuiTitle, EuiText } from
import { i18n } from '@kbn/i18n';
import React, { useMemo } from 'react';

import { LogRateResults as Results } from '../../../../../containers/logs/log_analysis/log_analysis_results';
import { LogEntryRateResults as Results } from '../../use_log_entry_rate_results';
import { TimeRange } from '../../../../../../common/http_api/shared/time_range';
import { LogEntryRateBarChart } from './bar_chart';
import { getLogEntryRatePartitionedSeries } from '../helpers/data_formatters';
Expand Down
Loading

0 comments on commit 3344f32

Please sign in to comment.