Skip to content

Commit

Permalink
change data structure for count
Browse files Browse the repository at this point in the history
  • Loading branch information
srinandan committed Aug 4, 2022
1 parent 4edd39e commit 17c5f0d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
35 changes: 20 additions & 15 deletions client/env/reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ type metric struct {
Values []string `json:"values,omitempty"`
}

var envAPICalls int
var mu sync.Mutex
type apiCalls struct {
count int
sync.Mutex
}

var ApiCalls = &apiCalls{count: 0}

func TotalAPICallsInMonthAsync(environment string, month int, year int, envDetails bool, wg *sync.WaitGroup) {
defer wg.Done()
Expand All @@ -63,7 +67,7 @@ func TotalAPICallsInMonthAsync(environment string, month int, year int, envDetai
clilog.Error.Println(err)
return
}
syncCount(total)
ApiCalls.incrementCount(total)
if envDetails {
w := tabwriter.NewWriter(os.Stdout, 26, 4, 0, ' ', 0)
fmt.Fprintf(w, "%s\t%d/%d\t%d", environment, month, year, total)
Expand Down Expand Up @@ -110,15 +114,16 @@ func TotalAPICallsInMonth(environment string, month int, year int) (total int, e
return apiCalls, nil
}

//GetEnvAPICalls
func GetEnvAPICalls() int {
return envAPICalls
//GetCount
func (c *apiCalls) GetCount() int {
return c.count
}

func ResetEnvAPICalls() {
mu.Lock()
defer mu.Unlock()
envAPICalls = 0
//ResetCount
func (c *apiCalls) ResetCount() {
c.Lock()
defer c.Unlock()
c.count = 0
}

// daysIn returns the number of days in a month for a given year.
Expand All @@ -128,9 +133,9 @@ func daysIn(m time.Month, year int) int {
return time.Date(year, m+1, 0, 0, 0, 0, 0, time.UTC).Day()
}

//syncCount synchronizes counting
func syncCount(total int) {
mu.Lock()
defer mu.Unlock()
envAPICalls = envAPICalls + total
//incrementCount synchronizes counting
func (c *apiCalls) incrementCount(total int) {
c.Lock()
defer c.Unlock()
c.count = c.count + total
}
4 changes: 2 additions & 2 deletions client/orgs/reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TotalAPICallsInMonth(month int, year int, envDetails bool, conn int) (total
var envList []string

//ensure the count is reset to zero before calculating the next set
defer env.ResetEnvAPICalls()
defer env.ApiCalls.ResetCount()

apiclient.SetPrintOutput(false)

Expand Down Expand Up @@ -75,7 +75,7 @@ func TotalAPICallsInMonth(month int, year int, envDetails bool, conn int) (total

apiclient.SetPrintOutput(true)

return env.GetEnvAPICalls(), nil
return env.ApiCalls.GetCount(), nil
}

func batchReport(envList []string, month int, year int, envDetails bool, pwg *sync.WaitGroup) {
Expand Down

0 comments on commit 17c5f0d

Please sign in to comment.