-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dashboard): Dashboard api returning different analytics
Signed-off-by: deo002 <[email protected]>
- Loading branch information
Showing
7 changed files
with
237 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters