| 221 | } |
| 222 | |
| 223 | func GetAllLogs(logType int, startTimestamp int64, endTimestamp int64, modelName string, username string, tokenName string, startIdx int, num int, channel int, group string) (logs []*Log, total int64, err error) { |
| 224 | var tx *gorm.DB |
| 225 | if logType == LogTypeUnknown { |
| 226 | tx = LOG_DB |
| 227 | } else { |
| 228 | tx = LOG_DB.Where("logs.type = ?", logType) |
| 229 | } |
| 230 | |
| 231 | if modelName != "" { |
| 232 | tx = tx.Where("logs.model_name like ?", modelName) |
| 233 | } |
| 234 | if username != "" { |
| 235 | tx = tx.Where("logs.username = ?", username) |
| 236 | } |
| 237 | if tokenName != "" { |
| 238 | tx = tx.Where("logs.token_name = ?", tokenName) |
| 239 | } |
| 240 | if startTimestamp != 0 { |
| 241 | tx = tx.Where("logs.created_at >= ?", startTimestamp) |
| 242 | } |
| 243 | if endTimestamp != 0 { |
| 244 | tx = tx.Where("logs.created_at <= ?", endTimestamp) |
| 245 | } |
| 246 | if channel != 0 { |
| 247 | tx = tx.Where("logs.channel_id = ?", channel) |
| 248 | } |
| 249 | if group != "" { |
| 250 | tx = tx.Where("logs."+logGroupCol+" = ?", group) |
| 251 | } |
| 252 | err = tx.Model(&Log{}).Count(&total).Error |
| 253 | if err != nil { |
| 254 | return nil, 0, err |
| 255 | } |
| 256 | err = tx.Order("logs.id desc").Limit(num).Offset(startIdx).Find(&logs).Error |
| 257 | if err != nil { |
| 258 | return nil, 0, err |
| 259 | } |
| 260 | |
| 261 | channelIdsMap := make(map[int]struct{}) |
| 262 | channelMap := make(map[int]string) |
| 263 | for _, log := range logs { |
| 264 | if log.ChannelId != 0 { |
| 265 | channelIdsMap[log.ChannelId] = struct{}{} |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | channelIds := make([]int, 0, len(channelIdsMap)) |
| 270 | for channelId := range channelIdsMap { |
| 271 | channelIds = append(channelIds, channelId) |
| 272 | } |
| 273 | if len(channelIds) > 0 { |
| 274 | var channels []struct { |
| 275 | Id int `gorm:"column:id"` |
| 276 | Name string `gorm:"column:name"` |
| 277 | } |
| 278 | if err = DB.Table("channels").Select("id, name").Where("id IN ?", channelIds).Find(&channels).Error; err != nil { |
| 279 | return logs, total, err |
| 280 | } |