Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: export log backend (#8157)
Danny Xu107c3f655172
diff --git a/backend/api/v1/logging_service.go b/backend/api/v1/logging_service.goindex eb3cfa639c..c31ea9676e 100644--- a/backend/api/v1/logging_service.go+++ b/backend/api/v1/logging_service.go@@ -292,6 +292,52 @@ func (s *LoggingService) GetLog(ctx context.Context, request *v1pb.GetLogRequestreturn logEntity, nil}+// ExportLogs exports logs.+func (s *LoggingService) ExportLogs(ctx context.Context, request *v1pb.ExportLogsRequest) (*v1pb.ExportLogsResponse, error) {+ activities, err := s.store.ListActivityV2(ctx, &store.FindActivityMessage{})+ if err != nil {+ return nil, status.Errorf(codes.Internal, "failed to list activity: %v", err.Error())+ }+ result := &v1pb.QueryResult{+ ColumnNames: []string{"time", "level", "action", "actor", "resource", "comment", "payload"},+ }+ for _, activity := range activities {+ logEntity, err := convertToLogEntity(ctx, s.store, activity)+ if err != nil {+ return nil, status.Errorf(codes.Internal, "failed to convert log entity, error: %v", err)+ }+ result.Rows = append(result.Rows, &v1pb.QueryRow{Values: []*v1pb.RowValue{+ {Kind: &v1pb.RowValue_StringValue{StringValue: logEntity.CreateTime.AsTime().Format(time.RFC3339)}},+ {Kind: &v1pb.RowValue_StringValue{StringValue: logEntity.Level.String()}},+ {Kind: &v1pb.RowValue_StringValue{StringValue: logEntity.Action.String()}},+ {Kind: &v1pb.RowValue_StringValue{StringValue: logEntity.Creator}},+ {Kind: &v1pb.RowValue_StringValue{StringValue: logEntity.Resource}},+ {Kind: &v1pb.RowValue_StringValue{StringValue: logEntity.Comment}},+ {Kind: &v1pb.RowValue_StringValue{StringValue: logEntity.Payload}},+ }})+ }++ var content []byte+ switch request.Format {+ case v1pb.ExportFormat_CSV:+ if content, err = exportCSV(result); err != nil {+ return nil, err+ }+ case v1pb.ExportFormat_JSON:+ if content, err = exportJSON(result); err != nil {+ return nil, err+ }+ case v1pb.ExportFormat_XLSX:+ if content, err = exportXLSX(result); err != nil {+ return nil, err+ }+ default:+ return nil, status.Errorf(codes.InvalidArgument, "unsupported export format: %s", request.Format.String())+ }++ return &v1pb.ExportLogsResponse{Content: content}, nil+}+func convertToLogEntity(ctx context.Context, db *store.Store, activity *store.ActivityMessage) (*v1pb.LogEntity, error) {resource := ""switch activity.Type {diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.goindex 5741d2eeab..74449b9a78 100644--- a/backend/api/v1/sql_service.go+++ b/backend/api/v1/sql_service.go@@ -372,11 +372,11 @@ func (s *SQLService) doExport(ctx context.Context, request *v1pb.ExportRequest,var content []byteswitch request.Format {case v1pb.ExportFormat_CSV:- if content, err = s.exportCSV(result[0]); err != nil {+ if content, err = exportCSV(result[0]); err != nil {return nil, durationNs, err}case v1pb.ExportFormat_JSON:- if content, err = s.exportJSON(result[0]); err != nil {+ if content, err = exportJSON(result[0]); err != nil {return nil, durationNs, err}case v1pb.ExportFormat_SQL:@@ -392,7 +392,7 @@ func (s *SQLService) doExport(ctx context.Context, request *v1pb.ExportRequest,return nil, durationNs, err}case v1pb.ExportFormat_XLSX:- if content, err = s.exportXLSX(result[0]); err != nil {+ if content, err = exportXLSX(result[0]); err != nil {return nil, durationNs, err}default:@@ -401,7 +401,7 @@ func (s *SQLService) doExport(ctx context.Context, request *v1pb.ExportRequest,return content, durationNs, nil}-func (*SQLService) exportCSV(result *v1pb.QueryResult) ([]byte, error) {+func exportCSV(result *v1pb.QueryResult) ([]byte, error) {var buf bytes.Bufferif _, err := buf.WriteString(strings.Join(result.ColumnNames, ",")); err != nil {return nil, err@@ -639,7 +639,7 @@ func convertValueValueToBytes(value *structpb.Value) []byte {}}-func (*SQLService) exportJSON(result *v1pb.QueryResult) ([]byte, error) {+func exportJSON(result *v1pb.QueryResult) ([]byte, error) {var results []map[string]anyfor _, row := range result.Rows {m := make(map[string]any)@@ -687,7 +687,7 @@ const (excelMaxColumn = 18278)-func (*SQLService) exportXLSX(result *v1pb.QueryResult) ([]byte, error) {+func exportXLSX(result *v1pb.QueryResult) ([]byte, error) {f := excelize.NewFile()defer f.Close()index, err := f.NewSheet("Sheet1")