index.community/backend/lib/backend/instance.ex

78 lines
2.1 KiB
Elixir
Raw Normal View History

2019-07-14 11:47:06 +00:00
defmodule Backend.Instance do
2019-08-21 12:30:47 +00:00
@moduledoc """
Model for storing everything related to an instance: not only the data from crawls, but also statistics, the time
of the next scheduled crawl, X and Y coordinates on the graph, and so on.
"""
2019-07-14 11:47:06 +00:00
use Ecto.Schema
import Ecto.Changeset
schema "instances" do
field :domain, :string
field :description, :string
field :user_count, :integer
field :status_count, :integer
field :version, :string
field :insularity, :float
2019-07-24 15:51:44 +00:00
field :type, :string
2019-07-27 17:58:40 +00:00
field :statuses_per_day, :float
2019-07-25 15:56:03 +00:00
field :base_domain, :string
2019-07-26 14:34:23 +00:00
field :opt_in, :boolean
field :opt_out, :boolean
2019-08-07 16:53:34 +00:00
field :next_crawl, :naive_datetime
2019-08-07 19:41:19 +00:00
field :crawl_error, :string
field :crawl_error_count, :integer
2019-07-14 11:47:06 +00:00
many_to_many :peers, Backend.Instance,
join_through: Backend.InstancePeer,
join_keys: [source_domain: :domain, target_domain: :domain]
# This may look like it's duplicating :peers above, but it allows us to insert peer relationships quickly.
# https://stackoverflow.com/a/56764241/3697202
has_many :instance_peers, Backend.InstancePeer,
foreign_key: :source_domain,
references: :domain
timestamps()
end
@doc false
def changeset(instance, attrs) do
instance
|> cast(attrs, [
:domain,
:description,
:user_count,
:status_count,
:version,
:insularity,
2019-07-24 15:51:44 +00:00
:updated_at,
2019-07-25 15:56:03 +00:00
:type,
2019-07-27 17:58:40 +00:00
:statuses_per_day,
2019-07-26 14:34:23 +00:00
:base_domain,
:opt_in,
2019-08-07 16:53:34 +00:00
:opt_out,
2019-08-07 19:41:19 +00:00
:next_crawl,
:crawl_error,
:crawl_error_count
2019-07-14 11:47:06 +00:00
])
|> validate_required([:domain])
|> put_assoc(:peers, attrs.peers)
end
2019-07-26 22:30:11 +00:00
defimpl Elasticsearch.Document, for: Backend.Instance do
def id(instance), do: instance.id
def routing(_), do: false
def encode(instance) do
# Make sure this corresponds with priv/elasticseach/instances.json
%{
domain: instance.domain,
description: instance.description,
type: instance.type,
user_count: instance.user_count,
opt_out: instance.opt_out
2019-07-26 22:30:11 +00:00
}
end
end
2019-07-14 11:47:06 +00:00
end