2023-08-14 14:32:12 +08:00
|
|
|
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package object
|
|
|
|
|
2023-08-14 15:43:09 +08:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
2025-02-11 17:20:45 +08:00
|
|
|
|
|
|
|
"github.com/casdoor/casdoor/conf"
|
2023-08-14 15:43:09 +08:00
|
|
|
)
|
2023-08-14 14:32:12 +08:00
|
|
|
|
2024-12-28 23:52:19 +08:00
|
|
|
type DashboardDateItem struct {
|
|
|
|
CreatedTime string `json:"createTime"`
|
|
|
|
}
|
|
|
|
|
2024-12-29 16:15:52 +08:00
|
|
|
type DashboardMapItem struct {
|
|
|
|
dashboardDateItems []DashboardDateItem
|
|
|
|
itemCount int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetDashboard(owner string) (*map[string][]int64, error) {
|
2024-03-15 19:52:19 +08:00
|
|
|
if owner == "All" {
|
|
|
|
owner = ""
|
|
|
|
}
|
|
|
|
|
2024-12-29 16:15:52 +08:00
|
|
|
dashboard := make(map[string][]int64)
|
|
|
|
dashboardMap := sync.Map{}
|
|
|
|
tableNames := []string{"organization", "user", "provider", "application", "subscription", "role", "group", "resource", "cert", "permission", "transaction", "model", "adapter", "enforcer"}
|
2023-08-14 15:43:09 +08:00
|
|
|
|
2024-12-29 16:15:52 +08:00
|
|
|
time30day := time.Now().AddDate(0, 0, -30)
|
2024-03-15 19:52:19 +08:00
|
|
|
var wg sync.WaitGroup
|
2024-12-29 16:15:52 +08:00
|
|
|
var err error
|
2025-02-11 17:20:45 +08:00
|
|
|
tableNamePrefix := conf.GetConfigString("tableNamePrefix")
|
2024-12-29 16:15:52 +08:00
|
|
|
wg.Add(len(tableNames))
|
2025-01-07 10:35:45 +08:00
|
|
|
ch := make(chan error, len(tableNames))
|
2024-12-29 16:15:52 +08:00
|
|
|
for _, tableName := range tableNames {
|
|
|
|
dashboard[tableName+"Counts"] = make([]int64, 31)
|
2025-02-11 17:20:45 +08:00
|
|
|
tableFullName := tableNamePrefix + tableName
|
2025-01-07 10:35:45 +08:00
|
|
|
go func(ch chan error) {
|
2024-12-29 16:15:52 +08:00
|
|
|
defer wg.Done()
|
|
|
|
dashboardDateItems := []DashboardDateItem{}
|
|
|
|
var countResult int64
|
|
|
|
|
|
|
|
dbQueryBefore := ormer.Engine.Cols("created_time")
|
|
|
|
dbQueryAfter := ormer.Engine.Cols("created_time")
|
|
|
|
|
|
|
|
if owner != "" {
|
|
|
|
dbQueryAfter = dbQueryAfter.And("owner = ?", owner)
|
|
|
|
dbQueryBefore = dbQueryBefore.And("owner = ?", owner)
|
|
|
|
}
|
2024-12-09 16:07:39 +08:00
|
|
|
|
2025-02-11 17:20:45 +08:00
|
|
|
if countResult, err = dbQueryBefore.And("created_time < ?", time30day).Table(tableFullName).Count(); err != nil {
|
2025-01-07 10:35:45 +08:00
|
|
|
ch <- err
|
|
|
|
return
|
2024-12-29 16:15:52 +08:00
|
|
|
}
|
2025-02-11 17:20:45 +08:00
|
|
|
if err = dbQueryAfter.And("created_time >= ?", time30day).Table(tableFullName).Find(&dashboardDateItems); err != nil {
|
2025-01-07 10:35:45 +08:00
|
|
|
ch <- err
|
|
|
|
return
|
2024-12-29 16:15:52 +08:00
|
|
|
}
|
2024-12-09 16:07:39 +08:00
|
|
|
|
2025-02-11 17:20:45 +08:00
|
|
|
dashboardMap.Store(tableFullName, DashboardMapItem{
|
2024-12-29 16:15:52 +08:00
|
|
|
dashboardDateItems: dashboardDateItems,
|
|
|
|
itemCount: countResult,
|
|
|
|
})
|
2025-01-07 10:35:45 +08:00
|
|
|
}(ch)
|
2024-12-29 16:15:52 +08:00
|
|
|
}
|
2024-12-09 16:07:39 +08:00
|
|
|
|
2023-08-14 15:43:09 +08:00
|
|
|
wg.Wait()
|
2025-01-07 10:35:45 +08:00
|
|
|
close(ch)
|
|
|
|
|
|
|
|
for err = range ch {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2023-08-14 15:43:09 +08:00
|
|
|
|
|
|
|
nowTime := time.Now()
|
2023-08-14 14:32:12 +08:00
|
|
|
for i := 30; i >= 0; i-- {
|
|
|
|
cutTime := nowTime.AddDate(0, 0, -i)
|
2024-12-29 16:15:52 +08:00
|
|
|
for _, tableName := range tableNames {
|
|
|
|
item, exist := dashboardMap.Load(tableName)
|
|
|
|
if !exist {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
dashboard[tableName+"Counts"][30-i] = countCreatedBefore(item.(DashboardMapItem), cutTime)
|
|
|
|
}
|
2023-08-14 14:32:12 +08:00
|
|
|
}
|
2024-12-29 16:15:52 +08:00
|
|
|
return &dashboard, nil
|
2023-08-14 14:32:12 +08:00
|
|
|
}
|
|
|
|
|
2024-12-29 16:15:52 +08:00
|
|
|
func countCreatedBefore(dashboardMapItem DashboardMapItem, before time.Time) int64 {
|
|
|
|
count := dashboardMapItem.itemCount
|
|
|
|
for _, e := range dashboardMapItem.dashboardDateItems {
|
|
|
|
createdTime, _ := time.Parse("2006-01-02T15:04:05-07:00", e.CreatedTime)
|
|
|
|
if createdTime.Before(before) {
|
|
|
|
count++
|
2024-12-28 23:52:19 +08:00
|
|
|
}
|
2023-08-14 14:32:12 +08:00
|
|
|
}
|
|
|
|
return count
|
|
|
|
}
|