github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/db/run_history.go (about) 1 // This file is part of the Smart Home 2 // Program complex distribution https://github.com/e154/smart-home 3 // Copyright (C) 2016-2023, Filippov Alex 4 // 5 // This library is free software: you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 3 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Library General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library. If not, see 17 // <https://www.gnu.org/licenses/>. 18 19 package db 20 21 import ( 22 "context" 23 "fmt" 24 "time" 25 26 "github.com/e154/smart-home/common/apperr" 27 28 "github.com/pkg/errors" 29 "gorm.io/gorm" 30 ) 31 32 // RunHistory ... 33 type RunHistory struct { 34 Db *gorm.DB 35 } 36 37 // RunStory ... 38 type RunStory struct { 39 Id int64 `gorm:"primary_key"` 40 Start time.Time 41 End *time.Time 42 } 43 44 // TableName ... 45 func (d *RunStory) TableName() string { 46 return "run_history" 47 } 48 49 // Add ... 50 func (n RunHistory) Add(ctx context.Context, story *RunStory) (id int64, err error) { 51 if err = n.Db.WithContext(ctx).Create(&story).Error; err != nil { 52 err = errors.Wrap(apperr.ErrRunStoryAdd, err.Error()) 53 return 54 } 55 id = story.Id 56 return 57 } 58 59 // Update ... 60 func (n RunHistory) Update(ctx context.Context, m *RunStory) (err error) { 61 q := map[string]interface{}{ 62 "end": m.End, 63 } 64 if err = n.Db.WithContext(ctx).Model(&RunStory{Id: m.Id}).Updates(q).Error; err != nil { 65 err = errors.Wrap(apperr.ErrRunStoryUpdate, err.Error()) 66 } 67 return 68 } 69 70 // List ... 71 func (n *RunHistory) List(ctx context.Context, limit, offset int, orderBy, sort string, from *time.Time) (list []*RunStory, total int64, err error) { 72 73 list = make([]*RunStory, 0) 74 q := n.Db.WithContext(ctx).Model(&RunStory{}) 75 76 if sort != "" && orderBy != "" { 77 q = q. 78 Order(fmt.Sprintf("%s %s", sort, orderBy)) 79 } 80 81 if from != nil { 82 q = q.Where("start > ?", from.UTC().Format(time.RFC3339)) 83 } 84 85 if err = q.Count(&total).Error; err != nil { 86 err = errors.Wrap(apperr.ErrRunStoryList, err.Error()) 87 return 88 } 89 90 q = q. 91 Limit(limit). 92 Offset(offset) 93 94 if err = q.Find(&list).Error; err != nil { 95 err = errors.Wrap(apperr.ErrRunStoryList, err.Error()) 96 } 97 98 return 99 } 100 101 // DeleteOldest ... 102 func (n *RunHistory) DeleteOldest(ctx context.Context, days int) (err error) { 103 story := &RunStory{} 104 if err = n.Db.WithContext(ctx).Last(&story).Error; err != nil { 105 err = errors.Wrap(apperr.ErrLogDelete, err.Error()) 106 return 107 } 108 err = n.Db.WithContext(ctx).Delete(&RunStory{}, 109 fmt.Sprintf(`start < CAST('%s' AS DATE) - interval '%d days'`, 110 story.Start.UTC().Format("2006-01-02 15:04:05"), days)).Error 111 if err != nil { 112 err = errors.Wrap(apperr.ErrEntityStorageDelete, err.Error()) 113 } 114 return 115 }