From 2a2aea8641e8b327544452f900ea4f5fcc43fc7e Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Thu, 12 Apr 2018 14:30:52 -0500 Subject: [PATCH] Don't use UUID to find entries unless requested --- model.go | 1 + model_timeentry.go | 81 ++++++++++++++++++++++++++++++++++++---------- 2 files changed, 65 insertions(+), 17 deletions(-) diff --git a/model.go b/model.go index d23222c..7f95acc 100644 --- a/model.go +++ b/model.go @@ -25,6 +25,7 @@ const ( TypeNoArchive = 3 // 011 TypeArchive = 4 // 100 TypeAll = 7 // 111 + TypeError = 8 //1000 ArchiveDays = time.Hour * 24 * 90 // Archive anything older than 90 days ) diff --git a/model_timeentry.go b/model_timeentry.go index ae46874..0fcbb7d 100644 --- a/model_timeentry.go +++ b/model_timeentry.go @@ -13,21 +13,25 @@ import ( func (gdb *GimeDB) GetLatestTimeEntry() (*TimeEntry, error) { var ret *TimeEntry var err error - tc := gdb.LoadTimeEntryCollection(TypeRecent) - for i := 0; i < tc.Length(); i++ { - te := tc.Get(i) - if ret == nil { - ret = te - } else { - if te.GetEnd().After(ret.GetEnd()) { - ret = te - } - } + + var useDb *boltease.DB + if useDb, err = gdb.openDBType(TypeRecent); err != nil { + return ret, err } - if ret == nil { - err = errors.New("Could not find latest time entry") + defer gdb.closeDBType(TypeRecent) + + var sttimes []string + if sttimes, err = useDb.GetBucketList([]string{TypeToString(TypeRecent)}); err != nil { + return ret, err } - 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 @@ -87,13 +91,20 @@ func (gdb *GimeDB) SaveTimeEntryType(tp int, te *TimeEntry) error { 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 -func (gdb *GimeDB) ArchiveTimeEntry(uuid string) error { +func (gdb *GimeDB) ArchiveTimeEntryByUUID(uuid string) error { archTime, tp, err := gdb.findTimeEntryAndTypeByUUID(uuid) if tp != TypeRecent { 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 gdb.SaveTimeEntryType(TypeArchive, archTime) @@ -106,14 +117,36 @@ func (gdb *GimeDB) UpdateTimeEntry(te *TimeEntry) error { if te.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 gdb.SaveTimeEntry(te) } // 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) if err != nil { return errors.New("Unable to find time entry with uuid " + uuid) @@ -193,6 +226,20 @@ func (gdb *GimeDB) dbGetAllTimeEntries(tp int) []TimeEntry { 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 // from the db and returns it. func (gdb *GimeDB) dbGetTimeEntry(tp int, sttm string) (*TimeEntry, error) {