Don't use UUID to find entries unless requested

This commit is contained in:
Brian Buller 2018-04-12 14:30:52 -05:00
parent c0a62ac26e
commit 2a2aea8641
2 changed files with 65 additions and 17 deletions

View File

@ -25,6 +25,7 @@ const (
TypeNoArchive = 3 // 011 TypeNoArchive = 3 // 011
TypeArchive = 4 // 100 TypeArchive = 4 // 100
TypeAll = 7 // 111 TypeAll = 7 // 111
TypeError = 8 //1000
ArchiveDays = time.Hour * 24 * 90 // Archive anything older than 90 days ArchiveDays = time.Hour * 24 * 90 // Archive anything older than 90 days
) )

View File

@ -13,21 +13,25 @@ import (
func (gdb *GimeDB) GetLatestTimeEntry() (*TimeEntry, error) { func (gdb *GimeDB) GetLatestTimeEntry() (*TimeEntry, error) {
var ret *TimeEntry var ret *TimeEntry
var err error var err error
tc := gdb.LoadTimeEntryCollection(TypeRecent)
for i := 0; i < tc.Length(); i++ { var useDb *boltease.DB
te := tc.Get(i) if useDb, err = gdb.openDBType(TypeRecent); err != nil {
if ret == nil {
ret = te
} else {
if te.GetEnd().After(ret.GetEnd()) {
ret = te
}
}
}
if ret == nil {
err = errors.New("Could not find latest time entry")
}
return ret, err return ret, err
}
defer gdb.closeDBType(TypeRecent)
var sttimes []string
if sttimes, err = useDb.GetBucketList([]string{TypeToString(TypeRecent)}); err != nil {
return ret, err
}
sort.Slice(sttimes, func(i, j int) bool {
return sttimes[j] < sttimes[i]
})
// The first entry should be the most recent
if len(sttimes) > 0 {
return gdb.dbGetTimeEntry(TypeRecent, sttimes[0])
}
return nil, errors.New("No recent time entries found")
} }
// findTimeEntryAndTypeByUUID searches all entries // findTimeEntryAndTypeByUUID searches all entries
@ -87,13 +91,20 @@ func (gdb *GimeDB) SaveTimeEntryType(tp int, te *TimeEntry) error {
return nil return nil
} }
func (gdb *GimeDB) ArchiveTimeEntry(te *TimeEntry) error {
if err := gdb.RemoveTimeEntryFromCategory(te, TypeRecent); err != nil {
return err
}
return gdb.SaveTimeEntryType(TypeArchive, te)
}
// ArchiveTimeEntry takes a time from TypeRecent and moves it to TypeArchive // ArchiveTimeEntry takes a time from TypeRecent and moves it to TypeArchive
func (gdb *GimeDB) ArchiveTimeEntry(uuid string) error { func (gdb *GimeDB) ArchiveTimeEntryByUUID(uuid string) error {
archTime, tp, err := gdb.findTimeEntryAndTypeByUUID(uuid) archTime, tp, err := gdb.findTimeEntryAndTypeByUUID(uuid)
if tp != TypeRecent { if tp != TypeRecent {
return errors.New("Couldn't find timer to archive in the 'Recent' bucket") return errors.New("Couldn't find timer to archive in the 'Recent' bucket")
} }
if err = gdb.RemoveTimeEntry(archTime.uuid); err != nil { if err = gdb.RemoveTimeEntry(archTime); err != nil {
return err return err
} }
return gdb.SaveTimeEntryType(TypeArchive, archTime) return gdb.SaveTimeEntryType(TypeArchive, archTime)
@ -106,14 +117,36 @@ func (gdb *GimeDB) UpdateTimeEntry(te *TimeEntry) error {
if te.uuid == "" { if te.uuid == "" {
return errors.New("Given time entry has no uuid") return errors.New("Given time entry has no uuid")
} }
if err = gdb.RemoveTimeEntry(te.uuid); err != nil { if err = gdb.RemoveTimeEntry(te); err != nil {
return err return err
} }
return gdb.SaveTimeEntry(te) return gdb.SaveTimeEntry(te)
} }
// RemoveTimeEntry removes a time entry with the given uuid from the database // RemoveTimeEntry removes a time entry with the given uuid from the database
func (gdb *GimeDB) RemoveTimeEntry(uuid string) error { func (gdb *GimeDB) RemoveTimeEntry(te *TimeEntry) error {
for _, v := range gdb.AllTypes {
if gdb.RemoveTimeEntryFromCategory(te, v) == nil {
return nil
}
}
return errors.New("Couldn't find Time Entry to Remove")
}
func (gdb *GimeDB) RemoveTimeEntryFromCategory(te *TimeEntry, tp int) error {
if !gdb.dbTimeEntryIsInCategory(te, tp) {
return errors.New("Couldn't find timer to remove in the given bucket")
}
var err error
var useDb *boltease.DB
if useDb, err = gdb.openDBType(tp); err != nil {
return err
}
defer gdb.closeDBType(tp)
return useDb.DeleteBucket([]string{TypeToString(tp)}, te.start.Format(time.RFC3339))
}
func (gdb *GimeDB) RemoveTimeEntryByUUID(uuid string) error {
fndEntry, tp, err := gdb.findTimeEntryAndTypeByUUID(uuid) fndEntry, tp, err := gdb.findTimeEntryAndTypeByUUID(uuid)
if err != nil { if err != nil {
return errors.New("Unable to find time entry with uuid " + uuid) return errors.New("Unable to find time entry with uuid " + uuid)
@ -193,6 +226,20 @@ func (gdb *GimeDB) dbGetAllTimeEntries(tp int) []TimeEntry {
return ret return ret
} }
func (gdb *GimeDB) dbFindTimeEntryCategory(te *TimeEntry) int {
for _, v := range gdb.AllTypes {
if gdb.dbTimeEntryIsInCategory(te, v) {
return v
}
}
return TypeError
}
func (gdb *GimeDB) dbTimeEntryIsInCategory(te *TimeEntry, tp int) bool {
_, err := gdb.dbGetTimeEntry(tp, te.start.Format(time.RFC3339))
return err == nil
}
// dbGetTimeEntry pulls a time entry of type tp with the given start time // dbGetTimeEntry pulls a time entry of type tp with the given start time
// from the db and returns it. // from the db and returns it.
func (gdb *GimeDB) dbGetTimeEntry(tp int, sttm string) (*TimeEntry, error) { func (gdb *GimeDB) dbGetTimeEntry(tp int, sttm string) (*TimeEntry, error) {