Skip to content

Commit

Permalink
add get interface for polling data
Browse files Browse the repository at this point in the history
  • Loading branch information
GuoYL123 committed Mar 2, 2020
1 parent 51d6e1b commit dee28ca
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 10 deletions.
4 changes: 2 additions & 2 deletions deployments/db.js
Original file line number Diff line number Diff line change
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
4 changes: 2 additions & 2 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ const (
QueryParamRev = "revision"
QueryParamMatch = "match"
QueryParamKeyID = "kv_id"
QueryLimit = "limit"
QueryOffset = "offset"
QueryPageNum = "pageNum"
QueryPageSize = "pageSize"
)

//http headers
Expand Down
12 changes: 6 additions & 6 deletions server/resource/v1/doc_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,17 @@ var (
ParamType: goRestful.QueryParameterKind,
Desc: "label pairs,for example &label=service:order&label=version:1.0.0",
}
DocQueryLimitParameters = &restful.Parameters{
DocQueryPageNumParameters = &restful.Parameters{
DataType: "string",
Name: common.QueryLimit,
Name: common.QueryPageNum,
ParamType: goRestful.QueryParameterKind,
Desc: "limit,for example &limit=10",
Desc: "pageNum,for example &pageNum=10",
}
DocQueryOffsetParameters = &restful.Parameters{
DocQueryPageSizeParameters = &restful.Parameters{
DataType: "string",
Name: common.QueryOffset,
Name: common.QueryPageSize,
ParamType: goRestful.QueryParameterKind,
Desc: "offset,for example &offset=10",
Desc: "PageSize,for example &pageSize=10",
}
)

Expand Down
63 changes: 63 additions & 0 deletions server/resource/v1/history_resource.go
Original file line number Diff line number Diff line change
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,50 @@ 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{}
sessionId := context.ReadQueryParameter("sessionId")
if sessionId != "" {
query.SessionID = sessionId
}
ip := context.ReadQueryParameter("ip")
if ip != "" {
query.IP = ip
}
urlPath := context.ReadQueryParameter("urlPath")
if urlPath != "" {
query.URLPath = urlPath
}
userAgent := context.ReadQueryParameter("userAgent")
if userAgent != "" {
query.UserAgent = userAgent
}
domain := ReadDomain(context)
if domain == nil {
WriteErrResponse(context, http.StatusInternalServerError, common.MsgDomainMustNotBeEmpty, common.ContentTypeText)
return
}
query.Domain = domain.(string)
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 +138,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/polling_data",
ResourceFunc: r.GetPollingData,
FuncDesc: "get all history record of get and list",
Parameters: []*restful.Parameters{
DocPathProject,
},
Returns: []*restful.Returns{
{
Code: http.StatusOK,
Message: "true",
Model: []model.PollingDetail{},
},
},
Consumes: []string{goRestful.MIME_JSON, common.ContentTypeYaml},
Produces: []string{goRestful.MIME_JSON, common.ContentTypeYaml},
},
}
}
48 changes: 48 additions & 0 deletions server/resource/v1/history_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
package v1_test

import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/apache/servicecomb-kie/pkg/model"
handler2 "github.com/apache/servicecomb-kie/server/handler"
v1 "github.com/apache/servicecomb-kie/server/resource/v1"
"github.com/apache/servicecomb-kie/server/service"
"github.com/go-chassis/go-chassis/core/common"
Expand Down Expand Up @@ -83,3 +85,49 @@ func TestHistoryResource_GetRevisions(t *testing.T) {
})

}

func TestHistoryResource_GetPollingData(t *testing.T) {
t.Run("put kv, label is service", func(t *testing.T) {
kv := &model.KVDoc{
Value: "1s",
Labels: map[string]string{"service": "utService"},
}
j, _ := json.Marshal(kv)
r, _ := http.NewRequest("PUT", "/v1/test/kie/kv/timeout", bytes.NewBuffer(j))
noopH := &handler2.NoopAuthHandler{}
chain, _ := handler.CreateChain(common.Provider, "testchain1", noopH.Name())
r.Header.Set("Content-Type", "application/json")
r.Header.Set("sessionID", "test")
kvr := &v1.KVResource{}
c, _ := restfultest.New(kvr, chain)
resp := httptest.NewRecorder()
c.ServeHTTP(resp, r)

body, err := ioutil.ReadAll(resp.Body)
assert.NoError(t, err)
data := &model.KVDoc{}
err = json.Unmarshal(body, data)
assert.NoError(t, err)
assert.NotEmpty(t, data.ID)
assert.Equal(t, kv.Value, data.Value)
assert.Equal(t, kv.Labels, data.Labels)
})
t.Run("get polling data", func(t *testing.T) {
r, _ := http.NewRequest("GET", "/v1/test/kie/polling_data?sessionId=test", nil)
noopH := &handler2.NoopAuthHandler{}
chain, _ := handler.CreateChain(common.Provider, "testchain1", noopH.Name())
r.Header.Set("Content-Type", "application/json")
revision := &v1.HistoryResource{}
c, err := restfultest.New(revision, chain)
assert.NoError(t, err)
resp := httptest.NewRecorder()
c.ServeHTTP(resp, r)
body, err := ioutil.ReadAll(resp.Body)
assert.NoError(t, err)
result := &[]model.PollingDetail{}
err = json.Unmarshal(body, result)
assert.NoError(t, err)
assert.NotEmpty(t, result)
})

}
41 changes: 41 additions & 0 deletions server/service/mongo/record/polling_detail_dao.go
Original file line number Diff line number Diff line change
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,42 @@ 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}
if detail.SessionID != "" {
queryFilter["session_id"] = detail.SessionID
}
if detail.IP != "" {
queryFilter["ip"] = detail.IP
}
if detail.UserAgent != "" {
queryFilter["user_agent"] = detail.UserAgent
}
if detail.URLPath != "" {
queryFilter["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
Original file line number Diff line number Diff line change
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 dee28ca

Please sign in to comment.