Skip to content

Conversation

@sinamics
Copy link
Owner

@sinamics sinamics commented Dec 4, 2025

No description provided.

@sinamics sinamics requested a review from Copilot December 4, 2025 18:58
@github-actions github-actions bot added documentation Improvements or additions to documentation ztnet Main Application labels Dec 4, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements several performance optimizations for API requests to reduce database queries and HTTP calls. The main focus is on addressing N+1 query problems through caching, parallel execution, and selective data fetching.

Key changes:

  • Introduces a credentials cache with 60-second TTL to avoid repeated database queries for API credentials
  • Creates a new local_network_and_membercount function for lightweight network listings without fetching full member details
  • Optimizes member synchronization to use a single bulk database query instead of N individual queries
  • Converts sequential member detail fetching to parallel execution using Promise.all

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
src/utils/ztApi.ts Adds credentials caching, implements new lightweight network fetching functions, adds timeout to GET requests, and parallelizes member detail fetching
src/server/api/services/memberService.ts Optimizes member synchronization by replacing N database queries with a single bulk query and in-memory Map lookups
src/server/api/routers/networkRouter.ts Updates function calls to use renamed local_network_and_members function
src/server/api/routers/adminRoute.ts Updates function calls to use renamed local_network_and_members function
src/pages/api/v1/org/[orgid]/network/index.ts Switches to lightweight local_network_and_membercount for list operations
src/pages/api/v1/org/[orgid]/network/[nwid]/member/[memberId]/index.ts Optimizes single member fetch to use member_details instead of fetching all members
src/pages/api/v1/org/[orgid]/network/[nwid]/index.ts Switches to lightweight local_network_and_membercount for individual network operations
src/pages/api/v1/network/index.ts Switches to lightweight local_network_and_membercount for user network listings
src/pages/api/v1/network/[id]/member/index.ts Updates function call to use renamed local_network_and_members function
src/pages/api/v1/network/[id]/index.ts Switches to lightweight local_network_and_membercount for individual network fetching
src/pages/api/tests/v1/network/networkById.test.ts Updates test mocks to reflect renamed function and new memberCount field
src/cronTasks.ts Updates function call to use renamed local_network_and_members function

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

}

return {
network: { ...network },
Copy link

Copilot AI Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary object spread. The { ...network } spread creates a shallow copy of the network object, but there's no apparent reason to do so here. The network object is returned directly from getData and not modified. Consider returning network directly to avoid the unnecessary allocation.

Suggested change
network: { ...network },
network,

Copilot uses AI. Check for mistakes.
);

return {
network: { ...network },
Copy link

Copilot AI Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary object spread. The { ...network } spread creates a shallow copy of the network object, but there's no apparent reason to do so here. Consider returning network directly to avoid the unnecessary allocation.

Suggested change
network: { ...network },
network: network,

Copilot uses AI. Check for mistakes.
Comment on lines +114 to +115
// Store in cache
credentialsCache.set(userId, { credentials, timestamp: now });
Copy link

Copilot AI Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The credentials cache lacks cache invalidation when user credentials are updated. If a user updates their API keys or controller URLs, they'll continue to receive stale credentials for up to 60 seconds. Consider implementing a cache invalidation mechanism that's triggered when user options are updated, or documenting this limitation.

Copilot uses AI. Check for mistakes.
* @param isCentral - Whether to use central API (default: false)
* @returns Network details with member count only
*/
export const local_network_and_membercount = async (
Copy link

Copilot AI Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function name inconsistency: This function is named local_network_and_membercount but accepts an isCentral parameter and can work with both local and central APIs. The name suggests it only works with local controllers, but it's actually API-agnostic. Consider renaming to network_and_membercount to match the actual functionality and be consistent with the similarly generic local_network_and_members function (which also has a misleading name).

Suggested change
export const local_network_and_membercount = async (
export const network_and_membercount = async (

Copilot uses AI. Check for mistakes.
@sinamics sinamics changed the title Optimize api requests Performance optimizations for API requests Dec 4, 2025
@sinamics sinamics merged commit 0ce41ea into main Dec 4, 2025
3 checks passed
@sinamics sinamics deleted the optimize-api-requests branch December 4, 2025 19:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation ztnet Main Application

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants