-
Notifications
You must be signed in to change notification settings - Fork 3
/
retention.go
46 lines (36 loc) · 1.19 KB
/
retention.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package postgresql
import (
"time"
"github.com/colonyos/colonies/pkg/core"
)
func (db *PQDatabase) calcTimestamp(retentionPeriod int64) (time.Time, time.Time) {
now := time.Now()
period := time.Duration(retentionPeriod) * time.Second
timestamp := now.Add(-period)
return now, timestamp
}
// retentionPeriod in seconds
func (db *PQDatabase) ApplyRetentionPolicy(retentionPeriod int64) error {
_, timestamp := db.calcTimestamp(retentionPeriod)
sqlStatement := `DELETE FROM ` + db.dbPrefix + `ATTRIBUTES WHERE ADDED<$1 AND STATE=$2`
_, err := db.postgresql.Exec(sqlStatement, timestamp, core.SUCCESS)
if err != nil {
return err
}
sqlStatement = `DELETE FROM ` + db.dbPrefix + `LOGS WHERE ADDED<$1`
_, err = db.postgresql.Exec(sqlStatement, timestamp)
if err != nil {
return err
}
sqlStatement = `DELETE FROM ` + db.dbPrefix + `PROCESSES WHERE SUBMISSION_TIME<$1 AND STATE=$2`
_, err = db.postgresql.Exec(sqlStatement, timestamp, core.SUCCESS)
if err != nil {
return err
}
sqlStatement = `DELETE FROM ` + db.dbPrefix + `PROCESSGRAPHS WHERE SUBMISSION_TIME<$1 AND STATE=$2`
_, err = db.postgresql.Exec(sqlStatement, timestamp, core.SUCCESS)
if err != nil {
return err
}
return nil
}