Skip to content

Commit

Permalink
Update left join to inner join where appropriate; improve error logs …
Browse files Browse the repository at this point in the history
…and remove raven client (#61)

* Update left join to inner join where appropriate

* Adjust logs and remove raven client
  • Loading branch information
cmyui authored Jun 24, 2024
1 parent 6de9676 commit 66d3af1
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 73 deletions.
2 changes: 1 addition & 1 deletion app/peppy/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func GetUser(c *fasthttp.RequestCtx, db *sqlx.DB) {
user_stats.playcount, user_stats.ranked_score, user_stats.total_score,
user_stats.pp, user_stats.avg_accuracy
FROM users
LEFT JOIN user_stats ON user_stats.user_id = users.id
INNER JOIN user_stats ON user_stats.user_id = users.id
%[1]s
LIMIT 1`,
whereClause,
Expand Down
4 changes: 2 additions & 2 deletions app/peppy/user_x.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ func getUserX(c *fasthttp.RequestCtx, db *sqlx.DB, orderBy string, limit int) {
%[1]s.full_combo, %[1]s.mods, users.id, %[1]s.time,
%[1]s.pp, %[1]s.accuracy
FROM %[1]s
LEFT JOIN beatmaps ON beatmaps.beatmap_md5 = %[1]s.beatmap_md5
LEFT JOIN users ON %[1]s.userid = users.id
INNER JOIN beatmaps ON beatmaps.beatmap_md5 = %[1]s.beatmap_md5
INNER JOIN users ON %[1]s.userid = users.id
WHERE %[2]s AND %[1]s.play_mode = ? AND users.privileges & 1 > 0
%[3]s
LIMIT %[4]d`, table, whereClause, orderBy, limit,
Expand Down
5 changes: 3 additions & 2 deletions app/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"crypto/sha256"
"database/sql"
"fmt"
"golang.org/x/exp/slog"
"time"

"golang.org/x/exp/slog"

"github.com/jmoiron/sqlx"
"github.com/osuAkatsuki/akatsuki-api/common"
)
Expand All @@ -23,7 +24,7 @@ func GetTokenFull(token string, db *sqlx.DB) (common.Token, bool) {
err := db.QueryRow(`SELECT
t.id, t.user, t.privileges, t.private, u.privileges
FROM tokens t
LEFT JOIN users u ON u.id = t.user
INNER JOIN users u ON u.id = t.user
WHERE token = ? LIMIT 1`,
fmt.Sprintf("%x", md5.Sum([]byte(token)))).
Scan(
Expand Down
6 changes: 3 additions & 3 deletions app/v1/clan.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func ClanLeaderboardGET(md common.MethodData) common.CodeMessager {
SUM(total_score), SUM(playcount),
AVG(avg_accuracy), clans.name, clans.id
FROM user_stats
LEFT JOIN users ON users.id = user_stats.user_id
INNER JOIN users ON users.id = user_stats.user_id
INNER JOIN clans ON clans.id = users.clan_id
WHERE users.clan_id <> 0
AND user_stats.mode = ?
Expand Down Expand Up @@ -168,7 +168,7 @@ func ClanStatsGET(md common.MethodData) common.CodeMessager {
q := `SELECT SUM(pp) / (COUNT(users.clan_id) + 1) AS pp, SUM(ranked_score),
SUM(total_score), SUM(playcount), SUM(replays_watched),
AVG(avg_accuracy), SUM(total_hits)
FROM user_stats LEFT JOIN users ON users.id = user_stats.user_id
FROM user_stats INNER JOIN users ON users.id = user_stats.user_id
WHERE users.clan_id = ? AND user_stats.mode = ? AND users.privileges & 1`
var pp float64
err = md.DB.QueryRow(q, id, mode+(relax*4)).Scan(
Expand All @@ -187,7 +187,7 @@ func ClanStatsGET(md common.MethodData) common.CodeMessager {
SELECT COUNT(pp)
FROM (
SELECT SUM(pp) / (COUNT(clan_id) + 1) AS pp
FROM user_stats LEFT JOIN users ON users.id = user_stats.user_id
FROM user_stats INNER JOIN users ON users.id = user_stats.user_id
WHERE clan_id <> 0
AND user_stats.mode = ?
AND (users.privileges & 3) >= 3
Expand Down
4 changes: 2 additions & 2 deletions app/v1/friend.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type friendsGETResponse struct {
// It retrieves an user's friends, and whether the friendship is mutual or not.
func FriendsGET(md common.MethodData) common.CodeMessager {
var myFrienders []int
myFriendersRaw, err := md.DB.Query("SELECT user1 FROM users_relationships LEFT JOIN users ON users_relationships.user1 = users.id WHERE user2 = ? AND privileges & 1", md.ID())
myFriendersRaw, err := md.DB.Query("SELECT user1 FROM users_relationships INNER JOIN users ON users_relationships.user1 = users.id WHERE user2 = ? AND privileges & 1", md.ID())
if err != nil {
md.Err(err)
return Err500
Expand All @@ -44,7 +44,7 @@ SELECT
users.id, users.username, users.register_datetime, users.privileges, users.latest_activity,
users.username_aka, users.country
FROM users_relationships
LEFT JOIN users
INNER JOIN users
ON users_relationships.user2 = users.id
WHERE users_relationships.user1 = ?
AND privileges & 1
Expand Down
6 changes: 3 additions & 3 deletions app/v1/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func UserFullGET(md common.MethodData) common.CodeMessager {
user_stats.replays_watched, user_stats.total_hits,
user_stats.avg_accuracy, user_stats.pp, user_stats.max_combo
FROM user_stats
LEFT JOIN users ON users.id = user_stats.user_id
INNER JOIN users ON users.id = user_stats.user_id
WHERE ` + whereClause + ` AND ` + md.User.OnlyUserPublic(true) + ` AND user_stats.mode = ?
`
for _, relaxMode := range []int{0, 1, 2} {
Expand Down Expand Up @@ -386,7 +386,7 @@ func UserFullGET(md common.MethodData) common.CodeMessager {
r.Followers = follower

rows, err = md.DB.Query("SELECT b.id, b.name, b.icon, b.colour FROM user_badges ub "+
"LEFT JOIN badges b ON ub.badge = b.id WHERE user = ?", r.ID)
"INNER JOIN badges b ON ub.badge = b.id WHERE user = ?", r.ID)
if err != nil {
md.Err(err)
}
Expand All @@ -408,7 +408,7 @@ func UserFullGET(md common.MethodData) common.CodeMessager {
}

rows, err = md.DB.Query("SELECT tb.id, tb.name, tb.icon FROM user_tourmnt_badges tub "+
"LEFT JOIN tourmnt_badges tb ON tub.badge = tb.id WHERE user = ?", r.ID)
"INNER JOIN tourmnt_badges tb ON tub.badge = tb.id WHERE user = ?", r.ID)
if err != nil {
md.Err(err)
}
Expand Down
59 changes: 8 additions & 51 deletions common/method_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package common
import (
"encoding/json"
"runtime"
"strconv"
"strings"

"github.com/getsentry/raven-go"
"github.com/jmoiron/sqlx"
"github.com/valyala/fasthttp"
"golang.org/x/exp/slog"
Expand Down Expand Up @@ -39,21 +37,15 @@ func (md MethodData) ClientIP() string {
return md.Ctx.RemoteIP().String()
}

// Err logs an error. If RavenClient is set, it will use the client to report
// the error to sentry, otherwise it will just write the error to stdout.
// Err logs to stdout when there is an error
func (md MethodData) Err(err error) {
user := &raven.User{
ID: strconv.Itoa(md.User.UserID),
Username: md.User.Value,
IP: md.Ctx.RemoteAddr().String(),
}
// Generate tags for error
tags := map[string]string{
"endpoint": string(md.Ctx.RequestURI()),
"token": md.User.Value,
}

_err(err, tags, user, md.Ctx)
_err(err, tags, md.Ctx)
}

// Err for peppy API calls
Expand All @@ -63,62 +55,27 @@ func Err(c *fasthttp.RequestCtx, err error) {
"endpoint": string(c.RequestURI()),
}

_err(err, tags, nil, c)
_err(err, tags, c)
}

// WSErr is the error function for errors happening in the websockets.
func WSErr(err error) {
_err(err, map[string]string{
"endpoint": "/api/v1/ws",
}, nil, nil)
}, nil)
}

// GenericError is just an error. Can't make a good description.
func GenericError(err error) {
_err(err, nil, nil, nil)
_err(err, nil, nil)
}

func _err(err error, tags map[string]string, user *raven.User, c *fasthttp.RequestCtx) {
func _err(err error, tags map[string]string, c *fasthttp.RequestCtx) {
_, file, no, ok := runtime.Caller(2)
if ok {
slog.Error("An error occurred", "filename", file, "line", no, "error", err.Error())
slog.ErrorContext(c, "An error occurred", "filename", file, "line", no, "error", err.Error())
} else {
slog.Error("An error occurred", "error", err.Error())
}
}

func generateRavenHTTP(ctx *fasthttp.RequestCtx) *raven.Http {
if ctx == nil {
return nil
}

// build uri
uri := ctx.URI()
// safe to use b2s because a new string gets allocated eventually for
// concatenation
sURI := b2s(uri.Scheme()) + "://" + b2s(uri.Host()) + b2s(uri.Path())

// build header map
// using ctx.Request.Header.Len would mean calling .VisitAll two times
// which can be quite expensive since it means iterating over all the
// headers, so we give a rough estimate of the number of headers we expect
// to have
m := make(map[string]string, 16)
ctx.Request.Header.VisitAll(func(k, v []byte) {
// not using b2s because we mustn't keep references to the underlying
// k and v
m[string(k)] = string(v)
})

return &raven.Http{
URL: sURI,
// Not using b2s because raven sending is concurrent and may happen
// AFTER the request, meaning that values could potentially be replaced
// by new ones.
Method: string(ctx.Method()),
Query: string(uri.QueryString()),
Cookies: string(ctx.Request.Header.Peek("Cookie")),
Headers: m,
slog.ErrorContext(c, "An error occurred", "error", err.Error())
}
}

Expand Down
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.20

require (
github.com/buaazp/fasthttprouter v0.1.1
github.com/getsentry/raven-go v0.2.0
github.com/go-sql-driver/mysql v1.6.0
github.com/jmoiron/sqlx v1.3.4
github.com/joho/godotenv v1.5.1
Expand All @@ -23,13 +22,11 @@ require (
require (
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/klauspost/compress v1.15.0 // indirect
github.com/onsi/ginkgo v1.16.4 // indirect
github.com/onsi/gomega v1.13.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa
golang.org/x/net v0.10.0 // indirect
Expand Down
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@ github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuP
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
github.com/buaazp/fasthttprouter v0.1.1 h1:4oAnN0C3xZjylvZJdP35cxfclyn4TYkW6Y+DSvS+h8Q=
github.com/buaazp/fasthttprouter v0.1.1/go.mod h1:h/Ap5oRVLeItGKTVBb+heQPks+HdIUtGmI4H5WCYijM=
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d h1:S2NE3iHSwP0XV47EEXL8mWmRdEfGscSJ+7EgePNgt0s=
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d/go.mod h1:sGbDF6GwGcLpkNXPUTkMRoywsNa/ol15pxFe6ERfguA=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/getsentry/raven-go v0.2.0 h1:no+xWJRb5ZI7eE8TWgIq1jLulQiIoLG0IfYxv5JYMGs=
github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
Expand Down Expand Up @@ -61,8 +57,6 @@ github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7J
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.13.0 h1:7lLHu94wT9Ij0o6EWWclhu0aOh32VxhkwEJvzuWPeak=
github.com/onsi/gomega v1.13.0/go.mod h1:lRk9szgn8TxENtWd0Tp4c3wjlRfMTMH27I+3Je41yGY=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e h1:zWKUYT07mGmVBH+9UgnHXd/ekCK99C8EbDSAt5qsjXE=
github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e/go.mod h1:Yow6lPLSAXx2ifx470yD/nUe22Dv5vBvxK/UK9UUTVs=
Expand Down

0 comments on commit 66d3af1

Please sign in to comment.