Skip to content
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

feat: add tables and table data #112

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
438 changes: 220 additions & 218 deletions cmd/api-server/service/proxy.go

Large diffs are not rendered by default.

442 changes: 225 additions & 217 deletions cmd/api-server/service/routers.go

Large diffs are not rendered by default.

101 changes: 101 additions & 0 deletions cmd/api-server/service/table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/

package service

import (
"errors"
"net/http"
"net/url"
"strconv"

"github.com/go-chi/chi/v5"
"github.com/go-chi/render"

"github.com/TencentBlueKing/bk-bscp/internal/iam/auth"
tableparser "github.com/TencentBlueKing/bk-bscp/internal/table-parser"
"github.com/TencentBlueKing/bk-bscp/pkg/dal/table"
"github.com/TencentBlueKing/bk-bscp/pkg/i18n"
"github.com/TencentBlueKing/bk-bscp/pkg/kit"
pbcs "github.com/TencentBlueKing/bk-bscp/pkg/protocol/config-server"
"github.com/TencentBlueKing/bk-bscp/pkg/rest"
"github.com/TencentBlueKing/bk-bscp/pkg/types"
)

type tableService struct {
authorizer auth.Authorizer
cfgClient pbcs.ConfigClient
}

func newTableService(authorizer auth.Authorizer,
cfgClient pbcs.ConfigClient) *tableService {
s := &tableService{
authorizer: authorizer,
cfgClient: cfgClient,
}
return s
}

func (m *tableService) Import(w http.ResponseWriter, r *http.Request) {
kit := kit.MustGetKit(r.Context())
// Ensure r.Body is closed after reading
defer r.Body.Close()

dataSourceMappingIdStr := chi.URLParam(r, "data_source_mapping_id")
dataSourceMappingId, _ := strconv.Atoi(dataSourceMappingIdStr)

format := chi.URLParam(r, "format")
format, err := url.PathUnescape(format)
if err != nil {
_ = render.Render(w, r, rest.BadRequest(errors.New(i18n.T(kit, "invalid file name"))))
return
}

var columns []table.Columns_
var rows []map[string]interface{}
var tableName string

switch format {
case "sql", "SQL":
tableName, columns, rows, err = tableparser.NewSqlImport().Import(kit, r.Body)
if err != nil {
_ = render.Render(w, r, rest.BadRequest(errors.New(i18n.T(kit, "failed to parse SQL: %v", err))))
return
}
case "xls", "xlsx":
tableName, columns, rows, err = tableparser.NewExcelImport().Import(kit, r.Body)
if err != nil {
_ = render.Render(w, r, rest.BadRequest(errors.New(i18n.T(kit, "failed to parse SQL: %v", err))))
return
}
case "csv":
default:
return
}

cols := make([]types.Columns_, 0)
// 不是0表示已存在表结构和表数据,需要对比结构和数据
if dataSourceMappingId == 0 {
for _, v := range columns {
cols = append(cols, types.Columns_{
Columns_: v,
Status: table.KvStateAdd.String(),
})
}
}

_ = render.Render(w, r, rest.OKRender(&types.TableImportResp{
TableName: tableName,
Columns: cols,
Rows: rows,
}))
}
Loading