Skip to content

Commit

Permalink
Merge 0937554 into cd2aabb
Browse files Browse the repository at this point in the history
  • Loading branch information
GuoYL123 committed Mar 11, 2020
2 parents cd2aabb + 0937554 commit ca86b91
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 29 deletions.
16 changes: 6 additions & 10 deletions deployments/db.js
Expand Up @@ -98,39 +98,35 @@ db.createCollection( "view", {
} );

db.createCollection( "polling_detail", {
capped: true,
max: 100,
validator: { $jsonSchema: {
bsonType: "object",
required: [ "id","params","session_id","url_path" ],
required: [ "id","session_id","domain","url_path" ],
properties: {
id: {
bsonType: "string",
},
session_id: {
bsonType: "string",
},
domain: {
bsonType: "string",
},
params: {
bsonType: "string"
bsonType: "object"
},
ip: {
bsonType: "string"
},
user_agent: {
bsonType: "string"
},
url_path: {
bsonType: "string"
},
response_body: {
bsonType: "object"
},
response_header: {
bsonType: "object"
},
response_code: {
bsonType: "string"
bsonType: "number"
}
}
} }
Expand All @@ -142,7 +138,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
14 changes: 14 additions & 0 deletions pkg/model/db_schema.go
Expand Up @@ -55,3 +55,17 @@ type ViewDoc struct {
Domain string `json:"domain,omitempty" yaml:"domain,omitempty"`
Criteria string `json:"criteria,omitempty" yaml:"criteria,omitempty"`
}

//PollingDetail is db struct, it record operation history
type PollingDetail struct {
ID string `json:"id,omitempty" yaml:"id,omitempty"`
SessionID string `json:"session_id,omitempty" bson:"session_id," yaml:"session_id,omitempty"`
Domain string `json:"domain,omitempty" yaml:"domain,omitempty"`
PollingData map[string]interface{} `json:"params,omitempty" yaml:"params,omitempty"`
IP string `json:"ip,omitempty" yaml:"ip,omitempty"`
UserAgent string `json:"user_agent,omitempty" bson:"user_agent," yaml:"user_agent,omitempty"`
URLPath string `json:"url_path,omitempty" bson:"url_path," yaml:"url_path,omitempty"`
ResponseBody interface{} `json:"response_body,omitempty" bson:"response_body," yaml:"response_body,omitempty"`
ResponseHeader map[string][]string `json:"response_header,omitempty" bson:"response_header," yaml:"response_header,omitempty"`
ResponseCode int `json:"response_code,omitempty" bson:"response_code," yaml:"response_code,omitempty"`
}
14 changes: 0 additions & 14 deletions pkg/model/kv.go
Expand Up @@ -53,20 +53,6 @@ type ViewResponse struct {
Data []*ViewDoc `json:"data,omitempty"`
}

//PollingDetail record operation history
type PollingDetail struct {
ID string `json:"id,omitempty" yaml:"id,omitempty"`
SessionID string `json:"session_id,omitempty" yaml:"session_id,omitempty"`
Domain string `json:"domain,omitempty" yaml:"domain,omitempty"`
PollingData map[string]interface{} `json:"params,omitempty" yaml:"params,omitempty"`
IP string `json:"ip,omitempty" yaml:"ip,omitempty"`
UserAgent string `json:"user_agent,omitempty" yaml:"user_agent,omitempty"`
URLPath string `json:"url_path,omitempty" yaml:"url_path,omitempty"`
ResponseBody interface{} `json:"response_body,omitempty" yaml:"response_body,omitempty"`
ResponseHeader map[string][]string `json:"response_header,omitempty" yaml:"response_header,omitempty"`
ResponseCode int `json:"response_code,omitempty" yaml:"response_code,omitempty"`
}

//DocResponseSingleKey is response doc
type DocResponseSingleKey struct {
CreateRevision int64 `json:"create_revision"`
Expand Down
2 changes: 1 addition & 1 deletion server/resource/v1/common.go
Expand Up @@ -39,7 +39,7 @@ import (
//const of server
const (
HeaderUserAgent = "User-Agent"
HeaderSessionID = "sessionID"
HeaderSessionID = "X-Session-Id"
PathParameterProject = "project"
PathParameterKey = "key"
AttributeDomainKey = "domain"
Expand Down
63 changes: 63 additions & 0 deletions server/resource/v1/history_resource.go
Expand Up @@ -19,6 +19,7 @@ package v1

import (
"github.com/apache/servicecomb-kie/pkg/model"
"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 polling data 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 @@ -92,5 +137,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},
},
}
}
39 changes: 39 additions & 0 deletions server/resource/v1/history_resource_test.go
Expand Up @@ -21,6 +21,7 @@ import (
"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 +84,41 @@ func TestHistoryResource_GetRevisions(t *testing.T) {
})

}

func TestHistoryResource_GetPollingData(t *testing.T) {
t.Run("list kv by service label, to create a polling data", func(t *testing.T) {
r, _ := http.NewRequest("GET", "/v1/test/kie/kv", nil)
noopH := &handler2.NoopAuthHandler{}
chain, _ := handler.CreateChain(common.Provider, "testchain1", noopH.Name())
r.Header.Set("Content-Type", "application/json")
r.Header.Set("X-Session-Id", "test")
kvr := &v1.KVResource{}
c, err := restfultest.New(kvr, chain)
assert.NoError(t, err)
resp := httptest.NewRecorder()
c.ServeHTTP(resp, r)
body, err := ioutil.ReadAll(resp.Body)
assert.NoError(t, err)
result := &model.KVResponse{}
err = json.Unmarshal(body, result)
assert.NoError(t, err)
})
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)
})

}
3 changes: 0 additions & 3 deletions server/resource/v1/kv_resource_test.go
Expand Up @@ -76,7 +76,6 @@ func TestKVResource_Put(t *testing.T) {
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()
Expand All @@ -101,7 +100,6 @@ func TestKVResource_Put(t *testing.T) {
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()
Expand All @@ -127,7 +125,6 @@ func TestKVResource_Put(t *testing.T) {
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()
Expand Down
43 changes: 42 additions & 1 deletion 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 @@ -40,9 +42,48 @@ func CreateOrUpdate(ctx context.Context, detail *model.PollingDetail) (*model.Po
}
return nil, res.Err()
}
_, err := collection.UpdateOne(ctx, queryFilter, detail)
_, err := collection.UpdateOne(ctx, queryFilter, bson.D{{"$set", detail}})
if err != nil {
return nil, err
}
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
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 ca86b91

Please sign in to comment.