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
c193208c
Commit
c193208c
authored
Jul 02, 2015
by
Adam Heinz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rewrite query builder to allow for multiple time series.
parent
923f9345
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
55 additions
and
72 deletions
+55
-72
public/app/plugins/datasource/elasticsearch/datasource.js
+21
-38
public/app/plugins/datasource/elasticsearch/queryBuilder.js
+34
-34
No files found.
public/app/plugins/datasource/elasticsearch/datasource.js
View file @
c193208c
...
...
@@ -295,46 +295,29 @@ function (angular, _, config, kbn, moment, ElasticQueryBuilder) {
};
ElasticDatasource
.
prototype
.
query
=
function
(
options
)
{
var
self
=
this
;
var
allQueries
=
options
.
targets
.
map
(
function
(
target
)
{
if
(
target
.
hide
)
{
return
[];
}
var
queryBuilder
=
new
ElasticQueryBuilder
(
target
);
var
query
=
queryBuilder
.
build
();
console
.
log
(
target
);
query
=
query
.
replace
(
/
\$
interval/g
,
target
.
interval
||
options
.
interval
);
query
=
query
.
replace
(
/
\$
rangeFrom/g
,
options
.
range
.
from
);
query
=
query
.
replace
(
/
\$
rangeTo/g
,
options
.
range
.
to
);
query
=
query
.
replace
(
/
\$
maxDataPoints/g
,
options
.
maxDataPoints
);
query
=
query
.
replace
(
/
\$
keyField/g
,
target
.
keyField
);
query
=
query
.
replace
(
/
\$
valueField/g
,
target
.
valueField
);
query
=
query
.
replace
(
/
\$
termKey/g
,
target
.
termKey
);
query
=
query
.
replace
(
/
\$
termValue/g
,
target
.
termValue
);
query
=
templateSrv
.
replace
(
query
,
options
.
scopedVars
);
return
query
;
}).
join
(
"
\
n"
);
console
.
log
(
allQueries
);
return
this
.
_post
(
'/_search?search_type=count'
,
allQueries
).
then
(
function
(
results
)
{
if
(
!
results
||
!
results
.
facets
)
{
return
{
data
:
[]
};
}
return
{
data
:
self
.
_getTimeSeries
(
results
.
facets
)
};
});
var
queryBuilder
=
new
ElasticQueryBuilder
;
var
query
=
queryBuilder
.
build
(
options
.
targets
);
query
=
query
.
replace
(
/
\$
interval/g
,
options
.
interval
);
query
=
query
.
replace
(
/
\$
rangeFrom/g
,
options
.
range
.
from
);
query
=
query
.
replace
(
/
\$
rangeTo/g
,
options
.
range
.
to
);
query
=
query
.
replace
(
/
\$
maxDataPoints/g
,
options
.
maxDataPoints
);
query
=
templateSrv
.
replace
(
query
,
options
.
scopedVars
);
return
this
.
_post
(
'/_search?search_type=count'
,
query
).
then
(
this
.
_getTimeSeries
);
};
ElasticDatasource
.
prototype
.
_getTimeSeries
=
function
(
facets
)
{
var
self
=
this
;
var
targets
=
[
'metric'
];
var
data
=
targets
.
map
(
function
(
target
)
{
var
datapoints
=
facets
[
target
].
entries
.
map
(
self
.
_getDatapoint
);
return
{
target
:
target
,
datapoints
:
datapoints
};
});
return
data
;
};
ElasticDatasource
.
prototype
.
_getDatapoint
=
function
(
entry
)
{
return
[
entry
.
mean
,
entry
.
time
];
ElasticDatasource
.
prototype
.
_getTimeSeries
=
function
(
results
)
{
var
data
=
[];
if
(
results
&&
results
.
facets
)
{
for
(
var
target
in
results
.
facets
)
{
if
(
results
.
facets
.
hasOwnProperty
(
target
)
&&
results
.
facets
[
target
].
entries
)
{
var
datapoints
=
results
.
facets
[
target
].
entries
.
map
(
function
(
entry
)
{
return
[
entry
.
mean
,
entry
.
time
];
});
data
.
push
({
target
:
target
,
datapoints
:
datapoints
});
}
}
}
return
{
data
:
data
};
};
return
ElasticDatasource
;
...
...
public/app/plugins/datasource/elasticsearch/queryBuilder.js
View file @
c193208c
...
...
@@ -3,51 +3,51 @@ define([
function
()
{
'use strict'
;
function
ElasticQueryBuilder
(
target
)
{
this
.
target
=
target
;
function
ElasticQueryBuilder
()
{
}
ElasticQueryBuilder
.
prototype
.
build
=
function
()
{
var
target
=
this
.
target
;
ElasticQueryBuilder
.
prototype
.
build
=
function
(
targets
)
{
var
query
=
{
"facets"
:
{
"metric"
:
{
"facets"
:
{},
"size"
:
"$maxDataPoints"
};
var
self
=
this
;
targets
.
forEach
(
function
(
target
)
{
if
(
!
target
.
hide
)
{
query
[
"facets"
][
target
.
termKey
+
"_"
+
target
.
termValue
]
=
{
"date_histogram"
:
{
"interval"
:
"$interval"
,
"key_field"
:
"$keyField"
,
"interval"
:
target
.
interval
||
"$interval"
,
"key_field"
:
target
.
keyField
,
"min_doc_count"
:
0
,
"value_field"
:
"$valueField"
}
}
},
"query"
:
{
"filtered"
:
{
"filter"
:
{
"value_field"
:
target
.
valueField
},
"facet_filter"
:
{
"and"
:
[
{
"range"
:
{
"@timestamp"
:
{
"gte"
:
"$rangeFrom"
,
"lte"
:
"$rangeTo"
}
}
},
{
"term"
:
{
"$termKey"
:
"$termValue"
,
}
}
self
.
_buildRangeFilter
(
target
),
self
.
_buildTermFilter
(
target
)
]
}
}
},
"size"
:
"$maxDataPoints"
,
"sort"
:
"@timestamp"
};
};
}
});
query
=
JSON
.
stringify
(
query
);
target
.
query
=
query
;
return
query
;
};
ElasticQueryBuilder
.
prototype
.
_buildRangeFilter
=
function
(
target
)
{
var
filter
=
{
"range"
:{}};
filter
[
"range"
][
target
.
keyField
]
=
{
"gte"
:
"$rangeFrom"
,
"lte"
:
"$rangeTo"
};
return
filter
;
};
ElasticQueryBuilder
.
prototype
.
_buildTermFilter
=
function
(
target
)
{
var
filter
=
{
"term"
:{}};
filter
[
"term"
][
target
.
termKey
]
=
target
.
termValue
;
return
filter
;
};
return
ElasticQueryBuilder
;
});
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