From d1b6279996947513e31f80a54d3fed072f6a9c98 Mon Sep 17 00:00:00 2001 From: Yan Qing Date: Fri, 5 Jun 2020 13:16:21 +0800 Subject: [PATCH] fixed refreshSettingStatus and refreshLabelStatus function for empty data. --- src/model/common.go | 32 ++++++++++++++++---------------- src/model/user.go | 9 ++++++++- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/model/common.go b/src/model/common.go index 63947ad..e4b471f 100644 --- a/src/model/common.go +++ b/src/model/common.go @@ -318,7 +318,7 @@ func (m *Model) unlock(ctx context.Context, key string) { // 比如用户因为属于 n 个群组而被重复设置环境标签 func (m *Model) tryRefreshLabelStatus(ctx context.Context, labelID int64) { if err := m.refreshLabelStatus(ctx, labelID); err != nil { - logging.Infof("tryRefreshLabelStatus: labelID %d, error %v", labelID, err) + logging.Debugf("tryRefreshLabelStatus: labelID %d, error %v", labelID, err) } } func (m *Model) refreshLabelStatus(ctx context.Context, labelID int64) error { @@ -335,7 +335,7 @@ func (m *Model) refreshLabelStatus(ctx context.Context, labelID int64) error { } sd = m.DB.Select( - goqu.SUM(goqu.I("t2.status")).As("status")). + goqu.L("IFNULL(SUM(`t2`.`status`), 0)").As("status")). From( goqu.T(schema.TableGroupLabel).As("t1"), goqu.T(schema.TableGroup).As("t2")). @@ -355,7 +355,7 @@ func (m *Model) refreshLabelStatus(ctx context.Context, labelID int64) error { // 比如用户因为属于 n 个群组而被重复设置配置项 func (m *Model) tryRefreshSettingStatus(ctx context.Context, settingID int64) { if err := m.refreshSettingStatus(ctx, settingID); err != nil { - logging.Infof("tryRefreshSettingStatus: settingID %d, error %v", settingID, err) + logging.Debugf("tryRefreshSettingStatus: settingID %d, error %v", settingID, err) } } func (m *Model) refreshSettingStatus(ctx context.Context, settingID int64) error { @@ -372,7 +372,7 @@ func (m *Model) refreshSettingStatus(ctx context.Context, settingID int64) error } sd = m.DB.Select( - goqu.SUM(goqu.I("t2.status")).As("status")). + goqu.L("IFNULL(SUM(`t2`.`status`), 0)").As("status")). From( goqu.T(schema.TableGroupSetting).As("t1"), goqu.T(schema.TableGroup).As("t2")). @@ -391,7 +391,7 @@ func (m *Model) refreshSettingStatus(ctx context.Context, settingID int64) error // tryRefreshGroupStatus 更新指定 group 的 Status(成员数量统计)值 func (m *Model) tryRefreshGroupStatus(ctx context.Context, groupID int64) { if err := m.refreshGroupStatus(ctx, groupID); err != nil { - logging.Infof("tryRefreshGroupStatus: groupID %d, error %v", groupID, err) + logging.Debugf("tryRefreshGroupStatus: groupID %d, error %v", groupID, err) } } func (m *Model) refreshGroupStatus(ctx context.Context, groupID int64) error { @@ -414,7 +414,7 @@ func (m *Model) refreshGroupStatus(ctx context.Context, groupID int64) error { // tryRefreshModuleStatus 更新指定 module 的 Status(功能模块的配置项统计)值 func (m *Model) tryRefreshModuleStatus(ctx context.Context, moduleID int64) { if err := m.refreshModuleStatus(ctx, moduleID); err != nil { - logging.Infof("tryRefreshModuleStatus: moduleID %d, error %v", moduleID, err) + logging.Debugf("tryRefreshModuleStatus: moduleID %d, error %v", moduleID, err) } } func (m *Model) refreshModuleStatus(ctx context.Context, moduleID int64) error { @@ -437,7 +437,7 @@ func (m *Model) refreshModuleStatus(ctx context.Context, moduleID int64) error { // tryIncreaseStatisticStatus 加减指定 key 的统计值 func (m *Model) tryIncreaseStatisticStatus(ctx context.Context, key schema.StatisticKey, delta int) { if err := m.increaseStatisticStatus(ctx, key, delta); err != nil { - logging.Infof("tryIncreaseStatisticStatus: key %s, delta: %d, error %v", key, delta, err) + logging.Debugf("tryIncreaseStatisticStatus: key %s, delta: %d, error %v", key, delta, err) } } func (m *Model) increaseStatisticStatus(ctx context.Context, key schema.StatisticKey, delta int) error { @@ -478,7 +478,7 @@ func (m *Model) updateStatisticValue(ctx context.Context, key schema.StatisticKe // tryRefreshUsersTotalSize 更新用户总数 func (m *Model) tryRefreshUsersTotalSize(ctx context.Context) { if err := m.refreshUsersTotalSize(ctx); err != nil { - logging.Infof("refreshUsersTotalSize: error %v", err) + logging.Debugf("refreshUsersTotalSize: error %v", err) } } func (m *Model) refreshUsersTotalSize(ctx context.Context) error { @@ -499,7 +499,7 @@ func (m *Model) refreshUsersTotalSize(ctx context.Context) error { // tryRefreshGroupsTotalSize 更新群组总数 func (m *Model) tryRefreshGroupsTotalSize(ctx context.Context) { if err := m.refreshGroupsTotalSize(ctx); err != nil { - logging.Infof("refreshGroupsTotalSize: error %v", err) + logging.Debugf("refreshGroupsTotalSize: error %v", err) } } func (m *Model) refreshGroupsTotalSize(ctx context.Context) error { @@ -520,7 +520,7 @@ func (m *Model) refreshGroupsTotalSize(ctx context.Context) error { // tryRefreshProductsTotalSize 更新产品总数 func (m *Model) tryRefreshProductsTotalSize(ctx context.Context) { if err := m.refreshProductsTotalSize(ctx); err != nil { - logging.Infof("refreshProductsTotalSize: error %v", err) + logging.Debugf("refreshProductsTotalSize: error %v", err) } } func (m *Model) refreshProductsTotalSize(ctx context.Context) error { @@ -541,7 +541,7 @@ func (m *Model) refreshProductsTotalSize(ctx context.Context) error { // tryRefreshLabelsTotalSize 更新标签总数 func (m *Model) tryRefreshLabelsTotalSize(ctx context.Context) { if err := m.refreshLabelsTotalSize(ctx); err != nil { - logging.Infof("refreshLabelsTotalSize: error %v", err) + logging.Debugf("refreshLabelsTotalSize: error %v", err) } } func (m *Model) refreshLabelsTotalSize(ctx context.Context) error { @@ -562,7 +562,7 @@ func (m *Model) refreshLabelsTotalSize(ctx context.Context) error { // tryRefreshModulesTotalSize 更新模块总数 func (m *Model) tryRefreshModulesTotalSize(ctx context.Context) { if err := m.refreshModulesTotalSize(ctx); err != nil { - logging.Infof("refreshModulesTotalSize: error %v", err) + logging.Debugf("refreshModulesTotalSize: error %v", err) } } func (m *Model) refreshModulesTotalSize(ctx context.Context) error { @@ -583,7 +583,7 @@ func (m *Model) refreshModulesTotalSize(ctx context.Context) error { // tryRefreshSettingsTotalSize 更新配置项总数 func (m *Model) tryRefreshSettingsTotalSize(ctx context.Context) { if err := m.refreshSettingsTotalSize(ctx); err != nil { - logging.Infof("refreshSettingsTotalSize: error %v", err) + logging.Debugf("refreshSettingsTotalSize: error %v", err) } } func (m *Model) refreshSettingsTotalSize(ctx context.Context) error { @@ -603,7 +603,7 @@ func (m *Model) refreshSettingsTotalSize(ctx context.Context) error { func (m *Model) tryIncreaseLabelsStatus(ctx context.Context, labelIDs []int64, delta int) { if err := m.increaseLabelsStatus(ctx, labelIDs, delta); err != nil { - logging.Infof("increaseLabelsStatus: labelIDs [%v], delta: %d, error %v", labelIDs, delta, err) + logging.Debugf("increaseLabelsStatus: labelIDs [%v], delta: %d, error %v", labelIDs, delta, err) } } func (m *Model) increaseLabelsStatus(ctx context.Context, labelIDs []int64, delta int) error { @@ -623,7 +623,7 @@ func (m *Model) increaseLabelsStatus(ctx context.Context, labelIDs []int64, delt func (m *Model) tryIncreaseSettingsStatus(ctx context.Context, settingIDs []int64, delta int) { if err := m.increaseSettingsStatus(ctx, settingIDs, delta); err != nil { - logging.Infof("increaseSettingsStatus: settingIDs [%v], delta: %d, error %v", settingIDs, delta, err) + logging.Debugf("increaseSettingsStatus: settingIDs [%v], delta: %d, error %v", settingIDs, delta, err) } } func (m *Model) increaseSettingsStatus(ctx context.Context, settingIDs []int64, delta int) error { @@ -643,7 +643,7 @@ func (m *Model) increaseSettingsStatus(ctx context.Context, settingIDs []int64, func (m *Model) tryIncreaseModulesStatus(ctx context.Context, moduleIDs []int64, delta int) { if err := m.increaseModulesStatus(ctx, moduleIDs, delta); err != nil { - logging.Infof("increaseModulesStatus: moduleIDs [%v], delta: %d, error %v", moduleIDs, delta, err) + logging.Debugf("increaseModulesStatus: moduleIDs [%v], delta: %d, error %v", moduleIDs, delta, err) } } func (m *Model) increaseModulesStatus(ctx context.Context, moduleIDs []int64, delta int) error { diff --git a/src/model/user.go b/src/model/user.go index 9fb1a90..a2ca3c3 100644 --- a/src/model/user.go +++ b/src/model/user.go @@ -155,12 +155,12 @@ func (m *User) RefreshLabels(ctx context.Context, id int64, now int64, force boo if err != nil { return err } - defer scanner.Close() set := make(map[int64]struct{}) for scanner.Next() { myLabelInfo := schema.MyLabelInfo{} if err := scanner.ScanStruct(&myLabelInfo); err != nil { + scanner.Close() return err } if _, ok := set[myLabelInfo.ID]; ok { @@ -180,6 +180,13 @@ func (m *User) RefreshLabels(ctx context.Context, id int64, now int64, force boo }) } + if err := scanner.Close(); err != nil { + return err + } + if err := scanner.Err(); err != nil { + return err + } + refreshed = true user.ActiveAt = time.Now().UTC().Unix() _ = user.PutLabels(data)