Skip to content

Commit ecdee58

Browse files
author
Working On It
committed
feat: record user location and statistic
1 parent 28e3c55 commit ecdee58

File tree

5 files changed

+70
-2
lines changed

5 files changed

+70
-2
lines changed

controllers/activity.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ func (c *ApiController) GetActivities() {
3737
return
3838
}
3939

40-
c.ResponseOk(activities)
40+
usageMetadata, err := object.GetUsageMetadata()
41+
if err != nil {
42+
c.ResponseError(err.Error())
43+
return
44+
}
45+
46+
c.ResponseOk(activities, usageMetadata)
4147
}
4248

4349
// GetRangeActivities

object/activity.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ func getTargetfieldValue(record *Record, fieldName string) (string, error) {
5252
} else {
5353
return "", err
5454
}
55+
case "unit":
56+
return record.Unit, nil
57+
case "section":
58+
return record.Section, nil
5559
}
5660
return "", errors.New("no matched field")
5761
}

object/record.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ type Record struct {
6363

6464
IsTriggered bool `json:"isTriggered"`
6565
NeedCommit bool `json:"needCommit"`
66+
67+
Unit string `xorm:"varchar(100)" json:"unit"`
68+
Section string `xorm:"varchar(100)" json:"section"`
6669
}
6770

6871
type Response struct {
@@ -194,6 +197,12 @@ func NewRecord(ctx *context.Context) (*Record, error) {
194197
}
195198
languageCode := conf.GetLanguage(language)
196199

200+
// get location info from client ip
201+
locationInfo, err := util.FindMaxmind(ip)
202+
if err != nil {
203+
return nil, err
204+
}
205+
197206
record := Record{
198207
Name: util.GenerateId(),
199208
CreatedTime: util.GetCurrentTime(),
@@ -206,6 +215,8 @@ func NewRecord(ctx *context.Context) (*Record, error) {
206215
Object: object,
207216
Response: fmt.Sprintf("{\"status\":\"%s\",\"msg\":\"%s\"}", resp.Status, resp.Msg),
208217
IsTriggered: false,
218+
Unit: locationInfo.Region,
219+
Section: locationInfo.City,
209220
}
210221
return &record, nil
211222
}

object/record_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2025 The Casibase Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//go:build !skipCi
16+
// +build !skipCi
17+
18+
package object
19+
20+
import (
21+
"testing"
22+
23+
"github.com/casibase/casibase/util"
24+
)
25+
26+
func TestUpdateRecordsLocation(t *testing.T) {
27+
InitConfig()
28+
records, err := GetRecords("casbin")
29+
if err != nil {
30+
t.Error(err)
31+
}
32+
33+
for _, r := range records {
34+
if r.Unit == "" || r.Section == "" {
35+
locationInfo, err := util.FindMaxmind(r.ClientIp)
36+
if err != nil {
37+
t.Error(err)
38+
}
39+
r.Unit = locationInfo.Region
40+
r.Section = locationInfo.City
41+
_, err = adapter.engine.ID(r.Id).Update(r)
42+
if err != nil {
43+
t.Error(err)
44+
}
45+
}
46+
}
47+
}

web/src/ActivityPage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const {Option} = Select;
2626
class ActivityPage extends BaseListPage {
2727
constructor(props) {
2828
super(props);
29-
this.subPieCharts = ["client_ip", "language", "response"],
29+
this.subPieCharts = ["unit", "section", "response"],
3030
this.state = {
3131
classes: props,
3232
activities: null,

0 commit comments

Comments
 (0)