Skip to content

Commit

Permalink
feat(ui): Query data fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeWasTakenn committed Dec 27, 2021
1 parent 7b88fd4 commit 5eaed9f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 24 deletions.
38 changes: 37 additions & 1 deletion ui/src/components/routes/Home.svelte
Original file line number Diff line number Diff line change
@@ -1 +1,37 @@
<div>Some data here</div>
<script lang="ts">
import { useNuiEvent } from '../../utils/useNuiEvent';
import { debugData } from '../../utils/debugData';
debugData([
{
action: 'init',
data: {
resources: ['ox_inventory', 'luke_garages', 'npwd'],
totalQueries: 350,
totalTime: 75000,
},
},
]);
interface QueryData {
date: number;
query: string;
executionTime: number;
}
type QueryLog = Record<string, QueryData[]>;
let initData: QueryLog;
useNuiEvent('init', (data: QueryLog) => {
initData = data;
});
</script>

<div>
{#if initData}
<div>Resources: {initData.resources}</div>
<div>Total queries: {initData.totalQueries}</div>
<div>Total time: {initData.totalTime}</div>
{/if}
</div>
53 changes: 30 additions & 23 deletions ui/src/components/routes/ResourceData.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,44 @@
import { currentResource } from '../../store/stores';
import { fetchNui } from '../../utils/fetchNui';
import { onMount } from 'svelte';
import { useNuiEvent } from '../../utils/useNuiEvent';
import { debugData } from '../../utils/debugData';
export let params: { wild: string };
interface Data {
[key: string]: number[];
interface QueryData {
date: number;
query: string;
executionTime: number;
}
let queryData: number[];
let resource: string;
const debugQueries: Data = {
['luke_garages']: [300, 200, 100],
['npwd']: [600, 300, 723],
['ox_inventory']: [30, 270, 350],
};
let queryData: QueryData[] | undefined;
let isDataLoaded: boolean = false;
onMount(() => currentResource.set(params.wild));
currentResource.subscribe((value) => {
resource = value;
fetchNui('getResourceData', resource)
.then((retData) => {
queryData = retData;
})
.catch((e) => {
queryData = debugQueries[resource];
});
currentResource.subscribe((resource) => {
isDataLoaded = false;
fetchNui('fetchResource', resource);
debugData([
{
action: 'loadResource',
data: [
{ date: Date.now(), query: 'SELECT * FROM `somewhere`', executionTime: 2 },
{ date: Date.now(), query: 'SELECT * FROM `elsewhere`', executionTime: 5 },
{ date: Date.now(), query: 'SELECT * FROM `somewhere_elsewhere`', executionTime: 3 },
{ date: Date.now(), query: 'SELECT * FROM `selsewhere`', executionTime: 7 },
],
},
]);
});
useNuiEvent('loadResource', (data: QueryData[]) => {
queryData = data;
isDataLoaded = true;
});
</script>

<div>
{resource}
{queryData}
</div>
{#if isDataLoaded === true}
{JSON.stringify(queryData)}
{/if}

0 comments on commit 5eaed9f

Please sign in to comment.