diff --git a/lib/sanbase/social_data/trending_words.ex b/lib/sanbase/social_data/trending_words.ex index 069eec6528..d365289ebf 100644 --- a/lib/sanbase/social_data/trending_words.ex +++ b/lib/sanbase/social_data/trending_words.ex @@ -50,7 +50,7 @@ defmodule Sanbase.SocialData.TrendingWords do datetme: DateTime.t(), position: position } - @table "trending_words_docs_v3" + @table "trending_words_docs_v4" # When calculating the trending now words fetch the data for the last # N hours to ensure that there is some data and we're not in the middle @@ -72,13 +72,14 @@ defmodule Sanbase.SocialData.TrendingWords do query_struct = get_trending_words_query(from, to, interval, size, source, word_type_filter) ClickhouseRepo.query_reduce(query_struct, %{}, fn - [dt, word, project, score, context, summary, sentiment_ratios], acc -> + [dt, word, project, score, context, summary, sentiment_ratios, bb_sentiment_ratios], acc -> slug = if project, do: String.split(project, "_", parts: 2) |> List.last() datetime = DateTime.from_unix!(dt) # The percentage of the documents that mention the word that have # postive, negative or netural sentiment. The values are in the range [0, 1] # and add up to 1 [pos_sentiment, neg_sentiment, neu_sentiment] = sentiment_ratios + [bull_bb_sentiment, bear_bb_sentiment, neu_bb_sentiment] = bb_sentiment_ratios summaries = [%{source: source, datetime: datetime, summary: summary}] context = transform_context(context) @@ -93,7 +94,10 @@ defmodule Sanbase.SocialData.TrendingWords do summaries: summaries, positive_sentiment_ratio: pos_sentiment, negative_sentiment_ratio: neg_sentiment, - neutral_sentiment_ratio: neu_sentiment + neutral_sentiment_ratio: neu_sentiment, + bullish_bb_sentiment_ratio: bull_bb_sentiment, + bearish_bb_sentiment_ratio: bear_bb_sentiment, + neutral_bb_sentiment_ratio: neu_bb_sentiment } Map.update(acc, datetime, [elem], fn words -> [elem | words] end) @@ -244,7 +248,8 @@ defmodule Sanbase.SocialData.TrendingWords do score, context, summary, - sentiment_ratios + sentiment_ratios, + bb_sentiment_ratios FROM ( SELECT @@ -256,7 +261,8 @@ defmodule Sanbase.SocialData.TrendingWords do score / {{score_equalizer}} AS score, words_context AS context, summary, - tuple(pos_ratio, neg_ratio, neu_ratio) AS sentiment_ratios + tuple(pos_ratio, neg_ratio, neu_ratio) AS sentiment_ratios, + tuple(bull_bb_ratio, bear_bb_ratio, neu_bb_ratio) AS bb_sentiment_ratios FROM #{@table} WHERE dt >= toDateTime({{from}}) AND diff --git a/lib/sanbase_web/graphql/schema/types/social_data_types.ex b/lib/sanbase_web/graphql/schema/types/social_data_types.ex index eeb4fae12f..b3ca703d30 100644 --- a/lib/sanbase_web/graphql/schema/types/social_data_types.ex +++ b/lib/sanbase_web/graphql/schema/types/social_data_types.ex @@ -103,6 +103,10 @@ defmodule SanbaseWeb.Graphql.SocialDataTypes do field(:positive_sentiment_ratio, :float) field(:negative_sentiment_ratio, :float) field(:neutral_sentiment_ratio, :float) + # bearish/bullish sentiment ratios + field(:bullish_bb_sentiment_ratio, :float) + field(:bearish_bb_sentiment_ratio, :float) + field(:neutral_bb_sentiment_ratio, :float) field :project, :project do cache_resolve(&SocialDataResolver.project_from_root_slug/3) diff --git a/test/sanbase/social_data/trending_words/trending_words_test.exs b/test/sanbase/social_data/trending_words/trending_words_test.exs index 9ec2266235..b8b29d01df 100644 --- a/test/sanbase/social_data/trending_words/trending_words_test.exs +++ b/test/sanbase/social_data/trending_words/trending_words_test.exs @@ -44,7 +44,10 @@ defmodule Sanbase.SocialData.TrendingWordsTest do word: "ethereum", negative_sentiment_ratio: 0.3, neutral_sentiment_ratio: 0.5, - positive_sentiment_ratio: 0.2 + positive_sentiment_ratio: 0.2, + bearish_bb_sentiment_ratio: 0.3, + neutral_bb_sentiment_ratio: 0.5, + bullish_bb_sentiment_ratio: 0.2 }, %{ context: [%{score: 1.0, word: "usd"}, %{score: 0.5, word: "money"}], @@ -61,7 +64,10 @@ defmodule Sanbase.SocialData.TrendingWordsTest do word: "bitcoin", negative_sentiment_ratio: 0.3, neutral_sentiment_ratio: 0.5, - positive_sentiment_ratio: 0.2 + positive_sentiment_ratio: 0.2, + bearish_bb_sentiment_ratio: 0.3, + neutral_bb_sentiment_ratio: 0.5, + bullish_bb_sentiment_ratio: 0.2 } ], dt2 => [ @@ -80,7 +86,10 @@ defmodule Sanbase.SocialData.TrendingWordsTest do word: "boom", negative_sentiment_ratio: 0.1, neutral_sentiment_ratio: 0.7, - positive_sentiment_ratio: 0.2 + positive_sentiment_ratio: 0.2, + bearish_bb_sentiment_ratio: 0.1, + neutral_bb_sentiment_ratio: 0.7, + bullish_bb_sentiment_ratio: 0.2 }, %{ context: [%{score: 1.0, word: "usd"}, %{score: 0.5, word: "money"}], @@ -97,7 +106,10 @@ defmodule Sanbase.SocialData.TrendingWordsTest do word: "san", negative_sentiment_ratio: 0.1, neutral_sentiment_ratio: 0.5, - positive_sentiment_ratio: 0.4 + positive_sentiment_ratio: 0.4, + bearish_bb_sentiment_ratio: 0.1, + neutral_bb_sentiment_ratio: 0.5, + bullish_bb_sentiment_ratio: 0.4 } ], dt3 => [ @@ -116,7 +128,10 @@ defmodule Sanbase.SocialData.TrendingWordsTest do word: "xrp", negative_sentiment_ratio: 0.3, neutral_sentiment_ratio: 0.5, - positive_sentiment_ratio: 0.2 + positive_sentiment_ratio: 0.2, + bearish_bb_sentiment_ratio: 0.3, + neutral_bb_sentiment_ratio: 0.5, + bullish_bb_sentiment_ratio: 0.2 }, %{ context: [%{score: 1.0, word: "usd"}, %{score: 0.5, word: "money"}], @@ -133,7 +148,10 @@ defmodule Sanbase.SocialData.TrendingWordsTest do word: "eth", negative_sentiment_ratio: 0.3, neutral_sentiment_ratio: 0.5, - positive_sentiment_ratio: 0.2 + positive_sentiment_ratio: 0.2, + bearish_bb_sentiment_ratio: 0.3, + neutral_bb_sentiment_ratio: 0.5, + bullish_bb_sentiment_ratio: 0.2 } ] } @@ -181,7 +199,10 @@ defmodule Sanbase.SocialData.TrendingWordsTest do word: "xrp", negative_sentiment_ratio: 0.3, neutral_sentiment_ratio: 0.5, - positive_sentiment_ratio: 0.2 + positive_sentiment_ratio: 0.2, + bearish_bb_sentiment_ratio: 0.3, + neutral_bb_sentiment_ratio: 0.5, + bullish_bb_sentiment_ratio: 0.2 }, %{ context: [%{score: 1.0, word: "usd"}, %{score: 0.5, word: "money"}], @@ -198,7 +219,10 @@ defmodule Sanbase.SocialData.TrendingWordsTest do word: "eth", negative_sentiment_ratio: 0.3, neutral_sentiment_ratio: 0.5, - positive_sentiment_ratio: 0.2 + positive_sentiment_ratio: 0.2, + bearish_bb_sentiment_ratio: 0.3, + neutral_bb_sentiment_ratio: 0.5, + bullish_bb_sentiment_ratio: 0.2 } ] } @@ -308,12 +332,39 @@ defmodule Sanbase.SocialData.TrendingWordsTest do ] [ - [dt1_unix, "bitcoin", "BTC_bitcoin", 10, context, "summary1", [0.2, 0.3, 0.5]], - [dt1_unix, "ethereum", "ETH_ethereum", 5, context, "summary2", [0.2, 0.3, 0.5]], - [dt2_unix, "san", "SAN_santiment", 2, context, "summary3", [0.4, 0.1, 0.5]], - [dt2_unix, "boom", nil, 70, context, "summary4", [0.2, 0.1, 0.7]], - [dt3_unix, "eth", "ETH_ethereum", 1, context, "summary5", [0.2, 0.3, 0.5]], - [dt3_unix, "xrp", "XRP_ripple", 2, context, "summary6", [0.2, 0.3, 0.5]] + [ + dt1_unix, + "bitcoin", + "BTC_bitcoin", + 10, + context, + "summary1", + [0.2, 0.3, 0.5], + [0.2, 0.3, 0.5] + ], + [ + dt1_unix, + "ethereum", + "ETH_ethereum", + 5, + context, + "summary2", + [0.2, 0.3, 0.5], + [0.2, 0.3, 0.5] + ], + [ + dt2_unix, + "san", + "SAN_santiment", + 2, + context, + "summary3", + [0.4, 0.1, 0.5], + [0.4, 0.1, 0.5] + ], + [dt2_unix, "boom", nil, 70, context, "summary4", [0.2, 0.1, 0.7], [0.2, 0.1, 0.7]], + [dt3_unix, "eth", "ETH_ethereum", 1, context, "summary5", [0.2, 0.3, 0.5], [0.2, 0.3, 0.5]], + [dt3_unix, "xrp", "XRP_ripple", 2, context, "summary6", [0.2, 0.3, 0.5], [0.2, 0.3, 0.5]] ] end end