<commit_before>defmodule Pan.PodcastController do
  use Pan.Web, :controller
  alias Pan.Episode
  alias Pan.Category
  alias Pan.Podcast

  plug :scrub_params, "podcast" when action in [:create, :update]

  def orphans(conn, _params) do
    podcast_ids = from(a in "categories_podcasts", group_by: a.podcast_id,
                                                   select:   a.podcast_id)
                  |> Repo.all

    unassigned_podcasts = from(p in Podcast, where: not p.id in ^podcast_ids)
                          |> Repo.all

    podcast_ids = from(e in Episode, group_by: e.podcast_id,
                                     select:   e.podcast_id)
                  |> Repo.all

    podcasts_without_episodes = from(p in Podcast, where: not p.id in ^podcast_ids)
                                |> Repo.all


    render(conn, "orphans.html", unassigned_podcasts: unassigned_podcasts,
                                 podcasts_without_episodes: podcasts_without_episodes)
  end


  def assign_to_unsorted(conn, _params) do
    podcast_ids = from(a in "categories_podcasts", group_by: a.podcast_id,
                                                   select:   a.podcast_id)
                  |> Repo.all

    podcasts = from(p in Podcast, where: not p.id in ^podcast_ids)
              |> Repo.all

    category = Repo.get_by(Category, title: "Unsorted")
               |> Repo.preload(:podcasts)

    Ecto.Changeset.change(category)
    |> Ecto.Changeset.put_assoc(:podcasts, category.podcasts ++ podcasts)
    |> Repo.update!

    conn
    |> put_flash(:info, "Podcasts assigned successfully.")
    |> redirect(to: podcast_path(conn, :orphans))
  end


  def index(conn, _params) do
    ten_hours_ago = Timex.now()
                    |> Timex.shift(hours: -10)

    stale = from(p in Podcast, where: p.next_update <= ^Timex.now() and
                                      (is_nil(p.update_paused) or p.update_paused == false) and
                                      (is_nil(p.retired) or p.retired == false))
            |> Repo.aggregate(:count, :id)

    paused = from(p in Podcast, where: (p.update_paused == true) and
                                       (is_nil(p.retired) or p.retired == false))
             |> Repo.aggregate(:count, :id)

    retired = from(p in Podcast, where: p.retired == true)
              |> Repo.aggregate(:count, :id)

    render(conn, "index.html", stale: stale,
                                paused: paused,
                                retired: retired)
  end


  def datatable(conn, _params) do
    podcasts = from(p in Podcast, order_by: [asc: :updated_at],
                                  join: e in assoc(p, :engagements),
                                  where: e.role == "author",
                                  join: persona in assoc(e, :persona),
                                  select: %{id: p.id,
                                            title: p.title,
                                            author_name: persona.name,
                                            update_paused: p.update_paused,
                                            updated_at: p.updated_at,
                                            update_intervall: p.update_intervall,
                                            next_update: p.next_update,
                                            website: p.website})<commit_msg>removed unused variable<commit_after>defmodule Pan.PodcastController do
  use Pan.Web, :controller
  alias Pan.Episode
  alias Pan.Category
  alias Pan.Podcast

  plug :scrub_params, "podcast" when action in [:create, :update]

  def orphans(conn, _params) do
    podcast_ids = from(a in "categories_podcasts", group_by: a.podcast_id,
                                                   select:   a.podcast_id)
                  |> Repo.all

    unassigned_podcasts = from(p in Podcast, where: not p.id in ^podcast_ids)
                          |> Repo.all

    podcast_ids = from(e in Episode, group_by: e.podcast_id,
                                     select:   e.podcast_id)
                  |> Repo.all

    podcasts_without_episodes = from(p in Podcast, where: not p.id in ^podcast_ids)
                                |> Repo.all


    render(conn, "orphans.html", unassigned_podcasts: unassigned_podcasts,
                                 podcasts_without_episodes: podcasts_without_episodes)
  end


  def assign_to_unsorted(conn, _params) do
    podcast_ids = from(a in "categories_podcasts", group_by: a.podcast_id,
                                                   select:   a.podcast_id)
                  |> Repo.all

    podcasts = from(p in Podcast, where: not p.id in ^podcast_ids)
              |> Repo.all

    category = Repo.get_by(Category, title: "Unsorted")
               |> Repo.preload(:podcasts)

    Ecto.Changeset.change(category)
    |> Ecto.Changeset.put_assoc(:podcasts, category.podcasts ++ podcasts)
    |> Repo.update!

    conn
    |> put_flash(:info, "Podcasts assigned successfully.")
    |> redirect(to: podcast_path(conn, :orphans))
  end


  def index(conn, _params) do
    stale = from(p in Podcast, where: p.next_update <= ^Timex.now() and
                                      (is_nil(p.update_paused) or p.update_paused == false) and
                                      (is_nil(p.retired) or p.retired == false))
            |> Repo.aggregate(:count, :id)

    paused = from(p in Podcast, where: (p.update_paused == true) and
                                       (is_nil(p.retired) or p.retired == false))
             |> Repo.aggregate(:count, :id)

    retired = from(p in Podcast, where: p.retired == true)
              |> Repo.aggregate(:count, :id)

    render(conn, "index.html", stale: stale,
                                paused: paused,
                                retired: retired)
  end


  def datatable(conn, _params) do
    podcasts = from(p in Podcast, order_by: [asc: :updated_at],
                                  join: e in assoc(p, :engagements),
                                  where: e.role == "author",
                                  join: persona in assoc(e, :persona),
                                  select: %{id: p.id,
                                            title: p.title,
                                            author_name: persona.name,
                                            update_paused: p.update_paused,
                                            updated_at: p.updated_at,
                                            update_intervall: p.update_intervall,
                                            next_update: p.next_update,
                                            website: p.website})
{'fraction_non_alphanumeric': 0.09377804367334729, 'fraction_numerical': 0.0002991325157044571, 'mean_word_length': 1.3687566418703507, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 60, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1673062', 'n_tokens_mistral': 1965, 'n_tokens_neox': 1827, 'n_words': 419}