Skip to content

Commit

Permalink
Encode/decode social volume words to/from www form
Browse files Browse the repository at this point in the history
Otherwise words like $43,000 are considered as 2 words as the comma is
used for a separator. Encoding on our end and metricshub decoding on
its end will fix this problem.
  • Loading branch information
IvanIvanoff committed Oct 22, 2024
1 parent 8a61085 commit e09b2a6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
17 changes: 14 additions & 3 deletions lib/sanbase/social_data/social_volume.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,19 @@ defmodule Sanbase.SocialData.SocialVolume do

transformed_words =
case Keyword.get(opts, :treat_word_as_lucene_query, false) do
false -> transformed_words |> Enum.map(&String.downcase/1)
true -> transformed_words
false ->
transformed_words
|> Enum.map(fn word ->
word
|> String.downcase()
|> URI.encode_www_form()
end)

true ->
Enum.map(transformed_words, fn word ->
word
|> URI.encode_www_form()
end)
end

selector = %{selector | words: transformed_words}
Expand Down Expand Up @@ -98,7 +109,7 @@ defmodule Sanbase.SocialData.SocialVolume do
|> maybe_format_response(words)
|> Enum.map(fn {word, %{} = timeseries} ->
%{
word: word,
word: URI.decode_www_form(word),
timeseries_data: social_volume_result(timeseries)
}
end)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ defmodule SanbaseWeb.Graphql.WordsSocialVolumeApiTest do
body =
%{
"data" => %{
"btc" => %{
URI.encode_www_form("btc") => %{
"2024-09-28T00:00:00Z" => 373,
"2024-09-29T00:00:00Z" => 487,
"2024-09-30T00:00:00Z" => 323
},
"eth or nft" => %{
URI.encode_www_form("eth or nft") => %{
"2024-09-28T00:00:00Z" => 1681,
"2024-09-29T00:00:00Z" => 3246,
"2024-09-30T00:00:00Z" => 1577
Expand All @@ -38,8 +38,9 @@ defmodule SanbaseWeb.Graphql.WordsSocialVolumeApiTest do
Map.new(options[:params])
|> Map.get("search_texts")

# Assert that the words are lowercased before they are sent
assert search_texts == "eth or nft,btc"
# Assert that the words are lowercased and www form encoded
assert search_texts ==
URI.encode_www_form("eth or nft") <> "," <> URI.encode_www_form("btc")

{:ok, resp}
end)
Expand Down Expand Up @@ -97,12 +98,12 @@ defmodule SanbaseWeb.Graphql.WordsSocialVolumeApiTest do
body =
%{
"data" => %{
"BTC" => %{
URI.encode_www_form("BTC") => %{
"2024-09-28T00:00:00Z" => 373,
"2024-09-29T00:00:00Z" => 487,
"2024-09-30T00:00:00Z" => 323
},
"eth OR nft" => %{
URI.encode_www_form("eth OR nft") => %{
"2024-09-28T00:00:00Z" => 1681,
"2024-09-29T00:00:00Z" => 3246,
"2024-09-30T00:00:00Z" => 1577
Expand All @@ -119,7 +120,8 @@ defmodule SanbaseWeb.Graphql.WordsSocialVolumeApiTest do
|> Map.get("search_texts")

# Assert that the words are **not** lowercased before they are sent
assert search_texts == "eth OR nft,BTC"
assert search_texts ==
URI.encode_www_form("eth OR nft") <> "," <> URI.encode_www_form("BTC")

{:ok, resp}
end)
Expand Down

0 comments on commit e09b2a6

Please sign in to comment.