Skip to content

Commit

Permalink
feat(dashboard): Dashboard api returning different analytics
Browse files Browse the repository at this point in the history
Signed-off-by: deo002 <[email protected]>
  • Loading branch information
deo002 committed Feb 13, 2025
1 parent 035c554 commit 29912cd
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 0 deletions.
36 changes: 36 additions & 0 deletions cmd/laas/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,42 @@ const docTemplate = `{
}
}
},
"/dashboard": {
"get": {
"security": [
{
"ApiKeyAuth": [],
"{}": []
}
],
"description": "Fetches data to be displayed on the dashboard",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Dashboard"
],
"summary": "Fetches data to be displayed on the dashboard",
"operationId": "GetDashboardData",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/models.LicenseResponse"
}
},
"500": {
"description": "Something went wrong",
"schema": {
"$ref": "#/definitions/models.LicenseError"
}
}
}
}
},
"/health": {
"get": {
"description": "Check health of the service",
Expand Down
36 changes: 36 additions & 0 deletions cmd/laas/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,42 @@
}
}
},
"/dashboard": {
"get": {
"security": [
{
"ApiKeyAuth": [],
"{}": []
}
],
"description": "Fetches data to be displayed on the dashboard",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Dashboard"
],
"summary": "Fetches data to be displayed on the dashboard",
"operationId": "GetDashboardData",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/models.LicenseResponse"
}
},
"500": {
"description": "Something went wrong",
"schema": {
"$ref": "#/definitions/models.LicenseError"
}
}
}
}
},
"/health": {
"get": {
"description": "Check health of the service",
Expand Down
23 changes: 23 additions & 0 deletions cmd/laas/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,29 @@ paths:
summary: Get a changelog
tags:
- Audits
/dashboard:
get:
consumes:
- application/json
description: Fetches data to be displayed on the dashboard
operationId: GetDashboardData
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/models.LicenseResponse'
"500":
description: Something went wrong
schema:
$ref: '#/definitions/models.LicenseError'
security:
- '{}': []
ApiKeyAuth: []
summary: Fetches data to be displayed on the dashboard
tags:
- Dashboard
/health:
get:
consumes:
Expand Down
8 changes: 8 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ func Router() *gin.Engine {
audit.GET(":audit_id/changes", GetChangeLogs)
audit.GET(":audit_id/changes/:id", GetChangeLogbyId)
}
dashboard := authorizedv1.Group("/dashboard")
{
dashboard.GET("", GetDashboardData)
}
}
} else {
unAuthorizedv1 := r.Group("/api/v1")
Expand Down Expand Up @@ -213,6 +217,10 @@ func Router() *gin.Engine {
{
oidc.POST("", auth.CreateOidcUser)
}
dashboard := unAuthorizedv1.Group("/dashboard")
{
dashboard.GET("", GetDashboardData)
}
}

authorizedv1 := r.Group("/api/v1")
Expand Down
115 changes: 115 additions & 0 deletions pkg/api/dashboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// SPDX-FileCopyrightText: 2025 Siemens AG
// SPDX-FileContributor: Dearsh Oberoi <[email protected]>
//
// SPDX-License-Identifier: GPL-2.0-only

package api

import (
"log"
"net/http"
"time"

"github.com/fossology/LicenseDb/pkg/db"
"github.com/fossology/LicenseDb/pkg/models"
"github.com/gin-gonic/gin"
)

// GetDashboardData fetches data to be displayed on the dashboard
//
// @Summary Fetches data to be displayed on the dashboard
// @Description Fetches data to be displayed on the dashboard
// @Id GetDashboardData
// @Tags Dashboard
// @Accept json
// @Produce json
// @Success 200 {object} models.LicenseResponse
// @Failure 500 {object} models.LicenseError "Something went wrong"
// @Security ApiKeyAuth || {}
// @Router /dashboard [get]
func GetDashboardData(c *gin.Context) {
var licensesCount, obligationsCount, usersCount, licenseChangesSinceLastMonth int64
var licenseFrequency []models.RiskLicenseCount

var active = true
if err := db.DB.Model(&models.LicenseDB{}).Where(&models.LicenseDB{Active: &active}).Count(&licensesCount).Error; err != nil {
log.Printf("\033[31mError: error fetching licenses count: %s\033[0m", err.Error())
er := models.LicenseError{
Status: http.StatusInternalServerError,
Message: "something went wrong",
Error: "error fetching licenses count",
Path: c.Request.URL.Path,
Timestamp: time.Now().Format(time.RFC3339),
}
c.JSON(http.StatusInternalServerError, er)
return
}

if err := db.DB.Model(&models.Obligation{}).Where(&models.Obligation{Active: &active}).Count(&obligationsCount).Error; err != nil {
log.Printf("\033[31mError: error fetching obligations count: %s\033[0m", err.Error())
er := models.LicenseError{
Status: http.StatusInternalServerError,
Message: "something went wrong",
Error: "error fetching obligations count",
Path: c.Request.URL.Path,
Timestamp: time.Now().Format(time.RFC3339),
}
c.JSON(http.StatusInternalServerError, er)
return
}

if err := db.DB.Model(&models.User{}).Where(&models.User{Active: &active}).Count(&usersCount).Error; err != nil {
log.Printf("\033[31mError: error fetching users count: %s\033[0m", err.Error())
er := models.LicenseError{
Status: http.StatusInternalServerError,
Message: "something went wrong",
Error: "error fetching users count",
Path: c.Request.URL.Path,
Timestamp: time.Now().Format(time.RFC3339),
}
c.JSON(http.StatusInternalServerError, er)
return
}

now := time.Now()
startOfMonth := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.UTC)
iso8601StartOfMonth := startOfMonth.Format(time.RFC3339)
if err := db.DB.Model(&models.Audit{}).Where("timestamp > ? AND type='license'", iso8601StartOfMonth).Count(&licenseChangesSinceLastMonth).Error; err != nil {
log.Printf("\033[31mError: error fetching audits count: %s\033[0m", err.Error())
er := models.LicenseError{
Status: http.StatusInternalServerError,
Message: "something went wrong",
Error: "error fetching audits count",
Path: c.Request.URL.Path,
Timestamp: time.Now().Format(time.RFC3339),
}
c.JSON(http.StatusInternalServerError, er)
return
}

if err := db.DB.Debug().Model(&models.LicenseDB{}).Select("rf_risk as risk, count(*) as count").Group("rf_risk").Scan(&licenseFrequency).Error; err != nil {
log.Printf("\033[31mError: error fetching risk license frequencies: %s\033[0m", err.Error())
er := models.LicenseError{
Status: http.StatusInternalServerError,
Message: "something went wrong",
Error: "error fetching risk liicense frequencies",
Path: c.Request.URL.Path,
Timestamp: time.Now().Format(time.RFC3339),
}
c.JSON(http.StatusInternalServerError, er)
return
}

res := models.DashboardResponse{
Data: models.Dashboard{
LicensesCount: licensesCount,
ObligationsCount: obligationsCount,
LicenseChangesSinceLastMonth: licenseChangesSinceLastMonth,
UsersCount: usersCount,
RiskLicenseFrequency: licenseFrequency,
},
Status: http.StatusOK,
}

c.JSON(http.StatusOK, res)
}
1 change: 1 addition & 0 deletions pkg/api/licenses.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-FileCopyrightText: 2023 Kavya Shukla <[email protected]>
// SPDX-FileCopyrightText: 2023 Siemens AG
// SPDX-FileContributor: Gaurav Mishra <[email protected]>
// SPDX-FileContributor: Dearsh Oberoi <[email protected]>
//
// SPDX-License-Identifier: GPL-2.0-only

Expand Down
18 changes: 18 additions & 0 deletions pkg/models/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -741,3 +741,21 @@ type SwaggerDocAPISecurityScheme struct {
OperationId string `json:"operationId" example:"GetLicense"`
} `json:"paths"`
}

type RiskLicenseCount struct {
Risk int64 `json:"risk"`
Count int64 `json:"count"`
}

type Dashboard struct {
LicensesCount int64 `json:"licenses_count"`
ObligationsCount int64 `json:"obligations_count"`
UsersCount int64 `json:"users_count"`
LicenseChangesSinceLastMonth int64 `json:"monthly_license_changes_count"`
RiskLicenseFrequency []RiskLicenseCount `json:"risk_license_frequency"`
}

type DashboardResponse struct {
Status int `json:"status" example:"200"`
Data Dashboard `json:"data"`
}

0 comments on commit 29912cd

Please sign in to comment.