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
b0946211
Unverified
Commit
b0946211
authored
Jan 11, 2021
by
Hugo Häggmark
Committed by
GitHub
Jan 11, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
OpenTSDB: Support request cancellation properly (#29992)
parent
6dfa9b48
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
93 additions
and
93 deletions
+93
-93
public/app/plugins/datasource/opentsdb/datasource.ts
+0
-0
public/app/plugins/datasource/opentsdb/specs/datasource.test.ts
+93
-93
No files found.
public/app/plugins/datasource/opentsdb/datasource.ts
View file @
b0946211
This diff is collapsed.
Click to expand it.
public/app/plugins/datasource/opentsdb/specs/datasource.test.ts
View file @
b0946211
import
OpenTsDatasource
from
'../datasource'
;
import
{
backendSrv
}
from
'app/core/services/backend_srv'
;
// will use the version in __mocks__
import
{
OpenTsdbQuery
}
from
'../types'
;
import
{
createFetchResponse
}
from
'../../../../../test/helpers/createFetchResponse'
;
import
{
of
}
from
'rxjs'
;
jest
.
mock
(
'@grafana/runtime'
,
()
=>
({
...((
jest
.
requireActual
(
'@grafana/runtime'
)
as
unknown
)
as
object
),
getBackendSrv
:
()
=>
backendSrv
,
}));
describe
(
'opentsdb'
,
()
=>
{
const
datasourceRequestMock
=
jest
.
spyOn
(
backendSrv
,
'datasourceRequest'
);
const
metricFindQueryData
=
[
{
target
:
'prod1.count'
,
datapoints
:
[
[
10
,
1
],
[
12
,
1
],
],
},
];
beforeEach
(()
=>
{
describe
(
'opentsdb'
,
()
=>
{
function
getTestcontext
({
data
=
metricFindQueryData
}:
{
data
?:
any
}
=
{})
{
jest
.
clearAllMocks
();
});
const
fetchMock
=
jest
.
spyOn
(
backendSrv
,
'fetch'
);
fetchMock
.
mockImplementation
(()
=>
of
(
createFetchResponse
(
data
)));
const
ctx
=
{
ds
:
{},
templateSrv
:
{
replace
:
(
str
:
string
)
=>
str
,
},
}
as
any
;
const
instanceSettings
=
{
url
:
''
,
jsonData
:
{
tsdbVersion
:
1
}
};
const
instanceSettings
=
{
url
:
''
,
jsonData
:
{
tsdbVersion
:
1
}
};
const
replace
=
jest
.
fn
(
value
=>
value
);
const
templateSrv
:
any
=
{
replace
,
};
beforeEach
(()
=>
{
ctx
.
ctrl
=
new
OpenTsDatasource
(
instanceSettings
,
ctx
.
templateSrv
);
});
const
ds
=
new
OpenTsDatasource
(
instanceSettings
,
templateSrv
);
return
{
ds
,
templateSrv
,
fetchMock
};
}
describe
(
'When performing metricFindQuery'
,
()
=>
{
let
results
:
any
;
let
requestOptions
:
any
;
beforeEach
(
async
()
=>
{
datasourceRequestMock
.
mockImplementation
(
await
((
options
:
any
)
=>
{
requestOptions
=
options
;
return
Promise
.
resolve
({
data
:
[
{
target
:
'prod1.count'
,
datapoints
:
[
[
10
,
1
],
[
12
,
1
],
],
},
],
});
})
);
});
it
(
'metrics() should generate api suggest query'
,
async
()
=>
{
const
{
ds
,
fetchMock
}
=
getTestcontext
();
const
results
=
await
ds
.
metricFindQuery
(
'metrics(pew)'
);
it
(
'metrics() should generate api suggest query'
,
()
=>
{
ctx
.
ctrl
.
metricFindQuery
(
'metrics(pew)'
).
then
((
data
:
any
)
=>
{
results
=
data
;
});
expect
(
requestOptions
.
url
).
toBe
(
'/api/suggest'
);
expect
(
requestOptions
.
params
.
type
).
toBe
(
'metrics'
);
expect
(
requestOptions
.
params
.
q
).
toBe
(
'pew'
);
expect
(
fetchMock
).
toHaveBeenCalledTimes
(
1
);
expect
(
fetchMock
.
mock
.
calls
[
0
][
0
].
url
).
toBe
(
'/api/suggest'
);
expect
(
fetchMock
.
mock
.
calls
[
0
][
0
].
params
?.
type
).
toBe
(
'metrics'
);
expect
(
fetchMock
.
mock
.
calls
[
0
][
0
].
params
?.
q
).
toBe
(
'pew'
);
expect
(
results
).
not
.
toBe
(
null
);
});
it
(
'tag_names(cpu) should generate lookup query'
,
()
=>
{
ctx
.
ctrl
.
metricFindQuery
(
'tag_names(cpu)'
).
then
((
data
:
any
)
=>
{
results
=
data
;
});
expect
(
requestOptions
.
url
).
toBe
(
'/api/search/lookup'
);
expect
(
requestOptions
.
params
.
m
).
toBe
(
'cpu'
);
});
it
(
'tag_names(cpu) should generate lookup query'
,
async
()
=>
{
const
{
ds
,
fetchMock
}
=
getTestcontext
();
const
results
=
await
ds
.
metricFindQuery
(
'tag_names(cpu)'
);
it
(
'tag_values(cpu, test) should generate lookup query'
,
()
=>
{
ctx
.
ctrl
.
metricFindQuery
(
'tag_values(cpu, hostname)'
).
then
((
data
:
any
)
=>
{
results
=
data
;
});
expect
(
requestOptions
.
url
).
toBe
(
'/api/search/lookup'
);
expect
(
requestOptions
.
params
.
m
).
toBe
(
'cpu{hostname=*}'
);
expect
(
fetchMock
).
toHaveBeenCalledTimes
(
1
);
expect
(
fetchMock
.
mock
.
calls
[
0
][
0
].
url
).
toBe
(
'/api/search/lookup'
);
expect
(
fetchMock
.
mock
.
calls
[
0
][
0
].
params
?.
m
).
toBe
(
'cpu'
);
expect
(
results
).
not
.
toBe
(
null
);
});
it
(
'tag_values(cpu, test) should generate lookup query'
,
()
=>
{
ctx
.
ctrl
.
metricFindQuery
(
'tag_values(cpu, hostname, env=$env)'
).
then
((
data
:
any
)
=>
{
results
=
data
;
});
expect
(
requestOptions
.
url
).
toBe
(
'/api/search/lookup'
);
expect
(
requestOptions
.
params
.
m
).
toBe
(
'cpu{hostname=*,env=$env}'
);
it
(
'tag_values(cpu, test) should generate lookup query'
,
async
()
=>
{
const
{
ds
,
fetchMock
}
=
getTestcontext
();
const
results
=
await
ds
.
metricFindQuery
(
'tag_values(cpu, hostname)'
);
expect
(
fetchMock
).
toHaveBeenCalledTimes
(
1
);
expect
(
fetchMock
.
mock
.
calls
[
0
][
0
].
url
).
toBe
(
'/api/search/lookup'
);
expect
(
fetchMock
.
mock
.
calls
[
0
][
0
].
params
?.
m
).
toBe
(
'cpu{hostname=*}'
);
expect
(
results
).
not
.
toBe
(
null
);
});
it
(
'tag_values(cpu, test) should generate lookup query'
,
()
=>
{
ctx
.
ctrl
.
metricFindQuery
(
'tag_values(cpu, hostname, env=$env, region=$region)'
).
then
((
data
:
any
)
=>
{
results
=
data
;
});
expect
(
requestOptions
.
url
).
toBe
(
'/api/search/lookup'
);
expect
(
requestOptions
.
params
.
m
).
toBe
(
'cpu{hostname=*,env=$env,region=$region}'
);
it
(
'tag_values(cpu, test) should generate lookup query'
,
async
()
=>
{
const
{
ds
,
fetchMock
}
=
getTestcontext
();
const
results
=
await
ds
.
metricFindQuery
(
'tag_values(cpu, hostname, env=$env)'
);
expect
(
fetchMock
).
toHaveBeenCalledTimes
(
1
);
expect
(
fetchMock
.
mock
.
calls
[
0
][
0
].
url
).
toBe
(
'/api/search/lookup'
);
expect
(
fetchMock
.
mock
.
calls
[
0
][
0
].
params
?.
m
).
toBe
(
'cpu{hostname=*,env=$env}'
);
expect
(
results
).
not
.
toBe
(
null
);
});
it
(
'suggest_tagk() should generate api suggest query'
,
()
=>
{
ctx
.
ctrl
.
metricFindQuery
(
'suggest_tagk(foo)'
).
then
((
data
:
any
)
=>
{
results
=
data
;
});
expect
(
requestOptions
.
url
).
toBe
(
'/api/suggest'
);
expect
(
requestOptions
.
params
.
type
).
toBe
(
'tagk'
);
expect
(
requestOptions
.
params
.
q
).
toBe
(
'foo'
);
it
(
'tag_values(cpu, test) should generate lookup query'
,
async
()
=>
{
const
{
ds
,
fetchMock
}
=
getTestcontext
();
const
results
=
await
ds
.
metricFindQuery
(
'tag_values(cpu, hostname, env=$env, region=$region)'
);
expect
(
fetchMock
).
toHaveBeenCalledTimes
(
1
);
expect
(
fetchMock
.
mock
.
calls
[
0
][
0
].
url
).
toBe
(
'/api/search/lookup'
);
expect
(
fetchMock
.
mock
.
calls
[
0
][
0
].
params
?.
m
).
toBe
(
'cpu{hostname=*,env=$env,region=$region}'
);
expect
(
results
).
not
.
toBe
(
null
);
});
it
(
'suggest_tagv() should generate api suggest query'
,
()
=>
{
ctx
.
ctrl
.
metricFindQuery
(
'suggest_tagv(bar)'
).
then
((
data
:
any
)
=>
{
results
=
data
;
});
expect
(
requestOptions
.
url
).
toBe
(
'/api/suggest'
);
expect
(
requestOptions
.
params
.
type
).
toBe
(
'tagv'
);
expect
(
requestOptions
.
params
.
q
).
toBe
(
'bar'
);
it
(
'suggest_tagk() should generate api suggest query'
,
async
()
=>
{
const
{
ds
,
fetchMock
}
=
getTestcontext
();
const
results
=
await
ds
.
metricFindQuery
(
'suggest_tagk(foo)'
);
expect
(
fetchMock
).
toHaveBeenCalledTimes
(
1
);
expect
(
fetchMock
.
mock
.
calls
[
0
][
0
].
url
).
toBe
(
'/api/suggest'
);
expect
(
fetchMock
.
mock
.
calls
[
0
][
0
].
params
?.
type
).
toBe
(
'tagk'
);
expect
(
fetchMock
.
mock
.
calls
[
0
][
0
].
params
?.
q
).
toBe
(
'foo'
);
expect
(
results
).
not
.
toBe
(
null
);
});
});
describe
(
'When interpolating variables'
,
()
=>
{
beforeEach
(()
=>
{
jest
.
clearAllMocks
();
it
(
'suggest_tagv() should generate api suggest query'
,
async
()
=>
{
const
{
ds
,
fetchMock
}
=
getTestcontext
();
ctx
.
mockedTemplateSrv
=
{
replace
:
jest
.
fn
(),
};
const
results
=
await
ds
.
metricFindQuery
(
'suggest_tagv(bar)'
);
ctx
.
ds
=
new
OpenTsDatasource
(
instanceSettings
,
ctx
.
mockedTemplateSrv
);
expect
(
fetchMock
).
toHaveBeenCalledTimes
(
1
);
expect
(
fetchMock
.
mock
.
calls
[
0
][
0
].
url
).
toBe
(
'/api/suggest'
);
expect
(
fetchMock
.
mock
.
calls
[
0
][
0
].
params
?.
type
).
toBe
(
'tagv'
);
expect
(
fetchMock
.
mock
.
calls
[
0
][
0
].
params
?.
q
).
toBe
(
'bar'
);
expect
(
results
).
not
.
toBe
(
null
);
});
});
describe
(
'When interpolating variables'
,
()
=>
{
it
(
'should return an empty array if no queries are provided'
,
()
=>
{
expect
(
ctx
.
ds
.
interpolateVariablesInQueries
([],
{})).
toHaveLength
(
0
);
const
{
ds
}
=
getTestcontext
();
expect
(
ds
.
interpolateVariablesInQueries
([],
{})).
toHaveLength
(
0
);
});
it
(
'should replace correct variables'
,
()
=>
{
const
{
ds
,
templateSrv
}
=
getTestcontext
();
const
variableName
=
'someVar'
;
const
logQuery
:
OpenTsdbQuery
=
{
refId
:
'someRefId'
,
metric
:
`$
${
variableName
}
`
,
};
ctx
.
ds
.
interpolateVariablesInQueries
([
logQuery
],
{});
ds
.
interpolateVariablesInQueries
([
logQuery
],
{});
expect
(
ctx
.
mockedTemplateSrv
.
replace
).
toHaveBeenCalledWith
(
`$
${
variableName
}
`
,
{});
expect
(
ctx
.
mockedT
emplateSrv
.
replace
).
toHaveBeenCalledTimes
(
1
);
expect
(
templateSrv
.
replace
).
toHaveBeenCalledWith
(
'$someVar'
,
{});
expect
(
t
emplateSrv
.
replace
).
toHaveBeenCalledTimes
(
1
);
});
});
});
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