@adventurelabs/scout-core 1.0.65 → 1.0.67
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.js +5 -39
- package/dist/helpers/users.d.ts +2 -1
- package/dist/helpers/users.js +22 -5
- package/dist/hooks/useScoutRefresh.js +20 -50
- package/dist/supabase/server.js +1 -12
- package/dist/types/herd_module.js +1 -1
- package/package.json +1 -1
package/dist/helpers/herds.js
CHANGED
|
@@ -3,12 +3,7 @@ import { newServerClient } from "../supabase/server";
|
|
|
3
3
|
import { EnumWebResponse, IWebResponse, } from "../types/requests";
|
|
4
4
|
import { HerdModule, } from "../types/herd_module";
|
|
5
5
|
export async function get_herds(client) {
|
|
6
|
-
const queryStartTime = Date.now();
|
|
7
|
-
console.log(`[get_herds] Starting database query at ${new Date(queryStartTime).toISOString()}`);
|
|
8
6
|
const { data: herds } = await client.from("herds").select();
|
|
9
|
-
const queryEndTime = Date.now();
|
|
10
|
-
const queryDuration = queryEndTime - queryStartTime;
|
|
11
|
-
console.log(`[get_herds] Database query completed in ${queryDuration}ms`);
|
|
12
7
|
if (!herds) {
|
|
13
8
|
return {
|
|
14
9
|
status: EnumWebResponse.ERROR,
|
|
@@ -70,57 +65,28 @@ export async function createHerd(newHerd) {
|
|
|
70
65
|
}
|
|
71
66
|
}
|
|
72
67
|
export async function server_load_herd_modules() {
|
|
73
|
-
const requestReceivedTime = Date.now();
|
|
74
|
-
console.log(`[server_load_herd_modules] Request received at: ${new Date(requestReceivedTime).toISOString()}`);
|
|
75
68
|
const startTime = Date.now();
|
|
76
|
-
//
|
|
77
|
-
const supabaseStartTime = Date.now();
|
|
69
|
+
// load herds
|
|
78
70
|
const client_supabase = await newServerClient();
|
|
79
|
-
const supabaseTime = Date.now() - supabaseStartTime;
|
|
80
|
-
console.log(`[server_load_herd_modules] Supabase client creation took: ${supabaseTime}ms`);
|
|
81
|
-
// Measure database connection and initial query
|
|
82
|
-
const dbStartTime = Date.now();
|
|
83
71
|
let new_herds = await get_herds(client_supabase);
|
|
84
|
-
const dbConnectionTime = Date.now() - dbStartTime;
|
|
85
|
-
console.log(`[server_load_herd_modules] Database connection and initial herds query took: ${dbConnectionTime}ms`);
|
|
86
72
|
if (new_herds.status != EnumWebResponse.SUCCESS || !new_herds.data) {
|
|
87
|
-
const errorTime = Date.now();
|
|
88
73
|
return {
|
|
89
74
|
status: EnumWebResponse.ERROR,
|
|
90
75
|
msg: "No herds found",
|
|
91
76
|
data: null,
|
|
92
|
-
time_finished:
|
|
93
|
-
time_sent:
|
|
94
|
-
server_processing_time_ms:
|
|
77
|
+
time_finished: Date.now(),
|
|
78
|
+
time_sent: Date.now(),
|
|
79
|
+
server_processing_time_ms: Date.now() - startTime,
|
|
95
80
|
};
|
|
96
81
|
}
|
|
97
|
-
console.log(`[server_load_herd_modules] Loading ${new_herds.data.length} herds in parallel...`);
|
|
98
|
-
// Measure herd module creation
|
|
99
|
-
const herdModuleStartTime = Date.now();
|
|
100
82
|
let new_herd_modules = [];
|
|
101
83
|
const herdModulePromises = new_herds.data.map((herd) => HerdModule.from_herd(herd, client_supabase));
|
|
102
84
|
new_herd_modules = await Promise.all(herdModulePromises);
|
|
103
|
-
|
|
104
|
-
console.log(`[server_load_herd_modules] Herd module creation took: ${herdModuleTime}ms`);
|
|
105
|
-
// Measure serialization
|
|
106
|
-
const serializationStartTime = Date.now();
|
|
85
|
+
// now serialize the herd modules
|
|
107
86
|
let serialized_herd_modules = new_herd_modules.map((herd_module) => herd_module.to_serializable());
|
|
108
|
-
const serializationTime = Date.now() - serializationStartTime;
|
|
109
|
-
console.log(`[server_load_herd_modules] Serialization took: ${serializationTime}ms`);
|
|
110
87
|
const endTime = Date.now();
|
|
111
88
|
const totalLoadTime = endTime - startTime;
|
|
112
|
-
// Calculate detailed timing breakdown
|
|
113
|
-
const timeToClient = endTime - requestReceivedTime;
|
|
114
|
-
const processingTime = endTime - startTime;
|
|
115
89
|
console.log(`[server_load_herd_modules] Loaded ${new_herds.data.length} herds in ${totalLoadTime}ms (parallel processing)`);
|
|
116
|
-
console.log(`[server_load_herd_modules] Detailed timing breakdown:`);
|
|
117
|
-
console.log(` - Request received to start: ${startTime - requestReceivedTime}ms`);
|
|
118
|
-
console.log(` - Supabase client creation: ${supabaseTime}ms`);
|
|
119
|
-
console.log(` - Database connection & query: ${dbConnectionTime}ms`);
|
|
120
|
-
console.log(` - Herd module creation: ${herdModuleTime}ms`);
|
|
121
|
-
console.log(` - Serialization: ${serializationTime}ms`);
|
|
122
|
-
console.log(` - Total processing time: ${processingTime}ms`);
|
|
123
|
-
console.log(` - Total time to client: ${timeToClient}ms`);
|
|
124
90
|
// Record the time when we're about to send the response
|
|
125
91
|
const timeSent = Date.now();
|
|
126
92
|
return {
|
package/dist/helpers/users.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { IUser, IUserAndRole, IUserRolePerHerd, Role } from "../types/db";
|
|
2
2
|
import { IWebResponseCompatible } from "../types/requests";
|
|
3
|
+
import { SupabaseClient } from "@supabase/supabase-js";
|
|
3
4
|
export declare function server_get_user_roles(herd_id: number): Promise<IWebResponseCompatible<IUserRolePerHerd[]>>;
|
|
4
5
|
export declare function server_get_user(): Promise<IWebResponseCompatible<IUser | null>>;
|
|
5
|
-
export declare function server_get_users_with_herd_access(herd_id: number): Promise<IWebResponseCompatible<IUserAndRole[]
|
|
6
|
+
export declare function server_get_users_with_herd_access(herd_id: number, supabaseClient?: SupabaseClient): Promise<IWebResponseCompatible<IUserAndRole[]>>;
|
|
6
7
|
export declare function server_upsert_user_with_role(herd_id: number, username: string, role: Role): Promise<IWebResponseCompatible<IUserAndRole | null>>;
|
package/dist/helpers/users.js
CHANGED
|
@@ -25,16 +25,33 @@ export async function server_get_user() {
|
|
|
25
25
|
const new_user = data.user;
|
|
26
26
|
return IWebResponse.success(new_user).to_compatible();
|
|
27
27
|
}
|
|
28
|
-
export async function server_get_users_with_herd_access(herd_id) {
|
|
29
|
-
const supabase = await newServerClient();
|
|
28
|
+
export async function server_get_users_with_herd_access(herd_id, supabaseClient) {
|
|
29
|
+
const supabase = supabaseClient || (await newServerClient());
|
|
30
30
|
const { data, error } = await supabase
|
|
31
31
|
.from("users_roles_per_herd")
|
|
32
|
-
.select(
|
|
32
|
+
.select(`
|
|
33
|
+
role,
|
|
34
|
+
users (
|
|
35
|
+
id,
|
|
36
|
+
username
|
|
37
|
+
)
|
|
38
|
+
`)
|
|
33
39
|
.eq("herd_id", herd_id);
|
|
34
40
|
if (error) {
|
|
35
|
-
return
|
|
41
|
+
return {
|
|
42
|
+
status: EnumWebResponse.ERROR,
|
|
43
|
+
msg: error.message,
|
|
44
|
+
data: null,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
// Transform the data to match IUserAndRole interface
|
|
49
|
+
const transformedData = data.map((item) => ({
|
|
50
|
+
user: item.users,
|
|
51
|
+
role: item.role,
|
|
52
|
+
}));
|
|
53
|
+
return IWebResponse.success(transformedData).to_compatible();
|
|
36
54
|
}
|
|
37
|
-
return IWebResponse.success(data).to_compatible();
|
|
38
55
|
}
|
|
39
56
|
export async function server_upsert_user_with_role(herd_id, username, role) {
|
|
40
57
|
const supabase = await newServerClient();
|
|
@@ -60,6 +60,7 @@ export function useScoutRefresh(options = {}) {
|
|
|
60
60
|
(async () => {
|
|
61
61
|
const start = Date.now();
|
|
62
62
|
console.log(`[useScoutRefresh] Starting herd modules request at ${new Date(start).toISOString()}`);
|
|
63
|
+
// High priority request with optimization
|
|
63
64
|
const result = await server_load_herd_modules();
|
|
64
65
|
const duration = Date.now() - start;
|
|
65
66
|
console.log(`[useScoutRefresh] Herd modules request completed in ${duration}ms`);
|
|
@@ -68,6 +69,7 @@ export function useScoutRefresh(options = {}) {
|
|
|
68
69
|
(async () => {
|
|
69
70
|
const start = Date.now();
|
|
70
71
|
console.log(`[useScoutRefresh] Starting user request at ${new Date(start).toISOString()}`);
|
|
72
|
+
// High priority request with optimization
|
|
71
73
|
const result = await server_get_user();
|
|
72
74
|
const duration = Date.now() - start;
|
|
73
75
|
console.log(`[useScoutRefresh] User request completed in ${duration}ms`);
|
|
@@ -96,37 +98,13 @@ export function useScoutRefresh(options = {}) {
|
|
|
96
98
|
if (herdModulesResponse.status === EnumWebResponse.SUCCESS &&
|
|
97
99
|
herdModulesResponse.data) {
|
|
98
100
|
const serverFinishTime = herdModulesResponse.time_finished;
|
|
99
|
-
const serverSentTime = herdModulesResponse.time_sent;
|
|
100
101
|
const clientReceiveTime = Date.now();
|
|
101
|
-
|
|
102
|
-
const estimatedNetworkLatency = clientReceiveTime - serverSentTime;
|
|
102
|
+
const estimatedNetworkLatency = clientReceiveTime - serverFinishTime;
|
|
103
103
|
networkLatencyMs = Math.max(0, estimatedNetworkLatency);
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
console.log(`
|
|
107
|
-
console.log(` -
|
|
108
|
-
console.log(` - Server sent response at: ${new Date(serverSentTime).toISOString()}`);
|
|
109
|
-
console.log(` - Client received at: ${new Date(clientReceiveTime).toISOString()}`);
|
|
110
|
-
console.log(` - Server processing time: ${herdModulesResponse.server_processing_time_ms}ms`);
|
|
111
|
-
console.log(` - Server overhead (finish to send): ${serverOverhead}ms`);
|
|
112
|
-
console.log(` - Estimated network latency: ${networkLatencyMs}ms`);
|
|
113
|
-
console.log(` - Client total time: ${herdModulesDuration}ms`);
|
|
114
|
-
console.log(` - Breakdown: ${herdModulesResponse.server_processing_time_ms}ms (server) + ${serverOverhead}ms (overhead) + ${networkLatencyMs}ms (network) = ${herdModulesResponse.server_processing_time_ms +
|
|
115
|
-
serverOverhead +
|
|
116
|
-
networkLatencyMs}ms`);
|
|
117
|
-
// Additional analysis
|
|
118
|
-
if (herdModulesResponse.data.length > 0) {
|
|
119
|
-
const totalDevices = herdModulesResponse.data.reduce((sum, hm) => sum + (hm.devices?.length || 0), 0);
|
|
120
|
-
const totalEvents = herdModulesResponse.data.reduce((sum, hm) => sum + (hm.events?.length || 0), 0);
|
|
121
|
-
console.log(` - Response size analysis:`);
|
|
122
|
-
console.log(` * Herds: ${herdModulesResponse.data.length}`);
|
|
123
|
-
console.log(` * Total devices: ${totalDevices}`);
|
|
124
|
-
console.log(` * Total events: ${totalEvents}`);
|
|
125
|
-
console.log(` * Estimated payload: ~${Math.round((herdModulesResponse.data.length * 2 +
|
|
126
|
-
totalDevices * 3 +
|
|
127
|
-
totalEvents * 5) /
|
|
128
|
-
1024)}KB`);
|
|
129
|
-
}
|
|
104
|
+
console.log(`[useScoutRefresh] Herd modules performance:`);
|
|
105
|
+
console.log(` - Server processing: ${herdModulesResponse.server_processing_time_ms}ms`);
|
|
106
|
+
console.log(` - Network latency: ${networkLatencyMs}ms`);
|
|
107
|
+
console.log(` - Total client time: ${herdModulesDuration}ms`);
|
|
130
108
|
}
|
|
131
109
|
// Store timing values
|
|
132
110
|
timingRefs.current.herdModulesDuration = herdModulesDuration;
|
|
@@ -137,14 +115,11 @@ export function useScoutRefresh(options = {}) {
|
|
|
137
115
|
// Calculate network overhead
|
|
138
116
|
const totalApiTime = herdModulesDuration + userApiDuration;
|
|
139
117
|
const networkOverhead = parallelDuration - Math.max(herdModulesDuration, userApiDuration);
|
|
140
|
-
console.log(`[useScoutRefresh] API
|
|
141
|
-
console.log(` - Herd modules: ${herdModulesDuration}ms
|
|
142
|
-
console.log(` - User API: ${userApiDuration}ms
|
|
118
|
+
console.log(`[useScoutRefresh] API performance:`);
|
|
119
|
+
console.log(` - Herd modules: ${herdModulesDuration}ms`);
|
|
120
|
+
console.log(` - User API: ${userApiDuration}ms`);
|
|
143
121
|
console.log(` - Parallel execution: ${parallelDuration}ms`);
|
|
144
|
-
console.log(` -
|
|
145
|
-
if (networkLatencyMs > 0) {
|
|
146
|
-
console.log(` - Estimated network latency: ${networkLatencyMs}ms`);
|
|
147
|
-
}
|
|
122
|
+
console.log(` - Time saved with parallel: ${totalApiTime - parallelDuration}ms`);
|
|
148
123
|
// Validate API responses
|
|
149
124
|
const validationStartTime = Date.now();
|
|
150
125
|
if (!herdModulesResponse.data ||
|
|
@@ -199,16 +174,13 @@ export function useScoutRefresh(options = {}) {
|
|
|
199
174
|
const loadingDuration = Date.now() - startTime;
|
|
200
175
|
dispatch(setHerdModulesLoadedInMs(loadingDuration));
|
|
201
176
|
dispatch(setStatus(EnumScoutStateStatus.DONE_LOADING));
|
|
202
|
-
// Log
|
|
203
|
-
console.log(
|
|
204
|
-
console.log(` -
|
|
177
|
+
// Log essential performance metrics
|
|
178
|
+
console.log(`[useScoutRefresh] Refresh completed successfully:`);
|
|
179
|
+
console.log(` - Total duration: ${loadingDuration}ms`);
|
|
180
|
+
console.log(` - Herd modules: ${herdModulesDuration}ms`);
|
|
205
181
|
console.log(` - User API: ${userApiDuration}ms`);
|
|
206
182
|
console.log(` - Parallel execution: ${parallelDuration}ms`);
|
|
207
|
-
console.log(` -
|
|
208
|
-
console.log(` - Data processing: ${dataProcessingDuration}ms`);
|
|
209
|
-
console.log(` - LocalStorage operations: ${localStorageDuration}ms`);
|
|
210
|
-
console.log(` - Total duration: ${loadingDuration}ms`);
|
|
211
|
-
console.log(` - Time saved with parallel execution: ${totalApiTime - parallelDuration}ms`);
|
|
183
|
+
console.log(` - Time saved with parallel: ${totalApiTime - parallelDuration}ms`);
|
|
212
184
|
onRefreshComplete?.();
|
|
213
185
|
}
|
|
214
186
|
catch (error) {
|
|
@@ -218,13 +190,11 @@ export function useScoutRefresh(options = {}) {
|
|
|
218
190
|
dispatch(setHerdModulesLoadingState(EnumHerdModulesLoadingState.UNSUCCESSFULLY_LOADED));
|
|
219
191
|
dispatch(setHerdModulesLoadedInMs(loadingDuration));
|
|
220
192
|
dispatch(setStatus(EnumScoutStateStatus.DONE_LOADING));
|
|
221
|
-
// Log
|
|
222
|
-
console.log(
|
|
223
|
-
console.log(` - Herd modules API: ${timingRefs.current.herdModulesDuration}ms`);
|
|
224
|
-
console.log(` - User API: ${timingRefs.current.userApiDuration}ms`);
|
|
225
|
-
console.log(` - Data processing: ${timingRefs.current.dataProcessingDuration}ms`);
|
|
226
|
-
console.log(` - LocalStorage operations: ${timingRefs.current.localStorageDuration}ms`);
|
|
193
|
+
// Log essential error metrics
|
|
194
|
+
console.log(`[useScoutRefresh] Refresh failed:`);
|
|
227
195
|
console.log(` - Total duration: ${loadingDuration}ms`);
|
|
196
|
+
console.log(` - Herd modules: ${timingRefs.current.herdModulesDuration}ms`);
|
|
197
|
+
console.log(` - User API: ${timingRefs.current.userApiDuration}ms`);
|
|
228
198
|
}
|
|
229
199
|
finally {
|
|
230
200
|
refreshInProgressRef.current = false;
|
package/dist/supabase/server.js
CHANGED
|
@@ -1,14 +1,8 @@
|
|
|
1
1
|
import { createServerClient } from "@supabase/ssr";
|
|
2
2
|
import { cookies } from "next/headers";
|
|
3
3
|
export async function newServerClient() {
|
|
4
|
-
const startTime = Date.now();
|
|
5
|
-
console.log(`[newServerClient] Starting client creation at ${new Date(startTime).toISOString()}`);
|
|
6
|
-
const cookieStartTime = Date.now();
|
|
7
4
|
const cookieStore = await cookies();
|
|
8
|
-
|
|
9
|
-
console.log(`[newServerClient] Cookie retrieval took: ${cookieTime}ms`);
|
|
10
|
-
const supabaseStartTime = Date.now();
|
|
11
|
-
const client = createServerClient(process.env.NEXT_PUBLIC_SUPABASE_URL, process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY, {
|
|
5
|
+
return createServerClient(process.env.NEXT_PUBLIC_SUPABASE_URL, process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY, {
|
|
12
6
|
cookies: {
|
|
13
7
|
getAll() {
|
|
14
8
|
return cookieStore.getAll();
|
|
@@ -25,9 +19,4 @@ export async function newServerClient() {
|
|
|
25
19
|
},
|
|
26
20
|
},
|
|
27
21
|
});
|
|
28
|
-
const supabaseTime = Date.now() - supabaseStartTime;
|
|
29
|
-
console.log(`[newServerClient] Supabase client creation took: ${supabaseTime}ms`);
|
|
30
|
-
const totalTime = Date.now() - startTime;
|
|
31
|
-
console.log(`[newServerClient] Total client creation time: ${totalTime}ms`);
|
|
32
|
-
return client;
|
|
33
22
|
}
|
|
@@ -100,7 +100,7 @@ export class HerdModule {
|
|
|
100
100
|
console.warn(`[HerdModule] Failed to get zones and actions:`, error);
|
|
101
101
|
return { status: EnumWebResponse.ERROR, data: null };
|
|
102
102
|
}),
|
|
103
|
-
server_get_users_with_herd_access(herd.id).catch((error) => {
|
|
103
|
+
server_get_users_with_herd_access(herd.id, client).catch((error) => {
|
|
104
104
|
console.warn(`[HerdModule] Failed to get user roles:`, error);
|
|
105
105
|
return { status: EnumWebResponse.ERROR, data: null };
|
|
106
106
|
}),
|