Add leaderboard JSON

This commit is contained in:
Filip Gralinski 2020-10-15 22:27:16 +02:00
parent abaaf1c301
commit acedcef793
4 changed files with 32 additions and 1 deletions

View File

@ -153,6 +153,7 @@ instance Yesod App where
isAuthorized (QueryResultsR _) _ = regularAuthorization isAuthorized (QueryResultsR _) _ = regularAuthorization
isAuthorized ListChallengesR _ = regularAuthorization isAuthorized ListChallengesR _ = regularAuthorization
isAuthorized ListChallengesJsonR _ = regularAuthorization isAuthorized ListChallengesJsonR _ = regularAuthorization
isAuthorized (LeaderboardJsonR _) _ = regularAuthorization
isAuthorized (ViewVariantR _ ) _ = regularAuthorization isAuthorized (ViewVariantR _ ) _ = regularAuthorization
isAuthorized (ViewVariantTestR _ _) _ = regularAuthorization isAuthorized (ViewVariantTestR _ _) _ = regularAuthorization

View File

@ -16,9 +16,11 @@ getListArchivedChallengesR = generalListChallenges [ChallengeArchived ==. Just T
instance ToJSON (Entity Challenge) where instance ToJSON (Entity Challenge) where
toJSON (Entity _ ch) = object toJSON (Entity _ ch) = object
[ "link" .= ("/challenge/" <> (challengeName ch)) [ "name" .= challengeName ch
, "title" .= challengeTitle ch , "title" .= challengeTitle ch
, "description" .= challengeDescription ch , "description" .= challengeDescription ch
, "starred" .= challengeStarred ch
, "archived" .= challengeArchived ch
] ]
generalListChallengesJson :: [Filter Challenge] -> Handler Value generalListChallengesJson :: [Filter Challenge] -> Handler Value

View File

@ -48,6 +48,33 @@ import Data.List (nub)
import qualified Database.Esqueleto as E import qualified Database.Esqueleto as E
import Database.Esqueleto ((^.)) import Database.Esqueleto ((^.))
instance ToJSON LeaderboardEntry where
toJSON entry = object
[ "submitter" .= (formatSubmitter $ leaderboardUser entry)
, "when" .= (submissionStamp $ leaderboardBestSubmission entry)
, "version" .= (formatVersion $ leaderboardVersion entry)
, "description" .= descriptionToBeShown (leaderboardBestSubmission entry)
(leaderboardBestVariant entry)
(leaderboardParams entry)
, "times" .= leaderboardNumberOfSubmissions entry
]
getLeaderboardJsonR :: Text -> Handler Value
getLeaderboardJsonR name = do
app <- getYesod
let leaderboardStyle = appLeaderboardStyle $ appSettings app
Entity challengeId _ <- runDB $ getBy404 $ UniqueName name
(leaderboard, (_, tests)) <- getLeaderboardEntries 1 leaderboardStyle challengeId
return $ array $ map (leaderboardEntryJson tests) leaderboard
leaderboardEntryJson tests entry = object [
"metadata" .= entry,
"metrics" .=
map (\e@(Entity _ t) -> object [
"metric" .= testName t,
"score" .= (formatTruncatedScore (getTestFormattingOpts t) $ extractScoreFromLeaderboardEntry (getTestReference e) entry)]) tests]
getShowChallengeR :: Text -> Handler Html getShowChallengeR :: Text -> Handler Html
getShowChallengeR name = do getShowChallengeR name = do
app <- getYesod app <- getYesod

View File

@ -11,6 +11,7 @@
/open-view-progress/#Int OpenViewProgressR GET /open-view-progress/#Int OpenViewProgressR GET
/list-challenges ListChallengesR GET /list-challenges ListChallengesR GET
/api/list-challenges ListChallengesJsonR GET /api/list-challenges ListChallengesJsonR GET
/api/leaderboard/#Text LeaderboardJsonR GET
/list-archived-challenges ListArchivedChallengesR GET /list-archived-challenges ListArchivedChallengesR GET
/challenge-image/#ChallengeId ChallengeImageR GET /challenge-image/#ChallengeId ChallengeImageR GET