Skip to content
Merged
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
68 changes: 68 additions & 0 deletions api/analytics/hospitality.controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package api

import (
"context"
"net/http"
"time"

"github.com/Thanus-Kumaar/anokha-2025-backend/cmd"
db "github.com/Thanus-Kumaar/anokha-2025-backend/db/gen"
"github.com/Thanus-Kumaar/anokha-2025-backend/pkg"
"github.com/gin-gonic/gin"
)

func GetInsideCampusAnalytics(c *gin.Context) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

conn, err := cmd.DBPool.Acquire(ctx)
if pkg.HandleDbAcquireErr(c, err, "ANALYTICS") {
return
}
defer conn.Release()

q := db.New()

insideCampusSummary, err := q.GetInsideCampusAnalyticsQuery(ctx, conn)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Oops! Something happened. Please try again later",
})
pkg.Log.ErrorCtx(c, "[ANALYTICS-ERROR]: Failed to get inside campus summary", err)
return
}

c.JSON(http.StatusOK, gin.H{
"message": "Successfully fetched inside campus analytics",
"inside_campus_summary": insideCampusSummary,
})
pkg.Log.SuccessCtx(c)
}

func GetLiveBedsAnalytics(c *gin.Context) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

conn, err := cmd.DBPool.Acquire(ctx)
if pkg.HandleDbAcquireErr(c, err, "ANALYTICS") {
return
}
defer conn.Release()

q := db.New()

liveBedsSummary, err := q.GetLiveBedsAnalyticsQuery(ctx, conn)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Oops! Something happened. Please try again later",
})
pkg.Log.ErrorCtx(c, "[ANALYTICS-ERROR]: Failed to get live beds summary", err)
return
}

c.JSON(http.StatusOK, gin.H{
"message": "Successfully fetched live beds analytics",
"live_beds_summary": liveBedsSummary,
})
pkg.Log.SuccessCtx(c)
}
4 changes: 4 additions & 0 deletions api/analytics/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ func AnalyticsRoutes(r *gin.RouterGroup) {
r.GET("/registrations", mw.Auth, mw.CheckAdmin, GetEventRegistrationAnalytics)
// r.GET("/people", mw.Auth, mw.CheckAdmin, GetPeopleAnalytics)
r.GET("/transactions", mw.Auth, mw.CheckAdmin, GetTransactionAnalytics)

// Hospitality Analytics
r.GET("/hospitality/inside", mw.Auth, mw.CheckHospitality, GetInsideCampusAnalytics)
r.GET("/hospitality/beds", mw.Auth, mw.CheckHospitality, GetLiveBedsAnalytics)
}
16 changes: 16 additions & 0 deletions bruno/hospitalityAnalytics/GetInsideCampusAnalytics.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
meta {
name: GetInsideCampusAnalytics
type: http
seq: 1
}

get {
url: {{baseUrl}}/analytics/hospitality/inside
body: none
auth: inherit
}

settings {
encodeUrl: true
timeout: 0
}
16 changes: 16 additions & 0 deletions bruno/hospitalityAnalytics/GetLiveBedsAnalytics.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
meta {
name: GetLiveBedsAnalytics
type: http
seq: 2
}

get {
url: {{baseUrl}}/analytics/hospitality/beds
body: none
auth: inherit
}

settings {
encodeUrl: true
timeout: 0
}
8 changes: 8 additions & 0 deletions bruno/hospitalityAnalytics/folder.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
meta {
name: hospitalityAnalytics
seq: 18
}

auth {
mode: inherit
}
86 changes: 86 additions & 0 deletions db/gen/annalytics-for-accommodation-panel.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions db/queries/annalytics-for-accommodation-panel.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- name: GetInsideCampusAnalyticsQuery :many
SELECT
logged_at::date AS date,
jsonb_build_object(
'IN', COUNT(*) FILTER (WHERE direction = 'IN'),
'OUT', COUNT(*) FILTER (WHERE direction = 'OUT'),
'CURRENTLY_INSIDE', COUNT(*) FILTER (WHERE direction = 'IN') - COUNT(*) FILTER (WHERE direction = 'OUT')
) AS counts
FROM gate_management
GROUP BY date
ORDER BY date;

-- name: GetLiveBedsAnalyticsQuery :many
SELECT jsonb_build_object(
'hostels', jsonb_agg(
jsonb_build_object(
'id', id,
'hostel_name', hostel_name,
'room_count', room_count,
'room_filled', room_filled
)
),
'total_beds_filled', SUM(room_filled)
)
FROM hostel_metadata;