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
352961b3
Unverified
Commit
352961b3
authored
Oct 08, 2018
by
David
Committed by
GitHub
Oct 08, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #13540 from grafana/davkal/explore-compact-url-state
Explore: compact state URLs
parents
6fcc062b
5ec9adb7
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
82 additions
and
7 deletions
+82
-7
public/app/core/utils/explore.test.ts
+51
-2
public/app/core/utils/explore.ts
+23
-2
public/app/features/explore/Explore.tsx
+0
-2
public/app/features/explore/Wrapper.tsx
+8
-1
No files found.
public/app/core/utils/explore.test.ts
View file @
352961b3
...
...
@@ -36,14 +36,40 @@ describe('state functions', () => {
range
:
DEFAULT_RANGE
,
});
});
it
(
'returns a valid Explore state from URL parameter'
,
()
=>
{
const
paramValue
=
'%7B"datasource":"Local","queries":%5B%7B"query":"metric"%7D%5D,"range":%7B"from":"now-1h","to":"now"%7D%7D'
;
expect
(
parseUrlState
(
paramValue
)).
toMatchObject
({
datasource
:
'Local'
,
queries
:
[{
query
:
'metric'
}],
range
:
{
from
:
'now-1h'
,
to
:
'now'
,
},
});
});
it
(
'returns a valid Explore state from a compact URL parameter'
,
()
=>
{
const
paramValue
=
'%5B"now-1h","now","Local","metric"%5D'
;
expect
(
parseUrlState
(
paramValue
)).
toMatchObject
({
datasource
:
'Local'
,
queries
:
[{
query
:
'metric'
}],
range
:
{
from
:
'now-1h'
,
to
:
'now'
,
},
});
});
});
describe
(
'serializeStateToUrlParam'
,
()
=>
{
it
(
'returns url parameter value for a state object'
,
()
=>
{
const
state
=
{
...
DEFAULT_EXPLORE_STATE
,
datasourceName
:
'foo'
,
range
:
{
from
:
'now
-
5h'
,
from
:
'now
-
5h'
,
to
:
'now'
,
},
queries
:
[
...
...
@@ -57,10 +83,33 @@ describe('state functions', () => {
};
expect
(
serializeStateToUrlParam
(
state
)).
toBe
(
'{"datasource":"foo","queries":[{"query":"metric{test=
\\
"a/b
\\
"}"},'
+
'{"query":"super{foo=
\\
"x/z
\\
"}"}],"range":{"from":"now - 5h","to":"now"}}'
'{"query":"super{foo=
\\
"x/z
\\
"}"}],"range":{"from":"now-5h","to":"now"}}'
);
});
it
(
'returns url parameter value for a state object'
,
()
=>
{
const
state
=
{
...
DEFAULT_EXPLORE_STATE
,
datasourceName
:
'foo'
,
range
:
{
from
:
'now-5h'
,
to
:
'now'
,
},
queries
:
[
{
query
:
'metric{test="a/b"}'
,
},
{
query
:
'super{foo="x/z"}'
,
},
],
};
expect
(
serializeStateToUrlParam
(
state
,
true
)).
toBe
(
'["now-5h","now","foo","metric{test=
\\
"a/b
\\
"}","super{foo=
\\
"x/z
\\
"}"]'
);
});
});
describe
(
'interplay'
,
()
=>
{
it
(
'can parse the serialized state into the original state'
,
()
=>
{
const
state
=
{
...
...
public/app/core/utils/explore.ts
View file @
352961b3
...
...
@@ -60,7 +60,20 @@ export async function getExploreUrl(
export
function
parseUrlState
(
initial
:
string
|
undefined
):
ExploreUrlState
{
if
(
initial
)
{
try
{
return
JSON
.
parse
(
decodeURI
(
initial
));
const
parsed
=
JSON
.
parse
(
decodeURI
(
initial
));
if
(
Array
.
isArray
(
parsed
))
{
if
(
parsed
.
length
<=
3
)
{
throw
new
Error
(
'Error parsing compact URL state for Explore.'
);
}
const
range
=
{
from
:
parsed
[
0
],
to
:
parsed
[
1
],
};
const
datasource
=
parsed
[
2
];
const
queries
=
parsed
.
slice
(
3
).
map
(
query
=>
({
query
}));
return
{
datasource
,
queries
,
range
};
}
return
parsed
;
}
catch
(
e
)
{
console
.
error
(
e
);
}
...
...
@@ -68,11 +81,19 @@ export function parseUrlState(initial: string | undefined): ExploreUrlState {
return
{
datasource
:
null
,
queries
:
[],
range
:
DEFAULT_RANGE
};
}
export
function
serializeStateToUrlParam
(
state
:
ExploreState
):
string
{
export
function
serializeStateToUrlParam
(
state
:
ExploreState
,
compact
?:
boolean
):
string
{
const
urlState
:
ExploreUrlState
=
{
datasource
:
state
.
datasourceName
,
queries
:
state
.
queries
.
map
(
q
=>
({
query
:
q
.
query
})),
range
:
state
.
range
,
};
if
(
compact
)
{
return
JSON
.
stringify
([
urlState
.
range
.
from
,
urlState
.
range
.
to
,
urlState
.
datasource
,
...
urlState
.
queries
.
map
(
q
=>
q
.
query
),
]);
}
return
JSON
.
stringify
(
urlState
);
}
public/app/features/explore/Explore.tsx
View file @
352961b3
...
...
@@ -275,7 +275,6 @@ export class Explore extends React.PureComponent<ExploreProps, ExploreState> {
const
{
onChangeSplit
}
=
this
.
props
;
if
(
onChangeSplit
)
{
onChangeSplit
(
false
);
this
.
saveState
();
}
};
...
...
@@ -292,7 +291,6 @@ export class Explore extends React.PureComponent<ExploreProps, ExploreState> {
if
(
onChangeSplit
)
{
const
state
=
this
.
cloneState
();
onChangeSplit
(
true
,
state
);
this
.
saveState
();
}
};
...
...
public/app/features/explore/Wrapper.tsx
View file @
352961b3
...
...
@@ -38,10 +38,17 @@ export class Wrapper extends Component<WrapperProps, WrapperState> {
onChangeSplit
=
(
split
:
boolean
,
splitState
:
ExploreState
)
=>
{
this
.
setState
({
split
,
splitState
});
// When closing split, remove URL state for split part
if
(
!
split
)
{
delete
this
.
urlStates
[
STATE_KEY_RIGHT
];
this
.
props
.
updateLocation
({
query
:
this
.
urlStates
,
});
}
};
onSaveState
=
(
key
:
string
,
state
:
ExploreState
)
=>
{
const
urlState
=
serializeStateToUrlParam
(
state
);
const
urlState
=
serializeStateToUrlParam
(
state
,
true
);
this
.
urlStates
[
key
]
=
urlState
;
this
.
props
.
updateLocation
({
query
:
this
.
urlStates
,
...
...
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