Skip to content

Commit

Permalink
avg / rate calculation fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jpinsonneau committed Apr 13, 2022
1 parent d590fcf commit 5777538
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
12 changes: 10 additions & 2 deletions web/src/api/loki.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { TimeRange } from '../utils/datetime';
import { MetricFunction } from '../model/flow-query';
import { cyrb53 } from '../utils/hash';
import { Fields, Labels, Record } from './ipfix';
Expand Down Expand Up @@ -53,7 +54,14 @@ export interface TopologyMetrics {
/* calculate total for selected function
* loki will return matrix with multiple values (one per step = 60s)
*/
export const calculateMatrixTotals = (tm: TopologyMetrics, mf: MetricFunction) => {
export const calculateMatrixTotals = (tm: TopologyMetrics, mf: MetricFunction, range: number | TimeRange) => {
let rangeInMinutes: number;
if (typeof range === 'number') {
rangeInMinutes = range / 60;
} else {
rangeInMinutes = (range.from - range.to) / (1000 * 60);
}

tm.total = 0;
switch (mf) {
case 'max':
Expand All @@ -62,7 +70,7 @@ export const calculateMatrixTotals = (tm: TopologyMetrics, mf: MetricFunction) =
case 'avg':
case 'rate':
tm.values.forEach(v => (tm.total += Number(v[1])));
tm.total = tm.total / tm.values.length;
tm.total = tm.total / rangeInMinutes;
break;
case 'sum':
default:
Expand Down
5 changes: 3 additions & 2 deletions web/src/api/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { FlowQuery, MetricFunction } from '../model/flow-query';
import { Record } from './ipfix';
import { calculateMatrixTotals, parseStream, StreamResult, TopologyMetrics } from './loki';
import { Config, defaultConfig } from '../model/config';
import { TimeRange } from '../utils/datetime';

const host = '/api/proxy/plugin/network-observability-plugin/backend/';

Expand Down Expand Up @@ -39,13 +40,13 @@ export const getResources = (namespace: string, kind: string): Promise<string[]>
});
};

export const getTopology = (params: FlowQuery): Promise<TopologyMetrics[]> => {
export const getTopology = (params: FlowQuery, range: number | TimeRange): Promise<TopologyMetrics[]> => {
return axios.get(host + '/api/loki/topology', { params }).then(r => {
if (r.status >= 400) {
throw new Error(`${r.statusText} [code=${r.status}]`);
}
return (r.data.data.result as TopologyMetrics[]).flatMap(r =>
calculateMatrixTotals(r, params.function as MetricFunction)
calculateMatrixTotals(r, params.function as MetricFunction, range)
);
});
};
Expand Down
4 changes: 2 additions & 2 deletions web/src/components/netflow-traffic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export const NetflowTraffic: React.FC<{
});
break;
case 'topology':
getTopology(fq)
getTopology(fq, range)
.then(setMetrics)
.catch(err => {
setMetrics([]);
Expand All @@ -189,7 +189,7 @@ export const NetflowTraffic: React.FC<{
setLoading(false);
break;
}
}, [buildFlowQuery, selectedViewId]);
}, [buildFlowQuery, range, selectedViewId]);

usePoll(tick, interval);

Expand Down

0 comments on commit 5777538

Please sign in to comment.