diff --git a/CHANGELOG.md b/CHANGELOG.md index 9485dab..f30e835 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,18 @@ +# Release 1.1.0 +## Added +- Perform a database backup before starting a new learning cycle +- Provide a 'stats' command to display various statistics in an info log + +## Changed +- + +## Fixed +- + +## Known Issues +- There seems to be an issue with quotedprintable not properly reading in + malformed mails. Currently, such is likely to pass the filter. + # Release 1.0.0 ## Added - diff --git a/database.go b/database.go index 179083f..cf30997 100644 --- a/database.go +++ b/database.go @@ -72,6 +72,21 @@ func LoadDatabases(d []Maildir) (databases map[Maildir]*bolt.DB, err error) { return databases, nil } +// LoadBackupDatabases loads all backup databases from a given slice of Maildirs +func LoadBackupDatabases(d []Maildir) (databases map[Maildir]*bolt.DB, err error) { + databases = make(map[Maildir]*bolt.DB) + for _, val := range d { + databases[val], err = bolt.Open(string(val)+"/sisyphus.db.backup", 0600, nil) + if err != nil { + return databases, err + } + } + + log.Info("All databases loaded") + + return databases, nil +} + // CloseDatabases closes all databases from a given slice of Maildirs func CloseDatabases(databases map[Maildir]*bolt.DB) { for key, val := range databases { @@ -82,7 +97,7 @@ func CloseDatabases(databases map[Maildir]*bolt.DB) { }).Error("Unable to close database") } log.WithFields(log.Fields{ - "db": string(key) + "/sisyphus.db", + "maildir": string(key), }).Info("Database closed") } } diff --git a/sisyphus/sisyphus.go b/sisyphus/sisyphus.go index 8e6876c..5d032cd 100644 --- a/sisyphus/sisyphus.go +++ b/sisyphus/sisyphus.go @@ -218,9 +218,34 @@ COPYRIGHT: Name: "stats", Aliases: []string{"i"}, Usage: "show statistics", - Action: func(c *cli.Context) error { - log.Info("here, we should get statistics from the db, TBD...") - return nil + Action: func(c *cli.Context) { + + // Create missing Maildirs + err := sisyphus.LoadMaildirs(maildirs) + if err != nil { + log.WithFields(log.Fields{ + "err": err, + }).Fatal("Cannot load maildirs") + } + + // Open all backup databases + dbs, err := sisyphus.LoadBackupDatabases(maildirs) + if err != nil { + log.WithFields(log.Fields{ + "err": err, + }).Fatal("Cannot load backup databases") + } + defer sisyphus.CloseDatabases(dbs) + + for _, db := range dbs { + gTotal, jTotal, gWords, jWords := info(db) + log.WithFields(log.Fields{ + "good mails learned": gTotal, + "junk mails learned": jTotal, + "number of good words": gWords, + "number of junk words": jWords, + }).Info("Statistics") + } }, }, }