Skip to content

Commit

Permalink
Inverse logic and moving average filter
Browse files Browse the repository at this point in the history
  • Loading branch information
rafal-gorecki committed Jul 18, 2024
1 parent 4e0ddb8 commit 3c077e6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
48 changes: 38 additions & 10 deletions packages/studio-base/src/panels/Bar/Bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,30 @@ import type { Config } from "./types";

import "./styles.css";

class MovingAverage {
#windowSize: number;
#values: number[];
#sum: number;

public constructor(windowSize: number) {
this.#windowSize = windowSize;
this.#values = [];
this.#sum = 0;
}

public addValue(value: number) {
this.#values.push(value);
this.#sum += value;
if (this.#values.length > this.#windowSize) {
this.#sum -= this.#values.shift() ?? 0;
}
}

public getMean() {
return this.#values.length === 0 ? 0 : this.#sum / this.#values.length;
}
}

type Props = {
context: PanelExtensionContext;
};
Expand All @@ -34,6 +58,7 @@ type State = {
parsedPath: MessagePath | undefined;
latestMessage: MessageEvent | undefined;
latestMatchingQueriedData: unknown;
movingAverage: MovingAverage;
error: Error | undefined;
pathParseError: string | undefined;
};
Expand Down Expand Up @@ -70,6 +95,9 @@ function reducer(state: State, action: Action): State {
if (data != undefined) {
latestMatchingQueriedData = data;
latestMessage = message;
if (typeof data === "number") {
state.movingAverage.addValue(data);
}
}
}
}
Expand Down Expand Up @@ -103,6 +131,7 @@ function reducer(state: State, action: Action): State {
path: action.path,
parsedPath: newPath,
latestMatchingQueriedData,
movingAverage: new MovingAverage(100),
error,
pathParseError,
};
Expand All @@ -112,6 +141,7 @@ function reducer(state: State, action: Action): State {
...state,
latestMessage: undefined,
latestMatchingQueriedData: undefined,
movingAverage: new MovingAverage(100),
error: undefined,
};
}
Expand All @@ -138,6 +168,7 @@ export function Bar({ context }: Props): JSX.Element {
parsedPath: parseMessagePath(path),
latestMessage: undefined,
latestMatchingQueriedData: undefined,
movingAverage: new MovingAverage(100),
pathParseError: undefined,
error: undefined,
}),
Expand Down Expand Up @@ -201,20 +232,17 @@ export function Bar({ context }: Props): JSX.Element {
renderDone();
}, [renderDone]);

const rawValue =
typeof state.latestMatchingQueriedData === "number" ||
typeof state.latestMatchingQueriedData === "string"
? Number(state.latestMatchingQueriedData)
: NaN;
const latestMovingAverage = state.movingAverage.getMean();
const rawValue = typeof latestMovingAverage === "number" ? latestMovingAverage : NaN;

const { maxValue, reverse } = config;
const barPercentage = Math.round((100 * rawValue) / maxValue)
const barPercentage = Math.round((100 * rawValue) / maxValue);
const percentage = reverse ? -barPercentage : barPercentage;

const levelHeight = Math.max(Math.min(Math.abs(barPercentage), 100), 0) / 2; // 50% is the max height
const levelHeight = Math.max(Math.min(Math.abs(barPercentage), 100), 0) / 2; // 50% is the max height
const isPositive = reverse ? rawValue < 0 : rawValue >= 0;
const top = isPositive ? `${50 - levelHeight}%` : '50%';
const bottom = isPositive ? '50%' : `${50 - levelHeight}%`;
const top = isPositive ? `${50 - levelHeight}%` : "50%";
const bottom = isPositive ? "50%" : `${50 - levelHeight}%`;

return (
<Stack
Expand All @@ -226,7 +254,7 @@ export function Bar({ context }: Props): JSX.Element {
>
<div className="bar">
<div
className={`level ${isPositive ? 'positive' : 'negative'}`}
className={`level ${isPositive ? "positive" : "negative"}`}
style={{ height: `${levelHeight}%`, top, bottom }}
></div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion packages/studio-base/src/panels/EStop/EStop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ function EStopContent(
return;
}

const serviceName = eStopState === "go" ? config.stopServiceName : config.goServiceName;
const serviceName = eStopState === "go" ? config.goServiceName : config.stopServiceName;

if (!serviceName) {
setReqState({ status: "error", value: "Service name is not configured" });
Expand Down

0 comments on commit 3c077e6

Please sign in to comment.