Skip to content

Commit

Permalink
Merge 78d1b67 into 9d4a4e0
Browse files Browse the repository at this point in the history
  • Loading branch information
develpoerX committed Dec 25, 2020
2 parents 9d4a4e0 + 78d1b67 commit d457f18
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 88 deletions.
29 changes: 14 additions & 15 deletions deployments/db.js
Expand Up @@ -83,15 +83,15 @@ db.createCollection( "polling_detail", {
max: 100,
validator: { $jsonSchema: {
bsonType: "object",
required: [ "id","session_id","domain","url_path" ],
required: [ "id","session_id","revision","url_path" ],
properties: {
id: {
bsonType: "string",
},
domain: {
bsonType: "string",
},
params: {
polling_data: {
bsonType: "object"
},
ip: {
Expand All @@ -103,8 +103,8 @@ db.createCollection( "polling_detail", {
response_body: {
bsonType: "object"
},
response_header: {
bsonType: "object"
Revision: {
bsonType: "string"
},
response_code: {
bsonType: "number"
Expand All @@ -114,15 +114,14 @@ db.createCollection( "polling_detail", {
} );

//index
db.kv.createIndex({"id": 1}, { unique: true } );
db.kv.createIndex({key: 1, label_format: 1,domain:1,project:1},{ unique: true });
db.kv_revision.createIndex( { "delete_time": 1 }, { expireAfterSeconds: 7 * 24 * 3600 } );
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_id:1,domain:1}, { unique: true } );
db.counter.createIndex({name: 1,domain:1},{ unique: true });
db.view.createIndex({"id": 1}, { unique: true } );
db.view.createIndex({display:1,domain:1,project:1},{ unique: true });
db.kv.createIndex({"id": 1}, {unique: true});
db.kv.createIndex({key: 1, label_format: 1, domain: 1, project: 1}, {unique: true});
db.kv_revision.createIndex({"delete_time": 1}, {expireAfterSeconds: 7 * 24 * 3600});
db.label.createIndex({"id": 1}, {unique: true});
db.label.createIndex({format: 1, domain: 1, project: 1}, {unique: true});
db.polling_detail.createIndex({timestamp: 1}, {expireAfterSeconds: 7 * 24 * 3600});
db.counter.createIndex({name: 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
db.setProfilingLevel(1, {slowms: 80, sampleRate: 1} );
db.setProfilingLevel(1, {slowms: 80, sampleRate: 1});
2 changes: 0 additions & 2 deletions go.mod
Expand Up @@ -5,9 +5,7 @@ require (
github.com/emicklei/go-restful v2.12.0+incompatible
github.com/go-chassis/go-archaius v1.3.6-0.20201103103813-43dd1680ebfb
github.com/go-chassis/go-chassis/v2 v2.1.1-0.20201208062518-9c2e86bd7a6c

github.com/go-chassis/openlog v1.1.2
github.com/go-chassis/paas-lager v1.1.1
github.com/go-chassis/seclog v1.3.0
github.com/go-playground/universal-translator v0.17.0
github.com/go-playground/validator v9.31.0+incompatible
Expand Down
62 changes: 12 additions & 50 deletions go.sum

Large diffs are not rendered by default.

24 changes: 14 additions & 10 deletions pkg/model/db_schema.go
Expand Up @@ -17,6 +17,8 @@

package model

import "time"

//LabelDoc is database struct to store labels
type LabelDoc struct {
ID string `json:"id,omitempty" bson:"id,omitempty" yaml:"id,omitempty" swag:"string"`
Expand Down Expand Up @@ -57,16 +59,18 @@ type ViewDoc struct {

//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:"polling_data,omitempty" yaml:"polling_data,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 []*KVDoc `json:"kv,omitempty" bson:"kv," yaml:"kv,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"`
ID string `json:"id,omitempty" yaml:"id,omitempty"`
SessionID string `json:"session_id,omitempty" bson:"session_id," yaml:"session_id,omitempty"`
SessionGroup string `json:"session_group,omitempty" bson:"session_group," yaml:"session_group,omitempty"`
Domain string `json:"domain,omitempty" yaml:"domain,omitempty"`
PollingData map[string]interface{} `json:"polling_data,omitempty" yaml:"polling_data,omitempty"`
Revision string `json:"revision,omitempty" yaml:"revision,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 []*KVDoc `json:"kv,omitempty" bson:"kv," yaml:"kv,omitempty"`
ResponseCode int `json:"response_code,omitempty" bson:"response_code," yaml:"response_code,omitempty"`
Timestamp time.Time `json:"times_tamp,omitempty" yaml:"times_tamp,omitempty"`
}

// UpdateKVRequest is db struct, it contains kv update request params
Expand Down
5 changes: 4 additions & 1 deletion server/handler/track_handler.go
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/go-chassis/openlog"
"net/http"
"strings"
"time"
)

//const of noop auth handler
Expand Down Expand Up @@ -66,13 +67,15 @@ func (h *TrackHandler) Handle(chain *handler.Chain, inv *invocation.Invocation,
data := &model.PollingDetail{}
data.URLPath = req.Request.Method + " " + req.Request.URL.Path
data.SessionID = sessionID
data.SessionGroup = req.HeaderParameter(v1.HeaderSessionGroup)
data.UserAgent = req.HeaderParameter(v1.HeaderUserAgent)
data.Domain = v1.ReadDomain(req.Request.Context())
data.IP = iputil.ClientIP(req.Request)
data.ResponseBody = req.Attribute(common.RespBodyContextKey).([]*model.KVDoc)
data.Timestamp = time.Now()
data.ResponseCode = ir.Status
if resp != nil {
data.ResponseHeader = resp.Header()
data.Revision = resp.Header().Get(common.HeaderRevision)
}
data.PollingData = map[string]interface{}{
"revision": revStr,
Expand Down
1 change: 1 addition & 0 deletions server/resource/v1/common.go
Expand Up @@ -43,6 +43,7 @@ import (
const (
HeaderUserAgent = "User-Agent"
HeaderSessionID = "X-Session-Id"
HeaderSessionGroup = "X-Session-Group"
AttributeDomainKey = "domain"

FmtReadRequestError = "decode request body failed: %v"
Expand Down
12 changes: 3 additions & 9 deletions server/service/mongo/session/session.go
Expand Up @@ -222,19 +222,13 @@ func ensureKVLongPolling(session *mgo.Session) {
c := session.DB(DBName).C(CollectionPollingDetail)
err := c.Create(&mgo.CollectionInfo{Validator: bson.M{
"id": bson.M{"$exists": true},
"params": bson.M{"$exists": true},
"revision": bson.M{"$exists": true},
"session_id": bson.M{"$exists": true},
"url_path": bson.M{"$exists": true},
}})
wrapError(err, MsgDBExists)
err = c.EnsureIndex(mgo.Index{
Key: []string{"id"},
Unique: true,
})
wrapError(err)
err = c.EnsureIndex(mgo.Index{
Key: []string{"session_id", "domain"},
Unique: true,
Key: []string{"timestamp"},
ExpireAfter: 7 * 24 * time.Hour,
})
wrapError(err)
}
Expand Down
6 changes: 5 additions & 1 deletion server/service/mongo/track/polling_detail_dao.go
Expand Up @@ -29,9 +29,10 @@ import (
)

//CreateOrUpdate create a record or update exist record
//If revision and session_id is exit: update else: insert
func CreateOrUpdate(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}
queryFilter := bson.M{"revision": detail.Revision, "session_id": detail.SessionID}
res := collection.FindOne(ctx, queryFilter)
if res.Err() != nil {
if res.Err() == mongo.ErrNoDocuments {
Expand Down Expand Up @@ -67,6 +68,9 @@ func Get(ctx context.Context, detail *model.PollingDetail) ([]*model.PollingDeta
if detail.URLPath != "" {
queryFilter["url_path"] = detail.URLPath
}
if detail.Revision != "" {
queryFilter["revision"] = detail.Revision
}
cur, err := collection.Find(ctx, queryFilter)
if err != nil {
return nil, err
Expand Down

0 comments on commit d457f18

Please sign in to comment.