1
0
Fork 0

Refactor db ops measure

This commit is contained in:
Gregory Eremin 2015-03-21 21:58:40 +07:00
parent 0a21d10276
commit 68c1465c92
9 changed files with 25 additions and 28 deletions

View File

@ -15,7 +15,7 @@ type Contrib struct {
} }
func (c *Contrib) Save() { func (c *Contrib) Save() {
defer measure("SaveContrib", time.Now()) defer measure(time.Now(), "SaveContrib")
mustExecN(` mustExecN(`
insert into contribs (week, org_id, repo_id, user_id, commits, additions, deletions) insert into contribs (week, org_id, repo_id, user_id, commits, additions, deletions)
values (:week, :org_id, :repo_id, :user_id, :commits, :additions, :deletions) values (:week, :org_id, :repo_id, :user_id, :commits, :additions, :deletions)

View File

@ -11,7 +11,7 @@ import (
var ( var (
db *sqlx.DB db *sqlx.DB
queryQueue = make(chan func(), 1000) queryQueue = make(chan func())
) )
func Connect(params string) (err error) { func Connect(params string) (err error) {
@ -50,21 +50,18 @@ func Queue(fun func()) {
func processQueue() { func processQueue() {
for { for {
fun := <-queryQueue (<-queryQueue)()
fun()
} }
} }
func measure(op string, start time.Time) { func measure(start time.Time, op string) {
duration := time.Since(start).Nanoseconds() duration := time.Since(start).Nanoseconds()
outcome := "succeeded" outcome := "succeeded"
err := recover() err := recover()
if err != nil { if err != nil {
outcome = "failed" outcome = "failed"
defer panic(err)
} }
log.Printf("Operation %s %s; time: %d (%dms)\n", op, outcome, duration, duration/1000000) log.Printf("Operation %s %s; time: %d (%dms)\n", op, outcome, duration, duration/1000000)
if err != nil {
panic(err)
}
} }

View File

@ -8,7 +8,7 @@ import (
) )
func SaveOrgMembers(orgID int, members []int) { func SaveOrgMembers(orgID int, members []int) {
defer measure("SaveOrgMembers", time.Now()) defer measure(time.Now(), "SaveOrgMembers")
tx := db.MustBegin() tx := db.MustBegin()
var ids = []string{} var ids = []string{}
@ -37,7 +37,7 @@ func SaveOrgMembers(orgID int, members []int) {
} }
func SaveTeamMembers(orgID, teamID int, members []int) { func SaveTeamMembers(orgID, teamID int, members []int) {
defer measure("SaveTeamMembers", time.Now()) defer measure(time.Now(), "SaveTeamMembers")
tx := db.MustBegin() tx := db.MustBegin()
var ids = []string{} var ids = []string{}
@ -67,7 +67,7 @@ func SaveTeamMembers(orgID, teamID int, members []int) {
} }
func SaveTeamRepos(orgID, teamID int, repos []int) { func SaveTeamRepos(orgID, teamID int, repos []int) {
defer measure("SaveTeamRepos", time.Now()) defer measure(time.Now(), "SaveTeamRepos")
tx := db.MustBegin() tx := db.MustBegin()
var ids = []string{} var ids = []string{}

View File

@ -13,7 +13,7 @@ type Org struct {
} }
func (o *Org) Save() { func (o *Org) Save() {
defer measure("SaveOrg", time.Now()) defer measure(time.Now(), "SaveOrg")
mustExecN(` mustExecN(`
insert into orgs (id, login, company, avatar_url, updated_at) insert into orgs (id, login, company, avatar_url, updated_at)
values (:id, :login, :company, :avatar_url, now()) values (:id, :login, :company, :avatar_url, now())
@ -26,7 +26,7 @@ func (o *Org) Save() {
} }
func UserOrgs(login string) (orgs []*Org) { func UserOrgs(login string) (orgs []*Org) {
defer measure("UserOrgs", time.Now()) defer measure(time.Now(), "UserOrgs")
mustSelect(&orgs, ` mustSelect(&orgs, `
select o.* select o.*
from org_members m from org_members m

View File

@ -15,7 +15,7 @@ type Repo struct {
} }
func (r *Repo) Save() { func (r *Repo) Save() {
defer measure("SaveRepo", time.Now()) defer measure(time.Now(), "SaveRepo")
mustExecN(` mustExecN(`
insert into repos (org_id, name, description, is_private, is_fork, updated_at) insert into repos (org_id, name, description, is_private, is_fork, updated_at)
values (:org_id, :name, :description, :is_private, :is_fork, now()) values (:org_id, :name, :description, :is_private, :is_fork, now())
@ -30,7 +30,7 @@ func (r *Repo) Save() {
} }
func OrgRepos(login string) (repos []*Repo) { func OrgRepos(login string) (repos []*Repo) {
defer measure("OrgRepos", time.Now()) defer measure(time.Now(), "OrgRepos")
mustSelect(&repos, ` mustSelect(&repos, `
select * select *
from repos r from repos r

View File

@ -31,7 +31,7 @@ join repos r on
c.repo_id = r.id` c.repo_id = r.id`
func StatOrgTop(p map[string]interface{}) (res []StatItem) { func StatOrgTop(p map[string]interface{}) (res []StatItem) {
defer measure("StatOrgTop", time.Now()) defer measure(time.Now(), "StatOrgTop")
mustSelectN(&res, fmt.Sprintf(` mustSelectN(&res, fmt.Sprintf(`
select select
%s as item, %s as item,
@ -50,7 +50,7 @@ func StatOrgTop(p map[string]interface{}) (res []StatItem) {
} }
func StatOrgActivity(p map[string]interface{}) (res []StatPoint) { func StatOrgActivity(p map[string]interface{}) (res []StatPoint) {
defer measure("StatOrgActivity", time.Now()) defer measure(time.Now(), "StatOrgActivity")
mustSelectN(&res, fmt.Sprintf(` mustSelectN(&res, fmt.Sprintf(`
select select
%s as item, %s as item,
@ -70,7 +70,7 @@ func StatOrgActivity(p map[string]interface{}) (res []StatPoint) {
} }
func StatTeamTop(p map[string]interface{}) (res []StatItem) { func StatTeamTop(p map[string]interface{}) (res []StatItem) {
defer measure("StatTeamTop", time.Now()) defer measure(time.Now(), "StatTeamTop")
mustSelectN(&res, fmt.Sprintf(` mustSelectN(&res, fmt.Sprintf(`
select select
%s as item, %s as item,
@ -90,7 +90,7 @@ func StatTeamTop(p map[string]interface{}) (res []StatItem) {
} }
func StatTeamActivity(p map[string]interface{}) (res []StatPoint) { func StatTeamActivity(p map[string]interface{}) (res []StatPoint) {
defer measure("StatTeamActivity", time.Now()) defer measure(time.Now(), "StatTeamActivity")
mustSelectN(&res, fmt.Sprintf(` mustSelectN(&res, fmt.Sprintf(`
select select
%s as item, %s as item,
@ -111,7 +111,7 @@ func StatTeamActivity(p map[string]interface{}) (res []StatPoint) {
} }
func StatUserTop(p map[string]interface{}) (res []StatItem) { func StatUserTop(p map[string]interface{}) (res []StatItem) {
defer measure("StatUserTop", time.Now()) defer measure(time.Now(), "StatUserTop")
mustSelectN(&res, ` mustSelectN(&res, `
select select
r.name as item, r.name as item,
@ -136,7 +136,7 @@ func StatUserTop(p map[string]interface{}) (res []StatItem) {
} }
func StatUserActivity(p map[string]interface{}) (res []StatPoint) { func StatUserActivity(p map[string]interface{}) (res []StatPoint) {
defer measure("StatUserActivity", time.Now()) defer measure(time.Now(), "StatUserActivity")
mustSelectN(&res, ` mustSelectN(&res, `
select select
c.week as week, c.week as week,
@ -162,7 +162,7 @@ func StatUserActivity(p map[string]interface{}) (res []StatPoint) {
} }
func StatRepoTop(p map[string]interface{}) (res []StatItem) { func StatRepoTop(p map[string]interface{}) (res []StatItem) {
defer measure("StatRepoTop", time.Now()) defer measure(time.Now(), "StatRepoTop")
mustSelectN(&res, fmt.Sprintf(` mustSelectN(&res, fmt.Sprintf(`
select select
%s as item, %s as item,
@ -182,7 +182,7 @@ func StatRepoTop(p map[string]interface{}) (res []StatItem) {
} }
func StatRepoActivity(p map[string]interface{}) (res []StatPoint) { func StatRepoActivity(p map[string]interface{}) (res []StatPoint) {
defer measure("StatRepoActivity", time.Now()) defer measure(time.Now(), "StatRepoActivity")
mustSelectN(&res, fmt.Sprintf(` mustSelectN(&res, fmt.Sprintf(`
select select
c.week as week, c.week as week,

View File

@ -14,7 +14,7 @@ type Team struct {
} }
func (t *Team) Save() { func (t *Team) Save() {
defer measure("SaveTeam", time.Now()) defer measure(time.Now(), "SaveTeam")
mustExecN(` mustExecN(`
insert into teams (id, org_id, slug, name, permission, updated_at) insert into teams (id, org_id, slug, name, permission, updated_at)
values (:id, :org_id, :slug, :name, :permission, now()) values (:id, :org_id, :slug, :name, :permission, now())
@ -27,7 +27,7 @@ func (t *Team) Save() {
} }
func OrgTeams(login string) (teams []*Team) { func OrgTeams(login string) (teams []*Team) {
defer measure("OrgTeams", time.Now()) defer measure(time.Now(), "OrgTeams")
mustSelect(&teams, ` mustSelect(&teams, `
select t.* select t.*
from teams t from teams t

View File

@ -15,7 +15,7 @@ type Token struct {
} }
func (t *Token) Save() { func (t *Token) Save() {
defer measure("SaveToken", time.Now()) defer measure(time.Now(), "SaveToken")
mustExecN(` mustExecN(`
insert into tokens (user, token, quota, remaining, reset_at, created_at) insert into tokens (user, token, quota, remaining, reset_at, created_at)
values (:user, :token, :quota, :remaining, :reset_at, now()) values (:user, :token, :quota, :remaining, :reset_at, now())

View File

@ -13,7 +13,7 @@ type User struct {
} }
func (u *User) Save() { func (u *User) Save() {
defer measure("SaveUser", time.Now()) defer measure(time.Now(), "SaveUser")
mustExecN(` mustExecN(`
insert into users (id, login, name, avatar_url, updated_at) insert into users (id, login, name, avatar_url, updated_at)
values (:id, :login, :name, :avatar_url, now()) values (:id, :login, :name, :avatar_url, now())
@ -26,7 +26,7 @@ func (u *User) Save() {
} }
func OrgUsers(login string) (users []*User) { func OrgUsers(login string) (users []*User) {
defer measure("OrgUsers", time.Now()) defer measure(time.Now(), "OrgUsers")
mustSelect(&users, ` mustSelect(&users, `
select u.* select u.*
from org_members om from org_members om