Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
d99c553a
Unverified
Commit
d99c553a
authored
Nov 11, 2020
by
Giordano Ricci
Committed by
GitHub
Nov 11, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Elasticsearch: interpolate variables in Filters Bucket Aggregation (#28969)
parent
46d56812
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
10 deletions
+45
-10
public/app/plugins/datasource/elasticsearch/datasource.test.ts
+28
-0
public/app/plugins/datasource/elasticsearch/datasource.ts
+17
-10
No files found.
public/app/plugins/datasource/elasticsearch/datasource.test.ts
View file @
d99c553a
...
...
@@ -864,6 +864,34 @@ describe('ElasticDatasource', function(this: any) {
expect
(
typeof
JSON
.
parse
(
query
.
split
(
'
\
n'
)[
1
]).
query
.
bool
.
filter
[
0
].
range
[
'@time'
].
gte
).
toBe
(
'number'
);
});
});
it
(
'should correctly interpolate variables in query'
,
()
=>
{
const
query
=
{
alias
:
''
,
bucketAggs
:
[{
type
:
'filters'
,
settings
:
{
filters
:
[{
query
:
'$var'
,
label
:
''
}]
},
id
:
'1'
}],
metrics
:
[{
type
:
'count'
,
id
:
'1'
}],
query
:
'$var'
,
};
const
interpolatedQuery
=
ctx
.
ds
.
interpolateVariablesInQueries
([
query
],
{})[
0
];
expect
(
interpolatedQuery
.
query
).
toBe
(
'resolvedVariable'
);
expect
(
interpolatedQuery
.
bucketAggs
[
0
].
settings
.
filters
[
0
].
query
).
toBe
(
'resolvedVariable'
);
});
it
(
'should correctly handle empty query strings'
,
()
=>
{
const
query
=
{
alias
:
''
,
bucketAggs
:
[{
type
:
'filters'
,
settings
:
{
filters
:
[{
query
:
''
,
label
:
''
}]
},
id
:
'1'
}],
metrics
:
[{
type
:
'count'
,
id
:
'1'
}],
query
:
''
,
};
const
interpolatedQuery
=
ctx
.
ds
.
interpolateVariablesInQueries
([
query
],
{})[
0
];
expect
(
interpolatedQuery
.
query
).
toBe
(
'*'
);
expect
(
interpolatedQuery
.
bucketAggs
[
0
].
settings
.
filters
[
0
].
query
).
toBe
(
'*'
);
});
});
describe
(
'enhanceDataFrame'
,
()
=>
{
...
...
public/app/plugins/datasource/elasticsearch/datasource.ts
View file @
d99c553a
...
...
@@ -308,6 +308,11 @@ export class ElasticDatasource extends DataSourceApi<ElasticsearchQuery, Elastic
});
}
private
interpolateLuceneQuery
(
queryString
:
string
,
scopedVars
:
ScopedVars
)
{
// Elasticsearch queryString should always be '*' if empty string
return
this
.
templateSrv
.
replace
(
queryString
,
scopedVars
,
'lucene'
)
||
'*'
;
}
interpolateVariablesInQueries
(
queries
:
ElasticsearchQuery
[],
scopedVars
:
ScopedVars
):
ElasticsearchQuery
[]
{
let
expandedQueries
=
queries
;
if
(
queries
&&
queries
.
length
>
0
)
{
...
...
@@ -315,8 +320,16 @@ export class ElasticDatasource extends DataSourceApi<ElasticsearchQuery, Elastic
const
expandedQuery
=
{
...
query
,
datasource
:
this
.
name
,
query
:
this
.
templateSrv
.
replace
(
query
.
query
,
scopedVars
,
'lucene'
),
query
:
this
.
interpolateLuceneQuery
(
query
.
query
||
''
,
scopedVars
),
};
for
(
let
bucketAgg
of
query
.
bucketAggs
||
[])
{
if
(
bucketAgg
.
type
===
'filters'
)
{
for
(
let
filter
of
bucketAgg
.
settings
.
filters
)
{
filter
.
query
=
this
.
interpolateLuceneQuery
(
filter
.
query
,
scopedVars
);
}
}
}
return
expandedQuery
;
});
}
...
...
@@ -363,7 +376,7 @@ export class ElasticDatasource extends DataSourceApi<ElasticsearchQuery, Elastic
query
(
options
:
DataQueryRequest
<
ElasticsearchQuery
>
):
Promise
<
DataQueryResponse
>
{
let
payload
=
''
;
const
targets
=
_
.
cloneDeep
(
options
.
target
s
);
const
targets
=
this
.
interpolateVariablesInQueries
(
_
.
cloneDeep
(
options
.
targets
),
options
.
scopedVar
s
);
const
sentTargets
:
ElasticsearchQuery
[]
=
[];
// add global adhoc filters to timeFilter
...
...
@@ -374,25 +387,19 @@ export class ElasticDatasource extends DataSourceApi<ElasticsearchQuery, Elastic
continue
;
}
let
queryString
=
this
.
templateSrv
.
replace
(
target
.
query
,
options
.
scopedVars
,
'lucene'
);
// Elasticsearch queryString should always be '*' if empty string
if
(
!
queryString
||
queryString
===
''
)
{
queryString
=
'*'
;
}
let
queryObj
;
if
(
target
.
isLogsQuery
||
queryDef
.
hasMetricOfType
(
target
,
'logs'
))
{
target
.
bucketAggs
=
[
queryDef
.
defaultBucketAgg
()];
target
.
metrics
=
[];
// Setting this for metrics queries that are typed as logs
target
.
isLogsQuery
=
true
;
queryObj
=
this
.
queryBuilder
.
getLogsQuery
(
target
,
adhocFilters
,
queryString
);
queryObj
=
this
.
queryBuilder
.
getLogsQuery
(
target
,
adhocFilters
,
target
.
query
);
}
else
{
if
(
target
.
alias
)
{
target
.
alias
=
this
.
templateSrv
.
replace
(
target
.
alias
,
options
.
scopedVars
,
'lucene'
);
}
queryObj
=
this
.
queryBuilder
.
build
(
target
,
adhocFilters
,
queryString
);
queryObj
=
this
.
queryBuilder
.
build
(
target
,
adhocFilters
,
target
.
query
);
}
const
esQuery
=
angular
.
toJson
(
queryObj
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment