feat(backend): analytics - update routes to read the payload and placeholder for logic
This commit is contained in:
parent
cf571446ce
commit
b2b7fc0dca
4 changed files with 231 additions and 33 deletions
|
|
@ -1,11 +1,13 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
analyticsConfig "openreplay/backend/internal/config/analytics"
|
||||
"openreplay/backend/pkg/common"
|
||||
"openreplay/backend/pkg/logger"
|
||||
"strconv"
|
||||
"sync"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
|
|
@ -67,7 +69,7 @@ func (e *Router) getAnalytics(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func (e *Router) routes() {
|
||||
e.router.HandleFunc("/{projectId}/dashboards", e.createDashboards).Methods("POST")
|
||||
e.router.HandleFunc("/{projectId}/dashboards", e.createDashboard).Methods("POST")
|
||||
e.router.HandleFunc("/{projectId}/dashboards", e.getDashboards).Methods("GET")
|
||||
e.router.HandleFunc("/{projectId}/dashboards/{dashboardId}", e.getDashboard).Methods("GET")
|
||||
e.router.HandleFunc("/{projectId}/dashboards/{dashboardId}", e.updateDashboard).Methods("PUT")
|
||||
|
|
@ -94,43 +96,194 @@ func (e *Router) routes() {
|
|||
e.router.HandleFunc("/{projectId}/cards/{cardId}", e.deleteCard).Methods("DELETE")
|
||||
}
|
||||
|
||||
func (e *Router) createDashboards(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
// CreateDashboardSchema TODO - refactor this to a separate file
|
||||
type CreateDashboardSchema struct {
|
||||
DashboardID int `json:"dashboard_id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
IsPublic bool `json:"is_public"`
|
||||
IsPinned bool `json:"is_pinned"`
|
||||
Metrics []int `json:"metrics"`
|
||||
}
|
||||
|
||||
type CurrentContext struct {
|
||||
UserID int `json:"user_id"`
|
||||
}
|
||||
|
||||
// createDashboard TODO - refactor this to a separate service
|
||||
func (e *Router) createDashboard(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
projectId, err := strconv.Atoi(vars["projectId"])
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid project ID", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Received projectId: %s\n", projectId)
|
||||
|
||||
var data CreateDashboardSchema
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
http.Error(w, "Invalid request payload", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
context := e.getCurrentContext(r)
|
||||
if context == nil {
|
||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Received request to create dashboard: %+v\n", data)
|
||||
|
||||
response := map[string]string{
|
||||
"message": "Dashboard created successfully",
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
err = json.NewEncoder(w).Encode(response)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Router) getCurrentContext(r *http.Request) *CurrentContext {
|
||||
// retrieving user info from headers or tokens
|
||||
return &CurrentContext{UserID: 1}
|
||||
}
|
||||
|
||||
func (e *Router) getDashboards(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
vars := mux.Vars(r)
|
||||
projectId := vars["projectId"]
|
||||
fmt.Printf("Fetching dashboards for projectId: %s\n", projectId)
|
||||
|
||||
dashboards := []CreateDashboardSchema{
|
||||
{DashboardID: 1, Name: "Dashboard 1", Description: "Description 1", IsPublic: true, IsPinned: false, Metrics: []int{1, 2, 3}},
|
||||
{DashboardID: 2, Name: "Dashboard 2", Description: "Description 2", IsPublic: false, IsPinned: true, Metrics: []int{4, 5, 6}},
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
err := json.NewEncoder(w).Encode(dashboards)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Router) getDashboard(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
vars := mux.Vars(r)
|
||||
projectId := vars["projectId"]
|
||||
dashboardId := vars["dashboardId"]
|
||||
fmt.Printf("Fetching dashboard for projectId: %s, dashboardId: %s\n", projectId, dashboardId)
|
||||
|
||||
dashboard := CreateDashboardSchema{
|
||||
DashboardID: 1,
|
||||
Name: "Dashboard 1",
|
||||
Description: "Description 1",
|
||||
IsPublic: true,
|
||||
IsPinned: false,
|
||||
Metrics: []int{1, 2, 3},
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
err := json.NewEncoder(w).Encode(dashboard)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Router) updateDashboard(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
projectId := vars["projectId"]
|
||||
dashboardId := vars["dashboardId"]
|
||||
fmt.Printf("Updating dashboard %s for project %s", dashboardId, projectId)
|
||||
|
||||
var data CreateDashboardSchema
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
http.Error(w, "Invalid request payload", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Placeholder for updating logic
|
||||
w.WriteHeader(http.StatusOK)
|
||||
err := json.NewEncoder(w).Encode(map[string]string{"message": "Dashboard updated successfully"})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Router) deleteDashboard(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
projectId := vars["projectId"]
|
||||
dashboardId := vars["dashboardId"]
|
||||
fmt.Printf("Deleting dashboard %s for project %s", dashboardId, projectId)
|
||||
|
||||
// Placeholder for delete logic
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (e *Router) pinDashboard(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
projectId := vars["projectId"]
|
||||
dashboardId := vars["dashboardId"]
|
||||
fmt.Printf("Pinning dashboard %s for project %s", dashboardId, projectId)
|
||||
|
||||
// Placeholder for pinning logic
|
||||
w.WriteHeader(http.StatusOK)
|
||||
err := json.NewEncoder(w).Encode(map[string]string{"message": "Dashboard pinned successfully"})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Router) addCardToDashboard(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
projectId := vars["projectId"]
|
||||
dashboardId := vars["dashboardId"]
|
||||
fmt.Printf("Adding card to dashboard %s for project %s\n", dashboardId, projectId)
|
||||
|
||||
// Placeholder for adding card logic
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
err := json.NewEncoder(w).Encode(map[string]string{"message": "Card added to dashboard successfully"})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Router) createMetricAndAddToDashboard(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
projectId := vars["projectId"]
|
||||
dashboardId := vars["dashboardId"]
|
||||
fmt.Printf("Creating metric and adding to dashboard %s for project %s\n", dashboardId, projectId)
|
||||
|
||||
// Placeholder for creating metric logic
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
err := json.NewEncoder(w).Encode(map[string]string{"message": "Metric created and added to dashboard successfully"})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Router) updateWidgetInDashboard(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
projectId := vars["projectId"]
|
||||
dashboardId := vars["dashboardId"]
|
||||
widgetId := vars["widgetId"]
|
||||
fmt.Printf("Updating widget %s in dashboard %s for project %s\n", widgetId, dashboardId, projectId)
|
||||
|
||||
// Placeholder for updating widget logic
|
||||
w.WriteHeader(http.StatusOK)
|
||||
err := json.NewEncoder(w).Encode(map[string]string{"message": "Widget updated successfully"})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Router) removeWidgetFromDashboard(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
projectId := vars["projectId"]
|
||||
dashboardId := vars["dashboardId"]
|
||||
widgetId := vars["widgetId"]
|
||||
fmt.Printf("Removing widget %s from dashboard %s for project %s\n", widgetId, dashboardId, projectId)
|
||||
|
||||
// Placeholder for removing widget logic
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,30 +1,30 @@
|
|||
package analytics
|
||||
|
||||
import (
|
||||
"openreplay/backend/internal/config/analytics"
|
||||
"openreplay/backend/pkg/common/api/auth"
|
||||
"openreplay/backend/pkg/db/postgres/pool"
|
||||
"openreplay/backend/pkg/flakeid"
|
||||
"openreplay/backend/pkg/logger"
|
||||
"openreplay/backend/pkg/objectstorage"
|
||||
"openreplay/backend/pkg/objectstorage/store"
|
||||
)
|
||||
|
||||
type ServicesBuilder struct {
|
||||
Flaker *flakeid.Flaker
|
||||
ObjStorage objectstorage.ObjectStorage
|
||||
Auth auth.Auth
|
||||
}
|
||||
|
||||
func NewServiceBuilder(log logger.Logger, cfg *analytics.Config, pgconn pool.Pool) (*ServicesBuilder, error) {
|
||||
objStore, err := store.NewStore(&cfg.ObjectsConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
flaker := flakeid.NewFlaker(cfg.WorkerID)
|
||||
return &ServicesBuilder{
|
||||
Flaker: flaker,
|
||||
ObjStorage: objStore,
|
||||
Auth: auth.NewAuth(log, cfg.JWTSecret, pgconn),
|
||||
}, nil
|
||||
}
|
||||
//import (
|
||||
// "openreplay/backend/internal/config/analytics"
|
||||
// "openreplay/backend/pkg/common/api/auth"
|
||||
// "openreplay/backend/pkg/db/postgres/pool"
|
||||
// "openreplay/backend/pkg/flakeid"
|
||||
// "openreplay/backend/pkg/logger"
|
||||
// "openreplay/backend/pkg/objectstorage"
|
||||
// "openreplay/backend/pkg/objectstorage/store"
|
||||
//)
|
||||
//
|
||||
//type ServicesBuilder struct {
|
||||
// Flaker *flakeid.Flaker
|
||||
// ObjStorage objectstorage.ObjectStorage
|
||||
// Auth auth.Auth
|
||||
//}
|
||||
//
|
||||
//func NewServiceBuilder(log logger.Logger, cfg *analytics.Config, pgconn pool.Pool) (*ServicesBuilder, error) {
|
||||
// objStore, err := store.NewStore(&cfg.ObjectsConfig)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// flaker := flakeid.NewFlaker(cfg.WorkerID)
|
||||
// return &ServicesBuilder{
|
||||
// Flaker: flaker,
|
||||
// ObjStorage: objStore,
|
||||
// Auth: auth.NewAuth(log, cfg.JWTSecret, pgconn),
|
||||
// }, nil
|
||||
//}
|
||||
|
|
|
|||
1
backend/pkg/analytics/service/card.go
Normal file
1
backend/pkg/analytics/service/card.go
Normal file
|
|
@ -0,0 +1 @@
|
|||
package service
|
||||
44
backend/pkg/analytics/service/dashboard.go
Normal file
44
backend/pkg/analytics/service/dashboard.go
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
//package service
|
||||
//
|
||||
//import (
|
||||
// "encoding/json"
|
||||
// "fmt"
|
||||
// "github.com/gorilla/mux"
|
||||
// "net/http"
|
||||
//)
|
||||
//
|
||||
//type CreateDashboardSchema struct {
|
||||
// DashboardID int `json:"dashboard_id"`
|
||||
// Name string `json:"name"`
|
||||
// Description string `json:"description"`
|
||||
// IsPublic bool `json:"is_public"`
|
||||
// IsPinned bool `json:"is_pinned"`
|
||||
// Metrics []int `json:"metrics"`
|
||||
//}
|
||||
//
|
||||
//type CurrentContext struct {
|
||||
// UserID int `json:"user_id"`
|
||||
//}
|
||||
//
|
||||
//func (e *Router) createDashboard(w http.ResponseWriter, r *http.Request) {
|
||||
// vars := mux.Vars(r)
|
||||
// projectId := vars["projectId"]
|
||||
// fmt.Printf("Received projectId: %s\n", projectId)
|
||||
//
|
||||
// var data CreateDashboardSchema
|
||||
// if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
// http.Error(w, "Invalid request payload", http.StatusBadRequest)
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// context := e.getCurrentContext(r)
|
||||
// if context == nil {
|
||||
// http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// data.DashboardID = 1 // Placeholder for dashboard ID generation logic
|
||||
//
|
||||
// w.Header().Set("Content-Type", "application/json")
|
||||
// json.NewEncoder(w).Encode(data)
|
||||
//}
|
||||
Loading…
Add table
Reference in a new issue