Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add client for erdbeere #748

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
"cospeakers",
"datetime",
"dockerfiles",
"Erdbeere",
"factorybot",
"helpdesk",
"justfile",
Expand Down
23 changes: 11 additions & 12 deletions app/controllers/erdbeere_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,31 @@ def current_ability
end

def show_example
response = Faraday.get(ENV.fetch("ERDBEERE_API") + "/examples/#{params[:id]}")
response = Clients::ErdbeereClient.get("examples/#{params[:id]}")
fosterfarrell9 marked this conversation as resolved.
Show resolved Hide resolved
@content = if response.status == 200
JSON.parse(response.body)["embedded_html"]
else
"Something went wrong."
I18n.t("erdbeere.error")
end
end

def show_property
response = Faraday.get(ENV.fetch("ERDBEERE_API") + "/properties/#{params[:id]}")
response = Clients::ErdbeereClient.get("properties/#{params[:id]}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If an endpoint cannot be reached (e.g. locally kill the erdbeere docker container), we get an error on this page: http://localhost:3000/lectures/1/show_structures

In this case, we could show a nice error message to the user saying that the erdbeere server that hosts the examples is unfortunately not reachable at the moment, otherwise we might be spammed with these errors in our email box and it's not the best experience for the user neither. In addition, we could be informed by email via the erdbeere service itself that it is down.

Or we might reserve this better error handling for later and just get erdbeere up and running for now ;)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've verified that error handling is graceful in case of editing a lecture via http://localhost:3000/lectures/1/edit#info

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added better error handling in the latest commit. Please check if it works.


@content = if response.status == 200
JSON.parse(response.body)["embedded_html"]
else
"Something went wrong."
I18n.t("erdbeere.error")
end
end

def show_structure
params[:id]
response = Faraday.get(ENV.fetch("ERDBEERE_API") + "/structures/#{params[:id]}")
response = Clients::ErdbeereClient.get("structures/#{params[:id]}")
@content = if response.status == 200
JSON.parse(response.body)["embedded_html"]
else
"Something went wrong."
I18n.t("erdbeere.error")
end
end

Expand All @@ -51,11 +51,10 @@ def cancel_edit_tags
def display_info
@id = params[:id]
@sort = params[:sort]
response = Faraday.get(ENV.fetch("ERDBEERE_API") +
"/#{@sort.downcase.pluralize}/#{@id}/view_info")
response = Clients::ErdbeereClient.get("#{@sort.downcase.pluralize}/#{@id}/view_info")
@content = JSON.parse(response.body)
if response.status != 200
@info = "Something went wrong"
@info = I18n.t("erdbeere.error")
return
end
@info = if @sort == "Structure"
Expand Down Expand Up @@ -87,7 +86,7 @@ def update_tags
end

def fill_realizations_select
response = Faraday.get("#{ENV.fetch("ERDBEERE_API")}/structures/")
response = Clients::ErdbeereClient.get("structures")
@tag = Tag.find_by(id: params[:id])
hash = JSON.parse(response.body)
@structures = hash["data"].map do |d|
Expand All @@ -102,11 +101,11 @@ def fill_realizations_select
end

def find_example
response = Faraday.get("#{ENV.fetch("ERDBEERE_API")}/find?#{find_params.to_query}")
response = Clients::ErdbeereClient.get("find?#{find_params.to_query}")
@content = if response.status == 200
JSON.parse(response.body)["embedded_html"]
else
"Something went wrong."
I18n.t("erdbeere.error")
end
end

Expand Down
17 changes: 11 additions & 6 deletions app/controllers/lectures_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,12 @@ def edit_structures

def search_examples
if @lecture.structure_ids.any?
response = Faraday.get("#{ENV.fetch("ERDBEERE_API")}/search")
response = Clients::ErdbeereClient.get("search")
if response.status != 200
@erdbeere_error = true
render layout: "application"
return
end
@form = JSON.parse(response.body)["embedded_html"]
# rubocop:disable Style/StringConcatenation
@form.gsub!("token_placeholder",
Expand Down Expand Up @@ -422,12 +427,12 @@ def eager_load_stuff

def set_erdbeere_data
@structure_ids = @lecture.structure_ids
response = Faraday.get("#{ENV.fetch("ERDBEERE_API")}/structures")
response_hash = if response.status == 200
JSON.parse(response.body)
else
{ "data" => {}, "included" => {} }
response = Clients::ErdbeereClient.get("structures")
if response.status != 200
@erdbeere_error = true
return
end
response_hash = JSON.parse(response.body)
@all_structures = response_hash["data"]
@structures = @all_structures.select do |s|
s["id"].to_i.in?(@structure_ids)
Expand Down
7 changes: 6 additions & 1 deletion app/views/lectures/edit_structures.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<% if @erdbeere_error %>
$('#erdbeereStructuresBody').empty()
.append('<%= t("erdbeere.error") %>')
<% else %>
$('#erdbeereStructuresBody').empty()
.append('<%= j render partial: "lectures/edit/structures",
locals: { lecture: @lecture,
Expand Down Expand Up @@ -31,4 +35,5 @@ renderMathInElement structuresBody,
throwOnError: false

initBootstrapPopovers()
registerErdbeereExampleChanges()
registerErdbeereExampleChanges()
<% end %>
24 changes: 14 additions & 10 deletions app/views/lectures/search_examples.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
<div class="row p-2 mb-3">
<div class="col-12">
<%= link_to t('erdbeere.structure_index'),
show_structures_path(@lecture),
class: 'btn btn-lg btn-outline-secondary' %>
<% if @erdbeere_error %>
<%= t("erdbeere.error") %>
<% else %>
<div class="row p-2 mb-3">
<div class="col-12">
<%= link_to t('erdbeere.structure_index'),
show_structures_path(@lecture),
class: 'btn btn-lg btn-outline-secondary' %>
</div>
</div>
</div>
<%= raw(@form) %>
<span id="lectureStructuresInfo"
data-structures="<%= @lecture.structure_ids.to_json %>">
</span>
<%= raw(@form) %>
<span id="lectureStructuresInfo"
data-structures="<%= @lecture.structure_ids.to_json %>">
</span>
<% end %>
12 changes: 8 additions & 4 deletions app/views/lectures/show_structures.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<%= render partial: 'lectures/show/structures',
locals: { lecture: @lecture,
structures: @structures,
properties: @properties } %>
<% if @erdbeere_error %>
<%= t("erdbeere.error") %>
<% else %>
<%= render partial: 'lectures/show/structures',
locals: { lecture: @lecture,
structures: @structures,
properties: @properties } %>
<% end %>
5 changes: 5 additions & 0 deletions config/initializers/erdbeere_connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ERDBEERE_API_URL = ENV.fetch("ERDBEERE_API")

ERDBEERE_CONNECTION = Faraday.new(url: ERDBEERE_API_URL) do |conn|
conn.adapter(Faraday.default_adapter)
end
Comment on lines +3 to +5
Copy link
Member

@Splines Splines Feb 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠ In production, how does MaMpf communicate with the Erdbeere server? Is Erdbeere just available on localhost or also available to the word-wide web. In the latter case, how does MaMpf authenticate at Erdbeere? I'm wondering in particular since Erdbeere itself has a frontend which allows to log in.

2 changes: 2 additions & 0 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3451,6 +3451,8 @@ de:
no_structures: >
Zu dieser Vorlesung sind keine Strukturen aus der Beispieldatenbank
assoziiert.
error: >
Es ist ein Fehler aufgetreten. Bitte versuche es später noch einmal.
basics:
error: Fehler
course: 'Modul'
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3256,6 +3256,8 @@ en:
search_examples: 'Search for Examples'
no_structures: >
There are no structures associated to this lecture.
error: >
An error has occurred. Please try again later.
basics:
error: Error
course: 'Course'
Expand Down
6 changes: 4 additions & 2 deletions docker/development/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ services:
URL_HOST: localhost
URL_HOST_SHORT: localhost
SECRET_KEY_BASE: testenvironment
ERDBEERE_SERVER: https://erdbeere.mathi.uni-heidelberg.de
ERDBEERE_API: https://erdbeere.mathi.uni-heidelberg.de/api/v1
ERDBEERE_SERVER: http://erdbeere:3005
ERDBEERE_API: http://erdbeere:3005/api/v1
fosterfarrell9 marked this conversation as resolved.
Show resolved Hide resolved
MUESLI_SERVER: https://muesli.mathi.uni-heidelberg.de
PROJECT_EMAIL: project@localhost
FEEDBACK_EMAIL: feedback@localhost
Expand Down Expand Up @@ -139,6 +139,8 @@ services:
- solr
- redis
- mailcatcher
extra_hosts:
- "erdbeere:host-gateway"
labels:
de.uni-heidelberg.mathi.mampf.container-type: worker

Expand Down
15 changes: 15 additions & 0 deletions lib/clients/erdbeere_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Clients
class ErdbeereClient
fosterfarrell9 marked this conversation as resolved.
Show resolved Hide resolved
Response = Struct.new(:status, :body)

def self.get(path, params: nil, headers: {}, &block)
headers = headers.dup
headers["Host"] = "localhost" if Rails.env.docker_development?
ERDBEERE_CONNECTION.get(path, params, headers, &block)
rescue Faraday::Error => e
pp("---------------------------------")
Rails.logger.error("ErdbeereClient.get error: #{e.message}")
Response.new(500, { error: e.message }.to_json)
end
end
end