Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CBG-3727: Diagnostic API: Get all doc channels #6701

Merged
merged 12 commits into from
Feb 27, 2024
46 changes: 46 additions & 0 deletions rest/diagnostic_doc_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright 2024-Present Couchbase, Inc.

Use of this software is governed by the Business Source License included in
the file licenses/BSL-Couchbase.txt. As of the Change Date specified in that
file, in accordance with the Business Source License, use of this software will
be governed by the Apache License, Version 2.0, included in the file
licenses/APL2.txt.
*/

package rest

import (
"log"

"github.com/couchbase/sync_gateway/auth"
"github.com/couchbase/sync_gateway/db"
)

// HTTP handler for a GET of a document
func (h *handler) handleGetDocChannels() error {
docid := h.PathVar("docid")

doc, err := h.collection.GetDocument(h.ctx(), docid, db.DocUnmarshalSync)
if err != nil {
return err
}
if doc == nil {
return kNotFoundError
}
resp := make(map[string][]auth.GrantHistorySequencePair, len(doc.Channels))

for _, chanSetInfo := range doc.SyncData.ChannelSet {
resp[chanSetInfo.Name] = append(resp[chanSetInfo.Name], auth.GrantHistorySequencePair{StartSeq: chanSetInfo.Start, EndSeq: chanSetInfo.End})
for _, hist := range doc.SyncData.ChannelSetHistory {
if hist.Name == chanSetInfo.Name {
resp[chanSetInfo.Name] = append(resp[chanSetInfo.Name], auth.GrantHistorySequencePair{StartSeq: hist.Start, EndSeq: hist.End})
continue
}
}
}
mohammed-madi marked this conversation as resolved.
Show resolved Hide resolved
log.Print(resp)
mohammed-madi marked this conversation as resolved.
Show resolved Hide resolved

h.writeJSON(resp)
return nil
}
42 changes: 42 additions & 0 deletions rest/diagnostic_doc_api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright 2024-Present Couchbase, Inc.

Use of this software is governed by the Business Source License included in
the file licenses/BSL-Couchbase.txt. As of the Change Date specified in that
file, in accordance with the Business Source License, use of this software will
be governed by the Apache License, Version 2.0, included in the file
licenses/APL2.txt.
*/

package rest

import (
"encoding/json"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetAlldocChannels(t *testing.T) {
rt := NewRestTester(t, &RestTesterConfig{SyncFn: `function(doc) {channel(doc.channel);}`})
defer rt.Close()

version := rt.PutDoc("doc", `{"channel":["CHAN1"]}`)
updatedVersion := rt.UpdateDoc("doc", version, `{"channel":["CHAN2"]}`)
updatedVersion = rt.UpdateDoc("doc", updatedVersion, `{"channel":["CHAN1"]}`)
updatedVersion = rt.UpdateDoc("doc", updatedVersion, `{"channel":["CHAN1", "CHAN2"]}`)
updatedVersion = rt.UpdateDoc("doc", updatedVersion, `{"channel":["CHAN3"]}`)
_ = rt.UpdateDoc("doc", updatedVersion, `{"channel":["CHAN1"]}`)

response := rt.SendDiagnosticRequest("GET", "/{{.keyspace}}/doc/_all_channels", "")
RequireStatus(t, response, http.StatusOK)

var channelMap map[string][]string
err := json.Unmarshal(response.BodyBytes(), &channelMap)
assert.NoError(t, err)
assert.ElementsMatch(t, channelMap["CHAN1"], []string{"6-0", "1-2", "3-5"})
assert.ElementsMatch(t, channelMap["CHAN2"], []string{"4-5", "2-3"})
assert.ElementsMatch(t, channelMap["CHAN3"], []string{"5-6"})

}
6 changes: 5 additions & 1 deletion rest/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,11 @@ func CreateMetricRouter(sc *ServerContext) *mux.Router {

func createDiagnosticRouter(sc *ServerContext) *mux.Router {
r := CreatePingRouter(sc)

dbr := r.PathPrefix("/{db:" + dbRegex + "}/").Subrouter()
dbr.StrictSlash(true)
keyspace := r.PathPrefix("/{keyspace:" + dbRegex + "}/").Subrouter()
keyspace.StrictSlash(true)
keyspace.Handle("/{docid:"+docRegex+"}/_all_channels", makeHandler(sc, adminPrivs, []Permission{PermReadAppData}, nil, (*handler).handleGetDocChannels)).Methods("GET")
return r
}

Expand Down
Loading