From e6167449b070bb3646a77ac9c3d920065824bea0 Mon Sep 17 00:00:00 2001 From: bjwswang Date: Thu, 28 Mar 2024 02:59:51 +0000 Subject: [PATCH] feat: able to add pg datasource from apiserver Signed-off-by: bjwswang --- apiserver/graph/generated/generated.go | 234 ++++++++++++++++++++- apiserver/graph/generated/models_gen.go | 17 ++ apiserver/graph/schema/datasource.gql | 9 + apiserver/graph/schema/datasource.graphqls | 22 ++ apiserver/pkg/datasource/datasource.go | 21 +- 5 files changed, 300 insertions(+), 3 deletions(-) diff --git a/apiserver/graph/generated/generated.go b/apiserver/graph/generated/generated.go index 031003f59..fec796689 100644 --- a/apiserver/graph/generated/generated.go +++ b/apiserver/graph/generated/generated.go @@ -302,6 +302,7 @@ type ComplexityRoot struct { Name func(childComplexity int) int Namespace func(childComplexity int) int Oss func(childComplexity int) int + Pg func(childComplexity int) int Status func(childComplexity int) int Type func(childComplexity int) int UpdateTimestamp func(childComplexity int) int @@ -608,6 +609,10 @@ type ComplexityRoot struct { VolumeName func(childComplexity int) int } + Pg struct { + Database func(childComplexity int) int + } + Query struct { Application func(childComplexity int) int DataProcess func(childComplexity int) int @@ -2201,6 +2206,13 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.Datasource.Oss(childComplexity), true + case "Datasource.pg": + if e.complexity.Datasource.Pg == nil { + break + } + + return e.complexity.Datasource.Pg(childComplexity), true + case "Datasource.status": if e.complexity.Datasource.Status == nil { break @@ -3785,6 +3797,13 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.PersistentVolumeClaimSpec.VolumeName(childComplexity), true + case "Pg.database": + if e.complexity.Pg.Database == nil { + break + } + + return e.complexity.Pg.Database(childComplexity), true + case "Query.Application": if e.complexity.Query.Application == nil { break @@ -4864,6 +4883,7 @@ func (e *executableSchema) Exec(ctx context.Context) graphql.ResponseHandler { ec.unmarshalInputOssInput, ec.unmarshalInputParameterInput, ec.unmarshalInputPersistentVolumeClaimSpecInput, + ec.unmarshalInputPgInput, ec.unmarshalInputRAGDatasetInput, ec.unmarshalInputRAGMetricInput, ec.unmarshalInputRemoveDuplicateConfig, @@ -5935,6 +5955,12 @@ type Oss { object: String } +"""Postgresql的使用信息""" +type Pg { + """所用的数据库名称""" + database: String +} + type Web { recommendIntervalTime: Int } @@ -5985,6 +6011,12 @@ type Datasource { """ oss: Oss + """ + Postgresql访问信息 + 规则: 非空代表当前数据源为Postgresql数据源 + """ + pg: Pg + """ Web数据访问信息 规则: 非空代表当前数据源为web在线数据 @@ -6008,6 +6040,10 @@ input OssInput { object: String } +input PgInput { + database: String! +} + input WebInput { recommendIntervalTime: Int! } @@ -6043,6 +6079,9 @@ input CreateDatasourceInput { """数据源为对象存储类型时的输入""" ossinput: OssInput + """数据源为Postgresql时的输入""" + pginput: PgInput + """数据源为Web数据时的输入""" webinput: WebInput } @@ -6076,6 +6115,9 @@ input UpdateDatasourceInput { """数据源为对象存储类型时的输入""" ossinput: OssInput + """数据源为Postgresql时的输入""" + pginput: PgInput + """数据源为Web数据时的输入""" webinput: WebInput } @@ -16754,6 +16796,51 @@ func (ec *executionContext) fieldContext_Datasource_oss(ctx context.Context, fie return fc, nil } +func (ec *executionContext) _Datasource_pg(ctx context.Context, field graphql.CollectedField, obj *Datasource) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Datasource_pg(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.Pg, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + return graphql.Null + } + res := resTmp.(*Pg) + fc.Result = res + return ec.marshalOPg2ᚖgithubᚗcomᚋkubeagiᚋarcadiaᚋapiserverᚋgraphᚋgeneratedᚐPg(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_Datasource_pg(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "Datasource", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + switch field.Name { + case "database": + return ec.fieldContext_Pg_database(ctx, field) + } + return nil, fmt.Errorf("no field named %q was found under type Pg", field.Name) + }, + } + return fc, nil +} + func (ec *executionContext) _Datasource_web(ctx context.Context, field graphql.CollectedField, obj *Datasource) (ret graphql.Marshaler) { fc, err := ec.fieldContext_Datasource_web(ctx, field) if err != nil { @@ -17024,6 +17111,8 @@ func (ec *executionContext) fieldContext_DatasourceMutation_createDatasource(ctx return ec.fieldContext_Datasource_type(ctx, field) case "oss": return ec.fieldContext_Datasource_oss(ctx, field) + case "pg": + return ec.fieldContext_Datasource_pg(ctx, field) case "web": return ec.fieldContext_Datasource_web(ctx, field) case "status": @@ -17113,6 +17202,8 @@ func (ec *executionContext) fieldContext_DatasourceMutation_updateDatasource(ctx return ec.fieldContext_Datasource_type(ctx, field) case "oss": return ec.fieldContext_Datasource_oss(ctx, field) + case "pg": + return ec.fieldContext_Datasource_pg(ctx, field) case "web": return ec.fieldContext_Datasource_web(ctx, field) case "status": @@ -17254,6 +17345,8 @@ func (ec *executionContext) fieldContext_DatasourceQuery_getDatasource(ctx conte return ec.fieldContext_Datasource_type(ctx, field) case "oss": return ec.fieldContext_Datasource_oss(ctx, field) + case "pg": + return ec.fieldContext_Datasource_pg(ctx, field) case "web": return ec.fieldContext_Datasource_web(ctx, field) case "status": @@ -17343,6 +17436,8 @@ func (ec *executionContext) fieldContext_DatasourceQuery_checkDatasource(ctx con return ec.fieldContext_Datasource_type(ctx, field) case "oss": return ec.fieldContext_Datasource_oss(ctx, field) + case "pg": + return ec.fieldContext_Datasource_pg(ctx, field) case "web": return ec.fieldContext_Datasource_web(ctx, field) case "status": @@ -26641,6 +26736,47 @@ func (ec *executionContext) fieldContext_PersistentVolumeClaimSpec_dataSourceRef return fc, nil } +func (ec *executionContext) _Pg_database(ctx context.Context, field graphql.CollectedField, obj *Pg) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Pg_database(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.Database, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + return graphql.Null + } + res := resTmp.(*string) + fc.Result = res + return ec.marshalOString2ᚖstring(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_Pg_database(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "Pg", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return nil, errors.New("field of type String does not have child fields") + }, + } + return fc, nil +} + func (ec *executionContext) _Query_hello(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { fc, err := ec.fieldContext_Query_hello(ctx, field) if err != nil { @@ -35555,7 +35691,7 @@ func (ec *executionContext) unmarshalInputCreateDatasourceInput(ctx context.Cont asMap[k] = v } - fieldsInOrder := [...]string{"name", "namespace", "labels", "annotations", "displayName", "description", "endpointinput", "ossinput", "webinput"} + fieldsInOrder := [...]string{"name", "namespace", "labels", "annotations", "displayName", "description", "endpointinput", "ossinput", "pginput", "webinput"} for _, k := range fieldsInOrder { v, ok := asMap[k] if !ok { @@ -35618,6 +35754,13 @@ func (ec *executionContext) unmarshalInputCreateDatasourceInput(ctx context.Cont return it, err } it.Ossinput = data + case "pginput": + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("pginput")) + data, err := ec.unmarshalOPgInput2ᚖgithubᚗcomᚋkubeagiᚋarcadiaᚋapiserverᚋgraphᚋgeneratedᚐPgInput(ctx, v) + if err != nil { + return it, err + } + it.Pginput = data case "webinput": ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("webinput")) data, err := ec.unmarshalOWebInput2ᚖgithubᚗcomᚋkubeagiᚋarcadiaᚋapiserverᚋgraphᚋgeneratedᚐWebInput(ctx, v) @@ -37889,6 +38032,33 @@ func (ec *executionContext) unmarshalInputPersistentVolumeClaimSpecInput(ctx con return it, nil } +func (ec *executionContext) unmarshalInputPgInput(ctx context.Context, obj interface{}) (PgInput, error) { + var it PgInput + asMap := map[string]interface{}{} + for k, v := range obj.(map[string]interface{}) { + asMap[k] = v + } + + fieldsInOrder := [...]string{"database"} + for _, k := range fieldsInOrder { + v, ok := asMap[k] + if !ok { + continue + } + switch k { + case "database": + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("database")) + data, err := ec.unmarshalNString2string(ctx, v) + if err != nil { + return it, err + } + it.Database = data + } + } + + return it, nil +} + func (ec *executionContext) unmarshalInputRAGDatasetInput(ctx context.Context, obj interface{}) (RAGDatasetInput, error) { var it RAGDatasetInput asMap := map[string]interface{}{} @@ -38564,7 +38734,7 @@ func (ec *executionContext) unmarshalInputUpdateDatasourceInput(ctx context.Cont asMap[k] = v } - fieldsInOrder := [...]string{"name", "namespace", "labels", "annotations", "displayName", "description", "endpointinput", "ossinput", "webinput"} + fieldsInOrder := [...]string{"name", "namespace", "labels", "annotations", "displayName", "description", "endpointinput", "ossinput", "pginput", "webinput"} for _, k := range fieldsInOrder { v, ok := asMap[k] if !ok { @@ -38627,6 +38797,13 @@ func (ec *executionContext) unmarshalInputUpdateDatasourceInput(ctx context.Cont return it, err } it.Ossinput = data + case "pginput": + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("pginput")) + data, err := ec.unmarshalOPgInput2ᚖgithubᚗcomᚋkubeagiᚋarcadiaᚋapiserverᚋgraphᚋgeneratedᚐPgInput(ctx, v) + if err != nil { + return it, err + } + it.Pginput = data case "webinput": ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("webinput")) data, err := ec.unmarshalOWebInput2ᚖgithubᚗcomᚋkubeagiᚋarcadiaᚋapiserverᚋgraphᚋgeneratedᚐWebInput(ctx, v) @@ -41532,6 +41709,8 @@ func (ec *executionContext) _Datasource(ctx context.Context, sel ast.SelectionSe } case "oss": out.Values[i] = ec._Datasource_oss(ctx, field, obj) + case "pg": + out.Values[i] = ec._Datasource_pg(ctx, field, obj) case "web": out.Values[i] = ec._Datasource_web(ctx, field, obj) case "status": @@ -44395,6 +44574,42 @@ func (ec *executionContext) _PersistentVolumeClaimSpec(ctx context.Context, sel return out } +var pgImplementors = []string{"Pg"} + +func (ec *executionContext) _Pg(ctx context.Context, sel ast.SelectionSet, obj *Pg) graphql.Marshaler { + fields := graphql.CollectFields(ec.OperationContext, sel, pgImplementors) + + out := graphql.NewFieldSet(fields) + deferred := make(map[string]*graphql.FieldSet) + for i, field := range fields { + switch field.Name { + case "__typename": + out.Values[i] = graphql.MarshalString("Pg") + case "database": + out.Values[i] = ec._Pg_database(ctx, field, obj) + default: + panic("unknown field " + strconv.Quote(field.Name)) + } + } + out.Dispatch(ctx) + if out.Invalids > 0 { + return graphql.Null + } + + atomic.AddInt32(&ec.deferred, int32(len(deferred))) + + for label, dfs := range deferred { + ec.processDeferredGroup(graphql.DeferredGroup{ + Label: label, + Path: graphql.GetPath(ctx), + FieldSet: dfs, + Context: ctx, + }) + } + + return out +} + var queryImplementors = []string{"Query"} func (ec *executionContext) _Query(ctx context.Context, sel ast.SelectionSet) graphql.Marshaler { @@ -49323,6 +49538,21 @@ func (ec *executionContext) unmarshalOPersistentVolumeClaimSpecInput2ᚖgithub return &res, graphql.ErrorOnPath(ctx, err) } +func (ec *executionContext) marshalOPg2ᚖgithubᚗcomᚋkubeagiᚋarcadiaᚋapiserverᚋgraphᚋgeneratedᚐPg(ctx context.Context, sel ast.SelectionSet, v *Pg) graphql.Marshaler { + if v == nil { + return graphql.Null + } + return ec._Pg(ctx, sel, v) +} + +func (ec *executionContext) unmarshalOPgInput2ᚖgithubᚗcomᚋkubeagiᚋarcadiaᚋapiserverᚋgraphᚋgeneratedᚐPgInput(ctx context.Context, v interface{}) (*PgInput, error) { + if v == nil { + return nil, nil + } + res, err := ec.unmarshalInputPgInput(ctx, v) + return &res, graphql.ErrorOnPath(ctx, err) +} + func (ec *executionContext) unmarshalORAGDatasetInput2ᚕᚖgithubᚗcomᚋkubeagiᚋarcadiaᚋapiserverᚋgraphᚋgeneratedᚐRAGDatasetInputᚄ(ctx context.Context, v interface{}) ([]*RAGDatasetInput, error) { if v == nil { return nil, nil diff --git a/apiserver/graph/generated/models_gen.go b/apiserver/graph/generated/models_gen.go index 958cb7acb..3c399bf27 100644 --- a/apiserver/graph/generated/models_gen.go +++ b/apiserver/graph/generated/models_gen.go @@ -219,6 +219,8 @@ type CreateDatasourceInput struct { Endpointinput EndpointInput `json:"endpointinput"` // 数据源为对象存储类型时的输入 Ossinput *OssInput `json:"ossinput,omitempty"` + // 数据源为Postgresql时的输入 + Pginput *PgInput `json:"pginput,omitempty"` // 数据源为Web数据时的输入 Webinput *WebInput `json:"webinput,omitempty"` } @@ -669,6 +671,9 @@ type Datasource struct { // 对象存储访问信息 // 规则: 非空代表当前数据源为对象存储数据源 Oss *Oss `json:"oss,omitempty"` + // Postgresql访问信息 + // 规则: 非空代表当前数据源为Postgresql数据源 + Pg *Pg `json:"pg,omitempty"` // Web数据访问信息 // 规则: 非空代表当前数据源为web在线数据 Web *Web `json:"web,omitempty"` @@ -1394,6 +1399,16 @@ type PersistentVolumeClaimSpecInput struct { DataSourceRef *TypedObjectReferenceInput `json:"dataSourceRef,omitempty"` } +// Postgresql的使用信息 +type Pg struct { + // 所用的数据库名称 + Database *string `json:"database,omitempty"` +} + +type PgInput struct { + Database string `json:"database"` +} + type Query struct { } @@ -1745,6 +1760,8 @@ type UpdateDatasourceInput struct { Endpointinput *EndpointInput `json:"endpointinput,omitempty"` // 数据源为对象存储类型时的输入 Ossinput *OssInput `json:"ossinput,omitempty"` + // 数据源为Postgresql时的输入 + Pginput *PgInput `json:"pginput,omitempty"` // 数据源为Web数据时的输入 Webinput *WebInput `json:"webinput,omitempty"` } diff --git a/apiserver/graph/schema/datasource.gql b/apiserver/graph/schema/datasource.gql index a3ac8d6db..02d354380 100644 --- a/apiserver/graph/schema/datasource.gql +++ b/apiserver/graph/schema/datasource.gql @@ -21,6 +21,9 @@ mutation createDatasource($input: CreateDatasourceInput!) { bucket object } + pg { + database + } web { recommendIntervalTime } @@ -55,6 +58,9 @@ mutation updateDatasource($input: UpdateDatasourceInput) { bucket object } + pg { + database + } web { recommendIntervalTime } @@ -101,6 +107,9 @@ query listDatasources($input: ListCommonInput!) { bucket object } + pg { + database + } web { recommendIntervalTime } diff --git a/apiserver/graph/schema/datasource.graphqls b/apiserver/graph/schema/datasource.graphqls index 9c9a14140..192ff5def 100644 --- a/apiserver/graph/schema/datasource.graphqls +++ b/apiserver/graph/schema/datasource.graphqls @@ -16,6 +16,12 @@ type Oss { object: String } +"""Postgresql的使用信息""" +type Pg { + """所用的数据库名称""" + database: String +} + type Web { recommendIntervalTime: Int } @@ -66,6 +72,12 @@ type Datasource { """ oss: Oss + """ + Postgresql访问信息 + 规则: 非空代表当前数据源为Postgresql数据源 + """ + pg: Pg + """ Web数据访问信息 规则: 非空代表当前数据源为web在线数据 @@ -89,6 +101,10 @@ input OssInput { object: String } +input PgInput { + database: String! +} + input WebInput { recommendIntervalTime: Int! } @@ -124,6 +140,9 @@ input CreateDatasourceInput { """数据源为对象存储类型时的输入""" ossinput: OssInput + """数据源为Postgresql时的输入""" + pginput: PgInput + """数据源为Web数据时的输入""" webinput: WebInput } @@ -157,6 +176,9 @@ input UpdateDatasourceInput { """数据源为对象存储类型时的输入""" ossinput: OssInput + """数据源为Postgresql时的输入""" + pginput: PgInput + """数据源为Web数据时的输入""" webinput: WebInput } diff --git a/apiserver/pkg/datasource/datasource.go b/apiserver/pkg/datasource/datasource.go index 00ac4fd29..d46c32ed0 100644 --- a/apiserver/pkg/datasource/datasource.go +++ b/apiserver/pkg/datasource/datasource.go @@ -73,6 +73,10 @@ func datasource2model(datasource *v1alpha1.Datasource) (*generated.Datasource, e oss.Bucket = &datasource.Spec.OSS.Bucket oss.Object = &datasource.Spec.OSS.Object } + pg := generated.Pg{} + if datasource.Spec.PostgreSQL != nil { + pg.Database = &datasource.Spec.PostgreSQL.Database + } web := generated.Web{} if datasource.Spec.Web != nil { web.RecommendIntervalTime = &datasource.Spec.Web.RecommendIntervalTime @@ -90,6 +94,7 @@ func datasource2model(datasource *v1alpha1.Datasource) (*generated.Datasource, e Endpoint: &endpoint, Type: string(datasource.Spec.Type()), Oss: &oss, + Pg: &pg, Web: &web, Status: &status, Message: &message, @@ -118,6 +123,7 @@ func CreateDatasource(ctx context.Context, c client.Client, input generated.Crea } datasource.Spec.Endpoint = endpoint + // For object storage service if input.Ossinput != nil { datasource.Spec.OSS = &v1alpha1.OSS{ Bucket: input.Ossinput.Bucket, @@ -126,7 +132,13 @@ func CreateDatasource(ctx context.Context, c client.Client, input generated.Crea datasource.Spec.OSS.Object = *input.Ossinput.Object } } - + // For postgresql + if input.Pginput != nil { + datasource.Spec.PostgreSQL = &v1alpha1.PostgreSQL{ + Database: input.Pginput.Database, + } + } + // For web input if input.Webinput != nil { datasource.Spec.Web = &v1alpha1.Web{ RecommendIntervalTime: input.Webinput.RecommendIntervalTime, @@ -181,6 +193,13 @@ func UpdateDatasource(ctx context.Context, c client.Client, input *generated.Upd datasource.Spec.OSS = oss } + // Update postgresql + if input.Pginput != nil { + datasource.Spec.PostgreSQL = &v1alpha1.PostgreSQL{ + Database: input.Pginput.Database, + } + } + // Update webinput if input.Webinput != nil { datasource.Spec.Web = &v1alpha1.Web{