Skip to content

Commit

Permalink
fix(dashboard): fixed imprecise uptime
Browse files Browse the repository at this point in the history
  • Loading branch information
tabarra committed Jun 18, 2024
1 parent 5012d01 commit 1282c5f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
1 change: 1 addition & 0 deletions docs/dev_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [x] `lipsum` suffix to all the crash reasons
- [x] thread chart data is the cumulative sum and not the diff, so it "averages out"
- [x] use the fxRunner PID to get the correct fxserver process - perfUtils.fetchFxsMemory
- [x] fix the uptime detection
- [ ] investigate player desync issues

## Highlights
Expand Down
12 changes: 8 additions & 4 deletions panel/src/pages/Dashboard/FullPerfCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { LineChartIcon, Loader2Icon } from 'lucide-react';
import React, { ReactNode, memo, useEffect, useMemo, useRef, useState } from 'react';
import DebouncedResizeContainer from '@/components/DebouncedResizeContainer';
import drawFullPerfChart from './drawFullPerfChart';
import { BackendApiError, useBackendApi } from '@/hooks/fetch';
import { useBackendApi } from '@/hooks/fetch';
import type { PerfChartApiResp, PerfChartApiSuccessResp } from "@shared/otherTypes";
import useSWR from 'swr';
import { PerfSnapType, formatTickBoundary, getBucketTicketsEstimatedTime, getServerStatsData, getTimeWeightedHistogram, processPerfLog } from './chartingUtils';
Expand All @@ -16,12 +16,13 @@ import { useSetAtom } from 'jotai';
type FullPerfChartProps = {
threadName: string;
apiData: PerfChartApiSuccessResp;
apiDataAge: number;
width: number;
height: number;
isDarkMode: boolean;
};

const FullPerfChart = memo(({ threadName, apiData, width, height, isDarkMode }: FullPerfChartProps) => {
const FullPerfChart = memo(({ threadName, apiData, apiDataAge, width, height, isDarkMode }: FullPerfChartProps) => {
const setServerStats = useSetAtom(dashServerStatsAtom);
const svgRef = useRef<SVGSVGElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(null);
Expand Down Expand Up @@ -55,7 +56,7 @@ const FullPerfChart = memo(({ threadName, apiData, width, height, isDarkMode }:
}

//Calculate server stats here because the data comes from SWR instead of jotai
const serverStatsData = getServerStatsData(parsed.lifespans, 24);
const serverStatsData = getServerStatsData(parsed.lifespans, 24, apiDataAge);
setServerStats(serverStatsData);

return {
Expand All @@ -69,7 +70,7 @@ const FullPerfChart = memo(({ threadName, apiData, width, height, isDarkMode }:
});
},
}
}, [apiData, threadName, isDarkMode, renderError]);
}, [apiData, apiDataAge, threadName, isDarkMode, renderError]);


//Redraw chart when data or size changes
Expand Down Expand Up @@ -176,6 +177,7 @@ export default function FullPerfCard() {
const [chartSize, setChartSize] = useState({ width: 0, height: 0 });
const [selectedThread, setSelectedThread] = useState('svMain');
const [apiFailReason, setApiFailReason] = useState('');
const [apiDataAge, setApiDataAge] = useState(0);
const isDarkMode = useIsDarkMode();

const chartApi = useBackendApi<PerfChartApiResp>({
Expand All @@ -193,6 +195,7 @@ export default function FullPerfCard() {
setApiFailReason(data.fail_reason);
return null;
}
setApiDataAge(Date.now());
return data;
}, {});

Expand All @@ -207,6 +210,7 @@ export default function FullPerfCard() {
contentNode = <FullPerfChart
threadName={selectedThread}
apiData={swrChartApiResp.data as PerfChartApiSuccessResp}
apiDataAge={apiDataAge}
width={chartSize.width}
height={chartSize.height}
isDarkMode={isDarkMode}
Expand Down
13 changes: 9 additions & 4 deletions panel/src/pages/Dashboard/chartingUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,13 @@ export const getThreadDisplayName = (thread: string) => {
/**
* Process the data to return the median player count and uptime in the last X hours
*/
export const getServerStatsData = (lifespans: PerfLifeSpanType[], windowHours: number) => {
const now = Date.now();
export const getServerStatsData = (
lifespans: PerfLifeSpanType[],
windowHours: number,
apiDataAge: number,
) => {
const windowMs = windowHours * 60 * 60 * 1000;
const windowStart = now - windowMs;
const windowStart = apiDataAge - windowMs;

let uptime = 0;
const playerCounts = [];
Expand All @@ -251,7 +254,9 @@ export const getServerStatsData = (lifespans: PerfLifeSpanType[], windowHours: n
}

return {
uptimePct: uptime / windowMs * 100,
uptimePct: apiDataAge
? Math.min(100, uptime / windowMs * 100)
: undefined,
medianPlayerCount: d3.quantile(playerCounts, 0.5) ?? 0,
};
}
4 changes: 2 additions & 2 deletions panel/src/pages/Dashboard/dashboardHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type DashboardPerfCursorDataType = {
};

type DashboardServerStatsDataType = {
uptimePct: number;
medianPlayerCount: number;
uptimePct?: number;
medianPlayerCount?: number;
};

/**
Expand Down

0 comments on commit 1282c5f

Please sign in to comment.