Skip to content

Expedite spec recommendation by fully using db query#2116

Merged
cb-github-robot merged 3 commits intocloud-barista:mainfrom
seokho-son:main
Aug 26, 2025
Merged

Expedite spec recommendation by fully using db query#2116
cb-github-robot merged 3 commits intocloud-barista:mainfrom
seokho-son:main

Conversation

@seokho-son
Copy link
Copy Markdown
Member

speed up

ex)

from

elapsedTime=2179.311783 ms

to
elapsedTime=117.170231 ms

Signed-off-by: Seokho Son <shsongist@gmail.com>
Signed-off-by: Seokho Son <shsongist@gmail.com>
Signed-off-by: Seokho Son <shsongist@gmail.com>
Copilot AI review requested due to automatic review settings August 26, 2025 10:20
@seokho-son seokho-son requested a review from yunkon-kim as a code owner August 26, 2025 10:20
@github-actions github-actions bot added the src label Aug 26, 2025
Copy link
Copy Markdown
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 optimizes the spec recommendation system by moving from CSV-based to database-driven latency calculations. The changes include migrating runtime latency data to a database table, implementing database query optimization with indexes, and enhancing the filtering API with limit functionality for better performance.

  • Replaces CSV file-based latency lookups with database table queries for improved performance
  • Adds comprehensive database indexes for spec filtering and latency queries
  • Implements limit parameter in filtering API to control result set size

Reviewed Changes

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

Show a summary per file
File Description
src/main.go Migrates latency data from CSV to database and adds optimized indexes
src/interface/rest/server/resource/spec.go Updates API to support limit parameter for result pagination
src/interface/rest/docs/ Updates API documentation to reflect limit parameter changes
src/core/resource/spec.go Enhances filtering with database query optimization and limit support
src/core/model/spec.go Adds TbLatencyInfo model and database operations for latency data
src/core/infra/recommendation.go Refactors to use database-based sorting and latency lookups
src/core/infra/provisioning.go Minor variable naming improvements

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

Comment on lines +419 to +423
defer func() {
if closeErr := file.Close(); closeErr != nil {
log.Warn().Err(closeErr).Msg("Failed to close CSV file")
}
if v[0] == "" {
}()
Copy link

Copilot AI Aug 26, 2025

Choose a reason for hiding this comment

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

The deferred function captures closeErr in a closure but doesn't handle the original error from file.Close(). Consider using defer file.Close() directly or handle both errors appropriately.

Copilot uses AI. Check for mistakes.
Comment on lines +1689 to +1695
likeSearchFields := []string{
"AcceleratorModel", // e.g., "NVIDIA H100" -> search with "NVIDIA"
"Description", // Description text partial search
// Note: AcceleratorType removed - uses exact matching for better performance
// since values are typically standardized (GPU, TPU, etc.)
}

Copy link

Copilot AI Aug 26, 2025

Choose a reason for hiding this comment

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

The likeSearchFields slice is created on every function call. Consider moving this to a package-level variable or constant to avoid repeated allocations.

Suggested change
likeSearchFields := []string{
"AcceleratorModel", // e.g., "NVIDIA H100" -> search with "NVIDIA"
"Description", // Description text partial search
// Note: AcceleratorType removed - uses exact matching for better performance
// since values are typically standardized (GPU, TPU, etc.)
}

Copilot uses AI. Check for mistakes.
Comment on lines +469 to +476
latencySubquery := fmt.Sprintf(`
COALESCE((
SELECT latency_ms
FROM tb_latency_infos
WHERE source_region = '%s'
AND target_region = tb_spec_infos.provider_name || '+' || tb_spec_infos.region_name
LIMIT 1
), 999999)`, targetRegion)
Copy link

Copilot AI Aug 26, 2025

Choose a reason for hiding this comment

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

Using fmt.Sprintf to build SQL queries with user input can lead to SQL injection vulnerabilities. Consider using parameterized queries or proper SQL escaping for the targetRegion parameter.

Suggested change
latencySubquery := fmt.Sprintf(`
COALESCE((
SELECT latency_ms
FROM tb_latency_infos
WHERE source_region = '%s'
AND target_region = tb_spec_infos.provider_name || '+' || tb_spec_infos.region_name
LIMIT 1
), 999999)`, targetRegion)
), 999999)`, safeTargetRegion)

Copilot uses AI. Check for mistakes.
Comment on lines 533 to +537
vmObj, err := GetVmObject(nsId, mciId, vmIdList[0])
if err != nil {
temp := &model.TbMciInfo{}
return temp, err
}
Copy link

Copilot AI Aug 26, 2025

Choose a reason for hiding this comment

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

There's a potential index out of bounds error if vmIdList is empty. The code should check len(vmIdList) > 0 before accessing vmIdList[0].

Copilot uses AI. Check for mistakes.
@seokho-son
Copy link
Copy Markdown
Member Author

/approve

@github-actions github-actions bot added the approved This PR is approved and will be merged soon. label Aug 26, 2025
@cb-github-robot cb-github-robot merged commit 2f8b706 into cloud-barista:main Aug 26, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved This PR is approved and will be merged soon. src

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants