Skip to content
Merged
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
67 changes: 39 additions & 28 deletions lib/liquid_voting/voting.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ defmodule LiquidVoting.Voting do
@moduledoc """
The Voting context.
"""

require OpenTelemetry.Tracer, as: Tracer

import Ecto.Query, warn: false

alias __MODULE__.{Vote, Participant}
alias LiquidVoting.{Repo, Delegations}
alias __MODULE__.{Participant, Vote}
alias LiquidVoting.{Delegations, Repo}
alias LiquidVoting.Delegations.Delegation

@doc """
Expand All @@ -24,31 +25,42 @@ defmodule LiquidVoting.Voting do

"""
def create_vote(attrs \\ %{}) do
Repo.transaction(fn ->
# TODO: refactor case statements into small functions.
Tracer.with_span "#{__MODULE__} #{inspect(__ENV__.function)}" do
Tracer.set_attributes([
{:request_id, Logger.metadata()[:request_id]},
{:params,
[
{:organization_id, attrs[:organization_id]},
{:participant_email, attrs[:participant_email]},
{:proposal_url, attrs[:proposal_url]},
{:yes, attrs[:yes]}
]}
])

%Vote{}
|> Vote.changeset(attrs)
|> Repo.insert()
|> case do
{:ok, vote} ->
if delegation =
Repo.get_by(Delegation,
delegator_id: attrs[:participant_id],
organization_id: attrs[:organization_id]
) do
case Delegations.delete_delegation(delegation) do
{:ok, _delegation} -> vote
{:error, changeset} -> Repo.rollback(changeset)
Repo.transaction(fn ->
%Vote{}
|> Vote.changeset(attrs)
|> Repo.insert()
|> case do
{:ok, vote} ->
if delegation =
Repo.get_by(Delegation,
delegator_id: attrs[:participant_id],
organization_id: attrs[:organization_id]
) do
case Delegations.delete_delegation(delegation) do
{:ok, _delegation} -> vote
{:error, changeset} -> Repo.rollback(changeset)
end
else
vote
end
else
vote
end

{:error, changeset} ->
Repo.rollback(changeset)
end
end)

{:error, changeset} ->
Repo.rollback(changeset)
end
end)
end
end

@doc """
Expand All @@ -61,11 +73,10 @@ defmodule LiquidVoting.Voting do

"""
def list_votes(organization_id) do
Tracer.with_span "LV/voting" do
Copy link
Member

Choose a reason for hiding this comment

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

Why is the span being removed here?

Copy link
Member Author

@jinjagit jinjagit Mar 19, 2021

Choose a reason for hiding this comment

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

This relates to my general q: Should we be adding traces to the related core functions. E.g. resolver votes/3 calls list_votes. I am not sure a. if list_votes trace gives us useful information over the resolver trace? b. if it is outside the scope of focusing on Absinthe layer?

Copy link
Member

@oliverbarnes oliverbarnes Mar 19, 2021

Choose a reason for hiding this comment

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

I guess if we add it on each layer, we'll see where most processing goes etc. But difference might be negligible, and then we can remove later. I'd stick with it for now

Tracer.with_span "#{__MODULE__} #{inspect(__ENV__.function)}" do
Tracer.set_attributes([
{:action, "list_votes"},
{:request_id, Logger.metadata()[:request_id]},
{:organization_id, organization_id}
{:params, [{:organization_id, organization_id}]}
])

Vote
Expand Down
36 changes: 23 additions & 13 deletions lib/liquid_voting_web/resolvers/voting.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ defmodule LiquidVotingWeb.Resolvers.Voting do
do: {:ok, Voting.list_votes_by_proposal(proposal_url, organization_id)}

def votes(_, _, %{context: %{organization_id: organization_id}}) do
Tracer.with_span "resolvers/voting" do
Tracer.with_span "#{__MODULE__} #{inspect(__ENV__.function)}" do
Tracer.set_attributes([
{:action, "votes"},
{:request_id, Logger.metadata()[:request_id]},
{:organization_id, organization_id}
{:params, [{:organization_id, organization_id}]}
])

{:ok, Voting.list_votes(organization_id)}
Expand All @@ -45,17 +44,28 @@ defmodule LiquidVotingWeb.Resolvers.Voting do
def create_vote(_, %{participant_email: email, proposal_url: _, yes: _} = args, %{
context: %{organization_id: organization_id}
}) do
case Voting.upsert_participant(%{email: email, organization_id: organization_id}) do
{:error, changeset} ->
{:error,
message: "Could not create vote with given email",
details: ChangesetErrors.error_details(changeset)}
Tracer.with_span "#{__MODULE__} #{inspect(__ENV__.function)}" do
Tracer.set_attributes([
{:request_id, Logger.metadata()[:request_id]},
{:params,
[
{:organization_id, organization_id},
{:email, email}
]}
])

{:ok, participant} ->
args
|> Map.put(:organization_id, organization_id)
|> Map.put(:participant_id, participant.id)
|> create_vote_with_valid_arguments()
case Voting.upsert_participant(%{email: email, organization_id: organization_id}) do
{:error, changeset} ->
{:error,
message: "Could not create vote with given email",
details: ChangesetErrors.error_details(changeset)}

{:ok, participant} ->
args
|> Map.put(:organization_id, organization_id)
|> Map.put(:participant_id, participant.id)
|> create_vote_with_valid_arguments()
end
end
end

Expand Down