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
bcb52417
Commit
bcb52417
authored
Nov 17, 2017
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: refactoring InfluxDB query builder with policy PR #9473
parent
4b76c6d6
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
2 additions
and
242 deletions
+2
-242
public/app/plugins/datasource/influxdb/datasource.ts
+1
-2
public/app/plugins/datasource/influxdb/query_builder.d.ts
+0
-2
public/app/plugins/datasource/influxdb/query_builder.js
+0
-121
public/app/plugins/datasource/influxdb/query_ctrl.ts
+1
-3
public/app/plugins/datasource/influxdb/specs/query_builder_specs.ts
+0
-114
No files found.
public/app/plugins/datasource/influxdb/datasource.ts
View file @
bcb52417
...
...
@@ -4,8 +4,7 @@ import * as dateMath from 'app/core/utils/datemath';
import
InfluxSeries
from
'./influx_series'
;
import
InfluxQuery
from
'./influx_query'
;
import
ResponseParser
from
'./response_parser'
;
import
InfluxQueryBuilder
from
'./query_builder'
;
import
{
InfluxQueryBuilder
}
from
'./query_builder'
;
export
default
class
InfluxDatasource
{
type
:
string
;
...
...
public/app/plugins/datasource/influxdb/query_builder.d.ts
deleted
100644 → 0
View file @
4b76c6d6
declare
var
test
:
any
;
export
default
test
;
public/app/plugins/datasource/influxdb/query_builder.js
deleted
100644 → 0
View file @
4b76c6d6
define
([
'lodash'
],
function
(
_
)
{
'use strict'
;
function
InfluxQueryBuilder
(
target
,
database
)
{
this
.
target
=
target
;
this
.
database
=
database
;
}
function
renderTagCondition
(
tag
,
index
)
{
var
str
=
""
;
var
operator
=
tag
.
operator
;
var
value
=
tag
.
value
;
if
(
index
>
0
)
{
str
=
(
tag
.
condition
||
'AND'
)
+
' '
;
}
if
(
!
operator
)
{
if
(
/^
\/
.*
\/
$/
.
test
(
tag
.
value
))
{
operator
=
'=~'
;
}
else
{
operator
=
'='
;
}
}
// quote value unless regex or number
if
(
operator
!==
'=~'
&&
operator
!==
'!~'
&&
isNaN
(
+
value
))
{
value
=
"'"
+
value
+
"'"
;
}
return
str
+
'"'
+
tag
.
key
+
'" '
+
operator
+
' '
+
value
;
}
var
p
=
InfluxQueryBuilder
.
prototype
;
p
.
build
=
function
()
{
return
this
.
target
.
rawQuery
?
this
.
_modifyRawQuery
()
:
this
.
_buildQuery
();
};
p
.
buildExploreQuery
=
function
(
type
,
withKey
,
withMeasurementFilter
)
{
var
query
;
var
measurement
;
var
policy
;
if
(
type
===
'TAG_KEYS'
)
{
query
=
'SHOW TAG KEYS'
;
measurement
=
this
.
target
.
measurement
;
policy
=
this
.
target
.
policy
;
}
else
if
(
type
===
'TAG_VALUES'
)
{
query
=
'SHOW TAG VALUES'
;
measurement
=
this
.
target
.
measurement
;
policy
=
this
.
target
.
policy
;
}
else
if
(
type
===
'MEASUREMENTS'
)
{
query
=
'SHOW MEASUREMENTS'
;
if
(
withMeasurementFilter
)
{
query
+=
' WITH MEASUREMENT =~ /'
+
withMeasurementFilter
+
'/'
;
}
}
else
if
(
type
===
'FIELDS'
)
{
measurement
=
this
.
target
.
measurement
;
policy
=
this
.
target
.
policy
;
if
(
!
measurement
.
match
(
'^/.*/'
))
{
measurement
=
'"'
+
measurement
+
'"'
;
if
(
policy
)
{
if
(
!
policy
.
match
(
'^/.*/'
))
{
policy
=
'"'
+
policy
+
'"'
;
}
measurement
=
policy
+
'.'
+
measurement
;
}
}
return
'SHOW FIELD KEYS FROM '
+
measurement
;
}
else
if
(
type
===
'RETENTION POLICIES'
)
{
query
=
'SHOW RETENTION POLICIES on "'
+
this
.
database
+
'"'
;
return
query
;
}
if
(
measurement
)
{
if
(
!
measurement
.
match
(
'^/.*/'
)
&&
!
measurement
.
match
(
/^merge
\(
.*
\)
/
))
{
measurement
=
'"'
+
measurement
+
'"'
;
}
if
(
policy
)
{
if
(
!
policy
.
match
(
'^/.*/'
)
&&
!
policy
.
match
(
/^merge
\(
.*
\)
/
))
{
policy
=
'"'
+
policy
+
'"'
;
}
measurement
=
policy
+
'.'
+
measurement
;
}
query
+=
' FROM '
+
measurement
;
}
if
(
withKey
)
{
query
+=
' WITH KEY = "'
+
withKey
+
'"'
;
}
if
(
this
.
target
.
tags
&&
this
.
target
.
tags
.
length
>
0
)
{
var
whereConditions
=
_
.
reduce
(
this
.
target
.
tags
,
function
(
memo
,
tag
)
{
// do not add a condition for the key we want to explore for
if
(
tag
.
key
===
withKey
)
{
return
memo
;
}
memo
.
push
(
renderTagCondition
(
tag
,
memo
.
length
));
return
memo
;
},
[]);
if
(
whereConditions
.
length
>
0
)
{
query
+=
' WHERE '
+
whereConditions
.
join
(
' '
);
}
}
if
(
type
===
'MEASUREMENTS'
)
{
query
+=
' LIMIT 100'
;
//Solve issue #2524 by limiting the number of measurements returned
//LIMIT must be after WITH MEASUREMENT and WHERE clauses
//This also could be used for TAG KEYS and TAG VALUES, if desired
}
return
query
;
};
return
InfluxQueryBuilder
;
});
public/app/plugins/datasource/influxdb/query_ctrl.ts
View file @
bcb52417
///<reference path="../../../headers/common.d.ts" />
import
angular
from
'angular'
;
import
_
from
'lodash'
;
import
InfluxQueryBuilder
from
'./query_builder'
;
import
{
InfluxQueryBuilder
}
from
'./query_builder'
;
import
InfluxQuery
from
'./influx_query'
;
import
queryPart
from
'./query_part'
;
import
{
QueryCtrl
}
from
'app/plugins/sdk'
;
...
...
public/app/plugins/datasource/influxdb/specs/query_builder_specs.ts
deleted
100644 → 0
View file @
4b76c6d6
import
{
describe
,
it
,
expect
}
from
'test/lib/common'
;
import
InfluxQueryBuilder
from
'../query_builder'
;
describe
(
'InfluxQueryBuilder'
,
function
()
{
describe
(
'when building explore queries'
,
function
()
{
it
(
'should only have measurement condition in tag keys query given query with measurement'
,
function
()
{
var
builder
=
new
InfluxQueryBuilder
({
measurement
:
'cpu'
,
tags
:
[]
});
var
query
=
builder
.
buildExploreQuery
(
'TAG_KEYS'
);
expect
(
query
).
to
.
be
(
'SHOW TAG KEYS FROM "cpu"'
);
});
it
(
'should handle regex measurement in tag keys query'
,
function
()
{
var
builder
=
new
InfluxQueryBuilder
({
measurement
:
'/.*/'
,
tags
:
[]
});
var
query
=
builder
.
buildExploreQuery
(
'TAG_KEYS'
);
expect
(
query
).
to
.
be
(
'SHOW TAG KEYS FROM /.*/'
);
});
it
(
'should have no conditions in tags keys query given query with no measurement or tag'
,
function
()
{
var
builder
=
new
InfluxQueryBuilder
({
measurement
:
''
,
tags
:
[]
});
var
query
=
builder
.
buildExploreQuery
(
'TAG_KEYS'
);
expect
(
query
).
to
.
be
(
'SHOW TAG KEYS'
);
});
it
(
'should have where condition in tag keys query with tags'
,
function
()
{
var
builder
=
new
InfluxQueryBuilder
({
measurement
:
''
,
tags
:
[{
key
:
'host'
,
value
:
'se1'
}]
});
var
query
=
builder
.
buildExploreQuery
(
'TAG_KEYS'
);
expect
(
query
).
to
.
be
(
"SHOW TAG KEYS WHERE
\"
host
\"
= 'se1'"
);
});
it
(
'should have no conditions in measurement query for query with no tags'
,
function
()
{
var
builder
=
new
InfluxQueryBuilder
({
measurement
:
''
,
tags
:
[]
});
var
query
=
builder
.
buildExploreQuery
(
'MEASUREMENTS'
);
expect
(
query
).
to
.
be
(
'SHOW MEASUREMENTS LIMIT 100'
);
});
it
(
'should have no conditions in measurement query for query with no tags and empty query'
,
function
()
{
var
builder
=
new
InfluxQueryBuilder
({
measurement
:
''
,
tags
:
[]
});
var
query
=
builder
.
buildExploreQuery
(
'MEASUREMENTS'
,
undefined
,
''
);
expect
(
query
).
to
.
be
(
'SHOW MEASUREMENTS LIMIT 100'
);
});
it
(
'should have WITH MEASUREMENT in measurement query for non-empty query with no tags'
,
function
()
{
var
builder
=
new
InfluxQueryBuilder
({
measurement
:
''
,
tags
:
[]
});
var
query
=
builder
.
buildExploreQuery
(
'MEASUREMENTS'
,
undefined
,
'something'
);
expect
(
query
).
to
.
be
(
'SHOW MEASUREMENTS WITH MEASUREMENT =~ /something/ LIMIT 100'
);
});
it
(
'should have WITH MEASUREMENT WHERE in measurement query for non-empty query with tags'
,
function
()
{
var
builder
=
new
InfluxQueryBuilder
({
measurement
:
''
,
tags
:
[{
key
:
'app'
,
value
:
'email'
}]
});
var
query
=
builder
.
buildExploreQuery
(
'MEASUREMENTS'
,
undefined
,
'something'
);
expect
(
query
).
to
.
be
(
"SHOW MEASUREMENTS WITH MEASUREMENT =~ /something/ WHERE
\"
app
\"
= 'email' LIMIT 100"
);
});
it
(
'should have where condition in measurement query for query with tags'
,
function
()
{
var
builder
=
new
InfluxQueryBuilder
({
measurement
:
''
,
tags
:
[{
key
:
'app'
,
value
:
'email'
}]});
var
query
=
builder
.
buildExploreQuery
(
'MEASUREMENTS'
);
expect
(
query
).
to
.
be
(
"SHOW MEASUREMENTS WHERE
\"
app
\"
= 'email' LIMIT 100"
);
});
it
(
'should have where tag name IN filter in tag values query for query with one tag'
,
function
()
{
var
builder
=
new
InfluxQueryBuilder
({
measurement
:
''
,
tags
:
[{
key
:
'app'
,
value
:
'asdsadsad'
}]});
var
query
=
builder
.
buildExploreQuery
(
'TAG_VALUES'
,
'app'
);
expect
(
query
).
to
.
be
(
'SHOW TAG VALUES WITH KEY = "app"'
);
});
it
(
'should have measurement tag condition and tag name IN filter in tag values query'
,
function
()
{
var
builder
=
new
InfluxQueryBuilder
({
measurement
:
'cpu'
,
tags
:
[{
key
:
'app'
,
value
:
'email'
},
{
key
:
'host'
,
value
:
'server1'
}]});
var
query
=
builder
.
buildExploreQuery
(
'TAG_VALUES'
,
'app'
);
expect
(
query
).
to
.
be
(
'SHOW TAG VALUES FROM "cpu" WITH KEY = "app" WHERE "host" =
\'
server1
\'
'
);
});
it
(
'should select from policy correctly if policy is specified'
,
function
()
{
var
builder
=
new
InfluxQueryBuilder
({
measurement
:
'cpu'
,
policy
:
'one_week'
,
tags
:
[{
key
:
'app'
,
value
:
'email'
},
{
key
:
'host'
,
value
:
'server1'
}]
});
var
query
=
builder
.
buildExploreQuery
(
'TAG_VALUES'
,
'app'
);
expect
(
query
).
to
.
be
(
'SHOW TAG VALUES FROM "one_week"."cpu" WITH KEY = "app" WHERE "host" =
\'
server1
\'
'
);
});
it
(
'should switch to regex operator in tag condition'
,
function
()
{
var
builder
=
new
InfluxQueryBuilder
({
measurement
:
'cpu'
,
tags
:
[{
key
:
'host'
,
value
:
'/server.*/'
}]
});
var
query
=
builder
.
buildExploreQuery
(
'TAG_VALUES'
,
'app'
);
expect
(
query
).
to
.
be
(
'SHOW TAG VALUES FROM "cpu" WITH KEY = "app" WHERE "host" =~ /server.*/'
);
});
it
(
'should build show field query'
,
function
()
{
var
builder
=
new
InfluxQueryBuilder
({
measurement
:
'cpu'
,
tags
:
[{
key
:
'app'
,
value
:
'email'
}]});
var
query
=
builder
.
buildExploreQuery
(
'FIELDS'
);
expect
(
query
).
to
.
be
(
'SHOW FIELD KEYS FROM "cpu"'
);
});
it
(
'should build show field query with regexp'
,
function
()
{
var
builder
=
new
InfluxQueryBuilder
({
measurement
:
'/$var/'
,
tags
:
[{
key
:
'app'
,
value
:
'email'
}]});
var
query
=
builder
.
buildExploreQuery
(
'FIELDS'
);
expect
(
query
).
to
.
be
(
'SHOW FIELD KEYS FROM /$var/'
);
});
it
(
'should build show retention policies query'
,
function
()
{
var
builder
=
new
InfluxQueryBuilder
({
measurement
:
'cpu'
,
tags
:
[]},
'site'
);
var
query
=
builder
.
buildExploreQuery
(
'RETENTION POLICIES'
);
expect
(
query
).
to
.
be
(
'SHOW RETENTION POLICIES on "site"'
);
});
});
});
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