add computed duration field

This commit is contained in:
byte[] 2020-07-06 14:00:02 -04:00
parent 78d94dfe84
commit be10ad4663
11 changed files with 45 additions and 5 deletions

View file

@ -52,7 +52,7 @@ defmodule Philomena.Elasticsearch do
index_name = index.index_name()
mapping = index.mapping().mappings.properties
Elastix.Mapping.put(elastic_url(), index_name, "_doc", %{properties: mapping})
Elastix.Mapping.put(elastic_url(), index_name, "_doc", %{properties: mapping}, include_type_name: true)
end
def index_document(doc, module) do

View file

@ -31,6 +31,7 @@ defmodule Philomena.Images.ElasticsearchIndex do
downvoters: %{type: "keyword"},
downvotes: %{type: "integer"},
duplicate_id: %{type: "integer"},
duration: %{type: "float"},
faves: %{type: "integer"},
favourited_by_user_ids: %{type: "keyword"},
favourited_by_users: %{type: "keyword"},
@ -46,7 +47,9 @@ defmodule Philomena.Images.ElasticsearchIndex do
mime_type: %{type: "keyword"},
orig_sha512_hash: %{type: "keyword"},
original_format: %{type: "keyword"},
pixels: %{type: "integer"},
score: %{type: "integer"},
size: %{type: "integer"},
sha512_hash: %{type: "keyword"},
source_url: %{type: "keyword"},
tag_count: %{type: "integer"},
@ -93,6 +96,9 @@ defmodule Philomena.Images.ElasticsearchIndex do
comment_count: image.comments_count,
width: image.image_width,
height: image.image_height,
pixels: image.image_width * image.image_height,
size: image.image_size,
duration: image.image_duration,
tag_count: length(image.tags),
aspect_ratio: image.image_aspect_ratio,
wilson_score: wilson_score(image),

View file

@ -49,6 +49,7 @@ defmodule Philomena.Images.Image do
field :image_format, :string
field :image_mime_type, :string
field :image_aspect_ratio, :float
field :image_duration, :float
field :image_is_animated, :boolean, source: :is_animated
field :ip, EctoNetwork.INET
field :fingerprint, :string
@ -131,6 +132,7 @@ defmodule Philomena.Images.Image do
:image_format,
:image_mime_type,
:image_aspect_ratio,
:image_duration,
:image_orig_sha512_hash,
:image_sha512_hash,
:uploaded_image,
@ -145,6 +147,7 @@ defmodule Philomena.Images.Image do
:image_format,
:image_mime_type,
:image_aspect_ratio,
:image_duration,
:image_orig_sha512_hash,
:image_sha512_hash,
:uploaded_image,

View file

@ -69,11 +69,11 @@ defmodule Philomena.Images.Query do
defp anonymous_fields do
[
int_fields:
~W(id width height comment_count score upvotes downvotes faves uploader_id faved_by_id tag_count),
float_fields: ~W(aspect_ratio wilson_score),
~W(id width height comment_count score upvotes downvotes faves uploader_id faved_by_id tag_count pixels size),
float_fields: ~W(aspect_ratio wilson_score duration),
date_fields: ~W(created_at updated_at first_seen_at),
literal_fields:
~W(faved_by orig_sha512_hash sha512_hash uploader source_url original_format),
~W(faved_by orig_sha512_hash sha512_hash uploader source_url original_format mime_type),
ngram_fields: ~W(description),
custom_fields: ~W(gallery_id),
default_field: {"namespaced_tags.name", :term},

View file

@ -31,6 +31,7 @@ defmodule Philomena.Uploader do
"size" => analysis.size,
"format" => analysis.extension,
"mime_type" => analysis.mime_type,
"duration" => analysis.duration,
"aspect_ratio" => analysis.aspect_ratio,
"orig_sha512_hash" => analysis.sha512,
"sha512_hash" => analysis.sha512,

View file

@ -14,6 +14,9 @@ defmodule PhilomenaWeb.ImageSorter do
comment_count
tag_count
wilson_score
pixels
size
duration
)
def parse_sort(params, query) do

View file

@ -61,6 +61,14 @@
= @image.image_width
| x
= @image.image_height
= if not is_nil(@image.image_duration) and @image.image_is_animated and @image.image_duration > 0 do
span.image-size
|  
- dur = ceil(@image.image_duration)
- {hh, mm, ss} = {div(dur, 3600), div(rem(dur, 3600), 60), rem(dur, 60)}
= :io_lib.format("~2..0B:~2..0B:~2..0B", [hh, mm, ss])
=<> String.upcase(to_string(@image.image_format))
- size_kb = div(@image.image_size, 1000)
- size_mb = Float.round(size_kb / 1000.0, 2)

View file

@ -36,6 +36,7 @@
= error_tag f, :image_height
= error_tag f, :image_name
= error_tag f, :image_mime_type
= error_tag f, :image_duration
= error_tag f, :image_orig_sha512_hash
.field.field--inline

View file

@ -59,6 +59,8 @@ defmodule PhilomenaWeb.Api.Json.ImageView do
width: image.image_width,
height: image.image_height,
mime_type: image.image_mime_type,
size: image.image_size,
duration: image.image_duration,
format: image.image_format,
aspect_ratio: image.image_aspect_ratio,
name: image.image_name,

View file

@ -0,0 +1,14 @@
defmodule Philomena.Repo.Migrations.AddDurationToImages do
use Ecto.Migration
def change do
alter table("images") do
add :image_duration, :float
end
# After successful migration:
# alias Philomena.Elasticsearch
# alias Philomena.Images.Image
# Elasticsearch.update_mapping!(Image)
end
end

View file

@ -882,7 +882,8 @@ CREATE TABLE public.images (
destroyed_content boolean DEFAULT false NOT NULL,
hidden_image_key character varying,
scratchpad character varying,
hides_count integer DEFAULT 0 NOT NULL
hides_count integer DEFAULT 0 NOT NULL,
image_duration double precision
);
@ -4706,3 +4707,4 @@ INSERT INTO public."schema_migrations" (version) VALUES (20200503002523);
INSERT INTO public."schema_migrations" (version) VALUES (20200607000511);
INSERT INTO public."schema_migrations" (version) VALUES (20200617111116);
INSERT INTO public."schema_migrations" (version) VALUES (20200617113333);
INSERT INTO public."schema_migrations" (version) VALUES (20200706171350);