Compare commits

..

1 Commits

Author SHA1 Message Date
5dfbf7011e Add basic functions for keeping track of Deepl account usage 2023-03-15 11:35:26 +01:00
2 changed files with 22 additions and 1 deletions

View File

@ -48,6 +48,19 @@ defmodule Outlook.Translators do
|> Repo.update_all([inc: [our_character_count: billed_characters]]) |> Repo.update_all([inc: [our_character_count: billed_characters]])
end end
def get_uptodate_deepl_counts(user) do
deepl_counts = Deepl.get_usage_counts(get_deepl_auth_key(user))
our_character_count = deepl_account_for_user(user)
|> select([:our_character_count])
|> Repo.one()
|> Map.get(:our_character_count)
most_accurate = max(our_character_count, deepl_counts.character_count)
%{character_limit: deepl_counts.character_limit,
character_count: deepl_counts.character_count,
our_character_count: our_character_count,
percent_used: most_accurate * 100 / deepl_counts.character_limit}
end
def translate(translation, current_user) do def translate(translation, current_user) do
%{language: target_lang, %{language: target_lang,
article: %{content: article_tree, language: source_lang} article: %{content: article_tree, language: source_lang}
@ -71,6 +84,14 @@ defmodule Outlook.Translators do
process_translation(result.translation, tunit_ids) process_translation(result.translation, tunit_ids)
end end
def update_deepl_counts(user, counts) do
deepl_account_for_user(user)
|> Repo.update_all([set: [
character_limit: counts.character_limit,
character_count: counts.character_count
]])
end
defp deepl_account_for_user(user) when is_struct(user), do: deepl_account_for_user(user.id) defp deepl_account_for_user(user) when is_struct(user), do: deepl_account_for_user(user.id)
defp deepl_account_for_user(user_id) do defp deepl_account_for_user(user_id) do
DeeplAccount |> where(user_id: ^user_id) DeeplAccount |> where(user_id: ^user_id)

View File

@ -31,7 +31,7 @@ defmodule OutlookWeb.ArticleLive.NewComponents do
<div>Review Raw InternalTree</div> <div>Review Raw InternalTree</div>
<div class="flex"> <div class="flex">
<div id="html-preview" class="article"> <div id="html-preview" class="article">
<.render_doc tree={@raw_internal_tree} ></.render_doc> <%= InternalTree.render_html_preview(@raw_internal_tree) |> raw %>
</div> </div>
<div id="html-tree"> <div id="html-tree">
<.render_tree tree={@raw_internal_tree} ></.render_tree> <.render_tree tree={@raw_internal_tree} ></.render_tree>