Skip to content

Commit

Permalink
[ML] load entity values
Browse files Browse the repository at this point in the history
  • Loading branch information
darnautov committed Dec 30, 2019
1 parent 5b315b7 commit f4595c4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ declare interface Ml {
results: {
getMaxAnomalyScore: (jobIds: string[], earliestMs: number, latestMs: number) => Promise<any>;
fetchPartitionFieldsValues: (
jobId: string,
criteriaFields: Array<{ fieldName: string; fieldValue: any }>,
earliestMs: number,
latestMs: number
) => Observable<Record<string, string[]>>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ export const results = {
});
},

fetchPartitionFieldsValues(jobId, earliestMs, latestMs) {
fetchPartitionFieldsValues(criteriaFields, earliestMs, latestMs) {
return http$(`${basePath}/results/partition_fields_values`, {
method: 'POST',
body: {
jobId,
criteriaFields,
earliestMs,
latestMs,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import _ from 'lodash';
import { ML_MEDIAN_PERCENTS } from '../../../../common/util/job_utils';
import { JobId } from '../../jobs/new_job/common/job_creator/configs';
import { ml } from '../ml_api_service';
import { ML_RESULTS_INDEX_PATTERN } from '../../../../common/constants/index_patterns';
import { CriteriaField } from './index';
Expand Down Expand Up @@ -534,6 +533,10 @@ export function getScheduledEventsByBucket(
);
}

export function fetchPartitionFieldsValues(jobId: JobId, earliestMs: number, latestMs: number) {
return ml.results.fetchPartitionFieldsValues(jobId, earliestMs, latestMs);
export function fetchPartitionFieldsValues(
criteriaFields: Array<{ fieldName: string; fieldValue: any }>,
earliestMs: number,
latestMs: number
) {
return ml.results.fetchPartitionFieldsValues(criteriaFields, earliestMs, latestMs);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* React component for rendering Single Metric Viewer.
*/

import { chain, difference, each, find, first, get, has, isEqual, without } from 'lodash';
import { difference, each, find, first, get, has, isEqual, without } from 'lodash';
import moment from 'moment-timezone';
import { Subject, Subscription, forkJoin } from 'rxjs';
import { map, debounceTime, switchMap, tap, withLatestFrom } from 'rxjs/operators';
Expand Down Expand Up @@ -423,7 +423,7 @@ export class TimeSeriesExplorer extends React.Component {
* Loads available entity values.
* @param callback
*/
loadEntityValues = (callback = () => {}) => {
loadEntityValues = async (callback = () => {}) => {
const { timefilter } = this.props;
const { detectorId, entities, selectedJob } = this.state;

Expand All @@ -432,55 +432,44 @@ export class TimeSeriesExplorer extends React.Component {
const bounds = timefilter.getActiveBounds();
const detectorIndex = +detectorId;

mlResultsService
.fetchPartitionFieldsValues(selectedJob.job_id, bounds.min.valueOf(), bounds.max.valueOf())
.subscribe(v => {
console.info(v);
});

mlResultsService
.getRecordsForCriteria(
[selectedJob.job_id],
[{ fieldName: 'detector_index', fieldValue: detectorIndex }],
0,
const {
partition_field: partitionField,
over_field: overField,
by_field: byField,
} = await mlResultsService
.fetchPartitionFieldsValues(
[
{
fieldName: 'job_id',
fieldValue: selectedJob.job_id,
},
{
fieldName: 'detector_index',
fieldValue: detectorIndex,
},
],
bounds.min.valueOf(),
bounds.max.valueOf(),
ANOMALIES_TABLE_DEFAULT_QUERY_SIZE
bounds.max.valueOf()
)
.toPromise()
.then(resp => {
if (resp.records && resp.records.length > 0) {
const firstRec = resp.records[0];
.toPromise();

this.setState(
{
entities: entities.map(entity => {
const newEntity = { ...entity };
if (firstRec.partition_field_name === newEntity.fieldName) {
newEntity.fieldValues = chain(resp.records)
.pluck('partition_field_value')
.uniq()
.value();
}
if (firstRec.over_field_name === newEntity.fieldName) {
newEntity.fieldValues = chain(resp.records)
.pluck('over_field_value')
.uniq()
.value();
}
if (firstRec.by_field_name === newEntity.fieldName) {
newEntity.fieldValues = chain(resp.records)
.pluck('by_field_value')
.uniq()
.value();
}
return newEntity;
}),
},
callback
);
}
});
this.setState(
{
entities: entities.map(entity => {
if (partitionField.name === entity.fieldName) {
entity.fieldValues = partitionField.values;
}
if (overField.name === entity.fieldName) {
entity.fieldValues = overField.values;
}
if (byField.name === entity.fieldName) {
entity.fieldValues = byField.values;
}
return entity;
}),
},
callback
);
};

loadForForecastId = forecastId => {
Expand Down

0 comments on commit f4595c4

Please sign in to comment.