Add initial beatmap reqeusts API to get current status

This commit is contained in:
Howl 2016-09-06 20:11:46 +02:00
parent 6eda6aea87
commit dd00c39075
4 changed files with 87 additions and 22 deletions

View File

@ -84,6 +84,7 @@ func Start(conf common.Conf, dbO *sqlx.DB) *gin.Engine {
gv1.GET("/users/scores/recent", Method(v1.UserScoresRecentGET))
gv1.GET("/badges", Method(v1.BadgesGET))
gv1.GET("/beatmaps", Method(v1.BeatmapGET))
gv1.GET("/beatmaps/rank_requests/status", Method(v1.BeatmapRankRequestsStatusGET))
gv1.GET("/leaderboard", Method(v1.LeaderboardGET))
gv1.GET("/tokens", Method(v1.TokenGET))
gv1.GET("/users/self", Method(v1.UserSelfGET))
@ -122,12 +123,12 @@ func Start(conf common.Conf, dbO *sqlx.DB) *gin.Engine {
common.PrivilegeManageUser, common.PrivilegeAPIMeta))
// in the new osu-web, the old endpoints are also in /v1 it seems. So /shrug
p.GET("/get_user", PeppyMethod(peppy.GetUser))
p.GET("/get_match", PeppyMethod(peppy.GetMatch))
p.GET("/get_user_recent", PeppyMethod(peppy.GetUserRecent))
p.GET("/get_user_best", PeppyMethod(peppy.GetUserBest))
p.GET("/get_scores", PeppyMethod(peppy.GetScores))
p.GET("/get_beatmaps", PeppyMethod(peppy.GetBeatmap))
gv1.GET("/get_user", PeppyMethod(peppy.GetUser))
gv1.GET("/get_match", PeppyMethod(peppy.GetMatch))
gv1.GET("/get_user_recent", PeppyMethod(peppy.GetUserRecent))
gv1.GET("/get_user_best", PeppyMethod(peppy.GetUserBest))
gv1.GET("/get_scores", PeppyMethod(peppy.GetScores))
gv1.GET("/get_beatmaps", PeppyMethod(peppy.GetBeatmap))
}
api.GET("/status", internals.Status)

View File

@ -0,0 +1,55 @@
package v1
import (
"strconv"
"time"
"git.zxq.co/ripple/rippleapi/common"
)
type rankRequestsStatusResponse struct {
common.ResponseBase
QueueSize int `json:"queue_size"`
MaxPeruser int `json:"max_per_user"`
Submitted int `json:"submitted"`
SubmittedByUser int `json:"submitted_by_user"`
CanSubmit bool `json:"can_submit"`
NextExpiration *time.Time `json:"next_expiration"`
}
// BeatmapRankRequestsStatusGET gets the current status for beatmap ranking requests.
func BeatmapRankRequestsStatusGET(md common.MethodData) common.CodeMessager {
c := common.GetConf()
rows, err := md.DB.Query("SELECT userid, time FROM rank_requests WHERE time > ? ORDER BY id ASC LIMIT "+strconv.Itoa(c.RankQueueSize), time.Now().Add(-time.Hour*24).Unix())
if err != nil {
md.Err(err)
return Err500
}
var r rankRequestsStatusResponse
isFirst := true
for rows.Next() {
var (
user int
timestamp common.UnixTimestamp
)
err := rows.Scan(&user, &timestamp)
if err != nil {
md.Err(err)
continue
}
if user == md.ID() {
r.SubmittedByUser++
}
if isFirst {
x := time.Time(timestamp)
r.NextExpiration = &x
isFirst = false
}
r.Submitted++
}
r.QueueSize = c.RankQueueSize
r.MaxPeruser = c.BeatmapRequestsPerUser
r.CanSubmit = r.Submitted < r.QueueSize && r.SubmittedByUser < r.MaxPeruser
r.Code = 200
return r
}

View File

@ -1,5 +0,0 @@
package v1
func init() {
go removeUseless()
}

View File

@ -19,6 +19,8 @@ type Conf struct {
Unix bool `description:"Bool indicating whether ListenTo is a UNIX socket or an address."`
SentryDSN string `description:"thing for sentry whatever"`
HanayoKey string
BeatmapRequestsPerUser int
RankQueueSize int
}
var cachedConf *Conf
@ -38,9 +40,21 @@ func Load() (c Conf, halt bool) {
ListenTo: ":40001",
Unix: false,
HanayoKey: "Potato",
BeatmapRequestsPerUser: 2,
RankQueueSize: 25,
}, "api.conf")
fmt.Println("Please compile the configuration file (api.conf).")
}
cachedConf = &c
return
}
// GetConf returns the cachedConf.
func GetConf() *Conf {
if cachedConf == nil {
return nil
}
// so that the cachedConf cannot actually get modified
c := *cachedConf
return &c
}