Skip to content

Commit

Permalink
feat(nui): Fetch chart data from resources
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeWasTakenn committed Jan 27, 2022
1 parent b944271 commit 31fb361
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
17 changes: 17 additions & 0 deletions src/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,35 @@ RegisterCommand(

let totalQueries: number = 0;
let totalTime = 0;
let chartData: [
{
x: number; // Number of queries
y: number; // Execution time
z: string; // Resource name
}
] = [
{
x: 0,
y: 0,
z: '',
},
];

for (const resource in logStorage) {
const queries = logStorage[resource];
let totalResourceTime = 0;

totalQueries += queries.length;
totalTime += queries.reduce((totalTime, query) => (totalTime += query.executionTime), 0);
totalResourceTime += queries.reduce((totalResourceTime, query) => (totalResourceTime += query.executionTime), 0);
chartData.push({ x: queries.length, y: totalResourceTime, z: resource });
}

emitNet(`oxmysql:openUi`, source, {
resources: Object.keys(logStorage),
totalQueries,
totalTime,
chartData,
});
},
true
Expand Down
5 changes: 5 additions & 0 deletions ui/src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ debugData([
resources: ['ox_inventory', 'luke_garages', 'es_extended'],
totalQueries: 732,
totalTime: 258,
chartData: [
{ x: 13, y: 350, z: 'ox_inventory' },
{ x: 27, y: 752, z: 'es_extended' },
{ x: 41, y: 52, z: 'luke_garages' },
],
},
},
]);
Expand Down
7 changes: 6 additions & 1 deletion ui/src/components/NavBars/LeftBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import { useNuiEvent } from '../../hooks/useNuiEvent';
import type { InitData } from '../../types';

const LeftBar: React.FC = () => {
const [initData, setInitData] = useState<InitData>({ resources: [''], totalQueries: 0, totalTime: 0 });
const [initData, setInitData] = useState<InitData>({
resources: [''],
totalQueries: 0,
totalTime: 0,
chartData: [{ x: 0, y: 0, z: '' }],
});

useNuiEvent<InitData>('openUI', (data) => {
setInitData(data);
Expand Down
17 changes: 9 additions & 8 deletions ui/src/components/NavBars/RightBar.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { useState } from 'react';
import { Box, VStack, Center, Spacer, Flex } from '@chakra-ui/react';
import { Box, VStack, Center, Spacer, Flex, Text } from '@chakra-ui/react';
import { useNuiEvent } from '../../hooks/useNuiEvent';
import { VictoryPie, VictoryTooltip } from 'victory';
import type { InitData } from '../../types';

const RightBar: React.FC = () => {
const [initData, setInitData] = useState<InitData>({ totalQueries: 0, totalTime: 0, resources: [''] });
const [initData, setInitData] = useState<InitData>({
totalQueries: 0,
totalTime: 0,
resources: [''],
chartData: [{ x: 0, y: 0, z: '' }],
});

// Debug data defined in LeftBar.tsx

Expand All @@ -27,11 +32,7 @@ const RightBar: React.FC = () => {
<Box>
<VictoryPie
colorScale="qualitative"
data={[
{ x: 'ox_inventory', y: 63 },
{ x: 'es_extended', y: 129 },
{ x: 'luke_garages', y: 73 },
]}
data={initData.chartData}
labelComponent={<VictoryTooltip flyoutPadding={18} style={{ fontSize: 24 }} cornerRadius={15} />}
style={{
labels: {
Expand All @@ -40,7 +41,7 @@ const RightBar: React.FC = () => {
fontSize: 28,
},
}}
labels={({ datum }) => `${datum.x}: ${datum.y}ms`}
labels={({ datum }) => `${datum.z}: ${datum.x} (${Math.trunc(datum.y)}ms)`}
labelPlacement="parallel"
/>
</Box>
Expand Down
15 changes: 11 additions & 4 deletions ui/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
interface ChartData {
x: number;
y: number;
z: string;
}

export type InitData = {
resources: string[];
totalQueries: number;
totalTime: number;
}
resources: string[];
totalQueries: number;
totalTime: number;
chartData: ChartData[];
};

0 comments on commit 31fb361

Please sign in to comment.