More Work

This commit is contained in:
Brian Buller 2017-07-31 07:00:16 -05:00
parent 1509bbdbe6
commit 4fd15f2c2b
8 changed files with 273 additions and 74 deletions

2
Godeps/Godeps.json generated
View File

@ -10,7 +10,7 @@
}, },
{ {
"ImportPath": "github.com/br0xen/boltease", "ImportPath": "github.com/br0xen/boltease",
"Rev": "50a16b90567dacab0e724ba3620174a19d80489f" "Rev": "69e69463f4b6e07bd4405fa507333ae55dc32791"
}, },
{ {
"ImportPath": "github.com/pborman/uuid", "ImportPath": "github.com/pborman/uuid",

View File

@ -3,6 +3,8 @@ package main
import ( import (
"fmt" "fmt"
"os" "os"
"strconv"
"strings"
"time" "time"
"git.bullercodeworks.com/brian/gime" "git.bullercodeworks.com/brian/gime"
@ -14,7 +16,7 @@ const (
) )
var validOperations map[string][]string var validOperations map[string][]string
var activeTimeEntry *gime.TimeEntry var activeTimeEntries *gime.TimeEntryCollection
var gdb *gime.GimeDB var gdb *gime.GimeDB
func main() { func main() {
@ -48,6 +50,7 @@ func main() {
func printHelp() int { func printHelp() int {
fmt.Println("gime - A simple timekeeping application\n") fmt.Println("gime - A simple timekeeping application\n")
fmt.Println("Usage: gime [@timerID] [operation] [tags...]")
for _, v := range validOperations { for _, v := range validOperations {
for vi := range v { for vi := range v {
fmt.Println(" ", v[vi]) fmt.Println(" ", v[vi])
@ -57,31 +60,56 @@ func printHelp() int {
return 0 return 0
} }
func loadActiveTimeEntries() {
activeTimeEntries = gdb.LoadTimeEntryCollection(gime.TypeCurrent)
}
func printStatus() int { func printStatus() int {
if activeTimeEntry == nil { loadActiveTimeEntries()
curr := time.Now()
fmt.Println("Current Time:", curr.Format(time.Stamp))
if activeTimeEntries.Length() == 0 {
fmt.Println("No timer running") fmt.Println("No timer running")
} else { } else {
fmt.Print("Timer started at ") fmt.Print("Currently Active Timers\n")
curr := time.Now() for i := 0; i < activeTimeEntries.Length(); i++ {
if activeTimeEntry.GetStart().Day() == curr.Day() { v := activeTimeEntries.Get(i)
fmt.Println(activeTimeEntry.GetStart().Format("15:04")) if v.GetStart().Day() == curr.Day() {
} else { fmt.Print("@"+strconv.Itoa(i)+" ", v.GetStart().Format("15:04"), " ")
fmt.Println(activeTimeEntry.GetStart().Format(time.Stamp)) } else {
fmt.Print("@"+strconv.Itoa(i)+" ", v.GetStart().Format(time.Stamp), " ")
}
since := time.Since(v.GetStart()).String()
since = strings.Split(since, ".")[0]
fmt.Print("(" + since + "s) ")
fmt.Print(v.GetTags().Length())
if v.GetTags().Length() > 0 {
fmt.Print(" [ ")
for j := 0; j < v.GetTags().Length(); j++ {
fmt.Print(v.GetTags().Get(j))
if j < v.GetTags().Length()-1 {
fmt.Print(", ")
}
}
fmt.Print(" ] ")
}
fmt.Println("")
} }
fmt.Println(time.Since(activeTimeEntry.GetStart()).String())
} }
return 0 return 0
} }
// switchTimer performs a stop on any currently running timers
// and starts a new timer with the given arguments
func switchTimer(args []string) int {
return 0
}
// startTimer takes a list of arguments and returns the return code // startTimer takes a list of arguments and returns the return code
// to be passed along to os.Exit // to be passed along to os.Exit
func startTimer(args []string) int { func startTimer(args []string) int {
var err error var err error
tm := time.Now() tm := time.Now()
curr := gdb.LoadTimeEntryCollection(gime.TypeCurrent)
if curr.Length() > 0 {
// TODO: Stop all Timers in curr
}
tagStart := 0 tagStart := 0
if len(args) > 0 { if len(args) > 0 {
// Check if the first argument looks like a date/time // Check if the first argument looks like a date/time
@ -103,7 +131,11 @@ func startTimer(args []string) int {
timerArgs = args[tagStart:] timerArgs = args[tagStart:]
} }
fmt.Println(tm) fmt.Println(tm)
if entry, err = gime.CreateTimeEntry(tm, time.Time{}, timerArgs); err != nil { tc := new(gime.TagCollection)
for i := range timerArgs {
tc.Push(timerArgs[i])
}
if entry, err = gime.CreateTimeEntry(tm, time.Time{}, tc); err != nil {
fmt.Println(err) fmt.Println(err)
return 1 return 1
} }
@ -124,7 +156,6 @@ func stopTimer(args []string) int {
if err != nil { if err != nil {
tm, err = time.Parse(time.Kitchen, args[0]) tm, err = time.Parse(time.Kitchen, args[0])
} }
} }
_, _ = tm, st _, _ = tm, st
return 0 return 0

View File

@ -19,8 +19,11 @@ const (
TypeCurrent = "current" TypeCurrent = "current"
TypeRecent = "recent" TypeRecent = "recent"
TypeArchive = "archive" TypeArchive = "archive"
TypeUnknown = "unknown" // Not really a type, used for searches and stuff
) )
const AllTypes = []string{TypeCurrent, TypeRecent, TypeArchive}
// Load Database returns a database loaded off the files given // Load Database returns a database loaded off the files given
// name and archName located in path // name and archName located in path
func LoadDatabase(path, name, archName string) (*GimeDB, error) { func LoadDatabase(path, name, archName string) (*GimeDB, error) {
@ -36,6 +39,16 @@ func LoadDatabase(path, name, archName string) (*GimeDB, error) {
return &gdb, nil return &gdb, nil
} }
// Load a TimeEntry collection from a database
func (gdb *GimeDB) LoadTimeEntryCollection(tp string) *TimeEntryCollection {
ret := new(TimeEntryCollection)
entries := gdb.dbGetAllTimeEntries(tp)
for i := range entries {
ret.Push(&entries[i])
}
return ret
}
func (gdb *GimeDB) openDatabase() error { func (gdb *GimeDB) openDatabase() error {
gdb.dbOpened += 1 gdb.dbOpened += 1
if gdb.dbOpened == 1 { if gdb.dbOpened == 1 {

View File

@ -8,6 +8,30 @@ import (
"github.com/br0xen/boltease" "github.com/br0xen/boltease"
) )
// FindTimeEntryByUUID searches all entries of type tp (can be TypeUnknown)
// for the time entry with the given uuid
func (gdb *GimeDB) FindTimeEntryByUUID(tp, uuid string) (*TimeEntry, error) {
var ret *TimeEntry
var err error
if tp == TypeUnknown {
for _, v := range AllTypes {
ret, err = gdb.FindTimeEntryByUUID(v, uuid)
if ret != nil {
return ret, nil
}
}
return nil, errors.New("Unable to find time entry with uuid " + uuid)
}
tstEntries := gdb.dbGetAllTimeEntries(tp)
for i := range tstEntries {
if tstEntries[i].uuid == uuid {
// Found it!
return tstEntries[i], nil
}
}
return nil, errors.New("Unable to find time entry with uuid " + uuid)
}
// SaveTimeEntry creates a time entry in the database // SaveTimeEntry creates a time entry in the database
// If TimeEntry.end is zero, then it puts it in TypeCurrent // If TimeEntry.end is zero, then it puts it in TypeCurrent
func (gdb *GimeDB) SaveTimeEntry(te *TimeEntry) error { func (gdb *GimeDB) SaveTimeEntry(te *TimeEntry) error {
@ -28,27 +52,55 @@ func (gdb *GimeDB) SaveTimeEntry(te *TimeEntry) error {
tp = TypeArchive tp = TypeArchive
} }
} }
tePath := []string{tp, te.uuid} tePath := []string{tp, te.start.Format(time.RFC3339)}
if err = useDb.SetValue(tePath, "uuid", te.uuid); err != nil {
return err
}
if err = useDb.SetTimestamp(tePath, "start", te.start); err != nil { if err = useDb.SetTimestamp(tePath, "start", te.start); err != nil {
return err return err
} }
if err = useDb.SetTimestamp(tePath, "end", te.end); err != nil { if err = useDb.SetTimestamp(tePath, "end", te.end); err != nil {
return err return err
} }
for i := range te.tags { for i := 0; i < te.tags.Length(); i++ {
err = useDb.SetValue(append(tePath, "tags"), strconv.Itoa(i), te.tags[i]) err = useDb.SetValue(append(tePath, "tags"), strconv.Itoa(i), te.tags.Get(i))
} }
return nil return nil
} }
// dbGetAllTimeEntries gets all time entries of a specific type // UpdateTimeEntry updates the time entry in the database
// tp can be: // It first finds the current entry in the database by the uuid
// TypeCurrent = "current" func (gdb *GimeDB) UpdateTimeEntry(te *TimeEntry) error {
// TypeRecent = "recent" var err error
// TypeArchive = "archive" if te.uuid == "" {
// Getting all archived time entries has the potential to be a lot of data return errors.New("Given time entry has no uuid")
func (gdb *GimeDB) dbGetAllTimeEntries(tp string) []TimeEntry { }
var ret []TimeEntry oldTe, err := gdb.FindTimeEntryByUUID(TypeUnknown, te.uuid)
if err != nil {
return err
}
}
// RemoveTimeEntry removes a time entry of type tp (can be TypeUnknown)
// with the given uuid from the database
func (gdb *GimeDB) RemoveTimeEntry(tp, uuid string) error {
var fndEntry *TimeEntry
var err error
// For TypeUnknown, we want to manually loop, so we know where we found it
if tp == TypeUnknown {
for _, v := range AllTypes {
fndEntry, err = gdb.FindTimeEntryByUUID(v, uuid)
if err == nil {
tp = v
break
}
}
if fndEntry == nil {
return errors.New("Unable to find time entry with uuid " + uuid)
}
}
var useDb *boltease.DB var useDb *boltease.DB
var err error var err error
if useDb, err = gdb.openDBType(tp); err != nil { if useDb, err = gdb.openDBType(tp); err != nil {
@ -56,11 +108,36 @@ func (gdb *GimeDB) dbGetAllTimeEntries(tp string) []TimeEntry {
} }
defer gdb.closeDBType(tp) defer gdb.closeDBType(tp)
var uuids []string return nil, errors.New("Unable to find time entry with uuid " + uuid)
if uuids, err = useDb.GetBucketList([]string{tp}); err != nil { }
// dbGetAllTimeEntries gets all time entries of a specific type
// tp can be:
// TypeCurrent = "current"
// TypeRecent = "recent"
// TypeArchive = "archive"
// TypeUnknown = "unknown" - Returns _ALL_ entries
// Getting all archived time entries has the potential to be a lot of data
func (gdb *GimeDB) dbGetAllTimeEntries(tp string) []TimeEntry {
var ret []TimeEntry
if tp == TypeUnknown {
for _, v := range AllTypes {
ret = append(ret, gdb.dbGetAllTimeEntries(v))
}
return ret return ret
} }
for _, v := range uuids { var useDb *boltease.DB
var err error
if useDb, err = gdb.openDBType(tp); err != nil {
return ret
}
defer gdb.closeDBType(tp)
var sttimes []string
if sttimes, err = useDb.GetBucketList([]string{tp}); err != nil {
return ret
}
for _, v := range sttimes {
if te, _ := gdb.dbGetTimeEntry(tp, v); te != nil { if te, _ := gdb.dbGetTimeEntry(tp, v); te != nil {
ret = append(ret, *te) ret = append(ret, *te)
} }
@ -70,19 +147,30 @@ func (gdb *GimeDB) dbGetAllTimeEntries(tp string) []TimeEntry {
// dbGetTimeEntry pulls a time entry of type tp with the given id // dbGetTimeEntry pulls a time entry of type tp with the given id
// from the db and returns it. // from the db and returns it.
func (gdb *GimeDB) dbGetTimeEntry(tp, id string) (*TimeEntry, error) { func (gdb *GimeDB) dbGetTimeEntry(tp, sttm string) (*TimeEntry, error) {
var ret *TimeEntry var ret *TimeEntry
var err error var err error
if tp == TypeUnknown {
// Loop through the types and return the first entry found that matches
for _, v := range AllTypes {
ret, _ = gdb.dbGetTimeEntry(v, sttm)
if ret != nil {
return ret, nil
}
}
return nil, errors.Error("Couldn't find time entry")
}
var useDb *boltease.DB var useDb *boltease.DB
if useDb, err = gdb.openDBType(tp); err != nil { if useDb, err = gdb.openDBType(tp); err != nil {
return ret, err return ret, err
} }
defer gdb.closeDBType(tp) defer gdb.closeDBType(tp)
entryPath := []string{tp, id} entryPath := []string{tp, sttm}
ret = new(TimeEntry) ret = new(TimeEntry)
ret.uuid = id if ret.uuid, err = useDb.GetValue(entryPath, "uuid"); err != nil {
return nil, errors.New("Unable to read entry uuid")
}
if ret.start, err = useDb.GetTimestamp(entryPath, "start"); err != nil { if ret.start, err = useDb.GetTimestamp(entryPath, "start"); err != nil {
return nil, errors.New("Unable to read entry start time") return nil, errors.New("Unable to read entry start time")
} }
@ -94,10 +182,11 @@ func (gdb *GimeDB) dbGetTimeEntry(tp, id string) (*TimeEntry, error) {
if keys, err = useDb.GetKeyList(entryTagsPath); err != nil { if keys, err = useDb.GetKeyList(entryTagsPath); err != nil {
keys = []string{} keys = []string{}
} }
ret.tags = new(TagCollection)
for i := range keys { for i := range keys {
var val string var val string
if val, err = useDb.GetValue(entryTagsPath, keys[i]); err != nil { if val, err = useDb.GetValue(entryTagsPath, keys[i]); err == nil {
ret.tags = append(ret.tags, val) ret.tags.Push(val)
} }
} }
return ret, nil return ret, nil

80
tag_collection.go Normal file
View File

@ -0,0 +1,80 @@
package gime
import "fmt"
// TagCollection is a collection of strings for gomobile bindings
type TagCollection struct {
list []string
}
// Length returns the number of tags in the collection
func (tc *TagCollection) Length() int {
return len(tc.list)
}
// Get returns the string at idx or an empty string
func (tc *TagCollection) Get(idx int) string {
if idx < tc.Length() {
return tc.list[idx]
}
return ""
}
// Clear empties the list of Tags
func (tc *TagCollection) Clear() {
tc.list = tc.list[:0]
}
// Index finds the index of the given string or -1 if not found
func (tc *TagCollection) Index(t string) int {
for i, tst := range tc.list {
if tst == t {
return i
}
}
return -1
}
// Insert inserts a string into the collection at i
func (tc *TagCollection) Insert(i int, t string) {
if i < 0 || i > tc.Length() {
fmt.Println("gime: Attempted to insert string at invalid index")
}
tc.list = append(tc.list, "")
copy(tc.list[i+1:], tc.list[i:])
tc.list[i] = t
}
// Remove removes the string at i from the collection
func (tc *TagCollection) Remove(i int) {
if i < 0 || i >= tc.Length() {
fmt.Println("gime: Attempted to remove tag at invalid index")
}
copy(tc.list[i:], tc.list[i+1:])
tc.list[len(tc.list)-1] = ""
tc.list = tc.list[:len(tc.list)-1]
}
// Push adds an element to the end of the collection
func (tc *TagCollection) Push(t string) {
tc.Insert(tc.Length(), t)
}
// Pop removes the last element from the collection
func (tc *TagCollection) Pop() string {
ret := tc.list[tc.Length()-1]
tc.Remove(tc.Length() - 1)
return ret
}
// Unshift adds an element to the front of the collection
func (tc *TagCollection) Unshift(t string) {
tc.Insert(0, t)
}
// Shift removes an element from the front of the collection
func (tc *TagCollection) Shift() string {
ret := tc.list[0]
tc.Remove(0)
return ret
}

View File

@ -12,26 +12,29 @@ type TimeEntry struct {
uuid string uuid string
start time.Time start time.Time
end time.Time end time.Time
tags []string tags *TagCollection
} }
// CreateTimeEntry creates an entry with the given parameters and returns it. // CreateTimeEntry creates an entry with the given parameters and returns it.
// An error is returned if no start time is given or if the end time given is // An error is returned if no start time is given or if the end time given is
// non-zero and earlier than the start time. // non-zero and earlier than the start time.
func CreateTimeEntry(s, e time.Time, t []string) (*TimeEntry, error) { func CreateTimeEntry(s, e time.Time, t *TagCollection) (*TimeEntry, error) {
ret := new(TimeEntry) ret := new(TimeEntry)
ret.uuid = uuid.New() ret.uuid = uuid.New()
if s.IsZero() { if s.IsZero() {
// No start time given, return error // No start time given, return error
return ret, errors.New("No start time given") return ret, errors.New("No start time given")
} }
ret.start = s
if !e.IsZero() { if !e.IsZero() {
if !e.After(s) { if !e.After(s) {
// Given end time is earlier than start time // Given end time is earlier than start time
return ret, errors.New("End time is before start time") return ret, errors.New("End time is before start time")
} }
} }
return &TimeEntry{start: s, end: e, tags: t}, nil ret.end = e
ret.tags = t
return ret, nil
} }
// GetStart returns the start time for the entry // GetStart returns the start time for the entry
@ -60,14 +63,14 @@ func (t *TimeEntry) IsActive() bool {
} }
// GetTags returns all of the tags associated with this time entry // GetTags returns all of the tags associated with this time entry
func (t *TimeEntry) GetTags() []string { func (t *TimeEntry) GetTags() *TagCollection {
return t.tags return t.tags
} }
// HasTag returns if the time entry contains a specific tag // HasTag returns if the time entry contains a specific tag
func (t *TimeEntry) HasTag(s string) bool { func (t *TimeEntry) HasTag(s string) bool {
for i := range t.tags { for i := 0; i < t.tags.Length(); i++ {
if t.tags[i] == s { if t.tags.Get(i) == s {
return true return true
} }
} }
@ -77,22 +80,15 @@ func (t *TimeEntry) HasTag(s string) bool {
// AddTag adds a tag to the time entry (if it isn't already there) // AddTag adds a tag to the time entry (if it isn't already there)
func (t *TimeEntry) AddTag(s string) { func (t *TimeEntry) AddTag(s string) {
if !t.HasTag(s) { if !t.HasTag(s) {
t.tags = append(t.tags, s) t.tags.Push(s)
} }
} }
// RemoveTag removes a tag from the time entry // RemoveTag removes a tag from the time entry
func (t *TimeEntry) RemoveTag(s string) { func (t *TimeEntry) RemoveTag(s string) {
var i int i := t.tags.Index(s)
var fnd bool if i != -1 {
for i = range t.tags { t.tags.Remove(i)
if t.tags[i] == s {
fnd = true
break
}
}
if fnd {
t.tags = append(t.tags[:i], t.tags[i+1:]...)
} }
} }

View File

@ -7,16 +7,6 @@ type TimeEntryCollection struct {
list []TimeEntry list []TimeEntry
} }
// Load a TimeEntry collection from a database
func (gdb *GimeDB) LoadTimeEntryCollection(tp string) *TimeEntryCollection {
ret := new(TimeEntryCollection)
entries := gdb.dbGetAllTimeEntries(tp)
for i := range entries {
ret.Push(&entries[i])
}
return ret
}
// Length returns how many time entries are in the collection // Length returns how many time entries are in the collection
func (tc *TimeEntryCollection) Length() int { func (tc *TimeEntryCollection) Length() int {
return len(tc.list) return len(tc.list)
@ -24,7 +14,7 @@ func (tc *TimeEntryCollection) Length() int {
// Get returns the TimeEntry at idx or a nil entry // Get returns the TimeEntry at idx or a nil entry
func (tc *TimeEntryCollection) Get(idx int) *TimeEntry { func (tc *TimeEntryCollection) Get(idx int) *TimeEntry {
if idx <= tc.Length() { if idx < tc.Length() {
return &tc.list[idx] return &tc.list[idx]
} }
return nil return nil
@ -83,7 +73,7 @@ func (tc *TimeEntryCollection) Unshift(t *TimeEntry) {
} }
// Shift removes an element from the front of the collection // Shift removes an element from the front of the collection
func (tc *TimeEntryCollection) Shift(t *TimeEntry) *TimeEntry { func (tc *TimeEntryCollection) Shift() *TimeEntry {
ret := tc.list[0] ret := tc.list[0]
tc.Remove(0) tc.Remove(0)
return &ret return &ret

View File

@ -242,7 +242,7 @@ func (b *DB) GetBucketList(path []string) ([]string, error) {
} }
} }
// newBkt should have the last bucket in the path // bkt should have the last bucket in the path
berr = bkt.ForEach(func(k, v []byte) error { berr = bkt.ForEach(func(k, v []byte) error {
if v == nil { if v == nil {
// Must be a bucket // Must be a bucket
@ -271,20 +271,20 @@ func (b *DB) GetKeyList(path []string) ([]string, error) {
if bkt == nil { if bkt == nil {
return fmt.Errorf("Couldn't find bucket " + path[0]) return fmt.Errorf("Couldn't find bucket " + path[0])
} }
var berr error
if len(path) > 1 { if len(path) > 1 {
var newBkt *bolt.Bucket
for idx := 1; idx < len(path); idx++ { for idx := 1; idx < len(path); idx++ {
newBkt = bkt.Bucket([]byte(path[idx])) bkt = bkt.Bucket([]byte(path[idx]))
if newBkt == nil { if bkt == nil {
return fmt.Errorf("Couldn't find bucket " + strings.Join(path[:idx], "/")) return fmt.Errorf("Couldn't find bucket " + strings.Join(path[:idx], " / "))
} }
} }
bkt = newBkt
} }
// newBkt should have the last bucket in the path
berr := bkt.ForEach(func(k, v []byte) error { // bkt should have the last bucket in the path
berr = bkt.ForEach(func(k, v []byte) error {
if v != nil { if v != nil {
// Not a bucket // Must be a key
ret = append(ret, string(k)) ret = append(ret, string(k))
} }
return nil return nil