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
9c55500c
Unverified
Commit
9c55500c
authored
Feb 13, 2020
by
Andrej Ocenas
Committed by
GitHub
Feb 13, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Elastic: Replace range as number not string (#22173)
parent
e60b1240
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
57 additions
and
4 deletions
+57
-4
public/app/plugins/datasource/elasticsearch/datasource.test.ts
+51
-2
public/app/plugins/datasource/elasticsearch/datasource.ts
+6
-2
No files found.
public/app/plugins/datasource/elasticsearch/datasource.test.ts
View file @
9c55500c
import
angular
from
'angular'
;
import
angular
from
'angular'
;
import
{
dateMath
,
Field
}
from
'@grafana/data'
;
import
{
CoreApp
,
DataQueryRequest
,
dateMath
,
Field
}
from
'@grafana/data'
;
import
_
from
'lodash'
;
import
_
from
'lodash'
;
import
{
ElasticDatasource
}
from
'./datasource'
;
import
{
ElasticDatasource
}
from
'./datasource'
;
import
{
toUtc
,
dateTime
}
from
'@grafana/data'
;
import
{
toUtc
,
dateTime
}
from
'@grafana/data'
;
...
@@ -7,7 +7,7 @@ import { backendSrv } from 'app/core/services/backend_srv'; // will use the vers
...
@@ -7,7 +7,7 @@ import { backendSrv } from 'app/core/services/backend_srv'; // will use the vers
import
{
TimeSrv
}
from
'app/features/dashboard/services/TimeSrv'
;
import
{
TimeSrv
}
from
'app/features/dashboard/services/TimeSrv'
;
import
{
TemplateSrv
}
from
'app/features/templating/template_srv'
;
import
{
TemplateSrv
}
from
'app/features/templating/template_srv'
;
import
{
DataSourceInstanceSettings
}
from
'@grafana/data'
;
import
{
DataSourceInstanceSettings
}
from
'@grafana/data'
;
import
{
ElasticsearchOptions
}
from
'./types'
;
import
{
ElasticsearchOptions
,
ElasticsearchQuery
}
from
'./types'
;
jest
.
mock
(
'@grafana/runtime'
,
()
=>
({
jest
.
mock
(
'@grafana/runtime'
,
()
=>
({
...
jest
.
requireActual
(
'@grafana/runtime'
),
...
jest
.
requireActual
(
'@grafana/runtime'
),
...
@@ -613,8 +613,57 @@ describe('ElasticDatasource', function(this: any) {
...
@@ -613,8 +613,57 @@ describe('ElasticDatasource', function(this: any) {
expect
(
body
[
'aggs'
][
'1'
][
'terms'
].
size
).
not
.
toBe
(
0
);
expect
(
body
[
'aggs'
][
'1'
][
'terms'
].
size
).
not
.
toBe
(
0
);
});
});
});
});
describe
(
'query'
,
()
=>
{
it
(
'should replace range as integer not string'
,
()
=>
{
const
dataSource
=
new
ElasticDatasource
(
{
url
:
'http://es.com'
,
database
:
'[asd-]YYYY.MM.DD'
,
jsonData
:
{
interval
:
'Daily'
,
esVersion
:
2
,
timeField
:
'@time'
,
},
}
as
DataSourceInstanceSettings
<
ElasticsearchOptions
>
,
templateSrv
as
TemplateSrv
,
timeSrv
as
TimeSrv
);
(
dataSource
as
any
).
post
=
jest
.
fn
(()
=>
Promise
.
resolve
({
responses
:
[]
}));
dataSource
.
query
(
createElasticQuery
());
const
query
=
((
dataSource
as
any
).
post
as
jest
.
Mock
).
mock
.
calls
[
0
][
1
];
expect
(
typeof
JSON
.
parse
(
query
.
split
(
'
\
n'
)[
1
]).
query
.
bool
.
filter
[
0
].
range
[
'@time'
].
gte
).
toBe
(
'number'
);
});
});
});
});
const
createElasticQuery
=
():
DataQueryRequest
<
ElasticsearchQuery
>
=>
{
return
{
requestId
:
''
,
dashboardId
:
0
,
interval
:
''
,
panelId
:
0
,
scopedVars
:
{},
timezone
:
''
,
app
:
CoreApp
.
Dashboard
,
startTime
:
0
,
range
:
{
from
:
dateTime
([
2015
,
4
,
30
,
10
]),
to
:
dateTime
([
2015
,
5
,
1
,
10
]),
}
as
any
,
targets
:
[
{
refId
:
''
,
isLogsQuery
:
false
,
bucketAggs
:
[{
type
:
'date_histogram'
,
field
:
'@timestamp'
,
id
:
'2'
}],
metrics
:
[{
type
:
'count'
,
id
:
''
}],
query
:
'test'
,
},
],
};
};
const
logsResponse
=
{
const
logsResponse
=
{
data
:
{
data
:
{
responses
:
[
responses
:
[
...
...
public/app/plugins/datasource/elasticsearch/datasource.ts
View file @
9c55500c
...
@@ -368,8 +368,12 @@ export class ElasticDatasource extends DataSourceApi<ElasticsearchQuery, Elastic
...
@@ -368,8 +368,12 @@ export class ElasticDatasource extends DataSourceApi<ElasticsearchQuery, Elastic
return
Promise
.
resolve
({
data
:
[]
});
return
Promise
.
resolve
({
data
:
[]
});
}
}
payload
=
payload
.
replace
(
/
\$
timeFrom/g
,
options
.
range
.
from
.
valueOf
().
toString
());
// We replace the range here for actual values. We need to replace it together with enclosing "" so that we replace
payload
=
payload
.
replace
(
/
\$
timeTo/g
,
options
.
range
.
to
.
valueOf
().
toString
());
// it as an integer not as string with digits. This is because elastic will convert the string only if the time
// field is specified as type date (which probably should) but can also be specified as integer (millisecond epoch)
// and then sending string will error out.
payload
=
payload
.
replace
(
/"
\$
timeFrom"/g
,
options
.
range
.
from
.
valueOf
().
toString
());
payload
=
payload
.
replace
(
/"
\$
timeTo"/g
,
options
.
range
.
to
.
valueOf
().
toString
());
payload
=
this
.
templateSrv
.
replace
(
payload
,
options
.
scopedVars
);
payload
=
this
.
templateSrv
.
replace
(
payload
,
options
.
scopedVars
);
const
url
=
this
.
getMultiSearchUrl
();
const
url
=
this
.
getMultiSearchUrl
();
...
...
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