Merge pull request #995 from GLYASAI/sort-events

Bug Fix: sort by ID desc
This commit is contained in:
barnettZQG 2021-05-24 19:16:19 +08:00 committed by GitHub
commit 741263b958
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -131,7 +131,7 @@ func (c *EventDaoImpl) GetEventsByTarget(target, targetID string, offset, limit
if err := db.Model(&model.ServiceEvent{}).Count(&total).Error; err != nil { if err := db.Model(&model.ServiceEvent{}).Count(&total).Error; err != nil {
return nil, 0, err return nil, 0, err
} }
if err := db.Offset(offset).Limit(limit).Order("create_time DESC").Find(&result).Error; err != nil { if err := db.Offset(offset).Limit(limit).Order("create_time DESC, ID DESC").Find(&result).Error; err != nil {
if err == gorm.ErrRecordNotFound { if err == gorm.ErrRecordNotFound {
return result, 0, nil return result, 0, nil
} }
@ -143,20 +143,17 @@ func (c *EventDaoImpl) GetEventsByTarget(target, targetID string, offset, limit
// GetEventsByTenantID get event by tenantID // GetEventsByTenantID get event by tenantID
func (c *EventDaoImpl) GetEventsByTenantID(tenantID string, offset, limit int) ([]*model.ServiceEvent, int, error) { func (c *EventDaoImpl) GetEventsByTenantID(tenantID string, offset, limit int) ([]*model.ServiceEvent, int, error) {
var result []*model.ServiceEvent
var total int var total int
start := time.Now()
if err := c.DB.Model(&model.ServiceEvent{}).Where("tenant_id=?", tenantID).Count(&total).Error; err != nil { if err := c.DB.Model(&model.ServiceEvent{}).Where("tenant_id=?", tenantID).Count(&total).Error; err != nil {
return nil, 0, err return nil, 0, err
} }
logrus.Debugf("query event count %d take time %s", total, time.Now().Sub(start)) var result []*model.ServiceEvent
if err := c.DB.Where("tenant_id=?", tenantID).Offset(offset).Limit(limit).Order("start_time DESC").Find(&result).Error; err != nil { if err := c.DB.Where("tenant_id=?", tenantID).Offset(offset).Limit(limit).Order("start_time DESC, ID DESC").Find(&result).Error; err != nil {
if err == gorm.ErrRecordNotFound { if err == gorm.ErrRecordNotFound {
return result, 0, nil return result, 0, nil
} }
return nil, 0, err return nil, 0, err
} }
logrus.Debugf("query event list take time %s", time.Now().Sub(start))
return result, total, nil return result, total, nil
} }