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
382a12c0
Commit
382a12c0
authored
Jan 19, 2019
by
Andrej Ocenas
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add loop counter for full refresh in playlist
parent
d8430448
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
118 additions
and
7 deletions
+118
-7
public/app/features/playlist/playlist_srv.ts
+15
-7
public/app/features/playlist/specs/playlist_srv.test.ts
+103
-0
No files found.
public/app/features/playlist/playlist_srv.ts
View file @
382a12c0
...
@@ -4,12 +4,13 @@ import appEvents from 'app/core/app_events';
...
@@ -4,12 +4,13 @@ import appEvents from 'app/core/app_events';
import
_
from
'lodash'
;
import
_
from
'lodash'
;
import
{
toUrlParams
}
from
'app/core/utils/url'
;
import
{
toUrlParams
}
from
'app/core/utils/url'
;
class
PlaylistSrv
{
export
class
PlaylistSrv
{
private
cancelPromise
:
any
;
private
cancelPromise
:
any
;
private
dashboards
:
any
;
private
dashboards
:
Array
<
{
uri
:
string
}
>
;
private
index
:
number
;
private
index
:
number
;
private
interval
:
any
;
private
interval
:
number
;
private
startUrl
:
string
;
private
startUrl
:
string
;
private
numberOfLoops
=
0
;
isPlaying
:
boolean
;
isPlaying
:
boolean
;
/** @ngInject */
/** @ngInject */
...
@@ -20,8 +21,15 @@ class PlaylistSrv {
...
@@ -20,8 +21,15 @@ class PlaylistSrv {
const
playedAllDashboards
=
this
.
index
>
this
.
dashboards
.
length
-
1
;
const
playedAllDashboards
=
this
.
index
>
this
.
dashboards
.
length
-
1
;
if
(
playedAllDashboards
)
{
if
(
playedAllDashboards
)
{
window
.
location
.
href
=
this
.
startUrl
;
this
.
numberOfLoops
++
;
return
;
// This does full reload of the playlist to keep memory in check due to existing leaks but at the same time
// we do not want page to flicker after each full loop.
if
(
this
.
numberOfLoops
>=
3
)
{
window
.
location
.
href
=
this
.
startUrl
;
return
;
}
this
.
index
=
0
;
}
}
const
dash
=
this
.
dashboards
[
this
.
index
];
const
dash
=
this
.
dashboards
[
this
.
index
];
...
@@ -46,8 +54,8 @@ class PlaylistSrv {
...
@@ -46,8 +54,8 @@ class PlaylistSrv {
this
.
index
=
0
;
this
.
index
=
0
;
this
.
isPlaying
=
true
;
this
.
isPlaying
=
true
;
this
.
backendSrv
.
get
(
`/api/playlists/
${
playlistId
}
`
).
then
(
playlist
=>
{
return
this
.
backendSrv
.
get
(
`/api/playlists/
${
playlistId
}
`
).
then
(
playlist
=>
{
this
.
backendSrv
.
get
(
`/api/playlists/
${
playlistId
}
/dashboards`
).
then
(
dashboards
=>
{
return
this
.
backendSrv
.
get
(
`/api/playlists/
${
playlistId
}
/dashboards`
).
then
(
dashboards
=>
{
this
.
dashboards
=
dashboards
;
this
.
dashboards
=
dashboards
;
this
.
interval
=
kbn
.
interval_to_ms
(
playlist
.
interval
);
this
.
interval
=
kbn
.
interval_to_ms
(
playlist
.
interval
);
this
.
next
();
this
.
next
();
...
...
public/app/features/playlist/specs/playlist_srv.test.ts
0 → 100644
View file @
382a12c0
import
{
PlaylistSrv
}
from
'../playlist_srv'
;
const
dashboards
=
[{
uri
:
'dash1'
},
{
uri
:
'dash2'
}];
const
createPlaylistSrv
=
():
[
PlaylistSrv
,
{
url
:
jest
.
MockInstance
<
any
>
}]
=>
{
const
mockBackendSrv
=
{
get
:
jest
.
fn
(
url
=>
{
switch
(
url
)
{
case
'/api/playlists/1'
:
return
Promise
.
resolve
({
interval
:
'1s'
});
case
'/api/playlists/1/dashboards'
:
return
Promise
.
resolve
(
dashboards
);
default
:
throw
new
Error
(
`Unexpected url=
${
url
}
`
);
}
}),
};
const
mockLocation
=
{
url
:
jest
.
fn
(),
search
:
()
=>
({}),
};
const
mockTimeout
=
jest
.
fn
();
(
mockTimeout
as
any
).
cancel
=
jest
.
fn
();
return
[
new
PlaylistSrv
(
mockLocation
,
mockTimeout
,
mockBackendSrv
),
mockLocation
];
};
const
mockWindowLocation
=
():
[
jest
.
MockInstance
<
any
>
,
()
=>
void
]
=>
{
const
oldLocation
=
window
.
location
;
const
hrefMock
=
jest
.
fn
();
// JSDom defines window in a way that you cannot tamper with location so this seems to be the only way to change it.
// https://github.com/facebook/jest/issues/5124#issuecomment-446659510
delete
window
.
location
;
window
.
location
=
{}
as
any
;
// Only mocking href as that is all this test needs, but otherwise there is lots of things missing, so keep that
// in mind if this is reused.
Object
.
defineProperty
(
window
.
location
,
'href'
,
{
set
:
hrefMock
,
get
:
hrefMock
,
});
const
unmock
=
()
=>
{
window
.
location
=
oldLocation
;
};
return
[
hrefMock
,
unmock
];
};
describe
(
'PlaylistSrv'
,
()
=>
{
let
srv
:
PlaylistSrv
;
let
mockLocationService
:
{
url
:
jest
.
MockInstance
<
any
>
};
let
hrefMock
:
jest
.
MockInstance
<
any
>
;
let
unmockLocation
:
()
=>
void
;
const
initialUrl
=
'http://localhost/playlist'
;
beforeEach
(()
=>
{
[
srv
,
mockLocationService
]
=
createPlaylistSrv
();
[
hrefMock
,
unmockLocation
]
=
mockWindowLocation
();
// This will be cached in the srv when start() is called
hrefMock
.
mockReturnValue
(
initialUrl
);
});
afterEach
(()
=>
{
unmockLocation
();
});
it
(
'runs all dashboards in cycle and reloads page after 3 cycles'
,
async
()
=>
{
await
srv
.
start
(
1
);
for
(
let
i
=
0
;
i
<
6
;
i
++
)
{
expect
(
mockLocationService
.
url
).
toHaveBeenLastCalledWith
(
`dashboard/
${
dashboards
[
i
%
2
].
uri
}
?`
);
srv
.
next
();
}
expect
(
hrefMock
).
toHaveBeenCalledTimes
(
2
);
expect
(
hrefMock
).
toHaveBeenLastCalledWith
(
initialUrl
);
});
it
(
'keeps the refresh counter value after restarting'
,
async
()
=>
{
await
srv
.
start
(
1
);
// 1 complete loop
for
(
let
i
=
0
;
i
<
3
;
i
++
)
{
expect
(
mockLocationService
.
url
).
toHaveBeenLastCalledWith
(
`dashboard/
${
dashboards
[
i
%
2
].
uri
}
?`
);
srv
.
next
();
}
srv
.
stop
();
await
srv
.
start
(
1
);
// Another 2 loops
for
(
let
i
=
0
;
i
<
4
;
i
++
)
{
expect
(
mockLocationService
.
url
).
toHaveBeenLastCalledWith
(
`dashboard/
${
dashboards
[
i
%
2
].
uri
}
?`
);
srv
.
next
();
}
expect
(
hrefMock
).
toHaveBeenCalledTimes
(
3
);
expect
(
hrefMock
).
toHaveBeenLastCalledWith
(
initialUrl
);
});
});
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