diff --git a/deployments/db.js b/deployments/db.js index 0c8acb22..91ac2e7d 100644 --- a/deployments/db.js +++ b/deployments/db.js @@ -100,7 +100,7 @@ db.createCollection( "view", { db.createCollection( "polling_detail", { validator: { $jsonSchema: { bsonType: "object", - required: [ "id","params","session_id","url_path" ], + required: [ "id","params","session_id","domain","url_path" ], properties: { id: { bsonType: "string", @@ -142,7 +142,7 @@ db.kv.createIndex({key: 1, label_id: 1,domain:1,project:1},{ unique: true }); db.label.createIndex({"id": 1}, { unique: true } ); db.label.createIndex({format: 1,domain:1,project:1},{ unique: true }); db.polling_detail.createIndex({"id": 1}, { unique: true } ); -db.polling_detail.createIndex({session:1,domain:1}, { unique: true } ); +db.polling_detail.createIndex({session_id:1,domain:1}, { unique: true } ); db.view.createIndex({"id": 1}, { unique: true } ); db.view.createIndex({display:1,domain:1,project:1},{ unique: true }); //db config diff --git a/server/resource/v1/history_resource.go b/server/resource/v1/history_resource.go index 73ea4422..98a6397c 100644 --- a/server/resource/v1/history_resource.go +++ b/server/resource/v1/history_resource.go @@ -18,6 +18,7 @@ package v1 import ( + "github.com/apache/servicecomb-kie/server/service/mongo/record" "net/http" "github.com/apache/servicecomb-kie/pkg/common" @@ -72,6 +73,35 @@ func (r *HistoryResource) GetRevisions(context *restful.Context) { } } +//GetPollingData get the record of the get or list history +func (r *HistoryResource) GetPollingData(context *restful.Context) { + query := &model.PollingDetail{ + Domain: context.ReadQueryParameter("domain"), + SessionID: context.ReadQueryParameter("sessionId"), + IP: context.ReadQueryParameter("ip"), + ID: context.ReadQueryParameter("id"), + URLPath: context.ReadQueryParameter("urlPath"), + UserAgent: context.ReadQueryParameter("userAgent"), + } + records, err := record.Get(context.Ctx, query) + if err != nil { + if err == service.ErrRecordNotExists { + WriteErrResponse(context, http.StatusNotFound, err.Error(), common.ContentTypeText) + return + } + WriteErrResponse(context, http.StatusInternalServerError, err.Error(), common.ContentTypeText) + return + } + if len(records) == 0 { + WriteErrResponse(context, http.StatusNotFound, "no revisions found", common.ContentTypeText) + return + } + err = writeResponse(context, records) + if err != nil { + openlogging.Error(err.Error()) + } +} + //URLPatterns defined config operations func (r *HistoryResource) URLPatterns() []restful.Route { return []restful.Route{ @@ -93,5 +123,23 @@ func (r *HistoryResource) URLPatterns() []restful.Route { Consumes: []string{goRestful.MIME_JSON, common.ContentTypeYaml}, Produces: []string{goRestful.MIME_JSON, common.ContentTypeYaml}, }, + { + Method: http.MethodGet, + Path: "/v1/{project}/kie/record", + ResourceFunc: r.GetPollingData, + FuncDesc: "get all history record of get and list", + Parameters: []*restful.Parameters{ + DocPathProject, DocPathKeyID, + }, + Returns: []*restful.Returns{ + { + Code: http.StatusOK, + Message: "true", + Model: []model.KVDoc{}, + }, + }, + Consumes: []string{goRestful.MIME_JSON, common.ContentTypeYaml}, + Produces: []string{goRestful.MIME_JSON, common.ContentTypeYaml}, + }, } } diff --git a/server/service/mongo/record/polling_detail_dao.go b/server/service/mongo/record/polling_detail_dao.go index 3e76bcbf..d41d5686 100644 --- a/server/service/mongo/record/polling_detail_dao.go +++ b/server/service/mongo/record/polling_detail_dao.go @@ -20,7 +20,9 @@ package record import ( "context" "github.com/apache/servicecomb-kie/pkg/model" + "github.com/apache/servicecomb-kie/server/service" "github.com/apache/servicecomb-kie/server/service/mongo/session" + "github.com/go-mesh/openlogging" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" ) @@ -46,3 +48,37 @@ func CreateOrUpdate(ctx context.Context, detail *model.PollingDetail) (*model.Po } return detail, nil } + +//Get is to get a +func Get(ctx context.Context, detail *model.PollingDetail) ([]*model.PollingDetail, error) { + collection := session.GetDB().Collection(session.CollectionPollingDetail) + queryFilter := bson.M{ + "domain": detail.Domain, + "session_id": detail.SessionID, + "id": detail.ID, + "ip": detail.IP, + "user_agent": detail.UserAgent, + "url_path": detail.URLPath, + } + cur, err := collection.Find(ctx, queryFilter) + if err != nil { + return nil, err + } + defer cur.Close(ctx) + if cur.Err() != nil { + return nil, err + } + records := make([]*model.PollingDetail, 0) + for cur.Next(ctx) { + curRecord := &model.PollingDetail{} + if err := cur.Decode(curRecord); err != nil { + openlogging.Error("decode to KVs error: " + err.Error()) + return nil, err + } + records = append(records, curRecord) + } + if len(records) == 0 { + return nil, service.ErrRecordNotExists + } + return records, nil +} diff --git a/server/service/service.go b/server/service/service.go index c1451d5c..5720a6ca 100644 --- a/server/service/service.go +++ b/server/service/service.go @@ -35,6 +35,7 @@ var ( //db errors var ( ErrKeyNotExists = errors.New("can not find any key value") + ErrRecordNotExists = errors.New("can not find any polling data") ErrRevisionNotExist = errors.New("revision does not exist") ErrAliasNotGiven = errors.New("label alias not given") )