peck

peck

Hi everyone,

I’ve got a little hobby project (https://plaintexttides.com) that I’m working on and playing with sqlite a bit with it. I’m really only using a database to save myself some computation and API calls and speed up things, so I’m looking to use a sqlite database to essentially do that “work” ahead of time by bulk downloading the predictions at dev time vs runtime..

Essentially I’m bulk downloading and storing the tide predictions from NOAA for all the tide stations in the US for this calendar year. 4 tides, 365 days, and about 3300 stations. Not a huge amount of data, but instead of reaching out to NOAA for each time I have a user request to show the info, I have a little (~100Mb) sqlite database to query from using ecto.

I currently have it live in “priv/” and all seems to work nicely, but I don’t have a great strategy for how to “build” that database from the ground up. Using migrations, which is what I have now, doesn’t seem like quite like the “right” approach since I’ll never be doing it at runtime, but maybe it is.

Currently this is the only Repo in my app, but there’s a possibility that I could add a postgresql (or another sqlite) in the future so I want to keep options open, and since it’s a hobby project I don’t have to worry as much about YAGNI.

I was wondering if any of you had suggestions or approaches that you’ve had success with in building sqlite databases as something like static assets, maybe as a mix task or otherwise?

Thanks for any suggestions and thoughts.

Showing Posts 1 to 8

evadne

evadne

  1. Etso may be useful?

  2. Bundling a 100MB file in priv over Hex would be quite heavy

  3. Would suggest checking how tzdata handles distribution of its database. Also check approach used by evision, exla.

Eiji

Eiji

The best way is to have SQLite/CSV/JSON/XML in priv directory and fetch all data at compile-time. Using said data you could generate Elixir code, so you can fetch specific data using patter-matching.

I wonder if storing those data in a directory with CSV files (each for every table) wouldn’t be better for tasks like that. It would be not only easier to correct data manually, but also regenerate it using Elixir code.

# config
  live_reload: [
    patterns: [
      ...,
      ~r"priv/data/.*(csv|json|sqlite3|xml)$"
    ]
  ]

# code:
defmodule Example do
  alias NimbleCSV.RFC4180, as: CSV

  path_to_posts = :my_app |> :code.priv_dir() |> Path.join(~w[data posts.csv])

  @external_resource path_to_posts

  fetched_posts = path_to_posts |> File.stream!() |> CSV.parse_stream()

  for post <- fetched_posts do
    def get_post(unquote(post.id)), do: unquote(post)
    # or:
    def get_post(unquote(post.slug)), do: unquote(post)
  end

  # …
end

I would recommend to read this article: Welcome to our blog: how it was made! - Dashbit Blog

peck

peck OP

These are great suggestions.

I do like the relational nature, and overall niceness that is ecto, so I hadn’t thought about doing the code-generation approach for this project, though I have used it successfully in the past.

To give a little more context on what my files and models look like I’ll drop them here since like I said it’s just a hobby project to give myself a playground to stay relatively up to speed on phoenix, and to scratch my own itch of being very annoyed with all the tide prediction sites covered with ads and user-hostile patterns.

defmodule Tide.Station do
  use Ecto.Schema
  import Ecto.Changeset
  @primary_key {:id, :string, autogenerate: false}
  import Ecto.Query

  schema "stations" do
    field :name, :string
    field :latitude, :float
    field :longitude, :float
    field :time_zone_correction, :integer
    field :time_zone_name, :string
    field :distance, :float, virtual: true
    has_many :predictions, Tide.Prediction

  end
end
defmodule Tide.Prediction do
  use Ecto.Schema
  import Ecto.Changeset
  import Ecto.Query
  @primary_key {:id, :string, autogenerate: false}


  schema "predictions" do
    belongs_to :station, Tide.Station
    field :timestamp, :utc_datetime
    field :volume, :float
    field :type, :string
  end
end

The data files, that I currently have living in /priv/predictions, are just dumps from the nice folks at https://api.tidesandcurrents.noaa.gov/api/prod/datagetter and look like this:

[
{"t":"2023-01-01T03:46:00Z","type":"L","v":"0.078"},
{"t":"2023-01-01T09:56:00Z","type":"H","v":"2.820"},
{"t":"2023-01-01T16:36:00Z","type":"L","v":"0.301"},
{"t":"2023-01-01T22:09:00Z","type":"H","v":"2.347"}
...
]

And to wrap things up my currently seed script is just seeds.exs that does this:

for station <- Tide.Repo.all(Tide.Station) do
    if !File.exists?("priv/predictions/#{station.id}.json") do
      {:ok, predictions} = Tide.tide_predictions(station.id, ~D[2023-01-01])
      json_data = Jason.encode_to_iodata!(predictions)
      File.write!("priv/predictions/#{station.id}.json", json_data)
    end
end

for file_name <- File.ls!("priv/predictions/") do
    [station_id, _extension] = String.split(file_name, ".")

    changesets = File.read!("priv/predictions/#{file_name}")
    |> Jason.decode!()
    |> Enum.map(fn(x) ->

      y = %{
        "type" => x["type"],
        "volume" => x["v"],
        "timestamp" => x["t"],
        "station_id" => station_id
      }

      %Tide.Prediction{}
      |> Tide.Prediction.changeset(y)
      end)

    Tide.Repo.transaction(fn ->
      Enum.each(changesets, fn cs -> Tide.Repo.insert(cs) end)
    end)
end

Hasn’t been cleaned up yet, so a little messy and probably some duplicative logic in there.

But anyway at the end up it I end up with a closer to 200Mb /priv/tide.db sqlite database. I’m not mad about that and don’t mind the size or anything, though obviously lots of places for optimization. What I’d love to understand are some general patterns and approaches for generating static resources.

Eiji

Eiji

So why not to make your code work nicely like ecto? You can collect even more data at compile-time or write your own preload functions. Just take a look at code below and see yourself how simple it is:

defmodule Example do
  # some post data
  posts = [
    %{"contents" => "Contents #1", "id" => 1, "title" => "Title #1"},
    %{"contents" => "Contents #2", "id" => 2, "title" => "Title #2"},
    %{"contents" => "Contents #3", "id" => 3, "title" => "Title #3"},
    %{"contents" => "Contents #4", "id" => 4, "title" => "Title #4"},
    %{"contents" => "Contents #5", "id" => 5, "title" => "Title #5"}
  ]

  # some comment data
  comments = [
    %{"id" => 1, "post_id" => 1, "contents" => "Contents #1 - #1"},
    %{"id" => 2, "post_id" => 1, "contents" => "Contents #1 - #2"},
    %{"id" => 3, "post_id" => 2, "contents" => "Contents #2 - #1"},
    %{"id" => 4, "post_id" => 2, "contents" => "Contents #2 - #2"},
    %{"id" => 5, "post_id" => 3, "contents" => "Contents #3 - #1"},
    %{"id" => 6, "post_id" => 3, "contents" => "Contents #3 - #2"},
    %{"id" => 7, "post_id" => 4, "contents" => "Contents #4 - #1"},
    %{"id" => 8, "post_id" => 4, "contents" => "Contents #4 - #2"},
    %{"id" => 9, "post_id" => 5, "contents" => "Contents #5 - #1"},
    %{"id" => 10, "post_id" => 5, "contents" => "Contents #5 - #2"}
  ]

  grouped_comments =
    for %{"id" => id, "post_id" => post_id} = comment <- comments, reduce: %{} do
      acc ->
        # define a function to get comment by id
        def get_comment(unquote(id)), do: unquote(Macro.escape(comment))
        # and add comment to accumulator
        Map.update(acc, post_id, [comment], &[comment | &1])
    end

  # in case we want to work more on runtime
  # instead of fetching all comments
  # we simply define the relation
  # just like intermediate table in ecto
  for {post_id, comments} <- grouped_comments do
    comment_ids = Enum.map(comments, & &1["id"])
    def get_comment_ids(unquote(post_id)), do: unquote(comment_ids)
  end

  for %{"id" => id} = post <- posts do
    comments = Enum.sort_by(grouped_comments[id], & &1["id"])
    post_with_comments = Map.put(post, "comments", comments)
    # define a function to get post only
    def get_post(unquote(id)), do: unquote(Macro.escape(post))
    # or define a function to get post with comments
    def get_post_with_comments(unquote(id)), do: unquote(Macro.escape(post_with_comments))
  end

  # preloading a belongs to is dead simple
  # since in map we already have id
  def preload(comment, :post) do
    comment |> Map.fetch!("post_id") |> get_post() |> then(&Map.put(comment, "post", &1))
  end

  # on the other side preloading a has many requires some association data
  # like the one we already defined
  def preload(post, :comments) do
    post
    |> Map.fetch!("id")
    |> get_comment_ids()
    |> Enum.map(&get_comment/1)
    |> then(&Map.put(post, "comments", &1))
  end
end
evadne

evadne

It’s precisely the goal of Etso… or ETSo

Fill the ETS table at startup then query it with Ecto

Eiji

Eiji

Not always … fetching few libraries just for simple for loops like above does not makes much sense.

axelson

axelson

Scenic Core Team

I’d handle this with two parts:

  • Use an in-memory write-through cache (such as cachex or nebulex) to to serve requests from memory and also write the results of the request to your sqlite db
  • A GenServer that is part of your supervision tree that populates the sqlite db with all the results for this calendar year

Personally I like that better than a pure memory-only solution because when you restart the server you won’t need to make 3000 requests to NOAA’s API.

krasenyp

krasenyp

I would do what @axelson proposed but also think in a slightly different direction. If you can afford to use cached external resources, and if you expose an HTTP interface, why don’t you employ an HTTP cache, like Varnish. You can make use of cache-control and etag headers and lift the responsibility off your application.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews