Skip to content

Commit

Permalink
Added support for importing leaderboard csv
Browse files Browse the repository at this point in the history
  • Loading branch information
mellelieuwes committed Jun 21, 2023
1 parent eca871d commit 42dab12
Show file tree
Hide file tree
Showing 17 changed files with 716 additions and 49 deletions.
6 changes: 6 additions & 0 deletions core/assets/tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ module.exports = {
30: "120px",
34: "136px",
35: "140px",
border: "2px",
"action_menu-width": "180px",
"tablet-menu-width": "72px",
"desktop-menu-width": "296px",
Expand Down Expand Up @@ -141,6 +142,7 @@ module.exports = {
"image-preview-circle-sm": "150px",
"campaign-banner": "224px",
"button-sm": "14px",
"file-selector": "96px",
},
fontFamily: {
title0: ["Finador-Black", "sans-serif"],
Expand All @@ -160,6 +162,8 @@ module.exports = {
label: ["Finador-Bold", "sans-serif"],
body: ["Finador-Light", "sans-serif"],
hint: ["Finador-LightOblique", "sans-serif"],
tablehead: ["Finador-Bold", "sans-serif"],
tablerow: ["Finador-Regular", "sans-serif"],
},
fontSize: {
title0: ["64px", "68px"],
Expand Down Expand Up @@ -187,6 +191,8 @@ module.exports = {
bodylinkmedium: ["30px", "30px"],
link: ["16px", "24px"],
hint: ["20px", "24px"],
tablehead: ["14px", "16px"],
tablerow: ["14px", "16px"],
},
minWidth: {
"1/2": "50%",
Expand Down
8 changes: 4 additions & 4 deletions core/bundles/next/lib/user/signin.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ defmodule Next.User.Signin do
field = String.to_atom(checkbox)

new_value =
case Ecto.Changeset.fetch_field(changeset, field) |> IO.inspect(label: "FETCH") do
case Ecto.Changeset.fetch_field(changeset, field) do
:error -> true
value -> not value
{:changes, value} -> not value
{:data, value} -> not value
end

changeset =
Ecto.Changeset.cast(changeset, %{field => new_value}, [field]) |> IO.inspect(label: "OEPS")
changeset = Ecto.Changeset.cast(changeset, %{field => new_value}, [field])

{:noreply, socket |> assign(changeset: changeset)}
end
Expand Down
2 changes: 1 addition & 1 deletion core/config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ config :core,
]

config :core, :rate,
prune_interval: 5 * 1000,
prune_interval: 5 * 60 * 1000,
quotas: [
[service: :azure_blob, limit: 1, unit: :call, window: :second, scope: :local],
[service: :azure_blob, limit: 100, unit: :byte, window: :second, scope: :local]
Expand Down
22 changes: 22 additions & 0 deletions core/frameworks/pixel/components/text.ex
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,28 @@ defmodule Frameworks.Pixel.Text do
"""
end

attr(:color, :string, default: "text-grey1")
slot(:inner_block, required: true)

def table_head(assigns) do
~H"""
<div class={"text-tablehead font-tablehead"}>
<%= render_slot(@inner_block) %>
</div>
"""
end

attr(:color, :string, default: "text-grey1")
slot(:inner_block, required: true)

def table_row(assigns) do
~H"""
<div class={"text-tablerow font-tablerow"}>
<%= render_slot(@inner_block) %>
</div>
"""
end

attr(:color, :string, default: "text-grey1")
attr(:margin, :string, default: "mb-7 lg:mb-9")
slot(:inner_block, required: true)
Expand Down
40 changes: 36 additions & 4 deletions core/priv/gettext/en/LC_MESSAGES/eyra-benchmark.po
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ msgid "content.title"
msgstr "Challenge"

#, elixir-autogen, elixir-format
msgid "tabbar.item.leaderboards"
msgstr "Leaderboards"
msgid "tabbar.item.leaderboard"
msgstr "Leaderboard"

#, elixir-autogen, elixir-format
msgid "tabbar.item.leaderboards.forward"
msgstr "Leaderboards"
msgid "tabbar.item.leaderboard.forward"
msgstr "Leaderboard"

#, elixir-autogen, elixir-format
msgid "form.data_set.label"
Expand Down Expand Up @@ -160,3 +160,35 @@ msgstr "Provide a team name that will be posted with your submissions on the lea
#, elixir-autogen, elixir-format, fuzzy
msgid "spot.form.name"
msgstr "Name"

#, elixir-autogen, elixir-format
msgid "tabbar.item.submissions"
msgstr "Submissions"

#, elixir-autogen, elixir-format
msgid "tabbar.item.submissions.forward"
msgstr "Submissions"

#, elixir-autogen, elixir-format
msgid "import.leaderboard.title"
msgstr "Import"

#, elixir-autogen, elixir-format
msgid "leaderboard.latest.title"
msgstr "Latest version"

#, elixir-autogen, elixir-format
msgid "csv-import-button"
msgstr "Import selected file"

#, elixir-autogen, elixir-format
msgid "csv-replace-file-button"
msgstr "Replace file"

#, elixir-autogen, elixir-format
msgid "csv-select-file-button"
msgstr "Select file"

#, elixir-autogen, elixir-format
msgid "csv-select-placeholder"
msgstr "Select a leaderboard csv file"
36 changes: 34 additions & 2 deletions core/priv/gettext/eyra-benchmark.pot
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ msgid "content.title"
msgstr ""

#, elixir-autogen, elixir-format
msgid "tabbar.item.leaderboards"
msgid "tabbar.item.leaderboard"
msgstr ""

#, elixir-autogen, elixir-format
msgid "tabbar.item.leaderboards.forward"
msgid "tabbar.item.leaderboard.forward"
msgstr ""

#, elixir-autogen, elixir-format
Expand Down Expand Up @@ -160,3 +160,35 @@ msgstr ""
#, elixir-autogen, elixir-format
msgid "spot.form.name"
msgstr ""

#, elixir-autogen, elixir-format
msgid "tabbar.item.submissions"
msgstr ""

#, elixir-autogen, elixir-format
msgid "tabbar.item.submissions.forward"
msgstr ""

#, elixir-autogen, elixir-format
msgid "import.leaderboard.title"
msgstr ""

#, elixir-autogen, elixir-format
msgid "leaderboard.latest.title"
msgstr ""

#, elixir-autogen, elixir-format
msgid "csv-import-button"
msgstr ""

#, elixir-autogen, elixir-format
msgid "csv-replace-file-button"
msgstr ""

#, elixir-autogen, elixir-format
msgid "csv-select-file-button"
msgstr ""

#, elixir-autogen, elixir-format
msgid "csv-select-placeholder"
msgstr ""
40 changes: 36 additions & 4 deletions core/priv/gettext/nl/LC_MESSAGES/eyra-benchmark.po
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ msgid "content.title"
msgstr "Challenge"

#, elixir-autogen, elixir-format
msgid "tabbar.item.leaderboards"
msgstr "Ranglijsten"
msgid "tabbar.item.leaderboard"
msgstr "Ranglijst"

#, elixir-autogen, elixir-format
msgid "tabbar.item.leaderboards.forward"
msgstr "Ranglijsten"
msgid "tabbar.item.leaderboard.forward"
msgstr "Ranglijst"

#, elixir-autogen, elixir-format
msgid "form.data_set.label"
Expand Down Expand Up @@ -160,3 +160,35 @@ msgstr "Geef een team naam op die op het scoreboard bij jouw ingediende methodes
#, elixir-autogen, elixir-format, fuzzy
msgid "spot.form.name"
msgstr "Naam"

#, elixir-autogen, elixir-format
msgid "tabbar.item.submissions"
msgstr "Methodes"

#, elixir-autogen, elixir-format
msgid "tabbar.item.submissions.forward"
msgstr "Methodes"

#, elixir-autogen, elixir-format
msgid "import.leaderboard.title"
msgstr "Importeren"

#, elixir-autogen, elixir-format
msgid "leaderboard.latest.title"
msgstr "Laatste versie"

#, elixir-autogen, elixir-format
msgid "csv-import-button"
msgstr "Importeer gekozen bestand"

#, elixir-autogen, elixir-format
msgid "csv-replace-file-button"
msgstr "Vervang bestand"

#, elixir-autogen, elixir-format
msgid "csv-select-file-button"
msgstr "Kies bestand"

#, elixir-autogen, elixir-format
msgid "csv-select-placeholder"
msgstr "Kies een ranglijst csv bestand"
114 changes: 108 additions & 6 deletions core/systems/benchmark/_public.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule Systems.Benchmark.Public do
import Ecto.Query, warn: false

alias CoreWeb.UI.Timestamp
alias Ecto.Multi
alias Ecto.Changeset
alias Core.Repo
Expand All @@ -11,7 +12,17 @@ defmodule Systems.Benchmark.Public do
}

def get_tool!(id, preload \\ []) do
from(b in Benchmark.ToolModel, preload: ^preload)
from(tool in Benchmark.ToolModel, preload: ^preload)
|> Repo.get!(id)
end

def get_submission!(id, preload \\ []) do
from(submission in Benchmark.SubmissionModel, preload: ^preload)
|> Repo.get!(id)
end

def get_spot!(id, preload \\ []) do
from(spot in Benchmark.SpotModel, preload: ^preload)
|> Repo.get!(id)
end

Expand All @@ -26,11 +37,6 @@ defmodule Systems.Benchmark.Public do
|> set_tool_status(status)
end

def get_spot!(id, preload \\ []) do
from(s in Benchmark.SpotModel, preload: ^preload)
|> Repo.get!(id)
end

def create(
%{title: _, director: _} = attrs,
%Authorization.Node{} = auth_node
Expand Down Expand Up @@ -74,6 +80,70 @@ defmodule Systems.Benchmark.Public do
)
end

def import(entries, tool_id) when is_list(entries) do
tool = get_tool!(tool_id)

names =
entries
|> List.first(%{scores: []})
|> Map.get(:scores)
|> Enum.map(& &1.name)

Multi.new()
|> prepare_leaderboards(tool, names)
|> prepare_leaderboard_entries(entries)
|> Repo.transaction()
end

defp prepare_leaderboards(multi, _, []), do: multi

defp prepare_leaderboards(multi, tool, names) when is_list(names) do
date = Timestamp.format_user_input_date(Timestamp.now())

multi =
Multi.run(multi, :version, fn _, _ ->
count = Benchmark.Public.count_leaderboard_versions()
{:ok, "#{date}_#{count + 1}"}
end)

Enum.reduce(names, multi, &prepare_leaderboard(&2, &1, tool))
end

defp prepare_leaderboard(multi, name, tool) when is_binary(name) do
Multi.insert(multi, {:leaderboard, name}, fn %{version: version} ->
%Benchmark.LeaderboardModel{}
|> Benchmark.LeaderboardModel.changeset(%{name: name, version: version})
|> Ecto.Changeset.put_assoc(:tool, tool)
end)
end

defp prepare_leaderboard_entries(multi, entries) when is_list(entries) do
Enum.reduce(entries, multi, &prepare_leaderboard_entry(&2, &1))
end

defp prepare_leaderboard_entry(multi, %{submission_id: submission_id, scores: scores})
when is_list(scores) do
prepare_scores(multi, scores, submission_id)
end

defp prepare_scores(multi, scores, submission_id) when is_list(scores) do
Enum.reduce(scores, multi, &prepare_score(&2, &1, submission_id))
end

defp prepare_score(multi, %{name: name, score: score}, submission_id) do
Multi.insert(multi, {:score, "#{name}-#{submission_id}"}, fn %{
{:leaderboard, ^name} =>
leaderboard
} ->
submission = get_submission!(submission_id)

%Benchmark.ScoreModel{}
|> Benchmark.ScoreModel.changeset(%{score: score})
|> Ecto.Changeset.put_assoc(:leaderboard, leaderboard)
|> Ecto.Changeset.put_assoc(:submission, submission)
end)
end

def list_spots_for_tool(user, tool_id, preload \\ []) do
node_ids =
Authorization.query_node_ids(
Expand Down Expand Up @@ -113,6 +183,38 @@ defmodule Systems.Benchmark.Public do
|> Repo.all()
end

def list_leaderboard_categories(tool_id, preload \\ []) do
max_version =
from(leaderboard in Benchmark.LeaderboardModel,
where: leaderboard.tool_id == ^tool_id,
select: max(leaderboard.version)
)
|> Repo.one()

if max_version do
from(leaderboard in Benchmark.LeaderboardModel,
where: leaderboard.tool_id == ^tool_id,
where: leaderboard.version == ^max_version,
preload: ^preload
)
|> Repo.all()
else
[]
end
end

def count_leaderboard_versions() do
list =
from(leaderboard in Benchmark.LeaderboardModel,
group_by: leaderboard.version,
distinct: leaderboard.version,
select: count("*")
)
|> Repo.all()

Enum.count(list)
end

def delete(%Benchmark.SubmissionModel{} = submission) do
Repo.delete(submission)
end
Expand Down
Loading

0 comments on commit 42dab12

Please sign in to comment.