Skip to content

Commit

Permalink
multiple tags and request with query
Browse files Browse the repository at this point in the history
  • Loading branch information
Loggy committed Jul 28, 2021
1 parent 98721fa commit c52bbae
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 27 deletions.
88 changes: 67 additions & 21 deletions webapp/javascript/components/TagsBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,76 @@ import { connect } from "react-redux";
import "react-dom";
import { Menu, SubMenu, MenuItem, MenuButton } from "@szhsin/react-menu";

import { fetchTags, fetchTagValues } from "../redux/actions";
import { fetchTags, fetchTagValues, updateTags } from "../redux/actions";
import history from "../util/history";

function TagsBar({ tags, fetchTags, fetchTagValues, tagValuesLoading }) {
const [tagsValue, setTagsValue] = useState("");
function TagsBar({
tags,
fetchTags,
fetchTagValues,
updateTags,
tagValuesLoading,
labels,
}) {
const [tagsValue, setTagsValue] = useState("{}");

const loadTagValues = (tag) => {
if (tags[tag] && !tags[tag].length && tagValuesLoading !== tag) {
fetchTagValues(tag);
}
};

const onTagsValueChange = (tag, tagValue) => {
if (!tagsValue.includes(tag)) {
setTagsValue(
tagsValue.replace(
"}",
`${tagsValue === "{}" ? "" : ","}${tag}=${tagValue}}`
)
);
} else {
const tagPairs = tagsValue.replace(/[{}]/g, "").split(",");
tagPairs.forEach((pair, i) => {
if (pair.startsWith(tag)) {
tagPairs[i] = `${tag}=${tagValue}`;
}
});
setTagsValue(`{${tagPairs.join(",")}}`);
}
};

useEffect(() => {
fetchTags();
}, []);

useEffect(() => {
const url = new URL(window.location.href);
const tagsParams = [];
Object.keys(tags).forEach((tag) => {
if (url.search.includes(tag)) {
loadTagValues(tag);
setTagsValue(`{${tag}=${url.searchParams.get(tag)}}`);
tagsParams.push(`${tag}=${url.searchParams.get(tag)}`);
}
});
setTagsValue(`{${tagsParams.join(",")}}`);
}, [tags]);

useEffect(() => {
const [name, value] = tagsValue.replace(/[{}]/g, "").split("=");
const tagPairs = tagsValue.replace(/[{}]/g, "").split(",");
const url = new URL(window.location.href);
if (value) {
url.searchParams.set(name, value);
} else {
url.searchParams.delete(name);
}
const tagsUpdater = [];
tagPairs.forEach((pair) => {
const [name, value] = pair.split("=");
if (value) {
url.searchParams.set(name, value);
tagsUpdater.push({ name, value });
} else {
url.searchParams.delete(name);
}
});
history.push(url.search);
updateTags(tagsUpdater);
console.log(tagsUpdater);
}, [tagsValue]);

return (
Expand All @@ -56,7 +91,6 @@ function TagsBar({ tags, fetchTags, fetchTagValues, tagValuesLoading }) {
loadTagValues(tag);
return tag;
}}
onChange={(e) => loadTagValues(e.value)}
className="active"
>
{tagValuesLoading === tag ? (
Expand All @@ -66,7 +100,7 @@ function TagsBar({ tags, fetchTags, fetchTagValues, tagValuesLoading }) {
<MenuItem
key={tagValue}
value={tagValue}
onClick={(e) => setTagsValue(`{${tag}=${e.value}}`)}
onClick={(e) => onTagsValueChange(tag, e.value)}
className={tagsValue.includes(tagValue) ? "active" : ""}
>
{tagValue}
Expand All @@ -76,16 +110,28 @@ function TagsBar({ tags, fetchTags, fetchTagValues, tagValuesLoading }) {
</SubMenu>
))}
</Menu>
<input
className="tags-input"
type="text"
value={tagsValue}
onChange={(e) => setTagsValue(e.target.value)}
/>
<div className="tags-query">
<span className="tags-app-name">
{labels && labels.find((label) => label.name === "__name__").value}
</span>
<pre className="tags-highlighted highlight-promql" aria-hidden="true">
<code className="language-html" id="highlighting-content">
{tagsValue}
</code>
</pre>
<input
className="tags-input"
type="text"
value={tagsValue}
onChange={(e) => setTagsValue(e.target.value)}
/>
</div>
</div>
);
}

export default connect((state) => state, { fetchTags, fetchTagValues })(
TagsBar
);
export default connect((state) => state, {
fetchTags,
fetchTagValues,
updateTags,
})(TagsBar);
1 change: 1 addition & 0 deletions webapp/javascript/redux/actionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ export const REQUEST_TAGS = "REQUEST_TAGS";
export const RECEIVE_TAGS = "RECEIVE_TAGS";
export const REQUEST_TAG_VALUES = "REQUEST_TAG_VALUES";
export const RECEIVE_TAG_VALUES = "RECEIVE_TAG_VALUES";
export const UPDATE_TAGS = "UPDATE_TAGS";
3 changes: 3 additions & 0 deletions webapp/javascript/redux/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
SET_LEFT_UNTIL,
SET_RIGHT_FROM,
SET_RIGHT_UNTIL,
UPDATE_TAGS,
} from "./actionTypes";

export const setDateRange = (from, until) => ({
Expand Down Expand Up @@ -108,6 +109,8 @@ export const receiveTagValues = (values, tag) => ({
payload: { values, tag },
});

export const updateTags = (tags) => ({ type: UPDATE_TAGS, payload: { tags } });

export const requestNames = () => ({ type: REQUEST_NAMES, payload: {} });

export const receiveNames = (names) => ({
Expand Down
11 changes: 10 additions & 1 deletion webapp/javascript/redux/reducers/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
SET_RIGHT_FROM,
SET_LEFT_UNTIL,
SET_RIGHT_UNTIL,
UPDATE_TAGS,
} from "../actionTypes";

const defaultName = window.initialState.appNames.find(
Expand All @@ -41,6 +42,7 @@ const initialState = {
isJSONLoading: false,
maxNodes: 1024,
tags: [],
selectedTags: [],
};

window.uniqBy = uniqBy;
Expand Down Expand Up @@ -151,7 +153,9 @@ export default function (state = initialState, action) {
...state,
areTagsLoading: false,
tags: action.payload.tags.reduce((acc, tag) => {
acc[tag] = [];
if (tag !== "__name__") {
acc[tag] = [];
}
return acc;
}, {}),
};
Expand All @@ -170,6 +174,11 @@ export default function (state = initialState, action) {
[action.payload.tag]: action.payload.values,
},
};
case UPDATE_TAGS:
return {
...state,
selectedTags: action.payload.tags,
};
case REQUEST_NAMES:
return {
...state,
Expand Down
7 changes: 3 additions & 4 deletions webapp/javascript/util/updateRequests.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ export function buildRenderURL(state, fromOverride=null, untilOverride=null, sid
const nameLabel = state.labels.find((x) => x.name == '__name__');

if (nameLabel) {
url += `&name=${nameLabel.value}{`;
url += `&query=${nameLabel.value}{`;
} else {
url += '&name=unknown{';
url += '&query=unknown{';
}

// TODO: replace this so this is a real utility function
url += state.labels.filter((x) => x.name != '__name__').map((x) => `${x.name}=${x.value}`).join(',');
url += state.selectedTags.map((x) => `${x.name}="${x.value}"`).join(',');
url += '}';

if (state.refreshToken) {
Expand Down
2 changes: 1 addition & 1 deletion webapp/sass/components/labels.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

select {
margin-right: 10px;
max-width: 200px;
width: fit-content;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
Expand Down
37 changes: 37 additions & 0 deletions webapp/sass/components/tagsbar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,45 @@
}
}

.tags-query {
flex-grow: 1;
display: flex;
align-items: center;
}
.tags-app-name {
position: relative;
border-top-left-radius: 4px;
border-bottom-left-radius: 4px;
background-color: #272727;
border: 1px solid #4d4d4d;
height: 100%;
display: flex;
align-items: center;
padding: 0 5px;
border-right: none;
}
.tags-input {
flex-grow: 1;
border-top-left-radius: 0px;
border-bottom-left-radius: 0px;
caret-color: #fff;
color: rgba(0,0,0,0);
letter-spacing: 0;
font-family: arial;
font-size: 16px;
}

.tags-highlighted{
height: 0;
width: 0;
margin-top: -12px;
margin-right: -7px;
z-index: 1;
margin-left: 5px;
pointer-events: none;
code {
font-size: 16px;
font-family: arial;
}
}
}

0 comments on commit c52bbae

Please sign in to comment.