@adventurelabs/scout-core 1.0.60 → 1.0.62
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/helpers/herds.d.ts +2 -2
- package/dist/helpers/herds.js +15 -3
- package/dist/hooks/useScoutRefresh.js +62 -13
- package/dist/types/herd_module.d.ts +13 -0
- package/package.json +1 -1
package/dist/helpers/herds.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { IHerd } from "../types/db";
|
|
2
2
|
import { IWebResponseCompatible } from "../types/requests";
|
|
3
3
|
import { SupabaseClient } from "@supabase/supabase-js";
|
|
4
|
-
import {
|
|
4
|
+
import { IHerdModulesResponseWithStatus } from "../types/herd_module";
|
|
5
5
|
export declare function get_herds(client: SupabaseClient): Promise<IWebResponseCompatible<IHerd[]>>;
|
|
6
6
|
export declare function get_herd_by_slug(slug: string): Promise<IWebResponseCompatible<IHerd>>;
|
|
7
7
|
export declare function deleteHerd(herd_id: number): Promise<IWebResponseCompatible<boolean>>;
|
|
8
8
|
export declare function createHerd(newHerd: any): Promise<IWebResponseCompatible<boolean>>;
|
|
9
|
-
export declare function server_load_herd_modules(): Promise<
|
|
9
|
+
export declare function server_load_herd_modules(): Promise<IHerdModulesResponseWithStatus>;
|
package/dist/helpers/herds.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use server";
|
|
2
2
|
import { newServerClient } from "../supabase/server";
|
|
3
3
|
import { EnumWebResponse, IWebResponse, } from "../types/requests";
|
|
4
|
-
import { HerdModule } from "../types/herd_module";
|
|
4
|
+
import { HerdModule, } from "../types/herd_module";
|
|
5
5
|
export async function get_herds(client) {
|
|
6
6
|
const { data: herds } = await client.from("herds").select();
|
|
7
7
|
if (!herds) {
|
|
@@ -70,7 +70,13 @@ export async function server_load_herd_modules() {
|
|
|
70
70
|
const client_supabase = await newServerClient();
|
|
71
71
|
let new_herds = await get_herds(client_supabase);
|
|
72
72
|
if (new_herds.status != EnumWebResponse.SUCCESS || !new_herds.data) {
|
|
73
|
-
return
|
|
73
|
+
return {
|
|
74
|
+
status: EnumWebResponse.ERROR,
|
|
75
|
+
msg: "No herds found",
|
|
76
|
+
data: null,
|
|
77
|
+
time_finished: Date.now(),
|
|
78
|
+
server_processing_time_ms: Date.now() - startTime,
|
|
79
|
+
};
|
|
74
80
|
}
|
|
75
81
|
console.log(`[server_load_herd_modules] Loading ${new_herds.data.length} herds in parallel...`);
|
|
76
82
|
let new_herd_modules = [];
|
|
@@ -81,5 +87,11 @@ export async function server_load_herd_modules() {
|
|
|
81
87
|
const endTime = Date.now();
|
|
82
88
|
const totalLoadTime = endTime - startTime;
|
|
83
89
|
console.log(`[server_load_herd_modules] Loaded ${new_herds.data.length} herds in ${totalLoadTime}ms (parallel processing)`);
|
|
84
|
-
return
|
|
90
|
+
return {
|
|
91
|
+
status: EnumWebResponse.SUCCESS,
|
|
92
|
+
msg: "Herd modules loaded successfully",
|
|
93
|
+
data: serialized_herd_modules,
|
|
94
|
+
time_finished: endTime,
|
|
95
|
+
server_processing_time_ms: totalLoadTime,
|
|
96
|
+
};
|
|
85
97
|
}
|
|
@@ -4,6 +4,7 @@ import { EnumScoutStateStatus, setActiveHerdId, setHerdModules, setStatus, setHe
|
|
|
4
4
|
import { EnumHerdModulesLoadingState } from "../types/herd_module";
|
|
5
5
|
import { server_load_herd_modules } from "../helpers/herds";
|
|
6
6
|
import { server_get_user } from "../helpers/users";
|
|
7
|
+
import { EnumWebResponse } from "../types/requests";
|
|
7
8
|
/**
|
|
8
9
|
* Hook for refreshing scout data with detailed timing measurements
|
|
9
10
|
*
|
|
@@ -52,26 +53,71 @@ export function useScoutRefresh(options = {}) {
|
|
|
52
53
|
try {
|
|
53
54
|
dispatch(setStatus(EnumScoutStateStatus.LOADING));
|
|
54
55
|
dispatch(setHerdModulesLoadingState(EnumHerdModulesLoadingState.LOADING));
|
|
55
|
-
//
|
|
56
|
-
|
|
57
|
-
const
|
|
58
|
-
const
|
|
56
|
+
// Run API requests in parallel for better performance
|
|
57
|
+
console.log("[useScoutRefresh] Starting parallel API requests...");
|
|
58
|
+
const parallelStartTime = Date.now();
|
|
59
|
+
const [herdModulesResult, userResult] = await Promise.all([
|
|
60
|
+
(async () => {
|
|
61
|
+
const start = Date.now();
|
|
62
|
+
const result = await server_load_herd_modules();
|
|
63
|
+
const duration = Date.now() - start;
|
|
64
|
+
return { result, duration, start };
|
|
65
|
+
})(),
|
|
66
|
+
(async () => {
|
|
67
|
+
const start = Date.now();
|
|
68
|
+
const result = await server_get_user();
|
|
69
|
+
const duration = Date.now() - start;
|
|
70
|
+
return { result, duration, start };
|
|
71
|
+
})(),
|
|
72
|
+
]);
|
|
73
|
+
const parallelDuration = Date.now() - parallelStartTime;
|
|
74
|
+
console.log(`[useScoutRefresh] Parallel API requests completed in ${parallelDuration}ms`);
|
|
75
|
+
// Extract results and timing
|
|
76
|
+
const herdModulesResponse = herdModulesResult.result;
|
|
77
|
+
const res_new_user = userResult.result;
|
|
78
|
+
const herdModulesDuration = herdModulesResult.duration;
|
|
79
|
+
const userApiDuration = userResult.duration;
|
|
80
|
+
// Calculate network latency for herd modules
|
|
81
|
+
let networkLatencyMs = 0;
|
|
82
|
+
if (herdModulesResponse.status === EnumWebResponse.SUCCESS &&
|
|
83
|
+
herdModulesResponse.data) {
|
|
84
|
+
const serverFinishTime = herdModulesResponse.time_finished;
|
|
85
|
+
const clientReceiveTime = Date.now();
|
|
86
|
+
const estimatedNetworkLatency = clientReceiveTime - serverFinishTime;
|
|
87
|
+
networkLatencyMs = Math.max(0, estimatedNetworkLatency);
|
|
88
|
+
console.log(`[useScoutRefresh] Network latency calculation:`);
|
|
89
|
+
console.log(` - Server finished at: ${new Date(serverFinishTime).toISOString()}`);
|
|
90
|
+
console.log(` - Client received at: ${new Date(clientReceiveTime).toISOString()}`);
|
|
91
|
+
console.log(` - Estimated network latency: ${networkLatencyMs}ms`);
|
|
92
|
+
console.log(` - Server processing time: ${herdModulesResponse.server_processing_time_ms}ms`);
|
|
93
|
+
}
|
|
94
|
+
// Store timing values
|
|
59
95
|
timingRefs.current.herdModulesDuration = herdModulesDuration;
|
|
60
|
-
dispatch(setHerdModulesApiDuration(herdModulesDuration));
|
|
61
|
-
// Measure user API call duration
|
|
62
|
-
const userApiStartTime = Date.now();
|
|
63
|
-
const res_new_user = await server_get_user();
|
|
64
|
-
const userApiDuration = Date.now() - userApiStartTime;
|
|
65
96
|
timingRefs.current.userApiDuration = userApiDuration;
|
|
97
|
+
// Dispatch timing actions
|
|
98
|
+
dispatch(setHerdModulesApiDuration(herdModulesDuration));
|
|
66
99
|
dispatch(setUserApiDuration(userApiDuration));
|
|
100
|
+
// Calculate network overhead
|
|
101
|
+
const totalApiTime = herdModulesDuration + userApiDuration;
|
|
102
|
+
const networkOverhead = parallelDuration - Math.max(herdModulesDuration, userApiDuration);
|
|
103
|
+
console.log(`[useScoutRefresh] API timing breakdown:`);
|
|
104
|
+
console.log(` - Herd modules: ${herdModulesDuration}ms (server processing + network)`);
|
|
105
|
+
console.log(` - User API: ${userApiDuration}ms (server processing + network)`);
|
|
106
|
+
console.log(` - Parallel execution: ${parallelDuration}ms`);
|
|
107
|
+
console.log(` - Network overhead: ${networkOverhead}ms`);
|
|
108
|
+
if (networkLatencyMs > 0) {
|
|
109
|
+
console.log(` - Estimated network latency: ${networkLatencyMs}ms`);
|
|
110
|
+
}
|
|
67
111
|
// Validate API responses
|
|
68
|
-
if (!
|
|
69
|
-
!Array.isArray(
|
|
112
|
+
if (!herdModulesResponse.data ||
|
|
113
|
+
!Array.isArray(herdModulesResponse.data)) {
|
|
70
114
|
throw new Error("Invalid herd modules response");
|
|
71
115
|
}
|
|
72
116
|
if (!res_new_user || !res_new_user.data) {
|
|
73
117
|
throw new Error("Invalid user response");
|
|
74
118
|
}
|
|
119
|
+
// Use the validated data
|
|
120
|
+
const compatible_new_herd_modules = herdModulesResponse.data;
|
|
75
121
|
// Measure data processing duration
|
|
76
122
|
const dataProcessingStartTime = Date.now();
|
|
77
123
|
dispatch(setHerdModules(compatible_new_herd_modules));
|
|
@@ -113,13 +159,16 @@ export function useScoutRefresh(options = {}) {
|
|
|
113
159
|
const loadingDuration = Date.now() - startTime;
|
|
114
160
|
dispatch(setHerdModulesLoadedInMs(loadingDuration));
|
|
115
161
|
dispatch(setStatus(EnumScoutStateStatus.DONE_LOADING));
|
|
116
|
-
// Log timing statistics
|
|
117
|
-
console.log("[useScoutRefresh] Refresh completed successfully.
|
|
162
|
+
// Log comprehensive timing statistics
|
|
163
|
+
console.log("[useScoutRefresh] Refresh completed successfully. Full timing breakdown:");
|
|
118
164
|
console.log(` - Herd modules API: ${herdModulesDuration}ms`);
|
|
119
165
|
console.log(` - User API: ${userApiDuration}ms`);
|
|
166
|
+
console.log(` - Parallel execution: ${parallelDuration}ms`);
|
|
167
|
+
console.log(` - Network overhead: ${networkOverhead}ms`);
|
|
120
168
|
console.log(` - Data processing: ${dataProcessingDuration}ms`);
|
|
121
169
|
console.log(` - LocalStorage operations: ${localStorageDuration}ms`);
|
|
122
170
|
console.log(` - Total duration: ${loadingDuration}ms`);
|
|
171
|
+
console.log(` - Time saved with parallel execution: ${totalApiTime - parallelDuration}ms`);
|
|
123
172
|
onRefreshComplete?.();
|
|
124
173
|
}
|
|
125
174
|
catch (error) {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { SupabaseClient } from "@supabase/supabase-js";
|
|
2
2
|
import { IDevice, IEventWithTags, IHerd, IPlan, ILayer, IUserAndRole, IZoneWithActions, ISessionWithCoordinates } from "../types/db";
|
|
3
|
+
import { EnumWebResponse } from "./requests";
|
|
3
4
|
export declare enum EnumHerdModulesLoadingState {
|
|
4
5
|
NOT_LOADING = "NOT_LOADING",
|
|
5
6
|
LOADING = "LOADING",
|
|
@@ -39,3 +40,15 @@ export interface IHerdModule {
|
|
|
39
40
|
sessions: ISessionWithCoordinates[];
|
|
40
41
|
layers: ILayer[];
|
|
41
42
|
}
|
|
43
|
+
export interface IHerdModulesResponse {
|
|
44
|
+
data: IHerdModule[];
|
|
45
|
+
time_finished: number;
|
|
46
|
+
server_processing_time_ms: number;
|
|
47
|
+
}
|
|
48
|
+
export interface IHerdModulesResponseWithStatus {
|
|
49
|
+
status: EnumWebResponse;
|
|
50
|
+
msg: string;
|
|
51
|
+
data: IHerdModule[] | null;
|
|
52
|
+
time_finished: number;
|
|
53
|
+
server_processing_time_ms: number;
|
|
54
|
+
}
|