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

Feat/list-logs-by-webhhook-id-and-spaceid #31

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions model/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ type WebhookLog struct {
Data postgres.Jsonb `gorm:"column:data" json:"data" swaggertype:"primitive,string"`
ResponseBody postgres.Jsonb `gorm:"column:response_body" json:"response_body" swaggertype:"primitive,string"`
Tags postgres.Jsonb `gorm:"column:tags" json:"tags" swaggertype:"primitive,string"`
SpaceID uint `gorm:"column:space_id" json:"space_id"`
WebhookID uint `gorm:"column:webhook_id" json:"webhook_id"`
}
1 change: 1 addition & 0 deletions service/webhook/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func Router() chi.Router {
r.Post("/", create)
r.Get("/check", check)
})
r.Get("/space/{space_id}/webhook/{webhook_id}/logs", webhooklogs)
r.Route("/{webhook_id}", func(r chi.Router) {
r.Get("/", details)
r.Put("/", update)
Expand Down
105 changes: 105 additions & 0 deletions service/webhook/webhooklogs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package webhook

import (
"encoding/json"
"net/http"
"strconv"
"strings"

"github.com/davecgh/go-spew/spew"
"github.com/factly/hukz/config"
"github.com/factly/hukz/model"
"github.com/factly/x/errorx"
"github.com/factly/x/loggerx"
"github.com/factly/x/paginationx"
"github.com/factly/x/renderx"
"github.com/go-chi/chi"
)

// list - Get all webhooks logs
// @Summary Show all webhooks logs
// @Description Get all webhooks logs
// @Tags Webhooks
// @ID get-all-webhooks-logs
// @Produce json
// @Param X-User header string true "User ID"
// @Param limit query string false "limit per page"
// @Param page query string false "page number"
// @Param tag query string false "tags"
// @Success 200 {object} paging
// @Router /webhooks/logs [get]
func webhooklogs(w http.ResponseWriter, r *http.Request) {
spaceID := chi.URLParam(r, "space_id")
sID, err := strconv.Atoi(spaceID)
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.InvalidID()))
return
}

webhookID := chi.URLParam(r, "webhook_id")
wID, err := strconv.Atoi(webhookID)

if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.InvalidID()))
return
}

result := logPaging{}
result.Nodes = make([]model.WebhookLog, 0)

offset, limit := paginationx.Parse(r.URL.Query())

queryMap := r.URL.Query()

webhookLogsList := make([]model.WebhookLog, 0)
err = config.DB.Model(&model.WebhookLog{}).
Where(&model.WebhookLog{
WebhookID: uint(wID),
SpaceID: uint(sID),
}).Order("created_at DESC").Find(&webhookLogsList).Error
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DBError()))
return
}
spew.Dump(webhookLogsList)
tags := queryMap["tag"]
if tags != nil {
for _, webhook := range webhookLogsList {
var tagMap map[string]string
_ = json.Unmarshal(webhook.Tags.RawMessage, &tagMap)

count := 0
for _, t := range tags {
toks := strings.Split(t, ":")
if val, found := tagMap[toks[0]]; found && val == toks[1] {
count++
}
}
if count == len(tags) {
result.Nodes = append(result.Nodes, webhook)
}
}
} else {
result.Nodes = webhookLogsList
}

var end int
if limit+offset > len(result.Nodes) {
end = len(result.Nodes)
} else {
end = limit + offset
}
if offset > len(result.Nodes) {
offset = len(result.Nodes)
} else if offset < 0 {
offset = 0
}

result.Nodes = result.Nodes[offset:end]
result.Total = int64(len(result.Nodes))

renderx.JSON(w, http.StatusOK, result)
}
13 changes: 9 additions & 4 deletions util/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func FireWebhooks(m *nats.Msg) {
whData.CreatedAt = time.Now()
whData.Contains = []string{strings.Split(m.Subject, ".")[0]}
whData.Payload = payload

fmt.Printf("Received a [%v] event with data: %v\n", m.Subject, payload)

// Fetch event id
Expand Down Expand Up @@ -86,6 +85,8 @@ func PostWebhook(wh model.Webhook, event string, whData model.WebhookData) {
Data: postgres.Jsonb{RawMessage: bArr},
CreatedByID: wh.CreatedByID,
Tags: wh.Tags,
SpaceID: wh.SpaceID,
WebhookID: wh.ID,
}

var resp *http.Response
Expand Down Expand Up @@ -153,9 +154,13 @@ func PostWebhook(wh model.Webhook, event string, whData model.WebhookData) {
body_bytes = []byte(fmt.Sprint(`{"data":"`, string(body_bytes), `"}`))
}

webHookLog.ResponseBody = postgres.Jsonb{
RawMessage: body_bytes,
}
// if strings.Contains(wh.URL, "eo7uxl0sh2ccz15") {
// body_bytes = []byte(fmt.Sprint(`{"data":"`, string(body_bytes), `"}`))
// }

// webHookLog.ResponseBody = postgres.Jsonb{
// RawMessage: body_bytes,
// }

// Create a log entry for webhook
config.DB.Create(&webHookLog)
Expand Down