Skip to content

Commit

Permalink
Merge pull request #84 from metrico/jacovinus-feature-tables
Browse files Browse the repository at this point in the history
Fix: labels and logs styles
  • Loading branch information
jacovinus authored Apr 25, 2022
2 parents b885bad + 98b3df4 commit 955ff96
Show file tree
Hide file tree
Showing 16 changed files with 344 additions and 273 deletions.
29 changes: 17 additions & 12 deletions src/actions/loadLabels.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import axios from "axios";
import { setLabels } from "./setLabels";
import setLoading from "./setLoading";
import { setApiError } from "./setApiError";
import { createAlert } from "./createAlert";

export default function loadLabels(apiUrl) {
const origin = window.location.origin;
Expand All @@ -26,30 +27,34 @@ export default function loadLabels(apiUrl) {
.get(`${url.trim()}/loki/api/v1/labels`, options)
?.then((response) => {
if (response) {
if (response?.data?.data === [])
if (response?.data?.data?.length > 0) {
const labels = response?.data?.data
.sort()
.map((label) => ({
let labels = response?.data?.data
?.sort()
?.map((label) => ({
name: label,
selected: false,
values: [],
}));
dispatch(setLabels(labels || []));

dispatch(setApiError(""));
if (labels) {
dispatch(setLabels(labels || []));
dispatch(setApiError(""));
dispatch(setLoading(false));
}
} else {
dispatch(createAlert({type:'info',message:'No labels available for this API'}))
}
} else {
dispatch(setLoading(false));
dispatch(
setApiError("")
);

dispatch(setApiError(""));
dispatch(setLabels([]));

}
})
.catch((error) => {
console.log(error);
dispatch(createAlert({
type:"error",
message:'API NOT FOUND'
}))
dispatch(setLoading(false));
dispatch(setLabels([]));
});
Expand Down
15 changes: 11 additions & 4 deletions src/actions/loadLogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,29 @@ export default function loadLogs() {
} = localStore;
let { start: startTs, stop: stopTs } = localStore;

function adjustForTimezone(date){

var timeOffsetInMS = date.getTimezoneOffset() * 60000;
date.setTime(date.getTime() + timeOffsetInMS);
return date
}

function getTimeParsed(time) {
return time.getTime() + "000000";
}

const timeZone = new Date().getTimezoneOffset()
const parsedStart = getTimeParsed(startTs);
const parsedStop = getTimeParsed(stopTs);

const parsedTime = "&start=" + (from || parsedStart) + "&end=" + (to || parsedStop);

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

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

const origin = window.location.origin;
const url = apiUrl;
const queryStep = `&step=${step || 120}`;
Expand Down Expand Up @@ -113,13 +120,13 @@ export default function loadLogs() {
dispatch(setMatrixData(idResult || []));
dispatch(setLoading(false));
}
dispatch(setLoading(false));
// dispatch(setLoading(false));
} else {
dispatch(setLogs([]));
dispatch(setMatrixData([]));
dispatch(setLoading(false));
}
dispatch(setLoading(false));
// dispatch(setLoading(false));
})
.catch((error) => {

Expand Down
145 changes: 73 additions & 72 deletions src/components/DataView/DataView.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,73 @@
import React, { Component } from "react";
import { connect } from "react-redux";

import { DataViewCont, DataViewStyled, Loader } from "./styled";

import ClokiChart from "../../plugins/charts";
import QueryHistory from "../../plugins/queryhistory";
import LogsRow from "./LogsRow";
import EmptyView from "./EmptyView";

class DataView extends Component {
constructor(props) {
super(props);
this.state = {
limit: props.limit || 100,
messages: props.messages || [],
matrixData: props.matrixData || [],
loading: false,
};
}

getMatrixForChart = () => {
return this.props.matrixData;
};
getLimit = () => {
return this.props.limit;
};

render() {
return (
<DataViewStyled>
<DataViewCont>
{this.props.messages.length > 0 &&
this.getMatrixForChart().length < 1
? this.props.messages.map((message, key) => (
<LogsRow
message={message}
toggleTagsActive={this.toggleTagsActive}
key={key}
/>
))
: null}

{this.getMatrixForChart().length > 0 ? (
<ClokiChart
chartLimit={this.getLimit()}
matrixData={this.getMatrixForChart()}
/>
) : null}
{this.props.messages.length < 1 &&
this.getMatrixForChart().length < 1 &&
!this.props.loading && <EmptyView />}
<QueryHistory />
{this.props.loading && <Loader />}
</DataViewCont>
</DataViewStyled>
);
}
}

const mapStateToProps = (state) => {
return {
messages: state.logs,
start: state.start,
stop: state.stop,
limit: state.limit,
loading: state.loading,
matrixData: state.matrixData,
};
};

export default connect(mapStateToProps)(DataView);
import React, { Component } from "react";
import { connect } from "react-redux";

import { DataViewCont, DataViewStyled, Loader } from "./styled";
import ClokiChart from "../../plugins/charts";
import QueryHistory from "../../plugins/queryhistory";
import LogsRow from "./LogsRow";
import EmptyView from "./EmptyView";
// import Table from "../../plugins/tables/Table";

class DataView extends Component {
constructor(props) {
super(props);
this.state = {
limit: props.limit || 100,
messages: props.messages || [],
matrixData: props.matrixData || [],
loading: false,
};
}

getMatrixForChart = () => {
return this.props.matrixData;
};
getLimit = () => {
return this.props.limit;
};

render() {
return (
<DataViewStyled>
<DataViewCont>
{/* <Table/> */}
{this.props.messages.length > 0 &&
this.getMatrixForChart().length < 1
? this.props.messages.map((message, key) => (
<LogsRow
message={message}
toggleTagsActive={this.toggleTagsActive}
key={key}
/>
))
: null}

{this.getMatrixForChart().length > 0 ? (
<ClokiChart
chartLimit={this.getLimit()}
matrixData={this.getMatrixForChart()}
/>
) : null}
{this.props.messages.length < 1 &&
this.getMatrixForChart().length < 1 &&
!this.props.loading && <EmptyView />}
<QueryHistory />
{this.props.loading && <Loader />}
</DataViewCont>
</DataViewStyled>
);
}
}

const mapStateToProps = (state) => {
return {
messages: state.logs,
start: state.start,
stop: state.stop,
limit: state.limit,
loading: state.loading,
matrixData: state.matrixData,
};
};

export default connect(mapStateToProps)(DataView);
77 changes: 39 additions & 38 deletions src/components/DataView/LogsRow.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
import { formatDate, getRowColor, toggleActiveStyles } from "./helpers";
import { LogRow, RowLogContent, RowTimestamp } from "./styled";
import ValueTags from "./ValueTags";
import { useSelector, useDispatch } from "react-redux";
import setLogs from "../../actions/setLogs";

export default function LogsRow({ message }) {
const dispatch = useDispatch();
const messages = useSelector((store) => store.logs);

function toggleTagsActive(idx) {
let arrCopy = [...messages];
arrCopy.forEach((entry) => {
if (entry.id === idx) {
entry.showLabels = entry.showLabels ? false : true;
}
});
dispatch(setLogs(arrCopy));
}

return (
<LogRow
rowColor={getRowColor(message.tags)}
onClick={(e) => {
toggleTagsActive(message.id);
}}
>
<RowTimestamp>{formatDate(message.timestamp)}</RowTimestamp>
<RowLogContent>{message.text}</RowLogContent>

{message.tags && (
<div className={toggleActiveStyles(message)}>
<ValueTags tags={message.tags} />
</div>
)}
</LogRow>
);
}
import { formatDate, getRowColor, toggleActiveStyles } from "./helpers";
import { LogRow, RowLogContent, RowTimestamp } from "./styled";
import ValueTags from "./ValueTags";
import { useSelector, useDispatch } from "react-redux";
import setLogs from "../../actions/setLogs";

export default function LogsRow({ message }) {
const dispatch = useDispatch();
const messages = useSelector((store) => store.logs);

function toggleTagsActive(idx) {
let arrCopy = [...messages];
arrCopy.forEach((entry) => {
if (entry.id === idx) {
entry.showLabels = entry.showLabels ? false : true;
}
});
dispatch(setLogs(arrCopy));
}

return (
<LogRow
rowColor={getRowColor(message.tags)}
onClick={(e) => {
toggleTagsActive(message.id);
}}
>
<div className="log-ts-row">
<RowTimestamp>{formatDate(message.timestamp)}</RowTimestamp>
<RowLogContent>{message.text}</RowLogContent>
</div>
{message.tags && (
<div className={toggleActiveStyles(message)}>
<ValueTags tags={message.tags} />
</div>
)}
</LogRow>
);
}
16 changes: 8 additions & 8 deletions src/components/DataView/consts/consts.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export const TAGS_LEVEL = {
critical: ["emerg", "fatal", "alert", "crit", "critical"],
error: ["err", "eror", "error", "warning"],
warning: ["warn", "warning"],
info: ["info", "information", "notice"],
debug: ["dbug", "debug"],
trace: ["trace"],
};
export const TAGS_LEVEL = {
critical: ["emerg", "fatal", "alert", "crit", "critical"],
error: ["err", "eror", "error"],
warning: ["warn", "warning"],
info: ["info", "information", "notice"],
debug: ["dbug", "debug"],
trace: ["trace"],
};
49 changes: 25 additions & 24 deletions src/components/DataView/helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import { LEVEL_COLORS as lColors } from "../theme/theme";
import { TAGS_LEVEL as tLevel } from "../consts/consts";
import * as moment from "moment";

export function getRowColor(tags) {
if (tags?.["level"]) {
const level = Object.keys(tLevel).find((level) =>
tLevel[level].includes(tags.level.toLowerCase())
);
return lColors[level];
} else {
return lColors["unknown"];
}
}

export function toggleActiveStyles(idx) {
return idx.showLabels
? "value-tags-container labelsActive"
: "value-tags-container labelsInactive";
}

export function formatDate(timestamp) {
return moment(parseInt(timestamp)).format("YYYY-MM-DD HH:mm:ss.SSS UTC");
}
import { LEVEL_COLORS as lColors } from "../theme/theme";
import { TAGS_LEVEL as tLevel } from "../consts/consts";
import * as moment from "moment";

export function getRowColor(tags) {
const type = tags?.severity || tags?.level;
if (type) {
const level = Object.keys(tLevel).find((level) =>
tLevel[level].includes(type.toLowerCase())
);
return lColors[level];
} else {
return lColors["unknown"];
}
}

export function toggleActiveStyles(idx) {
return idx.showLabels
? "value-tags-container labelsActive"
: "value-tags-container labelsInactive";
}

export function formatDate(timestamp) {
return moment(parseInt(timestamp)).format("YYYY-MM-DD HH:mm:ss.SSS UTC");
}
Loading

0 comments on commit 955ff96

Please sign in to comment.