<commit_before>defmodule Wizard.Feeds do
  require Logger

  import Ecto.Query, warn: false

  alias Wizard.{Repo, User}
  alias Wizard.Sharepoint.{Drive, Item}
  alias Wizard.Feeds.{Event, Feed}

  @type event_info :: [type: String.t, actor: User.t, subject: map, payload: map, feed: Feed.t] |
                      [type: String.t, actor: User.t, subject: map, payload: map, grouping: String.t, feed: Feed.t]

  @type db_result :: {:ok, Event.t} | {:error, Ecto.Changeset.t}

  @type indexed_users :: %{optional(String.t) => User.t}

  @type id :: non_neg_integer
  @type ids :: list(id)

  def all_events(feed_id) do
    events = from(e in Event,
                  where: e.feed_id == ^feed_id,
                  order_by: [desc: e.created_at],
                  limit: 10)
             |> Repo.all()

    user_ids = events
               |> Enum.flat_map(&(&1.actor_ids))
               |> Enum.uniq()

    users = from(u in User, where: u.id in ^user_ids)
            |> Repo.all()

    indexed_users = for u <- users, into: %{}, do: {u.id, u}

    events = for e <- events, do: preload_event_actors(e, indexed_users)

    events
    |> Repo.preload(:previews)<commit_msg>I keep forgetting it's inserted_at<commit_after>defmodule Wizard.Feeds do
  require Logger

  import Ecto.Query, warn: false

  alias Wizard.{Repo, User}
  alias Wizard.Sharepoint.{Drive, Item}
  alias Wizard.Feeds.{Event, Feed}

  @type event_info :: [type: String.t, actor: User.t, subject: map, payload: map, feed: Feed.t] |
                      [type: String.t, actor: User.t, subject: map, payload: map, grouping: String.t, feed: Feed.t]

  @type db_result :: {:ok, Event.t} | {:error, Ecto.Changeset.t}

  @type indexed_users :: %{optional(String.t) => User.t}

  @type id :: non_neg_integer
  @type ids :: list(id)

  def all_events(feed_id) do
    events = from(e in Event,
                  where: e.feed_id == ^feed_id,
                  order_by: [desc: e.inserted_at],
                  limit: 10)
             |> Repo.all()

    user_ids = events
               |> Enum.flat_map(&(&1.actor_ids))
               |> Enum.uniq()

    users = from(u in User, where: u.id in ^user_ids)
            |> Repo.all()

    indexed_users = for u <- users, into: %{}, do: {u.id, u}

    events = for e <- events, do: preload_event_actors(e, indexed_users)

    events
    |> Repo.preload(:previews)
{'fraction_non_alphanumeric': 0.14436026936026936, 'fraction_numerical': 0.0025252525252525255, 'mean_word_length': 2.3812233285917497, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2534480', 'n_tokens_mistral': 885, 'n_tokens_neox': 803, 'n_words': 211}