Skip to content

Commit

Permalink
Use fields with timeRange in context, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ddelemeny committed Mar 26, 2024
1 parent 7bf9a4a commit 60b6f2d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 27 deletions.
28 changes: 10 additions & 18 deletions src/LogContext/LogContextProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
dateTime,
LogRowModel,
rangeUtil,
TimeRange,
} from '@grafana/data';

import { ElasticsearchQuery, Logs, LogsSortDirection} from '../types';
Expand All @@ -26,20 +27,16 @@ export enum LogRowContextQueryDirection {
Forward = 'FORWARD',
}

function createContextTimeRange(rowTimeEpochMs: number, direction: string) {
export function createContextTimeRange(rowTimeEpochMs: number, direction?: LogRowContextQueryDirection): TimeRange {
const offset = 7;
// For log context, we want to request data from 7 subsequent/previous indices
if (direction === LogRowContextQueryDirection.Forward) {
return {
from: dateTime(rowTimeEpochMs).utc(),
to: dateTime(rowTimeEpochMs).add(offset, 'hours').utc(),
};
} else {
return {
from: dateTime(rowTimeEpochMs).subtract(offset, 'hours').utc(),
to: dateTime(rowTimeEpochMs).utc(),
};
const timeFrom = dateTime(rowTimeEpochMs)
const timeTo = dateTime(rowTimeEpochMs)

const timeRange = {
from: (direction === LogRowContextQueryDirection.Forward) ? timeFrom.utc() : timeFrom.subtract(offset, 'hours').utc(),
to: (direction === LogRowContextQueryDirection.Backward) ? timeTo.utc() : timeTo.add(offset, 'hours').utc(),
}
return { ...timeRange, raw:timeRange }
}

export class LogContextProvider {
Expand Down Expand Up @@ -76,12 +73,7 @@ export class LogContextProvider {
query: this.contextQuery == null ? origQuery?.query : this.contextQuery,
};

const timeRange = createContextTimeRange(row.timeEpochMs, direction);
const range = {
from: timeRange.from,
to: timeRange.to,
raw: timeRange,
};
const range = createContextTimeRange(row.timeEpochMs, direction);

const interval = rangeUtil.calculateInterval(range, 1);

Expand Down
9 changes: 7 additions & 2 deletions src/LogContext/components/LogContextUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import { css } from "@emotion/css";
import { Button } from "@grafana/ui";
import { useQueryBuilder } from '@/QueryBuilder/lucene';
import { LogContextQueryBuilderSidebar } from "./LogContextQueryBuilderSidebar";
import { DatasourceContext, useRange } from "@/components/QueryEditor/ElasticsearchQueryContext";
import { DatasourceContext } from "@/components/QueryEditor/ElasticsearchQueryContext";
import { BaseQuickwitDataSource } from "@/datasource/base";
import { useDatasourceFields } from "@/datasource/utils";
import { Field, FieldContingency, Filter } from "../types";
import { createContextTimeRange } from "LogContext/LogContextProvider";

// TODO : define sensible defaults here
// const excludedFields = [
Expand Down Expand Up @@ -51,7 +52,11 @@ export function LogContextUI(props: LogContextUIProps ){
const {query, parsedQuery, setQuery, setParsedQuery} = builder;
const [canRunQuery, setCanRunQuery] = useState<boolean>(false);
const {row, origQuery, updateQuery, runContextQuery } = props;
const {fields, getSuggestions} = useDatasourceFields(props.datasource, getDefaultTimeRange());

const fieldsSuggestionTimeRange = useMemo(()=>createContextTimeRange(row.timeEpochMs), [row])
const defaultTimeRange = getDefaultTimeRange()
console.log("RANGES", fieldsSuggestionTimeRange, defaultTimeRange)
const {fields, getSuggestions} = useDatasourceFields(props.datasource, fieldsSuggestionTimeRange);

useEffect(()=>{
setQuery(origQuery?.query || '')
Expand Down
14 changes: 7 additions & 7 deletions src/hooks/useFields.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,39 +50,39 @@ describe('useFields hook', () => {
{ wrapper, initialProps: 'cardinality' }
);
result.current();
expect(getFields).toHaveBeenLastCalledWith({aggregatable:true, type:[], _range:timeRange});
expect(getFields).toHaveBeenLastCalledWith({aggregatable:true, type:[], range:timeRange});

// All other metric aggregations only work on numbers
rerender('avg');
result.current();
expect(getFields).toHaveBeenLastCalledWith({aggregatable:true, type:['number'], _range:timeRange});
expect(getFields).toHaveBeenLastCalledWith({aggregatable:true, type:['number'], range:timeRange});

//
// BUCKET AGGREGATIONS
//
// Date Histrogram only works on dates
rerender('date_histogram');
result.current();
expect(getFields).toHaveBeenLastCalledWith({aggregatable:true, type:['date'], _range:timeRange});
expect(getFields).toHaveBeenLastCalledWith({aggregatable:true, type:['date'], range:timeRange});

// Histrogram only works on numbers
rerender('histogram');
result.current();
expect(getFields).toHaveBeenLastCalledWith({aggregatable:true, type:['number'], _range:timeRange});
expect(getFields).toHaveBeenLastCalledWith({aggregatable:true, type:['number'], range:timeRange});

// Geohash Grid only works on geo_point data
rerender('geohash_grid');
result.current();
expect(getFields).toHaveBeenLastCalledWith({aggregatable:true, type:['geo_point'], _range:timeRange});
expect(getFields).toHaveBeenLastCalledWith({aggregatable:true, type:['geo_point'], range:timeRange});

// All other bucket aggregation work on any kind of data
rerender('terms');
result.current();
expect(getFields).toHaveBeenLastCalledWith({aggregatable:true, type:[], _range:timeRange});
expect(getFields).toHaveBeenLastCalledWith({aggregatable:true, type:[], range:timeRange});

// top_metrics work on only on numeric data in 7.7
rerender('top_metrics');
result.current();
expect(getFields).toHaveBeenLastCalledWith({aggregatable:true, type:['number'], _range:timeRange});
expect(getFields).toHaveBeenLastCalledWith({aggregatable:true, type:['number'], range:timeRange});
});
});

0 comments on commit 60b6f2d

Please sign in to comment.