feat(delivery): refine base service integrations
This commit is contained in:
@@ -37,17 +37,17 @@ type MachineService struct {
|
||||
}
|
||||
|
||||
type MachineListQuery struct {
|
||||
Page int
|
||||
Size int
|
||||
Hostname string
|
||||
AssetNumber string
|
||||
Type string
|
||||
Location string
|
||||
IP string
|
||||
Spec string
|
||||
BusinessLine string
|
||||
Source string
|
||||
Status string
|
||||
BusinessLineID uint64
|
||||
Page int
|
||||
Size int
|
||||
Hostname string
|
||||
AssetNumber string
|
||||
Type string
|
||||
Location string
|
||||
IP string
|
||||
Spec string
|
||||
Source string
|
||||
Status string
|
||||
}
|
||||
|
||||
type MachineResourceItem struct {
|
||||
@@ -189,25 +189,29 @@ func (s *MachineService) SyncNow(ctx context.Context) (*model.MachineSyncState,
|
||||
return s.syncState(ctx)
|
||||
}
|
||||
|
||||
func (s *MachineService) Overview(ctx context.Context) (*MachineOverview, error) {
|
||||
var total, physical int64
|
||||
if err := s.db.WithContext(ctx).Model(&model.MachineResource{}).Count(&total).Error; err != nil {
|
||||
func (s *MachineService) Overview(ctx context.Context, userID uint64, isAdmin bool, businessLineID uint64) (*MachineOverview, error) {
|
||||
baseDB, err := s.scopedMachineDB(ctx, userID, isAdmin, businessLineID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.db.WithContext(ctx).Model(&model.MachineResource{}).Where("resource_type = ?", "physical").Count(&physical).Error; err != nil {
|
||||
var total, physical int64
|
||||
if err := baseDB.Count(&total).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := baseDB.Session(&gorm.Session{}).Where("resource_type = ?", "physical").Count(&physical).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sourceCounts, err := s.sourceCounts(ctx)
|
||||
sourceCounts, err := s.sourceCounts(baseDB.Session(&gorm.Session{}))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cloudSources := []string{"aliyun", "ali", "alicloud", "aws", "qiniu"}
|
||||
cmdbTotal, err := s.countExcludingSources(ctx, cloudSources)
|
||||
cmdbTotal, err := s.countExcludingSources(baseDB.Session(&gorm.Session{}), cloudSources)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cmdbPhysical, err := s.countExcludingSourcesAndType(ctx, cloudSources, "physical")
|
||||
cmdbPhysical, err := s.countExcludingSourcesAndType(baseDB.Session(&gorm.Session{}), cloudSources, "physical")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -239,7 +243,7 @@ func (s *MachineService) Overview(ctx context.Context) (*MachineOverview, error)
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *MachineService) List(ctx context.Context, query MachineListQuery) (*MachineResourceList, error) {
|
||||
func (s *MachineService) List(ctx context.Context, userID uint64, isAdmin bool, query MachineListQuery) (*MachineResourceList, error) {
|
||||
if query.Page <= 0 {
|
||||
query.Page = 1
|
||||
}
|
||||
@@ -250,7 +254,10 @@ func (s *MachineService) List(ctx context.Context, query MachineListQuery) (*Mac
|
||||
query.Size = 200
|
||||
}
|
||||
|
||||
db := s.db.WithContext(ctx).Model(&model.MachineResource{})
|
||||
db, err := s.scopedMachineDB(ctx, userID, isAdmin, query.BusinessLineID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
db = applyMachineFilters(db, query)
|
||||
|
||||
var total int64
|
||||
@@ -270,6 +277,37 @@ func (s *MachineService) List(ctx context.Context, query MachineListQuery) (*Mac
|
||||
return &MachineResourceList{Total: total, Items: items}, nil
|
||||
}
|
||||
|
||||
func (s *MachineService) scopedMachineDB(ctx context.Context, userID uint64, isAdmin bool, businessLineID uint64) (*gorm.DB, error) {
|
||||
if businessLineID == 0 {
|
||||
return nil, errors.New("business_line_id is required")
|
||||
}
|
||||
if !isAdmin {
|
||||
var binding model.BusinessLineUser
|
||||
err := s.db.WithContext(ctx).Where("business_line_id = ? AND user_id = ?", businessLineID, userID).First(&binding).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errors.New("current user does not have business line permission")
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
var mappings []model.BusinessLineSinaOrganization
|
||||
if err := s.db.WithContext(ctx).Where("business_line_id = ?", businessLineID).Order("sina_organization_name ASC").Find(&mappings).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
names := make([]string, 0, len(mappings))
|
||||
for _, mapping := range mappings {
|
||||
if name := strings.TrimSpace(mapping.SinaOrganizationName); name != "" {
|
||||
names = append(names, name)
|
||||
}
|
||||
}
|
||||
db := s.db.WithContext(ctx).Model(&model.MachineResource{})
|
||||
if len(names) == 0 {
|
||||
return db.Where("1 = 0"), nil
|
||||
}
|
||||
return db.Where("business_line IN ?", names), nil
|
||||
}
|
||||
|
||||
func (s *MachineService) acquireSync() error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
@@ -527,18 +565,15 @@ func applyMachineFilters(db *gorm.DB, query MachineListQuery) *gorm.DB {
|
||||
if query.Spec != "" {
|
||||
db = db.Where("spec LIKE ?", "%"+query.Spec+"%")
|
||||
}
|
||||
if query.BusinessLine != "" {
|
||||
db = db.Where("business_line LIKE ?", "%"+query.BusinessLine+"%")
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
||||
func (s *MachineService) sourceCounts(ctx context.Context) (map[string]int64, error) {
|
||||
func (s *MachineService) sourceCounts(db *gorm.DB) (map[string]int64, error) {
|
||||
var rows []struct {
|
||||
Source string
|
||||
Count int64
|
||||
}
|
||||
if err := s.db.WithContext(ctx).Model(&model.MachineResource{}).Select("source, count(*) as count").Group("source").Scan(&rows).Error; err != nil {
|
||||
if err := db.Select("source, count(*) as count").Group("source").Scan(&rows).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := map[string]int64{}
|
||||
@@ -549,15 +584,15 @@ func (s *MachineService) sourceCounts(ctx context.Context) (map[string]int64, er
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *MachineService) countExcludingSources(ctx context.Context, sources []string) (int64, error) {
|
||||
func (s *MachineService) countExcludingSources(db *gorm.DB, sources []string) (int64, error) {
|
||||
var count int64
|
||||
err := s.db.WithContext(ctx).Model(&model.MachineResource{}).Where("source NOT IN ?", sources).Count(&count).Error
|
||||
err := db.Where("source NOT IN ?", sources).Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
func (s *MachineService) countExcludingSourcesAndType(ctx context.Context, sources []string, resourceType string) (int64, error) {
|
||||
func (s *MachineService) countExcludingSourcesAndType(db *gorm.DB, sources []string, resourceType string) (int64, error) {
|
||||
var count int64
|
||||
err := s.db.WithContext(ctx).Model(&model.MachineResource{}).Where("source NOT IN ? AND resource_type = ?", sources, resourceType).Count(&count).Error
|
||||
err := db.Where("source NOT IN ? AND resource_type = ?", sources, resourceType).Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user