Skip to content

feat(search): Add Search Functionality to Resource List in Insights Page #788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ CGO_ENABLED ?= 0
# Check if the SKIP_UI_BUILD flag is set to control the UI building process.
# If the flag is not set, the BUILD_UI variable is assigned the value 'build-ui'.
# If the flag is set, the BUILD_UI variable remains empty.
ifndef SKIP_UI_BUILD
BUILD_UI = build-ui
else
ifeq ($(SKIP_UI_BUILD),true)
BUILD_UI =
else
BUILD_UI = build-ui
endif

# If you encounter an error like "panic: permission denied" on MacOS,
Expand Down Expand Up @@ -148,16 +148,16 @@ fix-license: ## Adds missing license header to repo files

.PHONY: gen-api-spec
gen-api-spec: ## Generate API Specification with OpenAPI format
@which swag > /dev/null || (echo "Installing [email protected] ..."; $(GO) install github.com/swaggo/swag/cmd/[email protected] && echo "Installation complete!\n")
@which $(GOPATH)/bin/swag > /dev/null || (echo "Installing [email protected] ..."; $(GO) install github.com/swaggo/swag/cmd/[email protected] && echo "Installation complete!\n")
# Generate API documentation with OpenAPI format
@swag init --parseDependency --parseInternal --parseDepth 1 -g cmd/karpor/main.go -o api/openapispec/ && echo "🎉 Done!" || (echo "💥 Fail!"; exit 1)
@$(GOPATH)/bin/swag init --parseDependency --parseInternal --parseDepth 1 -g cmd/karpor/main.go -o api/openapispec/ && echo "🎉 Done!" || (echo "💥 Fail!"; exit 1)
# Format swagger comments
@swag fmt -g pkg/**/*.go && echo "🎉 Done!" || (echo "💥 Failed!"; exit 1)
@$(GOPATH)/bin/swag fmt -g pkg/**/*.go && echo "🎉 Done!" || (echo "💥 Failed!"; exit 1)

.PHONY: gen-api-doc
gen-api-doc: gen-api-spec ## Generate API Documentation by API Specification
@which swagger > /dev/null || (echo "Installing [email protected] ..."; $(GO) install github.com/go-swagger/go-swagger/cmd/[email protected] && echo "Installation complete!\n")
@swagger generate markdown -f ./api/openapispec/swagger.json --output=docs/api.md && echo "🎉 Done!" || (echo "💥 Fail!"; exit 1)
@which $(GOPATH)/bin/swagger > /dev/null || (echo "Installing [email protected] ..."; $(GO) install github.com/go-swagger/go-swagger/cmd/[email protected] && echo "Installation complete!\n")
@$(GOPATH)/bin/swagger generate markdown -f ./api/openapispec/swagger.json --output=docs/api.md && echo "🎉 Done!" || (echo "💥 Fail!"; exit 1)

.PHONY: gen-cli-doc
gen-cli-doc: ## Generate CLI Documentation
Expand Down
6 changes: 6 additions & 0 deletions api/openapispec/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2055,6 +2055,12 @@ var doc = `{
"description": "The current page to fetch. Default to 1",
"name": "page",
"in": "query"
},
{
"type": "string",
"description": "The keyword to use for search. Optional",
"name": "keyword",
"in": "query"
}
],
"responses": {
Expand Down
6 changes: 6 additions & 0 deletions api/openapispec/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -2039,6 +2039,12 @@
"description": "The current page to fetch. Default to 1",
"name": "page",
"in": "query"
},
{
"type": "string",
"description": "The keyword to use for search. Optional",
"name": "keyword",
"in": "query"
}
],
"responses": {
Expand Down
4 changes: 4 additions & 0 deletions api/openapispec/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1622,6 +1622,10 @@ paths:
in: query
name: page
type: string
- description: The keyword to use for search. Optional
in: query
name: keyword
type: string
produces:
- application/json
responses:
Expand Down
2 changes: 1 addition & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1712,6 +1712,7 @@ This endpoint returns an array of Kubernetes runtime Object matched using the qu

| Name | Source | Type | Go type | Separator | Required | Default | Description |
|------|--------|------|---------|-----------| :------: |---------|-------------|
| keyword | `query` | string | `string` | | | | The keyword to use for search. Optional |
| page | `query` | string | `string` | | | | The current page to fetch. Default to 1 |
| pageSize | `query` | string | `string` | | | | The size of the page. Default to 10 |
| pattern | `query` | string | `string` | | ✓ | | The search pattern. Can be either sql, dsl or nl. Required |
Expand Down Expand Up @@ -3011,4 +3012,3 @@ Status: Internal Server Error
|------|------|---------|:--------:| ------- |-------------|---------|
| object | [interface{}](#interface)| `interface{}` | | | Object is a JSON compatible map with string, float, int, bool, []interface{}, or</br>map[string]interface{}</br>children. | |


11 changes: 11 additions & 0 deletions pkg/core/handler/search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
package search

import (
"fmt"
"net/http"
"strconv"
"strings"

"github.com/KusionStack/karpor/pkg/core/handler"
"github.com/KusionStack/karpor/pkg/core/manager/ai"
Expand All @@ -38,6 +40,7 @@ import (
// @Param pattern query string true "The search pattern. Can be either sql, dsl or nl. Required"
// @Param pageSize query string false "The size of the page. Default to 10"
// @Param page query string false "The current page to fetch. Default to 1"
// @Param keyword query string false "The keyword to use for search. Optional"
// @Success 200 {array} runtime.Object "Array of runtime.Object"
// @Failure 400 {string} string "Bad Request"
// @Failure 401 {string} string "Unauthorized"
Expand All @@ -57,6 +60,7 @@ func SearchForResource(searchMgr *search.SearchManager, aiMgr *ai.AIManager, sea
searchPattern := r.URL.Query().Get("pattern")
searchPageSize, _ := strconv.Atoi(r.URL.Query().Get("pageSize"))
searchPage, _ := strconv.Atoi(r.URL.Query().Get("page"))
searchKeyword := r.URL.Query().Get("keyword")

format := r.URL.Query().Get("format")
formatter, err := ParseObjectFormatter(format)
Expand All @@ -71,6 +75,13 @@ func SearchForResource(searchMgr *search.SearchManager, aiMgr *ai.AIManager, sea
if searchPage <= 1 {
searchPage = 1
}
if searchKeyword != "" && searchQuery != "" {
if searchPattern == storage.SQLPatternType {
searchKeyword = strings.ReplaceAll(searchKeyword, " ", "")
searchQuery = fmt.Sprintf("%s AND (name LIKE '%%%s%%' OR namespace LIKE '%%%s%%')",
searchQuery, searchKeyword, searchKeyword)
}
}

query := searchQuery

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ const SourceTable = ({ queryStr, tableName }: IProps) => {
<Input
value={keyword}
allowClear
disabled
onChange={event => setKeyword(event?.target?.value)}
placeholder={t('FilterByName')}
suffix={<SearchOutlined />}
Expand Down
Loading