Skip to content

Commit

Permalink
Remove recommendation engine
Browse files Browse the repository at this point in the history
  • Loading branch information
jerodsanto committed Jan 10, 2024
1 parent 0b894b3 commit 3dc8f4a
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 147 deletions.
6 changes: 0 additions & 6 deletions lib/changelog/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ defmodule Changelog.Application do
ttl_check_interval: :timer.seconds(1),
global_ttl: :timer.seconds(60)
),
con_cache_child_spec(
:news_item_recommendations,
ttl_check_interval: :timer.seconds(30),
global_ttl: :timer.minutes(5),
touch_on_read: false
),
Changelog.EpisodeTracker,
Changelog.Metacasts.Filterer.Cache,
{Oban, oban_config()}
Expand Down
65 changes: 0 additions & 65 deletions lib/changelog/schema/news/news_item.ex
Original file line number Diff line number Diff line change
Expand Up @@ -387,69 +387,4 @@ defmodule Changelog.NewsItem do
|> Repo.all()
|> Enum.map(&load_object/1)
end

def recommend_podcasts(episode = %Episode{}, num_recommendations) do
recommendation_query = "SELECT * FROM query_related_podcast($1::integer, $2::integer)"
query_args = [episode.id, num_recommendations]

ConCache.fetch_or_store(
:news_item_recommendations,
{:podcast, episode.id, num_recommendations},
fn ->
query_recommendations(recommendation_query, query_args)
end
)
end

def recommend_news_items(news_item = %__MODULE__{}, num_recommendations) do
recommendation_query = "SELECT * FROM query_related_news_item($1::integer, $2::integer)"
query_args = [news_item.id, num_recommendations]

ConCache.fetch_or_store(
:news_item_recommendations,
{:news_item, news_item.id, num_recommendations},
fn ->
query_recommendations(recommendation_query, query_args)
end
)
end

def recommend_posts(news_item = %__MODULE__{}, num_recommendations) do
recommendation_query = "SELECT * FROM query_related_post($1::integer, $2::integer)"
query_args = [news_item.id, num_recommendations]

ConCache.fetch_or_store(
:news_item_recommendations,
{:post, news_item.id, num_recommendations},
fn ->
query_recommendations(recommendation_query, query_args)
end
)
end

defp query_recommendations(query, args) do
try do
query
|> Changelog.Repo.query(args)
|> case do
{:ok, %Postgrex.Result{columns: columns, rows: rows}} ->
results =
Enum.map(rows, fn row ->
columns
|> Enum.zip(row)
|> Map.new()
end)

{:ok, results}

error ->
Logger.warn("Failed to fetch recommended items: #{inspect(error)}")
{:error, error}
end
rescue
error ->
Logger.warn("Failed to fetch recommended items: #{inspect(error)}")
{:error, error}
end
end
end
76 changes: 0 additions & 76 deletions test/changelog/schema/news/news_item_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -88,80 +88,4 @@ defmodule Changelog.NewsItemTest do
refute Enum.member?(subscribed_ids, wilma.id)
end
end

describe "recommend_podcasts/2" do
test "should return a list of similar podcasts" do
podcast = insert(:podcast)

author = insert(:person, twitter_handle: "ohai")
t1 = insert(:topic, name: "iOS", slug: "ios", twitter_handle: "OfficialiOS")
t2 = insert(:topic, name: "Machine Learning", slug: "machine-learning")

e1 = insert(:episode, podcast: podcast, slug: "114", audio_bytes: 26_238_621)

i1 =
insert(:news_item,
author: author,
object_id: "#{podcast.id}:#{e1.id}",
type: :audio,
status: :published,
published_at: NaiveDateTime.utc_now()
)

e2 = insert(:episode, podcast: podcast, slug: "181", audio_bytes: 59_310_792)

i2 =
insert(:news_item,
author: author,
click_count: 10,
object_id: "#{podcast.id}:#{e2.id}",
type: :audio,
status: :published,
published_at: NaiveDateTime.utc_now()
)

e3 =
insert(:episode,
podcast: podcast,
slug: "182",
audio_bytes: 56_304_828,
reach_count: 10_000
)

i3 =
insert(:news_item,
author: author,
click_count: 50,
object_id: "#{podcast.id}:#{e3.id}",
type: :audio,
status: :published,
published_at: NaiveDateTime.utc_now()
)

e4 = insert(:episode, podcast: podcast, slug: "183", audio_bytes: 63_723_737)

i4 =
insert(:news_item,
author: author,
click_count: 100,
object_id: "#{podcast.id}:#{e4.id}",
type: :audio,
status: :published,
published_at: NaiveDateTime.utc_now()
)

insert(:news_item_topic, news_item: i1, topic: t1)
insert(:news_item_topic, news_item: i2, topic: t1)
insert(:news_item_topic, news_item: i3, topic: t2)
insert(:news_item_topic, news_item: i4, topic: t2)

i1
|> NewsItem.load_object()
|> Repo.preload([:news_item_topics, :topics])

assert {:ok, [rec1, rec2, rec3]} = NewsItem.recommend_podcasts(e1, 3)
assert rec1["ranking"] > rec2["ranking"]
assert rec2["ranking"] > rec3["ranking"]
end
end
end

0 comments on commit 3dc8f4a

Please sign in to comment.