# Music Library ```elixir Mix.install([ {:kino_db, "~> 0.2.10"}, {:exqlite, "~> 0.23.0"}, {:kino_vega_lite, "~> 0.1.11"} ]) ``` ## Setup ```elixir opts = [ database: "/Users/cloud/github.com/cloud8421/music_library/data/music_library_dev.db" ] {:ok, conn} = Kino.start_child({Exqlite, opts}) ``` ## Number of releases per record ```elixir count_by_releases = Exqlite.query!( conn, ~S""" select distinct json_extract(artists, '$[0].name') AS artist_name, title, json_array_length(release_ids) AS number_of_releases from records order by number_of_releases desc ; """, [] ) ``` ## Count of records per artist ```elixir count_by_artist = Exqlite.query!( conn, ~S""" select json_extract(artist.value, '$.name') as name, count(1) as c from records, json_each(records.artists) artist group by name order by c desc ; """, [] ) ``` ```elixir VegaLite.new(width: 600, title: "Count by Artist") |> VegaLite.data_from_values(count_by_artist, only: ["c", "name"]) |> VegaLite.mark(:bar) |> VegaLite.encode_field(:x, "c", type: :quantitative) |> VegaLite.encode_field(:y, "name", type: :nominal) ``` ## Count of records by genre ```elixir count_by_genre = Exqlite.query!( conn, ~S""" select genre.value, count(genre.value) as c from records, json_each(records.genres) genre group by genre.value order by c desc ; """, [] ) ``` ```elixir VegaLite.new(width: 600, title: "Count by genre (alt)") |> VegaLite.data_from_values(count_by_genre, only: ["c", "value"]) |> VegaLite.mark(:bar) |> VegaLite.encode_field(:x, "c", type: :quantitative) |> VegaLite.encode_field(:y, "value", type: :nominal, sort: "-c") ``` ```elixir VegaLite.new(width: 600, title: "Count by genre") |> VegaLite.data_from_values(count_by_genre, only: ["c", "value"]) |> VegaLite.mark(:bar) |> VegaLite.encode_field(:x, "c", type: :quantitative) |> VegaLite.encode_field(:y, "value", type: :nominal) ``` ## All unique genres ```elixir genres = Exqlite.query!( conn, ~S""" select distinct genre.value from records, json_each(records.genres) genre ; """, [] ) ``` ```elixir Enum.join(genres.rows, ",") ``` ## Total artists per country ```elixir total_artists_per_country = Exqlite.query!( conn, ~S""" SELECT count(*) AS total_artists, json_extract(musicbrainz_data, '$.country') AS country FROM artist_infos GROUP BY country ORDER BY total_artists DESC; """, [] ) ``` ```elixir VegaLite.new(width: 600, title: "Total artists per country") |> VegaLite.data_from_values(total_artists_per_country, only: ["total_artists", "country"] ) |> VegaLite.mark(:bar) |> VegaLite.encode_field(:x, "total_artists", type: :quantitative) |> VegaLite.encode_field(:y, "country", type: :nominal) ```