1
0
Fork 0

Logging, reports

This commit is contained in:
Gregory Eremin 2015-03-06 20:23:01 +07:00
parent 9f36068c54
commit 9251ad8c6d
9 changed files with 41 additions and 20 deletions

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"flag"
"io/ioutil"
"log"
"os"
)
@ -29,23 +30,23 @@ func C() Config {
}
func init() {
var (
path string
fd *os.File
contents []byte
err error
)
var err error
var path string
flag.StringVar(&path, "config", "config.json", "Path to configuration file")
flag.Parse()
var fd *os.File
if fd, err = os.Open(path); err != nil {
panic(err)
}
var contents []byte
if contents, err = ioutil.ReadAll(fd); err != nil {
panic(err)
}
if err = json.Unmarshal(contents, &conf); err != nil {
panic(err)
}
log.SetOutput(os.Stderr)
log.SetFlags(log.Ltime | log.Lshortfile)
}

View File

@ -1,8 +1,6 @@
package db
import (
"strings"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"github.com/jmoiron/sqlx/reflectx"
@ -14,7 +12,7 @@ var (
func Connect(params string) (err error) {
db, err = sqlx.Connect("mysql", params)
db.Mapper = reflectx.NewMapperFunc("json", strings.ToLower)
db.Mapper = reflectx.NewMapper("json")
return
}

View File

@ -10,8 +10,8 @@ type Token struct {
Token string `json:"token"`
Quota uint64 `json:"quota"`
Remaining uint64 `json:"remaining"`
ResetAt time.Time `json:"reset_at"`
CreatedAt time.Time `json:"created_at"`
ResetAt time.Time `json:"reset_at" db:"reset_at"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
}
const saveTokenQuery = `

View File

@ -4,7 +4,7 @@ type User struct {
Login string `json:"login"`
Name string `json:"name"`
ID uint64 `json:"id"`
AvatarURL string `json:"avatar_url"`
AvatarURL string `json:"avatar_url" db:"avatar_url"`
}
const saveUserQuery = `

View File

@ -24,7 +24,7 @@ func authCallbackHandler(w http.ResponseWriter, r *http.Request) {
}
code := r.FormValue("code")
log.Println("Got code: ", code)
log.Printf("Got code %q\n", code)
if _, login, err := task.Authenticate(code); err == nil {
createSession(r, login)
} else {

View File

@ -2,8 +2,8 @@ package server
import (
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"github.com/GeertJohan/go.rice"
@ -34,7 +34,7 @@ func init() {
}
func Start() {
fmt.Println("Starting server at http://localhost:8080")
log.Println("Starting server at http://localhost:8080")
http.ListenAndServe(":8080", nil)
}

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

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)

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)
}