diff --git a/db/contrib.go b/db/contrib.go index de908ba..6ca5f12 100644 --- a/db/contrib.go +++ b/db/contrib.go @@ -15,7 +15,7 @@ type Contrib struct { } func (c *Contrib) Save() { - defer measure("SaveContrib", time.Now()) + defer measure(time.Now(), "SaveContrib") mustExecN(` insert into contribs (week, org_id, repo_id, user_id, commits, additions, deletions) values (:week, :org_id, :repo_id, :user_id, :commits, :additions, :deletions) diff --git a/db/db.go b/db/db.go index 6aa19b0..4b16e5c 100644 --- a/db/db.go +++ b/db/db.go @@ -11,7 +11,7 @@ import ( var ( db *sqlx.DB - queryQueue = make(chan func(), 1000) + queryQueue = make(chan func()) ) func Connect(params string) (err error) { @@ -50,21 +50,18 @@ func Queue(fun func()) { func processQueue() { for { - fun := <-queryQueue - fun() + (<-queryQueue)() } } -func measure(op string, start time.Time) { +func measure(start time.Time, op string) { duration := time.Since(start).Nanoseconds() outcome := "succeeded" err := recover() if err != nil { outcome = "failed" + defer panic(err) } log.Printf("Operation %s %s; time: %d (%dms)\n", op, outcome, duration, duration/1000000) - if err != nil { - panic(err) - } } diff --git a/db/members.go b/db/members.go index 771167a..a2200e4 100644 --- a/db/members.go +++ b/db/members.go @@ -8,7 +8,7 @@ import ( ) func SaveOrgMembers(orgID int, members []int) { - defer measure("SaveOrgMembers", time.Now()) + defer measure(time.Now(), "SaveOrgMembers") tx := db.MustBegin() var ids = []string{} @@ -37,7 +37,7 @@ func SaveOrgMembers(orgID int, members []int) { } func SaveTeamMembers(orgID, teamID int, members []int) { - defer measure("SaveTeamMembers", time.Now()) + defer measure(time.Now(), "SaveTeamMembers") tx := db.MustBegin() var ids = []string{} @@ -67,7 +67,7 @@ func SaveTeamMembers(orgID, teamID int, members []int) { } func SaveTeamRepos(orgID, teamID int, repos []int) { - defer measure("SaveTeamRepos", time.Now()) + defer measure(time.Now(), "SaveTeamRepos") tx := db.MustBegin() var ids = []string{} diff --git a/db/org.go b/db/org.go index 41a749f..d69efff 100644 --- a/db/org.go +++ b/db/org.go @@ -13,7 +13,7 @@ type Org struct { } func (o *Org) Save() { - defer measure("SaveOrg", time.Now()) + defer measure(time.Now(), "SaveOrg") mustExecN(` insert into orgs (id, login, company, avatar_url, updated_at) values (:id, :login, :company, :avatar_url, now()) @@ -26,7 +26,7 @@ func (o *Org) Save() { } func UserOrgs(login string) (orgs []*Org) { - defer measure("UserOrgs", time.Now()) + defer measure(time.Now(), "UserOrgs") mustSelect(&orgs, ` select o.* from org_members m diff --git a/db/repo.go b/db/repo.go index 625de90..793d9b4 100644 --- a/db/repo.go +++ b/db/repo.go @@ -15,7 +15,7 @@ type Repo struct { } func (r *Repo) Save() { - defer measure("SaveRepo", time.Now()) + defer measure(time.Now(), "SaveRepo") mustExecN(` insert into repos (org_id, name, description, is_private, is_fork, updated_at) values (:org_id, :name, :description, :is_private, :is_fork, now()) @@ -30,7 +30,7 @@ func (r *Repo) Save() { } func OrgRepos(login string) (repos []*Repo) { - defer measure("OrgRepos", time.Now()) + defer measure(time.Now(), "OrgRepos") mustSelect(&repos, ` select * from repos r diff --git a/db/stat.go b/db/stat.go index c7e3f39..c9974ed 100644 --- a/db/stat.go +++ b/db/stat.go @@ -31,7 +31,7 @@ join repos r on c.repo_id = r.id` func StatOrgTop(p map[string]interface{}) (res []StatItem) { - defer measure("StatOrgTop", time.Now()) + defer measure(time.Now(), "StatOrgTop") mustSelectN(&res, fmt.Sprintf(` select %s as item, @@ -50,7 +50,7 @@ func StatOrgTop(p map[string]interface{}) (res []StatItem) { } func StatOrgActivity(p map[string]interface{}) (res []StatPoint) { - defer measure("StatOrgActivity", time.Now()) + defer measure(time.Now(), "StatOrgActivity") mustSelectN(&res, fmt.Sprintf(` select %s as item, @@ -70,7 +70,7 @@ func StatOrgActivity(p map[string]interface{}) (res []StatPoint) { } func StatTeamTop(p map[string]interface{}) (res []StatItem) { - defer measure("StatTeamTop", time.Now()) + defer measure(time.Now(), "StatTeamTop") mustSelectN(&res, fmt.Sprintf(` select %s as item, @@ -90,7 +90,7 @@ func StatTeamTop(p map[string]interface{}) (res []StatItem) { } func StatTeamActivity(p map[string]interface{}) (res []StatPoint) { - defer measure("StatTeamActivity", time.Now()) + defer measure(time.Now(), "StatTeamActivity") mustSelectN(&res, fmt.Sprintf(` select %s as item, @@ -111,7 +111,7 @@ func StatTeamActivity(p map[string]interface{}) (res []StatPoint) { } func StatUserTop(p map[string]interface{}) (res []StatItem) { - defer measure("StatUserTop", time.Now()) + defer measure(time.Now(), "StatUserTop") mustSelectN(&res, ` select r.name as item, @@ -136,7 +136,7 @@ func StatUserTop(p map[string]interface{}) (res []StatItem) { } func StatUserActivity(p map[string]interface{}) (res []StatPoint) { - defer measure("StatUserActivity", time.Now()) + defer measure(time.Now(), "StatUserActivity") mustSelectN(&res, ` select c.week as week, @@ -162,7 +162,7 @@ func StatUserActivity(p map[string]interface{}) (res []StatPoint) { } func StatRepoTop(p map[string]interface{}) (res []StatItem) { - defer measure("StatRepoTop", time.Now()) + defer measure(time.Now(), "StatRepoTop") mustSelectN(&res, fmt.Sprintf(` select %s as item, @@ -182,7 +182,7 @@ func StatRepoTop(p map[string]interface{}) (res []StatItem) { } func StatRepoActivity(p map[string]interface{}) (res []StatPoint) { - defer measure("StatRepoActivity", time.Now()) + defer measure(time.Now(), "StatRepoActivity") mustSelectN(&res, fmt.Sprintf(` select c.week as week, diff --git a/db/team.go b/db/team.go index 2a280c9..aff9da7 100644 --- a/db/team.go +++ b/db/team.go @@ -14,7 +14,7 @@ type Team struct { } func (t *Team) Save() { - defer measure("SaveTeam", time.Now()) + defer measure(time.Now(), "SaveTeam") mustExecN(` insert into teams (id, org_id, slug, name, permission, updated_at) values (:id, :org_id, :slug, :name, :permission, now()) @@ -27,7 +27,7 @@ func (t *Team) Save() { } func OrgTeams(login string) (teams []*Team) { - defer measure("OrgTeams", time.Now()) + defer measure(time.Now(), "OrgTeams") mustSelect(&teams, ` select t.* from teams t diff --git a/db/token.go b/db/token.go index acfc4b4..d0dfa40 100644 --- a/db/token.go +++ b/db/token.go @@ -15,7 +15,7 @@ type Token struct { } func (t *Token) Save() { - defer measure("SaveToken", time.Now()) + defer measure(time.Now(), "SaveToken") mustExecN(` insert into tokens (user, token, quota, remaining, reset_at, created_at) values (:user, :token, :quota, :remaining, :reset_at, now()) diff --git a/db/user.go b/db/user.go index 7abd602..2ff04d1 100644 --- a/db/user.go +++ b/db/user.go @@ -13,7 +13,7 @@ type User struct { } func (u *User) Save() { - defer measure("SaveUser", time.Now()) + defer measure(time.Now(), "SaveUser") mustExecN(` insert into users (id, login, name, avatar_url, updated_at) values (:id, :login, :name, :avatar_url, now()) @@ -26,7 +26,7 @@ func (u *User) Save() { } func OrgUsers(login string) (users []*User) { - defer measure("OrgUsers", time.Now()) + defer measure(time.Now(), "OrgUsers") mustSelect(&users, ` select u.* from org_members om