Skip to content

Commit

Permalink
fetch: fetch API data concurrently
Browse files Browse the repository at this point in the history
  • Loading branch information
Madh93 committed Jul 23, 2023
1 parent f686c94 commit 406b19f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 58 deletions.
16 changes: 0 additions & 16 deletions internal/toffu/signs.go

This file was deleted.

81 changes: 39 additions & 42 deletions internal/toffu/toffu.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ import (
)

type Toffu struct {
api *woffuapi.WoffuAPI
signs *Signs
api *woffuapi.WoffuAPI
signs woffuapi.Signs
signSlots woffuapi.SignSlots
workday *woffuapi.WorkDay
userId int
}

func New() *Toffu {
Expand All @@ -26,8 +29,29 @@ func (t *Toffu) SetAPI(api *woffuapi.WoffuAPI) {
t.api = api
}

func (t *Toffu) SetSigns(signs woffuapi.Signs) {
t.signs = &Signs{signs: signs}
func (t *Toffu) setSigns() (err error) {
t.signs, err = t.api.GetSigns()
return
}

func (t *Toffu) setSignSlots() (err error) {
t.signSlots, err = t.api.GetSignSlots()
return
}

func (t *Toffu) setWorkday() (err error) {
err = t.setUserId()
if err != nil {
return
}

t.workday, err = t.api.GetUserWorkDay(t.userId)
return
}

func (t *Toffu) setUserId() (err error) {
t.userId, err = getUserId()
return
}

func (t *Toffu) GenerateToken() (err error) {
Expand Down Expand Up @@ -70,31 +94,20 @@ func (t *Toffu) ClockIn() (err error) {
return err
}

userId, err := getUserId()
if err != nil {
return
}

signs, err := t.api.GetSigns()
err = runConcurrently(t.setSigns, t.setWorkday)
if err != nil {
return err
}
t.SetSigns(signs)

if t.signs.hasAlreadyClockedIn() {
if t.signs.HasAlreadyClockedIn() {
return errors.New("error clocking in, you have already clocked in")
}

workday, err := t.api.GetUserWorkDay(userId)
if err != nil {
return err
}

if workday.ScheduleHours <= 0.0 {
if t.workday.ScheduleHours <= 0.0 {
return errors.New("error clocking in, no scheduled working hours today")
}

err = t.api.Sign(userId)
err = t.api.Sign(t.userId)
if err != nil {
return fmt.Errorf("error clocking in: %v", err)
}
Expand All @@ -112,22 +125,16 @@ func (t *Toffu) ClockOut() (err error) {
return err
}

userId, err := getUserId()
if err != nil {
return
}

signs, err := t.api.GetSigns()
err = runConcurrently(t.setSigns, t.setUserId)
if err != nil {
return err
}
t.SetSigns(signs)

if !t.signs.hasAlreadyClockedIn() {
if !t.signs.HasAlreadyClockedIn() {
return errors.New("error clocking out, you have not clocked in or you have already clocked out")
}

err = t.api.Sign(userId)
err = t.api.Sign(t.userId)
if err != nil {
return fmt.Errorf("error clocking out: %v", err)
}
Expand All @@ -143,15 +150,15 @@ func (t *Toffu) GetStatus() (err error) {
return err
}

slots, err := t.api.GetSignSlots()
err = runConcurrently(t.setSignSlots, t.setWorkday)
if err != nil {
return err
}

// Current status
status := ""

if len(slots) > 0 && (woffuapi.SignSlotEvent{}) == slots[len(slots)-1].Out {
if len(t.signSlots) > 0 && (woffuapi.SignSlotEvent{}) == t.signSlots[len(t.signSlots)-1].Out {
status = "In Office"
} else {
status = "Out of Office"
Expand All @@ -166,7 +173,7 @@ func (t *Toffu) GetStatus() (err error) {
return err
}

for _, slot := range slots {
for _, slot := range t.signSlots {
inTime, _ := time.Parse("15:04:05", slot.In.ShortTrueTime)
outTime, _ := time.Parse("15:04:05", time.Now().In(location).Format("15:04:05"))
// In Office
Expand All @@ -182,17 +189,7 @@ func (t *Toffu) GetStatus() (err error) {
}

// Remaining hours
userId, err := getUserId()
if err != nil {
return
}

workday, err := t.api.GetUserWorkDay(userId)
if err != nil {
return err
}

remainingDuration := time.Duration(workday.ScheduleHours*float64(time.Hour)) - totalDuration
remainingDuration := time.Duration(t.workday.ScheduleHours*float64(time.Hour)) - totalDuration

// Show hours worked and remaining hours
fmt.Printf("Total hours worked today: %s", secondsToHumanReadable(totalDuration))
Expand Down
30 changes: 30 additions & 0 deletions internal/toffu/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"runtime"
"strconv"
"strings"
"sync"
"time"

"github.com/spf13/viper"
Expand Down Expand Up @@ -122,3 +123,32 @@ func secondsToHumanReadable(duration time.Duration) string {

return fmt.Sprintf("%dh %dm %ds", hours, minutes, seconds)
}

func runConcurrently(funcs ...func() error) (err error) {
var wg sync.WaitGroup
wg.Add(len(funcs))
errChan := make(chan error, len(funcs))

// Run goroutines
for _, f := range funcs {
go func(fn func() error) {
defer wg.Done()
if err := fn(); err != nil {
errChan <- err
}
}(f)
}

// Wait for all goroutines to finish
wg.Wait()
close(errChan)

// Collect any errors
for err := range errChan {
if err != nil {
return err
}
}

return nil
}
7 changes: 7 additions & 0 deletions internal/woffuapi/signs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ type Signs []struct {
TrueDate string `json:"TrueDate"` // Local Time
}

func (s Signs) HasAlreadyClockedIn() bool {
if len(s) > 0 {
return s[len(s)-1].SignIn
}
return false
}

func (w WoffuAPI) GetSigns() (Signs, error) {
if w.auth.Type() != "TokenAuth" {
return nil, errors.New("token authentication is required")
Expand Down

0 comments on commit 406b19f

Please sign in to comment.