Skip to content

Commit

Permalink
The total is calculated
Browse files Browse the repository at this point in the history
  • Loading branch information
umutphp committed May 14, 2020
1 parent 8306aee commit 1c5e082
Showing 1 changed file with 51 additions and 12 deletions.
63 changes: 51 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package main
import (
"fmt"
"context"
"time"
"log"
"errors"

"golang.org/x/oauth2"
"github.com/google/go-github/v31/github" // with go modules enabled (GO111MODULE=on or outside GOPATH)
Expand All @@ -25,16 +28,25 @@ func main() {
// list all repositories for the authenticated user
repos, _, err := client.Repositories.List(ctx, "umutphp", nil)

for i:=0;i<MAX_GOR_COUNT;i++ {
go repoStat(repoChannel, halt, client, ctx)
if err != nil {
log.Fatal(err)
}

if err == nil {
for _,repo := range repos {
repoChannel <- repo
}

if len(repos) == 0 {
log.Fatal(errors.New("umutphp has no repositories"))
}

accountTotal := make(chan int, len(repos))
accountUnique := make(chan int, len(repos))

for i:=0;i<MAX_GOR_COUNT;i++ {
go repoStat(accountTotal, accountUnique, repoChannel, halt, client, ctx)
}

for _,repo := range repos {
repoChannel <- repo
}

close(repoChannel)

finishedCount := 0
Expand All @@ -43,20 +55,47 @@ func main() {

if finishedCount == MAX_GOR_COUNT {
close(halt)
close(accountTotal)
close(accountUnique)
}
}

total := 0
for t := range accountTotal {
total += t
}

unique := 0
for u := range accountUnique {
unique += u
}

fmt.Println("Total", total, unique)
}

func repoStat(c chan *github.Repository, halt chan int, client *github.Client, ctx context.Context) {
func repoStat(
accountTotal chan int,
accountUnique chan int,
c chan *github.Repository,
halt chan int,
client *github.Client,
ctx context.Context) {

for repo := range c {
stats,_,_ := client.Repositories.ListTrafficViews(ctx, "umutphp", repo.GetName(), nil)
fmt.Printf("%s\n", repo.GetFullName())
fmt.Println("")
fmt.Println(stats.GetCount(), stats.GetUniques())

viewCount := 0
uniqueCount := 0
for _,view := range stats.Views {
fmt.Println(view.GetTimestamp(), view.GetCount(), view.GetUniques())
if (view.GetTimestamp().After(time.Now().AddDate(0, 0, -1))) {
uniqueCount += view.GetUniques()
viewCount += view.GetCount()
}
}

fmt.Println(repo.GetFullName(), viewCount, uniqueCount)
accountTotal <- viewCount
accountUnique <- uniqueCount
}

halt <- 1
Expand Down

0 comments on commit 1c5e082

Please sign in to comment.