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
d530ccff
Commit
d530ccff
authored
Sep 11, 2017
by
Mitsuhiro Tanda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(prometheus) support label value completion
parent
40b74e66
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
55 additions
and
13 deletions
+55
-13
public/app/plugins/datasource/prometheus/completer.ts
+55
-13
No files found.
public/app/plugins/datasource/prometheus/completer.ts
View file @
d530ccff
...
...
@@ -4,22 +4,27 @@ import {PrometheusDatasource} from "./datasource";
import
_
from
'lodash'
;
export
class
PromCompleter
{
labelQueryCache
:
any
;
labelNameCache
:
any
;
labelValueCache
:
any
;
identifierRegexps
=
[
/
[\[\]
a-zA-Z_0-9=
]
/
];
constructor
(
private
datasource
:
PrometheusDatasource
)
{
this
.
labelQueryCache
=
{};
this
.
labelNameCache
=
{};
this
.
labelValueCache
=
{};
}
getCompletions
(
editor
,
session
,
pos
,
prefix
,
callback
)
{
let
token
=
session
.
getTokenAt
(
pos
.
row
,
pos
.
column
);
var
metricName
;
switch
(
token
.
type
)
{
case
'label.name'
:
var
metricName
=
this
.
findMetricName
(
session
,
pos
.
row
,
pos
.
column
);
metricName
=
this
.
findMetricName
(
session
,
pos
.
row
,
pos
.
column
);
if
(
!
metricName
)
{
callback
(
null
,
this
.
transformToCompletions
([
'__name__'
,
'instance'
,
'job'
]));
callback
(
null
,
this
.
transformToCompletions
([
'__name__'
,
'instance'
,
'job'
]
,
'label name'
));
return
;
}
...
...
@@ -28,23 +33,45 @@ export class PromCompleter {
return
;
}
var
op
=
'=~'
;
if
(
/
[
a-zA-Z_:
][
a-zA-Z0-9_:
]
*/
.
test
(
metricName
))
{
op
=
'='
;
}
var
expr
=
'{__name__'
+
op
+
'"'
+
metricName
+
'"}'
;
this
.
datasource
.
performInstantQuery
({
expr
:
expr
},
new
Date
().
getTime
()
/
1000
).
then
(
response
=>
{
this
.
getLabelNameAndValueForMetric
(
metricName
).
then
(
result
=>
{
var
labelNames
=
this
.
transformToCompletions
(
_
.
uniq
(
_
.
flatten
(
res
ponse
.
data
.
data
.
res
ult
.
map
(
r
=>
{
_
.
uniq
(
_
.
flatten
(
result
.
map
(
r
=>
{
return
Object
.
keys
(
r
.
metric
);
})))
);
,
'label name'
);
this
.
labelNameCache
[
metricName
]
=
labelNames
;
callback
(
null
,
labelNames
);
});
return
;
case
'label.value'
:
callback
(
null
,
[]);
metricName
=
this
.
findMetricName
(
session
,
pos
.
row
,
pos
.
column
);
if
(
!
metricName
)
{
callback
(
null
,
[]);
return
;
}
var
labelNameToken
=
this
.
findToken
(
session
,
pos
.
row
,
pos
.
column
,
'label.name'
,
null
,
'paren.lparen'
);
if
(
!
labelNameToken
)
{
callback
(
null
,
[]);
return
;
}
var
labelName
=
labelNameToken
.
value
;
if
(
this
.
labelValueCache
[
metricName
]
&&
this
.
labelValueCache
[
metricName
][
labelName
])
{
callback
(
null
,
this
.
labelValueCache
[
metricName
][
labelName
]);
return
;
}
this
.
getLabelNameAndValueForMetric
(
metricName
).
then
(
result
=>
{
var
labelValues
=
this
.
transformToCompletions
(
_
.
uniq
(
result
.
map
(
r
=>
{
return
r
.
metric
[
labelName
];
}))
,
'label value'
);
this
.
labelValueCache
[
metricName
]
=
this
.
labelValueCache
[
metricName
]
||
{};
this
.
labelValueCache
[
metricName
][
labelName
]
=
labelValues
;
callback
(
null
,
labelValues
);
});
return
;
}
...
...
@@ -78,12 +105,27 @@ export class PromCompleter {
});
}
transformToCompletions
(
words
)
{
getLabelNameAndValueForMetric
(
metricName
)
{
if
(
this
.
labelQueryCache
[
metricName
])
{
return
Promise
.
resolve
(
this
.
labelQueryCache
[
metricName
]);
}
var
op
=
'=~'
;
if
(
/
[
a-zA-Z_:
][
a-zA-Z0-9_:
]
*/
.
test
(
metricName
))
{
op
=
'='
;
}
var
expr
=
'{__name__'
+
op
+
'"'
+
metricName
+
'"}'
;
return
this
.
datasource
.
performInstantQuery
({
expr
:
expr
},
new
Date
().
getTime
()
/
1000
).
then
(
response
=>
{
this
.
labelQueryCache
[
metricName
]
=
response
.
data
.
data
.
result
;
return
response
.
data
.
data
.
result
;
});
}
transformToCompletions
(
words
,
meta
)
{
return
words
.
map
(
name
=>
{
return
{
caption
:
name
,
value
:
name
,
meta
:
"label name"
,
meta
:
meta
,
score
:
Number
.
MAX_VALUE
};
});
...
...
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