Skip to content

Enhance pagination and search functionality for nodes in JSON endpoints#734

Merged
javuto merged 5 commits intojmpsec:mainfrom
zhuoyuan-liu:pagination
Oct 14, 2025
Merged

Enhance pagination and search functionality for nodes in JSON endpoints#734
javuto merged 5 commits intojmpsec:mainfrom
zhuoyuan-liu:pagination

Conversation

@zhuoyuan-liu
Copy link
Contributor

@zhuoyuan-liu zhuoyuan-liu commented Oct 14, 2025

#635

This PR:

  • Implemented paginated retrieval of nodes by environment with target filters.
  • Added counting methods for total nodes and filtered search results.
  • Updated front-end to forward ordering parameters for server-side handling.

For now, the query speed is about 10,000× faster than before with 10K nodes, and we can clearly see changes in both the database queries and the request URLs

[2.572ms] [rows:25] SELECT * FROM "osquery_nodes" WHERE environment = 'dev' AND "osquery_nodes"."deleted_at" IS NULL ORDER BY last_seen DESC LIMIT 25
image

@javuto javuto requested review from Copilot and javuto October 14, 2025 13:48
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 server-side pagination and search functionality for node management endpoints to improve performance with large datasets. The implementation includes paginated node retrieval, counting methods for accurate pagination metadata, and client-side ordering parameter forwarding.

  • Added server-side pagination with LIMIT/OFFSET queries instead of loading all nodes into memory
  • Implemented separate counting methods for total and filtered node counts
  • Updated frontend to pass DataTables ordering parameters to the backend

Reviewed Changes

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

File Description
pkg/nodes/nodes.go Added paginated retrieval methods with ordering and counting functionality
cmd/admin/handlers/json-nodes.go Updated handler to use paginated queries and process ordering parameters
cmd/admin/templates/table.html Modified frontend to forward DataTables ordering parameters to server
Comments suppressed due to low confidence (2)

cmd/admin/handlers/json-nodes.go:1

  • The orderBy parameter is directly concatenated into the SQL ORDER BY clause without validation. This could potentially lead to SQL injection if the mapDTColumnToDB function doesn't properly sanitize all possible inputs. Consider using a whitelist validation or GORM's safe ordering methods.
package handlers

cmd/admin/handlers/json-nodes.go:1

  • The orderBy parameter is directly concatenated into the SQL ORDER BY clause without validation. This could potentially lead to SQL injection if the mapDTColumnToDB function doesn't properly sanitize all possible inputs. Consider using a whitelist validation or GORM's safe ordering methods.
package handlers

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +204 to +212
if limit <= 0 { // safety default
limit = 25
}
if limit > 500 { // cap to avoid abuse
limit = 500
}
if offset < 0 {
offset = 0
}
Copy link

Copilot AI Oct 14, 2025

Choose a reason for hiding this comment

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

The limit validation logic is duplicated between GetByEnvPage and SearchByEnvPage methods. Consider extracting this into a helper function to avoid code duplication.

Copilot uses AI. Check for mistakes.
}
var nodes []OsqueryNode
likeTerm := "%" + term + "%"
query := n.DB.Where("environment = ? AND (uuid LIKE ? OR hostname LIKE ? OR localname LIKE ? OR ip_address LIKE ? OR username LIKE ? OR osquery_user LIKE ? OR platform LIKE ? OR osquery_version LIKE ?)", env, likeTerm, likeTerm, likeTerm, likeTerm, likeTerm, likeTerm, likeTerm, likeTerm)
Copy link

Copilot AI Oct 14, 2025

Choose a reason for hiding this comment

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

The complex search query with multiple LIKE conditions is duplicated between SearchByEnvPage and CountSearchByEnv. Consider extracting this query building logic into a helper method to ensure consistency and maintainability.

Copilot uses AI. Check for mistakes.
// CountSearchByEnv counts matching nodes for a search term with target filters
func (n *NodeManager) CountSearchByEnv(env, term, target string, hours int64) (int64, error) {
likeTerm := "%" + term + "%"
query := n.DB.Model(&OsqueryNode{}).Where("environment = ? AND (uuid LIKE ? OR hostname LIKE ? OR localname LIKE ? OR ip_address LIKE ? OR username LIKE ? OR osquery_user LIKE ? OR platform LIKE ? OR osquery_version LIKE ?)", env, likeTerm, likeTerm, likeTerm, likeTerm, likeTerm, likeTerm, likeTerm, likeTerm)
Copy link

Copilot AI Oct 14, 2025

Choose a reason for hiding this comment

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

The complex search query with multiple LIKE conditions is duplicated between SearchByEnvPage and CountSearchByEnv. Consider extracting this query building logic into a helper method to ensure consistency and maintainability.

Copilot uses AI. Check for mistakes.
@javuto javuto added osctrl-admin osctrl-admin related changes ⚡️ performance Performance related issues labels Oct 14, 2025
@javuto javuto merged commit 8257ad9 into jmpsec:main Oct 14, 2025
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

osctrl-admin osctrl-admin related changes ⚡️ performance Performance related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants