Skip to content

Commit

Permalink
feat: refresh logs and metrics separately
Browse files Browse the repository at this point in the history
  • Loading branch information
CeerDecy committed Jan 13, 2025
1 parent 4dacc50 commit f7b32d6
Show file tree
Hide file tree
Showing 8 changed files with 265 additions and 86 deletions.
26 changes: 22 additions & 4 deletions api/proto/core/monitor/settings/settings.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ service SettingsService {
check_token: true,
}
};

rpc GetSettings (GetSettingsRequest) returns (GetSettingsResponse) {
option (google.api.http) = {
get: "/api/global/settings?org_id={orgID}"
Expand All @@ -36,6 +36,13 @@ service SettingsService {
};
}

rpc PutSettingsWithType(PutSettingsWithTypeRequest) returns (PutSettingsWithTypeResponse) {
option (google.api.http) = {
put: "/api/global/settings/{monitorType}?org_id={orgID}&namespace={namespace}",
body: "data"
};
}

rpc RegisterMonitorConfig (RegisterMonitorConfigRequest) returns (RegisterMonitorConfigResponse) {
option (google.api.http) = {
put: "/api/config/register",
Expand All @@ -49,7 +56,7 @@ message GetSettingsRequest {
int64 orgID = 1 [(validate.rules).int64.gt = 0];
string workspace = 2;
}

message GetSettingsResponse {
map<string, ConfigGroups> data = 1;
}
Expand All @@ -59,11 +66,22 @@ message PutSettingsRequest {
map<string, ConfigGroups> data = 1;
int64 orgID = 2 [(validate.rules).int64.gt = 0];
}

message PutSettingsResponse {
string data = 1;
}

message PutSettingsWithTypeRequest {
ConfigGroup data = 1;
int64 orgID = 2 [(validate.rules).int64.gt = 0];
string namespace = 3;
string monitorType = 4;
}

message PutSettingsWithTypeResponse {
string data = 1;
}

// RegisterMonitorConfig
message RegisterMonitorConfigRequest {
repeated MonitorConfig data = 1;
Expand All @@ -80,7 +98,7 @@ message MonitorConfig {
bool enable = 7;
string desc = 8;
}

message RegisterMonitorConfigResponse {
string data = 1;
}
Expand Down
12 changes: 7 additions & 5 deletions internal/tools/monitor/common/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ import "github.com/jinzhu/gorm"
// DB .
type DB struct {
*gorm.DB
Monitor MonitorDb
InstanceTenant InstanceTenantDb
Monitor MonitorDb
InstanceTenant InstanceTenantDb
MonitorConfigRegister *MonitorConfigRegisterDB
}

// New .
func New(db *gorm.DB) *DB {
return &DB{
DB: db,
Monitor: MonitorDb{db},
InstanceTenant: InstanceTenantDb{db},
DB: db,
Monitor: MonitorDb{db},
InstanceTenant: InstanceTenantDb{db},
MonitorConfigRegister: NewMonitorConfigRegisterDB(db),

Check warning on line 33 in internal/tools/monitor/common/db/db.go

View check run for this annotation

Codecov / codecov/patch

internal/tools/monitor/common/db/db.go#L30-L33

Added lines #L30 - L33 were not covered by tests
}
}

Expand Down
51 changes: 51 additions & 0 deletions internal/tools/monitor/common/db/monitor_config_register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) 2025 Terminus, Inc.
//
// 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 db

import (
"errors"

"github.com/jinzhu/gorm"
)

type MonitorConfigRegisterDB struct {
*gorm.DB
}

func NewMonitorConfigRegisterDB(db *gorm.DB) *MonitorConfigRegisterDB {
return &MonitorConfigRegisterDB{db}

Check warning on line 28 in internal/tools/monitor/common/db/monitor_config_register.go

View check run for this annotation

Codecov / codecov/patch

internal/tools/monitor/common/db/monitor_config_register.go#L27-L28

Added lines #L27 - L28 were not covered by tests
}

func (m *MonitorConfigRegisterDB) ListRegisterByOrgId(orgId string) ([]SpMonitorConfigRegister, error) {
var res = make([]SpMonitorConfigRegister, 0)
if err := m.Model(&SpMonitorConfigRegister{}).Where("scope = 'org' and scope_id = ?", orgId).Find(&res).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return make([]SpMonitorConfigRegister, 0), nil
}
return nil, err

Check warning on line 37 in internal/tools/monitor/common/db/monitor_config_register.go

View check run for this annotation

Codecov / codecov/patch

internal/tools/monitor/common/db/monitor_config_register.go#L31-L37

Added lines #L31 - L37 were not covered by tests
}
return res, nil

Check warning on line 39 in internal/tools/monitor/common/db/monitor_config_register.go

View check run for this annotation

Codecov / codecov/patch

internal/tools/monitor/common/db/monitor_config_register.go#L39

Added line #L39 was not covered by tests
}

func (m *MonitorConfigRegisterDB) ListRegisterByType(tpy string) ([]SpMonitorConfigRegister, error) {
var res = make([]SpMonitorConfigRegister, 0)
if err := m.Model(&SpMonitorConfigRegister{}).Where("type = ?", tpy).Find(&res).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return make([]SpMonitorConfigRegister, 0), nil
}
return nil, err

Check warning on line 48 in internal/tools/monitor/common/db/monitor_config_register.go

View check run for this annotation

Codecov / codecov/patch

internal/tools/monitor/common/db/monitor_config_register.go#L42-L48

Added lines #L42 - L48 were not covered by tests
}
return res, nil

Check warning on line 50 in internal/tools/monitor/common/db/monitor_config_register.go

View check run for this annotation

Codecov / codecov/patch

internal/tools/monitor/common/db/monitor_config_register.go#L50

Added line #L50 was not covered by tests
}
23 changes: 21 additions & 2 deletions internal/tools/monitor/common/db/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import "time"

// table name
const (
InstanceTenantTable = "tb_tmc_instance_tenant"
MonitorTable = "sp_monitor"
InstanceTenantTable = "tb_tmc_instance_tenant"
MonitorTable = "sp_monitor"
SpMonitorConfigRegisterTable = `sp_monitor_config_register`
)

type (
Expand Down Expand Up @@ -49,6 +50,20 @@ type (
Created time.Time `gorm:"column:created;default:CURRENT_TIMESTAMP"`
Updated time.Time `gorm:"column:updated;default:CURRENT_TIMESTAMP"`
}

SpMonitorConfigRegister struct {
ID int `gorm:"column:id;primaryKey;autoIncrement" json:"id"`
Scope string `gorm:"column:scope;not null" json:"scope"` // org
ScopeID string `gorm:"column:scope_id;not null;default:''" json:"scope_id"` // org_id
Namespace string `gorm:"column:namespace;not null" json:"namespace"` // dev,test,staging,prod,other
Type string `gorm:"column:type;not null" json:"type"` // metric、log
Names string `gorm:"column:names;not null" json:"names"`
Filters string `gorm:"column:filters;not null;default:''" json:"filters"`
Enable bool `gorm:"column:enable;not null;default:1" json:"enable"`
UpdateTime time.Time `gorm:"column:update_time;not null" json:"update_time"`
Desc string `gorm:"column:desc;not null;default:''" json:"desc"`
Hash string `gorm:"column:hash;not null;unique" json:"hash"`
}
)

func (InstanceTenant) TableName() string {
Expand All @@ -58,3 +73,7 @@ func (InstanceTenant) TableName() string {
func (Monitor) TableName() string {
return MonitorTable
}

func (*SpMonitorConfigRegister) TableName() string {
return SpMonitorConfigRegisterTable

Check warning on line 78 in internal/tools/monitor/common/db/table.go

View check run for this annotation

Codecov / codecov/patch

internal/tools/monitor/common/db/table.go#L77-L78

Added lines #L77 - L78 were not covered by tests
}
4 changes: 4 additions & 0 deletions internal/tools/monitor/core/settings/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ type monitorConfigRegister struct {
Hash string `json:"hash" gorm:"column:hash"`
}

func (s *settingsService) generateKey(orgID, ns string) string {
return md5x.SumString(orgID + "/" + ns).String16()

Check warning on line 189 in internal/tools/monitor/core/settings/monitor.go

View check run for this annotation

Codecov / codecov/patch

internal/tools/monitor/core/settings/monitor.go#L188-L189

Added lines #L188 - L189 were not covered by tests
}

func (s *settingsService) updateMonitorConfig(tx *gorm.DB, orgid int64, orgName, ns, group string, keys map[string]interface{}) error {
if ns == "general" {
ns = ""
Expand Down
39 changes: 39 additions & 0 deletions internal/tools/monitor/core/settings/settings.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"strings"

"github.com/jinzhu/gorm"
"github.com/recallsong/go-utils/conv"
"google.golang.org/protobuf/types/known/structpb"

"github.com/erda-project/erda-infra/providers/i18n"
Expand Down Expand Up @@ -63,6 +64,44 @@ type settingsService struct {
t i18n.Translator
}

func (s *settingsService) PutSettingsWithType(ctx context.Context, req *pb.PutSettingsWithTypeRequest) (*pb.PutSettingsWithTypeResponse, error) {
orgName, err := s.getOrgName(req.OrgID)
if err != nil {
return nil, err
}
tx := s.db.Begin()
ttl := ttl{}
for _, item := range req.Data.Items {
if item.Key == LogsTTLKey || item.Key == MetricsTTLKey {
ttl.TTL = conv.ToInt64(item.Value.AsInterface(), 1)
}
if item.Key == LogsHotTTLKey || item.Key == MetricsHotTTLKey {
ttl.HotTTL = conv.ToInt64(item.Value.AsInterface(), 1)
}

Check warning on line 80 in internal/tools/monitor/core/settings/settings.service.go

View check run for this annotation

Codecov / codecov/patch

internal/tools/monitor/core/settings/settings.service.go#L67-L80

Added lines #L67 - L80 were not covered by tests

val := item.Value.AsInterface()
byts, _ := json.Marshal(val)

err := tx.Exec(globalSettingInsertUpdate, req.OrgID, orgName, req.Namespace, req.Data.Key, item.Key, item.Type, string(byts), item.Unit).Error
if err != nil {
tx.Rollback()
return nil, errors.NewDatabaseError(err)
}

Check warning on line 89 in internal/tools/monitor/core/settings/settings.service.go

View check run for this annotation

Codecov / codecov/patch

internal/tools/monitor/core/settings/settings.service.go#L82-L89

Added lines #L82 - L89 were not covered by tests

}
orgID := strconv.FormatInt(req.OrgID, 10)
key := s.generateKey(orgID, req.Namespace)

if err = s.updateMonitor(req.MonitorType, ttl, tx, req.OrgID, orgID, orgName, req.Namespace, key); err != nil {
tx.Rollback()
return nil, err
}
if err := tx.Commit().Error; err != nil {
return nil, errors.NewDatabaseError(err)
}
return &pb.PutSettingsWithTypeResponse{Data: "success"}, nil

Check warning on line 102 in internal/tools/monitor/core/settings/settings.service.go

View check run for this annotation

Codecov / codecov/patch

internal/tools/monitor/core/settings/settings.service.go#L92-L102

Added lines #L92 - L102 were not covered by tests
}

func (s *settingsService) GetSettings(ctx context.Context, req *pb.GetSettingsRequest) (*pb.GetSettingsResponse, error) {
identityInfo := apis.GetIdentityInfo(ctx)
if err := s.checkOrgPermission(identityInfo, uint64(req.OrgID), apistructs.GetAction); err != nil {
Expand Down
Loading

0 comments on commit f7b32d6

Please sign in to comment.