mirror of
https://github.com/APIParkLab/APIPark.git
synced 2026-06-14 20:41:15 +08:00
data mask log commit
This commit is contained in:
@@ -159,6 +159,7 @@ func (s *imlClusterService) Create(ctx context.Context, name string, resume stri
|
||||
UUID: apintoInfo.Cluster,
|
||||
Name: name,
|
||||
Resume: resume,
|
||||
Cluster: apintoInfo.Cluster,
|
||||
Creator: operator,
|
||||
Updater: operator,
|
||||
CreateAt: time.Now(),
|
||||
@@ -285,13 +286,15 @@ func (s *imlClusterService) UpdateAddress(ctx context.Context, id string, addres
|
||||
return nil, err
|
||||
}
|
||||
cv = &cluster.Cluster{
|
||||
UUID: id,
|
||||
Name: "默认集群",
|
||||
Resume: "默认集群",
|
||||
UUID: id,
|
||||
Name: "默认集群",
|
||||
Resume: "默认集群",
|
||||
|
||||
Creator: operator,
|
||||
CreateAt: now,
|
||||
}
|
||||
}
|
||||
cv.Cluster = info.Cluster
|
||||
|
||||
// check node
|
||||
nodeIds := utils.SliceToSlice(info.Nodes, func(i *admin.Node) string {
|
||||
|
||||
@@ -2,7 +2,7 @@ package cluster
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
|
||||
"github.com/APIParkLab/APIPark/stores/cluster"
|
||||
)
|
||||
|
||||
@@ -10,6 +10,7 @@ type Cluster struct {
|
||||
Uuid string
|
||||
Name string
|
||||
Resume string
|
||||
Cluster string
|
||||
Creator string
|
||||
Updater string
|
||||
Status int
|
||||
@@ -22,6 +23,7 @@ func FromEntity(entity *cluster.Cluster) *Cluster {
|
||||
Uuid: entity.UUID,
|
||||
Name: entity.Name,
|
||||
Resume: entity.Resume,
|
||||
Cluster: entity.Cluster,
|
||||
Creator: entity.Creator,
|
||||
Updater: entity.Updater,
|
||||
CreateTime: entity.CreateAt,
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
log_driver "github.com/APIParkLab/APIPark/log-driver"
|
||||
|
||||
"github.com/eolinker/go-common/utils"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
log_source "github.com/APIParkLab/APIPark/stores/log-source"
|
||||
)
|
||||
|
||||
var (
|
||||
_ ILogService = (*imlLogService)(nil)
|
||||
)
|
||||
|
||||
type imlLogService struct {
|
||||
store log_source.ILogSourceStore `autowired:""`
|
||||
}
|
||||
|
||||
func (i *imlLogService) OnComplete() {
|
||||
drivers := log_driver.Drivers()
|
||||
for _, d := range drivers {
|
||||
factory, has := log_driver.GetFactory(d)
|
||||
if !has {
|
||||
continue
|
||||
}
|
||||
s, err := i.GetLogSource(context.Background(), d)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
driver, err := factory.Create(s.Config)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
log_driver.SetDriver(d, driver)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func (i *imlLogService) UpdateLogSource(ctx context.Context, driver string, input *Save) error {
|
||||
factory, has := log_driver.GetFactory(driver)
|
||||
if !has {
|
||||
return errors.New("driver not found")
|
||||
}
|
||||
s, err := i.store.First(ctx, map[string]interface{}{"driver": driver})
|
||||
if err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
if input.ID == "" {
|
||||
input.ID = uuid.NewString()
|
||||
}
|
||||
if input.Cluster == nil || *input.Cluster == "" {
|
||||
return errors.New("cluster is required")
|
||||
}
|
||||
if input.Config == nil || *input.Config == "" {
|
||||
return errors.New("config is required")
|
||||
}
|
||||
now := time.Now()
|
||||
userId := utils.UserId(ctx)
|
||||
s = &log_source.Log{
|
||||
UUID: input.ID,
|
||||
Cluster: *input.Cluster,
|
||||
Driver: driver,
|
||||
Config: *input.Config,
|
||||
Creator: userId,
|
||||
Updater: userId,
|
||||
CreateAt: now,
|
||||
UpdateAt: now,
|
||||
}
|
||||
|
||||
} else {
|
||||
if input.Config == nil || *input.Config == "" {
|
||||
s.Config = *input.Config
|
||||
}
|
||||
s.Updater = utils.UserId(ctx)
|
||||
s.UpdateAt = time.Now()
|
||||
}
|
||||
newDriver, err := factory.Create(s.Config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = i.store.Save(ctx, s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log_driver.SetDriver(driver, newDriver)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *imlLogService) GetLogSource(ctx context.Context, driver string) (*Source, error) {
|
||||
s, err := i.store.First(ctx, map[string]interface{}{"driver": driver})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return FromEntity(s), nil
|
||||
}
|
||||
|
||||
func (i *imlLogService) Logs(ctx context.Context, driver string, cluster string, conditions map[string]string, start time.Time, end time.Time, limit int64, offset int64) ([]*Item, int64, error) {
|
||||
d, has := log_driver.GetDriver(driver)
|
||||
if !has {
|
||||
return nil, 0, errors.New("driver not found")
|
||||
}
|
||||
list, count, err := d.Logs(cluster, conditions, start, end, limit, offset)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
result := make([]*Item, 0, len(list))
|
||||
for _, l := range list {
|
||||
result = append(result, &Item{
|
||||
ID: l.ID,
|
||||
Service: l.Service,
|
||||
Method: l.Method,
|
||||
Url: l.Url,
|
||||
RemoteIP: l.RemoteIP,
|
||||
Consumer: l.Consumer,
|
||||
Authorization: l.Authorization,
|
||||
RecordTime: l.RecordTime,
|
||||
})
|
||||
}
|
||||
return result, count, nil
|
||||
}
|
||||
|
||||
func (i *imlLogService) LogCount(ctx context.Context, driver string, cluster string, conditions map[string]string, spendHour int64, group string) (map[string]int64, error) {
|
||||
d, has := log_driver.GetDriver(driver)
|
||||
if !has {
|
||||
return nil, errors.New("driver not found")
|
||||
}
|
||||
return d.LogCount(cluster, conditions, spendHour, group)
|
||||
}
|
||||
|
||||
func (i *imlLogService) LogInfo(ctx context.Context, driver string, cluster string, id string) (*Info, error) {
|
||||
d, has := log_driver.GetDriver(driver)
|
||||
if !has {
|
||||
return nil, errors.New("driver not found")
|
||||
}
|
||||
info, err := d.LogInfo(cluster, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Info{
|
||||
ID: info.ID,
|
||||
ContentType: info.ContentType,
|
||||
RequestBody: info.RequestBody,
|
||||
ProxyBody: info.ProxyBody,
|
||||
ProxyResponseBody: info.ProxyResponseBody,
|
||||
ResponseBody: info.ResponseBody,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
log_source "github.com/APIParkLab/APIPark/stores/log-source"
|
||||
)
|
||||
|
||||
type Save struct {
|
||||
ID string
|
||||
Cluster *string
|
||||
Config *string
|
||||
}
|
||||
|
||||
type Source struct {
|
||||
ID string
|
||||
Cluster string
|
||||
Driver string
|
||||
Config string
|
||||
Creator string
|
||||
Updater string
|
||||
CreateAt time.Time
|
||||
UpdateAt time.Time
|
||||
}
|
||||
|
||||
func FromEntity(ov *log_source.Log) *Source {
|
||||
return &Source{
|
||||
ID: ov.UUID,
|
||||
Cluster: ov.Cluster,
|
||||
Driver: ov.Driver,
|
||||
Config: ov.Config,
|
||||
Creator: ov.Creator,
|
||||
Updater: ov.Updater,
|
||||
CreateAt: ov.CreateAt,
|
||||
UpdateAt: ov.UpdateAt,
|
||||
}
|
||||
}
|
||||
|
||||
type Item struct {
|
||||
ID string
|
||||
Service string
|
||||
Method string
|
||||
Url string
|
||||
RemoteIP string
|
||||
Consumer string
|
||||
Authorization string
|
||||
RecordTime time.Time
|
||||
}
|
||||
|
||||
type Info struct {
|
||||
ID string
|
||||
ContentType string
|
||||
RequestBody string
|
||||
ProxyBody string
|
||||
ProxyResponseBody string
|
||||
ResponseBody string
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
_ "github.com/APIParkLab/APIPark/log-driver/loki"
|
||||
"github.com/eolinker/go-common/autowire"
|
||||
)
|
||||
|
||||
type ILogService interface {
|
||||
UpdateLogSource(ctx context.Context, driver string, input *Save) error
|
||||
GetLogSource(ctx context.Context, driver string) (*Source, error)
|
||||
Logs(ctx context.Context, driver string, cluster string, conditions map[string]string, start time.Time, end time.Time, limit int64, offset int64) ([]*Item, int64, error)
|
||||
LogCount(ctx context.Context, driver string, cluster string, conditions map[string]string, spendHour int64, group string) (map[string]int64, error)
|
||||
LogInfo(ctx context.Context, driver string, cluster string, id string) (*Info, error)
|
||||
}
|
||||
|
||||
func init() {
|
||||
logService := &imlLogService{}
|
||||
autowire.Auto[ILogService](func() reflect.Value {
|
||||
return reflect.ValueOf(logService)
|
||||
})
|
||||
}
|
||||
@@ -16,8 +16,8 @@ import (
|
||||
var _ IStrategyService = (*imlStrategyService)(nil)
|
||||
|
||||
type imlStrategyService struct {
|
||||
store strategy.IStrategyStore `autowired:""`
|
||||
commitService commit.ICommitService[StrategyCommit] `autowired:""`
|
||||
store strategy.IStrategyStore `autowired:""`
|
||||
commitService commit.ICommitService[Commit] `autowired:""`
|
||||
universally.IServiceCreate[Create]
|
||||
universally.IServiceEdit[Edit]
|
||||
}
|
||||
@@ -73,23 +73,24 @@ func (i *imlStrategyService) CommitStrategy(ctx context.Context, scope string, t
|
||||
key = fmt.Sprintf("%s-%s", scope, target)
|
||||
}
|
||||
|
||||
return i.commitService.Save(ctx, strategyId, key, &StrategyCommit{
|
||||
return i.commitService.Save(ctx, strategyId, key, &Commit{
|
||||
Id: data.Id,
|
||||
Name: data.Name,
|
||||
Priority: data.Priority,
|
||||
Filters: data.Filters,
|
||||
Config: data.Config,
|
||||
Driver: data.Driver,
|
||||
IsDelete: data.IsDelete,
|
||||
IsStop: data.IsStop,
|
||||
Version: data.UpdateAt.Format("20060102150405"),
|
||||
})
|
||||
}
|
||||
|
||||
func (i *imlStrategyService) GetStrategyCommit(ctx context.Context, commitId string) (*commit.Commit[StrategyCommit], error) {
|
||||
func (i *imlStrategyService) GetStrategyCommit(ctx context.Context, commitId string) (*commit.Commit[Commit], error) {
|
||||
return i.commitService.Get(ctx, commitId)
|
||||
}
|
||||
|
||||
func (i *imlStrategyService) LatestStrategyCommit(ctx context.Context, scope string, target string, strategyId string) (*commit.Commit[StrategyCommit], error) {
|
||||
func (i *imlStrategyService) LatestStrategyCommit(ctx context.Context, scope string, target string, strategyId string) (*commit.Commit[Commit], error) {
|
||||
key := scope
|
||||
if target != "" {
|
||||
key = fmt.Sprintf("%s-%s", scope, target)
|
||||
@@ -97,7 +98,7 @@ func (i *imlStrategyService) LatestStrategyCommit(ctx context.Context, scope str
|
||||
return i.commitService.Latest(ctx, strategyId, key)
|
||||
}
|
||||
|
||||
func (i *imlStrategyService) ListLatestStrategyCommit(ctx context.Context, scope string, target string, strategyIds ...string) ([]*commit.Commit[StrategyCommit], error) {
|
||||
func (i *imlStrategyService) ListLatestStrategyCommit(ctx context.Context, scope string, target string, strategyIds ...string) ([]*commit.Commit[Commit], error) {
|
||||
key := scope
|
||||
if target != "" {
|
||||
key = fmt.Sprintf("%s-%s", scope, target)
|
||||
@@ -105,7 +106,7 @@ func (i *imlStrategyService) ListLatestStrategyCommit(ctx context.Context, scope
|
||||
return i.commitService.ListLatest(ctx, key, strategyIds...)
|
||||
}
|
||||
|
||||
func (i *imlStrategyService) ListStrategyCommit(ctx context.Context, commitIds ...string) ([]*commit.Commit[StrategyCommit], error) {
|
||||
func (i *imlStrategyService) ListStrategyCommit(ctx context.Context, commitIds ...string) ([]*commit.Commit[Commit], error) {
|
||||
if len(commitIds) < 1 {
|
||||
return nil, fmt.Errorf("commit ids is empty")
|
||||
}
|
||||
|
||||
@@ -65,13 +65,14 @@ type Edit struct {
|
||||
IsStop *bool
|
||||
}
|
||||
|
||||
type StrategyCommit struct {
|
||||
type Commit struct {
|
||||
Id string
|
||||
Name string
|
||||
Priority int
|
||||
Filters string
|
||||
Config string
|
||||
Driver string
|
||||
IsDelete bool
|
||||
IsStop bool
|
||||
Version string
|
||||
}
|
||||
|
||||
@@ -25,15 +25,15 @@ type IStrategyService interface {
|
||||
Restore(ctx context.Context, id string) error
|
||||
|
||||
CommitStrategy(ctx context.Context, scope string, target string, strategyId string, data *Strategy) error
|
||||
GetStrategyCommit(ctx context.Context, commitId string) (*commit.Commit[StrategyCommit], error)
|
||||
LatestStrategyCommit(ctx context.Context, scope string, target string, strategyId string) (*commit.Commit[StrategyCommit], error)
|
||||
ListLatestStrategyCommit(ctx context.Context, scope string, target string, strategyIds ...string) ([]*commit.Commit[StrategyCommit], error)
|
||||
ListStrategyCommit(ctx context.Context, commitIds ...string) ([]*commit.Commit[StrategyCommit], error)
|
||||
GetStrategyCommit(ctx context.Context, commitId string) (*commit.Commit[Commit], error)
|
||||
LatestStrategyCommit(ctx context.Context, scope string, target string, strategyId string) (*commit.Commit[Commit], error)
|
||||
ListLatestStrategyCommit(ctx context.Context, scope string, target string, strategyIds ...string) ([]*commit.Commit[Commit], error)
|
||||
ListStrategyCommit(ctx context.Context, commitIds ...string) ([]*commit.Commit[Commit], error)
|
||||
}
|
||||
|
||||
func init() {
|
||||
autowire.Auto[IStrategyService](func() reflect.Value {
|
||||
return reflect.ValueOf(new(imlStrategyService))
|
||||
})
|
||||
commit.InitCommitService[StrategyCommit]("strategy")
|
||||
commit.InitCommitService[Commit]("strategy")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user