Skip to content

Commit

Permalink
Merge 1330bb7 into 51d6e1b
Browse files Browse the repository at this point in the history
  • Loading branch information
GuoYL123 committed Feb 26, 2020
2 parents 51d6e1b + 1330bb7 commit 7a4fa47
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 2 deletions.
4 changes: 2 additions & 2 deletions deployments/db.js
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down
48 changes: 48 additions & 0 deletions server/resource/v1/history_resource.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand All @@ -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},
},
}
}
36 changes: 36 additions & 0 deletions server/service/mongo/record/polling_detail_dao.go
Expand Up @@ -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"
)
Expand All @@ -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
}
1 change: 1 addition & 0 deletions server/service/service.go
Expand Up @@ -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")
)
Expand Down

0 comments on commit 7a4fa47

Please sign in to comment.