Logging, reports

This commit is contained in:
2015-03-06 20:23:01 +07:00
parent 9f36068c54
commit 9251ad8c6d
9 changed files with 41 additions and 20 deletions
+9 -4
View File
@@ -5,45 +5,49 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"time"
"github.com/localhots/empact/config"
"github.com/localhots/empact/db"
)
func Authenticate(code string) (token, login string, err error) {
report("Authenticate", time.Now())
if token, err = FetchAccessToken(code); err != nil {
return
}
fmt.Println("Got token: " + token)
log.Printf("Got token %q for code %q\n", token, code)
var user *db.User
if user, err = FetchUserInfoWithToken(token); err != nil {
return
}
login = user.Login
fmt.Println("Saving user", user)
log.Println("Saving user", user)
user.Save()
tok := &db.Token{
User: login,
Token: token,
}
fmt.Println("Saving token", tok)
log.Println("Saving token", tok)
tok.Save()
return
}
func FetchAccessToken(code string) (token string, err error) {
report("FetchAccessToken", time.Now())
payload := url.Values{}
payload.Set("client_id", config.C().ClientID)
payload.Set("client_secret", config.C().ClientSecret)
payload.Set("code", code)
payload.Set("redirect_uri", config.C().RedirectURI)
fmt.Println("Requesting token")
log.Printf("Requesting token for code %q", code)
buf := bytes.NewBuffer([]byte(payload.Encode()))
var resp *http.Response
if resp, err = http.Post(config.C().AccessTokenURL, "application/x-www-form-urlencoded", buf); err != nil {
@@ -64,6 +68,7 @@ func FetchAccessToken(code string) (token string, err error) {
}
func FetchUserInfoWithToken(token string) (u *db.User, err error) {
report("FetchUserInfoWithToken", time.Now())
var resp *http.Response
if resp, err = http.Get("https://api.github.com/user?access_token=" + token); err != nil {
return
+4
View File
@@ -1,11 +1,14 @@
package task
import (
"time"
"github.com/google/go-github/github"
"github.com/localhots/empact/db"
)
func SyncRepos(token, owner string) {
report("SyncRepos", time.Now())
client := newGithubClient(token)
opt := &github.RepositoryListByOrgOptions{
ListOptions: github.ListOptions{},
@@ -32,6 +35,7 @@ func SyncRepos(token, owner string) {
}
func SyncContrib(token, owner, repo string) {
report("SyncContrib", time.Now())
client := newGithubClient(token)
contribs, resp, err := client.Repositories.ListContributorsStats(owner, repo)
saveResponseMeta(token, resp)
+13
View File
@@ -1,6 +1,9 @@
package task
import (
"log"
"time"
"code.google.com/p/goauth2/oauth"
"github.com/google/go-github/github"
"github.com/localhots/empact/db"
@@ -25,3 +28,13 @@ func saveResponseMeta(token string, res *github.Response) {
}
tok.Save()
}
func report(task string, start time.Time) {
duration := time.Since(start).Nanoseconds()
outcome := "done"
if err := recover(); err != nil {
outcome = "failed"
}
log.Printf("Task %s %s; time: %d (%dms)\n", task, outcome, duration, duration/1000000)
}