Skip to content

Commit 4babb43

Browse files
committed
chore(backend): implement sdk config api
1 parent 19e222e commit 4babb43

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

backend/api/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func main() {
5959
// SDK routes
6060
r.PUT("/events", measure.ValidateAPIKey(), measure.PutEvents)
6161
r.PUT("/builds", measure.ValidateAPIKey(), measure.PutBuilds)
62+
r.GET("/config", measure.ValidateAPIKey(), measure.GetConfig)
6263

6364
// Proxy routes
6465
r.GET("/proxy/attachments", measure.ProxyAttachment)

backend/api/measure/session_targeting.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import (
77
"context"
88
"errors"
99
"fmt"
10+
"net/http"
1011
"sort"
1112
"time"
1213

14+
"github.com/gin-gonic/gin"
1315
"github.com/google/uuid"
1416
"github.com/leporo/sqlf"
1517
)
@@ -83,6 +85,16 @@ type STDashboardConfig struct {
8385
OperatorTypes OperatorTypes `json:"operator_types"`
8486
}
8587

88+
// Sesson targeting config response
89+
// used by the SDKs.
90+
type STConfigUnitResponse struct {
91+
ID uuid.UUID `json:"id"`
92+
Name string `json:"name"`
93+
Status int `json:"status"`
94+
SamplingRate float64 `json:"sampling_rate"`
95+
Rule string `json:"rule"`
96+
}
97+
8698
// GetSTRules provides session targeting rules
8799
// that matches various filter criteria
88100
// in a paginated fashion.
@@ -342,6 +354,75 @@ func GetSTDashboardConfig(ctx context.Context, appId uuid.UUID, osName string) (
342354
}, nil
343355
}
344356

357+
// GetConfig returns the SDK config
358+
// for session targeting
359+
func GetConfig(c *gin.Context) {
360+
appId, err := uuid.Parse(c.GetString("appId"))
361+
if err != nil {
362+
msg := `error parsing app's uuid`
363+
fmt.Println(msg, err)
364+
c.JSON(http.StatusInternalServerError, gin.H{"error": msg})
365+
return
366+
}
367+
368+
stmt := sqlf.PostgreSQL.From("session_targeting_rules").
369+
Select("id").
370+
Select("name").
371+
Select("status").
372+
Select("sampling_rate").
373+
Select("rule").
374+
Where("app_id = ?", appId).
375+
Where("status = 1").
376+
OrderBy("updated_at DESC")
377+
378+
defer stmt.Close()
379+
380+
rows, err := server.Server.PgPool.Query(c.Request.Context(), stmt.String(), stmt.Args()...)
381+
if err != nil {
382+
msg := "failed to fetch session targeting config"
383+
fmt.Println(msg, err)
384+
c.JSON(http.StatusInternalServerError, gin.H{
385+
"error": msg,
386+
"details": err.Error(),
387+
})
388+
return
389+
}
390+
defer rows.Close()
391+
392+
configs := make([]STConfigUnitResponse, 0)
393+
for rows.Next() {
394+
var config STConfigUnitResponse
395+
if err := rows.Scan(
396+
&config.ID,
397+
&config.Name,
398+
&config.Status,
399+
&config.SamplingRate,
400+
&config.Rule,
401+
); err != nil {
402+
msg := "failed to scan session targeting config"
403+
fmt.Println(msg, err)
404+
c.JSON(http.StatusInternalServerError, gin.H{
405+
"error": msg,
406+
"details": err.Error(),
407+
})
408+
return
409+
}
410+
configs = append(configs, config)
411+
}
412+
413+
if err := rows.Err(); err != nil {
414+
msg := "error iterating session targeting config"
415+
fmt.Println(msg, err)
416+
c.JSON(http.StatusInternalServerError, gin.H{
417+
"error": msg,
418+
"details": err.Error(),
419+
})
420+
return
421+
}
422+
423+
c.JSON(http.StatusOK, configs)
424+
}
425+
345426
func getUDAttrKeys(ctx context.Context, appId uuid.UUID) (attributes []AttrConfig, err error) {
346427
var table string = "user_def_attrs"
347428
substmt := sqlf.From(table).

0 commit comments

Comments
 (0)