2022-07-06 05:08:21 +00:00
|
|
|
package cassandradb
|
|
|
|
|
|
|
|
import (
|
2022-07-10 16:19:33 +00:00
|
|
|
"context"
|
2022-07-08 13:39:23 +00:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2022-07-06 05:08:21 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/db/models"
|
|
|
|
"github.com/authorizerdev/authorizer/server/graph/model"
|
2022-07-08 13:39:23 +00:00
|
|
|
"github.com/gocql/gocql"
|
|
|
|
"github.com/google/uuid"
|
2022-07-06 05:08:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// AddWebhookLog to add webhook log
|
2023-07-31 11:12:11 +00:00
|
|
|
func (p *provider) AddWebhookLog(ctx context.Context, webhookLog *models.WebhookLog) (*model.WebhookLog, error) {
|
2022-07-08 13:39:23 +00:00
|
|
|
if webhookLog.ID == "" {
|
|
|
|
webhookLog.ID = uuid.New().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
webhookLog.Key = webhookLog.ID
|
|
|
|
webhookLog.CreatedAt = time.Now().Unix()
|
|
|
|
webhookLog.UpdatedAt = time.Now().Unix()
|
|
|
|
|
2022-07-09 06:46:54 +00:00
|
|
|
insertQuery := fmt.Sprintf("INSERT INTO %s (id, http_status, response, request, webhook_id, created_at, updated_at) VALUES ('%s', %d,'%s', '%s', '%s', %d, %d)", KeySpace+"."+models.Collections.WebhookLog, webhookLog.ID, webhookLog.HttpStatus, webhookLog.Response, webhookLog.Request, webhookLog.WebhookID, webhookLog.CreatedAt, webhookLog.UpdatedAt)
|
|
|
|
err := p.db.Query(insertQuery).Exec()
|
2022-07-08 13:39:23 +00:00
|
|
|
if err != nil {
|
2022-07-10 16:19:33 +00:00
|
|
|
return nil, err
|
2022-07-08 13:39:23 +00:00
|
|
|
}
|
2022-07-10 16:19:33 +00:00
|
|
|
return webhookLog.AsAPIWebhookLog(), nil
|
2022-07-06 05:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListWebhookLogs to list webhook logs
|
2023-07-31 11:12:11 +00:00
|
|
|
func (p *provider) ListWebhookLogs(ctx context.Context, pagination *model.Pagination, webhookID string) (*model.WebhookLogs, error) {
|
2022-07-08 13:39:23 +00:00
|
|
|
webhookLogs := []*model.WebhookLog{}
|
|
|
|
paginationClone := pagination
|
|
|
|
totalCountQuery := fmt.Sprintf(`SELECT COUNT(*) FROM %s`, KeySpace+"."+models.Collections.WebhookLog)
|
|
|
|
// there is no offset in cassandra
|
|
|
|
// so we fetch till limit + offset
|
|
|
|
// and return the results from offset to limit
|
|
|
|
query := fmt.Sprintf("SELECT id, http_status, response, request, webhook_id, created_at, updated_at FROM %s LIMIT %d", KeySpace+"."+models.Collections.WebhookLog, pagination.Limit+pagination.Offset)
|
|
|
|
|
|
|
|
if webhookID != "" {
|
2022-07-12 06:18:42 +00:00
|
|
|
totalCountQuery = fmt.Sprintf(`SELECT COUNT(*) FROM %s WHERE webhook_id='%s' ALLOW FILTERING`, KeySpace+"."+models.Collections.WebhookLog, webhookID)
|
|
|
|
query = fmt.Sprintf("SELECT id, http_status, response, request, webhook_id, created_at, updated_at FROM %s WHERE webhook_id = '%s' LIMIT %d ALLOW FILTERING", KeySpace+"."+models.Collections.WebhookLog, webhookID, pagination.Limit+pagination.Offset)
|
2022-07-08 13:39:23 +00:00
|
|
|
}
|
|
|
|
|
2023-07-31 11:12:11 +00:00
|
|
|
err := p.db.Query(totalCountQuery).Consistency(gocql.One).Scan(paginationClone.Total)
|
2022-07-08 13:39:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
scanner := p.db.Query(query).Iter().Scanner()
|
|
|
|
counter := int64(0)
|
|
|
|
for scanner.Next() {
|
|
|
|
if counter >= pagination.Offset {
|
2023-07-31 11:12:11 +00:00
|
|
|
var webhookLog *models.WebhookLog
|
2022-07-08 13:39:23 +00:00
|
|
|
err := scanner.Scan(&webhookLog.ID, &webhookLog.HttpStatus, &webhookLog.Response, &webhookLog.Request, &webhookLog.WebhookID, &webhookLog.CreatedAt, &webhookLog.UpdatedAt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
webhookLogs = append(webhookLogs, webhookLog.AsAPIWebhookLog())
|
|
|
|
}
|
|
|
|
counter++
|
|
|
|
}
|
|
|
|
|
|
|
|
return &model.WebhookLogs{
|
2023-07-31 11:12:11 +00:00
|
|
|
Pagination: paginationClone,
|
2022-07-08 13:39:23 +00:00
|
|
|
WebhookLogs: webhookLogs,
|
|
|
|
}, nil
|
2022-07-06 05:08:21 +00:00
|
|
|
}
|