Skip to content

Commit

Permalink
feat: add tables and table data
Browse files Browse the repository at this point in the history
  • Loading branch information
Ambition9186 committed Jan 21, 2025
1 parent 0dd4118 commit f5faa17
Show file tree
Hide file tree
Showing 76 changed files with 41,410 additions and 29,185 deletions.
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

0 comments on commit f5faa17

Please sign in to comment.