This is an automated email from the ASF dual-hosted git repository.
hsluoyz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/casbin-gateway.git
The following commit(s) were added to refs/heads/master by this push:
new 3b3489f fix: compute record metrics without MySQL-only date functions
3b3489f is described below
commit 3b3489fd9d6e0c879fd717a672df8422f287b696
Author: Yang Luo <[email protected]>
AuthorDate: Tue Aug 18 19:33:23 2026 +0800
fix: compute record metrics without MySQL-only date functions
---
object/record.go | 27 +++++++++++++++++----------
util/time.go | 10 ++++++----
2 files changed, 23 insertions(+), 14 deletions(-)
diff --git a/object/record.go b/object/record.go
index 870f14c..1f7ba58 100644
--- a/object/record.go
+++ b/object/record.go
@@ -15,9 +15,11 @@
package object
import (
+ "fmt"
"strconv"
"time"
+ "github.com/apache/casbin-gateway/util"
"github.com/xorm-io/core"
)
@@ -105,9 +107,9 @@ type DataCount struct {
func GetMetrics(dataType string, startAt time.Time, top int) (*[]DataCount,
error) {
var dataCounts []DataCount
err := ormer.Engine.Table("record").
- Where("UNIX_TIMESTAMP(created_time) > ?", startAt.Unix()).
+ Where("created_time > ?", util.FormatTime(startAt)).
Select(dataType + " as data, COUNT(*) as count").
- GroupBy("data").
+ GroupBy(dataType).
Desc("count").
Limit(top).
Find(&dataCounts)
@@ -119,9 +121,9 @@ func GetMetrics(dataType string, startAt time.Time, top
int) (*[]DataCount, erro
func GetMetricsOverTime(startAt time.Time, timeType string) (*[]DataCount,
error) {
var dataCounts []DataCount
- createdTime := "DATE_FORMAT(created_time, '" +
timeType2Format(timeType) + "')"
+ createdTime := getCreatedTimeBucket(ormer.driverName, timeType)
err := ormer.Engine.Table("record").
- Where("UNIX_TIMESTAMP(created_time) > ?", startAt.Unix()).
+ Where("created_time > ?", util.FormatTime(startAt)).
GroupBy(createdTime).
Select(createdTime + " as data, COUNT(*) as count").
Asc("data").
@@ -132,16 +134,21 @@ func GetMetricsOverTime(startAt time.Time, timeType
string) (*[]DataCount, error
return &dataCounts, nil
}
-func timeType2Format(timeType string) string {
+func getCreatedTimeBucket(driverName string, timeType string) string {
+ length := 13
switch timeType {
- case "hour":
- return "%Y-%m-%d %H"
case "day":
- return "%Y-%m-%d"
+ length = 10
case "month":
- return "%Y-%m"
+ length = 7
}
- return "%Y-%m-%d %H"
+
+ substr := "SUBSTR"
+ if driverName == "mssql" {
+ substr = "SUBSTRING"
+ }
+
+ return fmt.Sprintf("REPLACE(%s(created_time, 1, %d), 'T', ' ')",
substr, length)
}
func GetRecordCount(owner, field, value string) (int64, error) {
diff --git a/util/time.go b/util/time.go
index 64a22eb..ceafadb 100644
--- a/util/time.go
+++ b/util/time.go
@@ -16,13 +16,15 @@ package util
import "time"
-func GetCurrentTime() string {
+func FormatTime(t time.Time) string {
location, err := time.LoadLocation("Asia/Singapore")
if err != nil {
panic(err)
}
- timestamp := time.Now().Unix()
- tm := time.Unix(timestamp, 0)
- return tm.In(location).Format(time.RFC3339)
+ return t.In(location).Format(time.RFC3339)
+}
+
+func GetCurrentTime() string {
+ return FormatTime(time.Now())
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]