mirror of
https://github.com/singchia/frontier.git
synced 2025-09-26 20:31:25 +08:00
add client rpc related repo logic
This commit is contained in:
@@ -7,28 +7,15 @@ import (
|
||||
)
|
||||
|
||||
type ClientQuery struct {
|
||||
// Pagination
|
||||
Page, PageSize int
|
||||
// Time range
|
||||
StartTime, EndTime int64
|
||||
// Search fields
|
||||
Query
|
||||
// Condition fields
|
||||
Meta string
|
||||
Addr string
|
||||
RPC string
|
||||
ClientID uint64
|
||||
// Order
|
||||
Order string
|
||||
Desc bool
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
ClientID uint64
|
||||
Meta string
|
||||
Addr string
|
||||
CreateTime int64
|
||||
}
|
||||
|
||||
func (dao *Dao) ListClients(query *ClientQuery) ([]*Client, error) {
|
||||
func (dao *Dao) ListClients(query *ClientQuery) ([]*model.Client, error) {
|
||||
tx := dao.db.Model(&model.Client{})
|
||||
tx = buildClientQuery(tx, query)
|
||||
|
||||
@@ -51,7 +38,7 @@ func (dao *Dao) ListClients(query *ClientQuery) ([]*Client, error) {
|
||||
})
|
||||
|
||||
// find
|
||||
clients := []*Client{}
|
||||
clients := []*model.Client{}
|
||||
result := tx.Find(&clients)
|
||||
return clients, result.Error
|
||||
}
|
||||
@@ -66,10 +53,10 @@ func (dao *Dao) CountClients(query *ClientQuery) (int64, error) {
|
||||
return count, result.Error
|
||||
}
|
||||
|
||||
func (dao *Dao) GetClient(clientID uint64) (*Client, error) {
|
||||
func (dao *Dao) GetClient(clientID uint64) (*model.Client, error) {
|
||||
tx := dao.db.Model(&model.Client{})
|
||||
tx.Where("client_id = ?", clientID)
|
||||
var client Client
|
||||
var client model.Client
|
||||
result := tx.First(&client)
|
||||
return &client, result.Error
|
||||
}
|
||||
@@ -78,7 +65,7 @@ func (dao *Dao) DeleteClient(clientID uint64) error {
|
||||
return dao.db.Where("client_id = ?", clientID).Delete(&model.Client{}).Error
|
||||
}
|
||||
|
||||
func (dao *Dao) CreateClient(client *Client) error {
|
||||
func (dao *Dao) CreateClient(client *model.Client) error {
|
||||
return dao.db.Create(client).Error
|
||||
}
|
||||
|
||||
@@ -104,3 +91,60 @@ func buildClientQuery(tx *gorm.DB, query *ClientQuery) *gorm.DB {
|
||||
}
|
||||
return tx
|
||||
}
|
||||
|
||||
type ClientRPCQuery struct {
|
||||
Query
|
||||
// Condition fields
|
||||
Meta string
|
||||
ClientID uint64
|
||||
}
|
||||
|
||||
// list RPCs doesn't handle order
|
||||
func (dao *Dao) ListClientRPCs(query *ClientRPCQuery) ([]string, error) {
|
||||
tx := dao.db.Model(&model.ClientRPC{})
|
||||
tx = buildClientRPCQuery(tx, query)
|
||||
// pagination
|
||||
if query.Page <= 0 || query.PageSize <= 0 {
|
||||
query.Page, query.PageSize = 1, 10
|
||||
}
|
||||
offset := query.PageSize * (query.Page - 1)
|
||||
tx = tx.Offset(offset).Limit(query.Page)
|
||||
|
||||
rpcs := []string{}
|
||||
result := tx.Distinct("rpc").Find(&rpcs)
|
||||
return rpcs, result.Error
|
||||
}
|
||||
|
||||
func (dao *Dao) CountClientRPCs(query *ClientRPCQuery) (int64, error) {
|
||||
tx := dao.db.Model(&model.ClientRPC{})
|
||||
tx = buildClientRPCQuery(tx, query)
|
||||
|
||||
// count
|
||||
var count int64
|
||||
result := tx.Distinct("rpc").Count(&count)
|
||||
return count, result.Error
|
||||
}
|
||||
|
||||
func (dao *Dao) DeleteClientRPCs(clientID uint64) error {
|
||||
return dao.db.Where("client_id = ?", clientID).Delete(&model.ClientRPC{}).Error
|
||||
}
|
||||
|
||||
func (dao *Dao) CreateClientRPC(rpc *model.ClientRPC) error {
|
||||
return dao.db.Create(rpc).Error
|
||||
}
|
||||
|
||||
func buildClientRPCQuery(tx *gorm.DB, query *ClientRPCQuery) *gorm.DB {
|
||||
// join
|
||||
if query.Meta != "" {
|
||||
tx = tx.InnerJoins("INNER JOIN clients ON clients.client_id = client_rpcs.client_id AND meta like ?", "%"+query.Meta+"%")
|
||||
}
|
||||
// time range
|
||||
if query.StartTime != 0 && query.EndTime != 0 && query.EndTime > query.StartTime {
|
||||
tx = tx.Where("create_time >= ? AND create_time < ?", query.StartTime, query.EndTime)
|
||||
}
|
||||
// equal
|
||||
if query.ClientID != 0 {
|
||||
tx = tx.Where("client_rpcs.client_id = ?", query.ClientID)
|
||||
}
|
||||
return tx
|
||||
}
|
||||
|
11
pkg/repo/dao/utils.go
Normal file
11
pkg/repo/dao/utils.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package dao
|
||||
|
||||
type Query struct {
|
||||
// Pagination
|
||||
Page, PageSize int
|
||||
// Time range
|
||||
StartTime, EndTime int64
|
||||
// Order
|
||||
Order string
|
||||
Desc bool
|
||||
}
|
@@ -1,7 +1,8 @@
|
||||
package model
|
||||
|
||||
const (
|
||||
TnClients = "clients"
|
||||
TnClients = "clients"
|
||||
TnClientRPCs = "client_rpcs"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
@@ -14,3 +15,13 @@ type Client struct {
|
||||
func (Client) TableName() string {
|
||||
return TnClients
|
||||
}
|
||||
|
||||
type ClientRPC struct {
|
||||
RPC string `gorm:"column:rpc;index:idx_rpc"`
|
||||
ClientID uint64 `gorm:"column:client_id;index:idx_client_id"`
|
||||
CreateTime int64 `gorm:"column:create_time;index:create_time"`
|
||||
}
|
||||
|
||||
func (ClientRPC) TableName() string {
|
||||
return TnClientRPCs
|
||||
}
|
||||
|
Reference in New Issue
Block a user