feat: add model, adapter and enforcer to the dashboard page chart (#3413)

* [feature] Add more data (Model, Adapter, Enforcer) to the dashboard page chart #3379

* feat: add model, adapter, enforcer to dashboard
This commit is contained in:
XIAOZHUOWU
2024-12-09 16:07:39 +08:00
committed by GitHub
parent b927c6d7b4
commit 7322f67ae0
2 changed files with 61 additions and 1 deletions

View File

@ -31,6 +31,9 @@ type Dashboard struct {
CertCounts []int `json:"certCounts"`
PermissionCounts []int `json:"permissionCounts"`
TransactionCounts []int `json:"transactionCounts"`
ModelCounts []int `json:"modelCounts"`
AdapterCounts []int `json:"adapterCounts"`
EnforcerCounts []int `json:"enforcerCounts"`
}
func GetDashboard(owner string) (*Dashboard, error) {
@ -50,6 +53,9 @@ func GetDashboard(owner string) (*Dashboard, error) {
CertCounts: make([]int, 31),
PermissionCounts: make([]int, 31),
TransactionCounts: make([]int, 31),
ModelCounts: make([]int, 31),
AdapterCounts: make([]int, 31),
EnforcerCounts: make([]int, 31),
}
organizations := []Organization{}
@ -63,9 +69,12 @@ func GetDashboard(owner string) (*Dashboard, error) {
certs := []Cert{}
permissions := []Permission{}
transactions := []Transaction{}
models := []Model{}
adapters := []Adapter{}
enforcers := []Enforcer{}
var wg sync.WaitGroup
wg.Add(11)
wg.Add(14)
go func() {
defer wg.Done()
if err := ormer.Engine.Find(&organizations, &Organization{Owner: owner}); err != nil {
@ -148,6 +157,27 @@ func GetDashboard(owner string) (*Dashboard, error) {
panic(err)
}
}()
go func() {
defer wg.Done()
if err := ormer.Engine.Find(&models, &Model{Owner: owner}); err != nil {
panic(err)
}
}()
go func() {
defer wg.Done()
if err := ormer.Engine.Find(&adapters, &Adapter{Owner: owner}); err != nil {
panic(err)
}
}()
go func() {
defer wg.Done()
if err := ormer.Engine.Find(&enforcers, &Enforcer{Owner: owner}); err != nil {
panic(err)
}
}()
wg.Wait()
nowTime := time.Now()
@ -164,6 +194,9 @@ func GetDashboard(owner string) (*Dashboard, error) {
dashboard.CertCounts[30-i] = countCreatedBefore(certs, cutTime)
dashboard.PermissionCounts[30-i] = countCreatedBefore(permissions, cutTime)
dashboard.TransactionCounts[30-i] = countCreatedBefore(transactions, cutTime)
dashboard.ModelCounts[30-i] = countCreatedBefore(models, cutTime)
dashboard.AdapterCounts[30-i] = countCreatedBefore(adapters, cutTime)
dashboard.EnforcerCounts[30-i] = countCreatedBefore(enforcers, cutTime)
}
return dashboard, nil
}
@ -248,6 +281,27 @@ func countCreatedBefore(objects interface{}, before time.Time) int {
count++
}
}
case []Model:
for _, m := range obj {
createdTime, _ := time.Parse("2006-01-02T15:04:05-07:00", m.CreatedTime)
if createdTime.Before(before) {
count++
}
}
case []Adapter:
for _, a := range obj {
createdTime, _ := time.Parse("2006-01-02T15:04:05-07:00", a.CreatedTime)
if createdTime.Before(before) {
count++
}
}
case []Enforcer:
for _, e := range obj {
createdTime, _ := time.Parse("2006-01-02T15:04:05-07:00", e.CreatedTime)
if createdTime.Before(before) {
count++
}
}
}
return count
}