Skip to content

Commit

Permalink
Merge pull request #66 from metrico/jacovinus-multiline-query
Browse files Browse the repository at this point in the history
hotfixes:

fixed starred container inside query history
one line row at query history for large queries (with tooltip)
auto close api selector if API is correct
auto request for logs if API is correct
auto clean logs, labels and matrix data if API is incorrect
  • Loading branch information
jacovinus authored Mar 18, 2022
2 parents 8c7f75a + 371b0e6 commit 47e086b
Show file tree
Hide file tree
Showing 9 changed files with 1,218 additions and 1,236 deletions.
13 changes: 10 additions & 3 deletions src/actions/LoadLabels.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { setLabels } from "./setLabels";
import setLoading from "./setLoading";
import { setApiError } from "./setApiError";
import { errorHandler } from "./errorHandler";
import { setLabelsBrowserOpen } from "./setLabelsBrowserOpen";

export default function loadLabels(apiUrl) {
const origin = window.location.origin;
Expand All @@ -21,9 +22,9 @@ export default function loadLabels(apiUrl) {
mode: "cors",
};

return async (dispatch) => {
return function (dispatch) {

await axios.get(`${url.trim()}/loki/api/v1/labels`, options)
axios.get(`${url.trim()}/loki/api/v1/labels`, options)
?.then((response) => {
if(response){
if(response?.data?.data === []) console.log('no labels found')
Expand All @@ -35,13 +36,19 @@ export default function loadLabels(apiUrl) {
values: [],
}));
dispatch(setLabels(labels || []));
dispatch(setApiError(''))
}

}else {
dispatch(setLoading(false))
dispatch(setApiError('API Not Found, Please adjust API URL'))
dispatch(setLabelsBrowserOpen(true))
dispatch(setLabels([]))
}

}).catch(error => {
console.log(error)
dispatch(setLoading(false))
dispatch(setLoading(false))
const {message,status} = errorHandler(url, error)
dispatch(setLabels([]))

Expand Down
1 change: 0 additions & 1 deletion src/actions/createAlert.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import store from '../store/store';

export const createAlert = ( action) => (dispatch) => {
console.log(action)
const notifications = store.getState().notifications
notifications.push({
message: action.message,
Expand Down
7 changes: 5 additions & 2 deletions src/actions/loadLabelValues.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export default function loadLabelValues(label, labelList, apiUrl) {
}
});
dispatch(setLabels(lsList))
} else if(!response) {
dispatch(setApiError('URL NOT FOUND'))
dispatch(setLabelValues([]))
}

dispatch(setLoading(false));
Expand All @@ -56,8 +59,8 @@ export default function loadLabelValues(label, labelList, apiUrl) {
}).catch(error => {
dispatch(setLoading(false))
const { message } = errorHandler(url, error)
dispatch(setApiError(message))

dispatch(setApiError(message || 'API NOT FOUND'))
dispatch(setLabelValues([]))
console.err(error)
})
}
Expand Down
197 changes: 102 additions & 95 deletions src/actions/loadLogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import setLogs from "./setLogs";
import setLoading from "./setLoading";
import store from "../store/store";
import setMatrixData from "./setMatrixData";
import {nanoid} from 'nanoid'
import { nanoid } from "nanoid";
import { findRangeByLabel } from "../plugins/daterangepicker/utils";
import { setStartTime, setStopTime } from "./";
// *query : LogQl Query
Expand All @@ -14,110 +14,117 @@ import { setStartTime, setStopTime } from "./";
// *direction : Determines sort order of logs. Either forward or backward. Default is backward

export default function loadLogs() {
// const step = 120
// const direction = 'backward'
const localStore = store.getState();
const { query, limit, step, apiUrl, label: rangeLabel } = localStore;
let { start: startTs, stop: stopTs } = localStore;
// const step = 120
// const direction = 'backward'
const localStore = store.getState();
const { query, limit, step, apiUrl, label: rangeLabel } = localStore;
let { start: startTs, stop: stopTs } = localStore;

if (findRangeByLabel(rangeLabel)) {
({ dateStart: startTs, dateEnd: stopTs } =
findRangeByLabel(rangeLabel));
}
if (findRangeByLabel(rangeLabel)) {
({ dateStart: startTs, dateEnd: stopTs } = findRangeByLabel(rangeLabel));
}

store.dispatch(setStartTime(startTs));
store.dispatch(setStopTime(stopTs));
store.dispatch(setStartTime(startTs));
store.dispatch(setStopTime(stopTs));

const origin = window.location.origin;
const url = apiUrl;
const parsedTime ="&start=" +
startTs?.getTime() +
"000000" +
"&end=" +
stopTs?.getTime() +
"000000";
const origin = window.location.origin;
const url = apiUrl;
const parsedTime =
"&start=" +
startTs?.getTime() +
"000000" +
"&end=" +
stopTs?.getTime() +
"000000";

const queryStep = `&step=${step || 120}`;
const queryStep = `&step=${step || 120}`;

const encodedQuery = `${encodeURIComponent(query)}`;
const encodedQuery = `${encodeURIComponent(query)}`;

const getUrl = `${url}/loki/api/v1/query_range?query=${encodedQuery}&limit=${limit}${parsedTime}${queryStep}`;
const getUrl = `${url}/loki/api/v1/query_range?query=${encodedQuery}&limit=${limit}${parsedTime}${queryStep}`;

const options = {
method: "GET",
headers: {
"Content-Type": "application/javascript",
"Access-Control-Allow-Origin": origin,
},
};
const options = {
method: "GET",
headers: {
"Content-Type": "application/javascript",
"Access-Control-Allow-Origin": origin,
},
};

const fromNanoSec = (ts) => parseInt(ts / 1000000); // stream type will come in Nanoseconds
const toMiliseC = (ts) => parseInt(ts * 1000); // matrix type will come without millisec
const getTimestamp = (type) => (ts) =>
type === "streams"
? fromNanoSec(ts)
: type === "matrix"
? toMiliseC(ts)
: ts;
const mapStreams = (streams, messages, type) => {
streams.forEach((stream) => {
stream.values.forEach((log, i) => {
let [ts, text] = log;
messages.push({
type,
timestamp: getTimestamp(type)(ts),
text,
tags:
type === "streams"
? stream.stream
: type === "matrix"
? stream.metric
: {},
showTs: true,
showLabels: false,
id: nanoid(),
});
});
const fromNanoSec = (ts) => parseInt(ts / 1000000); // stream type will come in Nanoseconds
const toMiliseC = (ts) => parseInt(ts * 1000); // matrix type will come without millisec
const getTimestamp = (type) => (ts) =>
type === "streams"
? fromNanoSec(ts)
: type === "matrix"
? toMiliseC(ts)
: ts;
const mapStreams = (streams, messages, type) => {
streams.forEach((stream) => {
stream.values.forEach((log, i) => {
let [ts, text] = log;
messages.push({
type,
timestamp: getTimestamp(type)(ts),
text,
tags:
type === "streams"
? stream.stream
: type === "matrix"
? stream.metric
: {},
showTs: true,
showLabels: false,
id: nanoid(),
});
};
});
});
};

//const mapMatrix
return function (dispatch) {
dispatch(setLoading(true));
dispatch(setLogs([]));
dispatch(setMatrixData([]));
//const mapMatrix
return async function (dispatch) {
dispatch(setLoading(true));
dispatch(setLogs([]));
dispatch(setMatrixData([]));

await axios
.get(getUrl, options)
?.then((response) => {
if (response?.data?.data) {
let messages = [];
const result = response?.data?.data?.result; // array
const type = response?.data?.data?.resultType;
if (type === "streams") {
mapStreams(result, messages, type);
dispatch(setMatrixData([]));
const messSorted = messages?.sort((a, b) =>
a.timestamp < b.timestamp ? 1 : -1
);
if (messSorted) {
dispatch(setLogs(messSorted || []));

axios
.get(getUrl, options)
?.then((response) => {
if (response?.data?.data) {
let messages = [];
const result = response?.data?.data?.result; // array
const type = response?.data?.data?.resultType;
if (type === "streams") {
mapStreams(result, messages, type);
dispatch(setMatrixData([]));
const messSorted = messages?.sort((a, b) => (a.timestamp < b.timestamp) ? 1 : -1);
dispatch(setLogs(messSorted || []));
dispatch(setLoading(false));
}
dispatch(setLoading(false));
}
}

if (type === "matrix") {
const idResult = result.map( m => ({...m,id:nanoid()}))
dispatch(setMatrixData(idResult || []));
dispatch(setLoading(false));
}
dispatch(setLoading(false));
} else {
dispatch(setLogs([]));
dispatch(setMatrixData([]));
dispatch(setLoading(false));
}
dispatch(setLoading(false));
})
.catch((error) => {
dispatch(setLoading(false));
console.log(error);
});
};
if (type === "matrix") {
const idResult = result.map((m) => ({ ...m, id: nanoid() }));
dispatch(setMatrixData(idResult || []));
dispatch(setLoading(false));
}
dispatch(setLoading(false));
} else {
dispatch(setLogs([]));
dispatch(setMatrixData([]));
dispatch(setLoading(false));
}
dispatch(setLoading(false));
})
.catch((error) => {
dispatch(setLogs([]));
dispatch(setMatrixData([]));
dispatch(setLoading(false));
console.log(error);
});
};
}
2 changes: 2 additions & 0 deletions src/components/LabelBrowser/QueryBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export const QueryBar = () => {
const handleInputKeyDown = (e) => {

if (e.code === "Enter" && e.ctrlKey) {
dispatch(setLoading(true))
onSubmit(e);
}
};
Expand All @@ -143,6 +144,7 @@ export const QueryBar = () => {
dispatch(setQueryHistory(historyUpdated));
dispatch(setLabelsBrowserOpen(false));
decodeQuery(query, apiUrl);
dispatch(setLoading(true))
dispatch(loadLogs());
const storedUrl = saveUrl.add({
data: window.location.href,
Expand Down
2 changes: 1 addition & 1 deletion src/components/LabelBrowser/ValuesList.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const ValuesList = (props) => {
const dispatch = useDispatch()
const debug = useSelector((store) => store.debug)
const apiUrl = useSelector((store) => store.apiUrl)
if(debug) console.log('🚧 LOGIC/LabelBrowser/ValuesList', apiUrl)
//if(debug) console.log('🚧 LOGIC/LabelBrowser/ValuesList', apiUrl)
const labelsBrowserOpen = useSelector((store) => store.labelsBrowserOpen)

const CLEAR = "clear"
Expand Down
4 changes: 2 additions & 2 deletions src/components/LogView.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ class LogView extends Component {
matrixData={this.getMatrixForChart()}
/>
) : null}
{this.props.messages.length < 1 &&
{(this.props.messages.length < 1 &&
this.getMatrixForChart().length < 1 &&
!this.props.loading && (
!this.props.loading) && (
<EmptyViewContainer>
{
"Please adjust search parameters and click on ‘Show Logs’ button"
Expand Down
Loading

0 comments on commit 47e086b

Please sign in to comment.