From fb01bbb290d37f491d603bdf015d994a39e153c7 Mon Sep 17 00:00:00 2001 From: miraikumiko Date: Mon, 21 Apr 2025 10:26:21 +0200 Subject: Init --- lib/nulla.ex | 9 + lib/nulla/application.ex | 36 ++ lib/nulla/mailer.ex | 3 + lib/nulla/repo.ex | 5 + lib/nulla_web.ex | 116 ++++ lib/nulla_web/components/core_components.ex | 676 +++++++++++++++++++++ lib/nulla_web/components/layouts.ex | 14 + lib/nulla_web/components/layouts/app.html.heex | 32 + lib/nulla_web/components/layouts/root.html.heex | 17 + lib/nulla_web/controllers/error_html.ex | 24 + lib/nulla_web/controllers/error_json.ex | 21 + lib/nulla_web/controllers/page_controller.ex | 9 + lib/nulla_web/controllers/page_html.ex | 10 + lib/nulla_web/controllers/page_html/home.html.heex | 222 +++++++ lib/nulla_web/endpoint.ex | 53 ++ lib/nulla_web/gettext.ex | 25 + lib/nulla_web/router.ex | 44 ++ lib/nulla_web/telemetry.ex | 93 +++ 18 files changed, 1409 insertions(+) create mode 100644 lib/nulla.ex create mode 100644 lib/nulla/application.ex create mode 100644 lib/nulla/mailer.ex create mode 100644 lib/nulla/repo.ex create mode 100644 lib/nulla_web.ex create mode 100644 lib/nulla_web/components/core_components.ex create mode 100644 lib/nulla_web/components/layouts.ex create mode 100644 lib/nulla_web/components/layouts/app.html.heex create mode 100644 lib/nulla_web/components/layouts/root.html.heex create mode 100644 lib/nulla_web/controllers/error_html.ex create mode 100644 lib/nulla_web/controllers/error_json.ex create mode 100644 lib/nulla_web/controllers/page_controller.ex create mode 100644 lib/nulla_web/controllers/page_html.ex create mode 100644 lib/nulla_web/controllers/page_html/home.html.heex create mode 100644 lib/nulla_web/endpoint.ex create mode 100644 lib/nulla_web/gettext.ex create mode 100644 lib/nulla_web/router.ex create mode 100644 lib/nulla_web/telemetry.ex (limited to 'lib') diff --git a/lib/nulla.ex b/lib/nulla.ex new file mode 100644 index 0000000..ceb0132 --- /dev/null +++ b/lib/nulla.ex @@ -0,0 +1,9 @@ +defmodule Nulla do + @moduledoc """ + Nulla keeps the contexts that define your domain + and business logic. + + Contexts are also responsible for managing your data, regardless + if it comes from the database, an external API or others. + """ +end diff --git a/lib/nulla/application.ex b/lib/nulla/application.ex new file mode 100644 index 0000000..7e86858 --- /dev/null +++ b/lib/nulla/application.ex @@ -0,0 +1,36 @@ +defmodule Nulla.Application do + # See https://hexdocs.pm/elixir/Application.html + # for more information on OTP Applications + @moduledoc false + + use Application + + @impl true + def start(_type, _args) do + children = [ + NullaWeb.Telemetry, + Nulla.Repo, + {DNSCluster, query: Application.get_env(:nulla, :dns_cluster_query) || :ignore}, + {Phoenix.PubSub, name: Nulla.PubSub}, + # Start the Finch HTTP client for sending emails + {Finch, name: Nulla.Finch}, + # Start a worker by calling: Nulla.Worker.start_link(arg) + # {Nulla.Worker, arg}, + # Start to serve requests, typically the last entry + NullaWeb.Endpoint + ] + + # See https://hexdocs.pm/elixir/Supervisor.html + # for other strategies and supported options + opts = [strategy: :one_for_one, name: Nulla.Supervisor] + Supervisor.start_link(children, opts) + end + + # Tell Phoenix to update the endpoint configuration + # whenever the application is updated. + @impl true + def config_change(changed, _new, removed) do + NullaWeb.Endpoint.config_change(changed, removed) + :ok + end +end diff --git a/lib/nulla/mailer.ex b/lib/nulla/mailer.ex new file mode 100644 index 0000000..53f4221 --- /dev/null +++ b/lib/nulla/mailer.ex @@ -0,0 +1,3 @@ +defmodule Nulla.Mailer do + use Swoosh.Mailer, otp_app: :nulla +end diff --git a/lib/nulla/repo.ex b/lib/nulla/repo.ex new file mode 100644 index 0000000..76b8b7a --- /dev/null +++ b/lib/nulla/repo.ex @@ -0,0 +1,5 @@ +defmodule Nulla.Repo do + use Ecto.Repo, + otp_app: :nulla, + adapter: Ecto.Adapters.Postgres +end diff --git a/lib/nulla_web.ex b/lib/nulla_web.ex new file mode 100644 index 0000000..862aea6 --- /dev/null +++ b/lib/nulla_web.ex @@ -0,0 +1,116 @@ +defmodule NullaWeb do + @moduledoc """ + The entrypoint for defining your web interface, such + as controllers, components, channels, and so on. + + This can be used in your application as: + + use NullaWeb, :controller + use NullaWeb, :html + + The definitions below will be executed for every controller, + component, etc, so keep them short and clean, focused + on imports, uses and aliases. + + Do NOT define functions inside the quoted expressions + below. Instead, define additional modules and import + those modules here. + """ + + def static_paths, do: ~w(assets fonts images favicon.ico robots.txt) + + def router do + quote do + use Phoenix.Router, helpers: false + + # Import common connection and controller functions to use in pipelines + import Plug.Conn + import Phoenix.Controller + import Phoenix.LiveView.Router + end + end + + def channel do + quote do + use Phoenix.Channel + end + end + + def controller do + quote do + use Phoenix.Controller, + formats: [:html, :json], + layouts: [html: NullaWeb.Layouts] + + use Gettext, backend: NullaWeb.Gettext + + import Plug.Conn + + unquote(verified_routes()) + end + end + + def live_view do + quote do + use Phoenix.LiveView, + layout: {NullaWeb.Layouts, :app} + + unquote(html_helpers()) + end + end + + def live_component do + quote do + use Phoenix.LiveComponent + + unquote(html_helpers()) + end + end + + def html do + quote do + use Phoenix.Component + + # Import convenience functions from controllers + import Phoenix.Controller, + only: [get_csrf_token: 0, view_module: 1, view_template: 1] + + # Include general helpers for rendering HTML + unquote(html_helpers()) + end + end + + defp html_helpers do + quote do + # Translation + use Gettext, backend: NullaWeb.Gettext + + # HTML escaping functionality + import Phoenix.HTML + # Core UI components + import NullaWeb.CoreComponents + + # Shortcut for generating JS commands + alias Phoenix.LiveView.JS + + # Routes generation with the ~p sigil + unquote(verified_routes()) + end + end + + def verified_routes do + quote do + use Phoenix.VerifiedRoutes, + endpoint: NullaWeb.Endpoint, + router: NullaWeb.Router, + statics: NullaWeb.static_paths() + end + end + + @doc """ + When used, dispatch to the appropriate controller/live_view/etc. + """ + defmacro __using__(which) when is_atom(which) do + apply(__MODULE__, which, []) + end +end diff --git a/lib/nulla_web/components/core_components.ex b/lib/nulla_web/components/core_components.ex new file mode 100644 index 0000000..3687e1e --- /dev/null +++ b/lib/nulla_web/components/core_components.ex @@ -0,0 +1,676 @@ +defmodule NullaWeb.CoreComponents do + @moduledoc """ + Provides core UI components. + + At first glance, this module may seem daunting, but its goal is to provide + core building blocks for your application, such as modals, tables, and + forms. The components consist mostly of markup and are well-documented + with doc strings and declarative assigns. You may customize and style + them in any way you want, based on your application growth and needs. + + The default components use Tailwind CSS, a utility-first CSS framework. + See the [Tailwind CSS documentation](https://tailwindcss.com) to learn + how to customize them or feel free to swap in another framework altogether. + + Icons are provided by [heroicons](https://heroicons.com). See `icon/1` for usage. + """ + use Phoenix.Component + use Gettext, backend: NullaWeb.Gettext + + alias Phoenix.LiveView.JS + + @doc """ + Renders a modal. + + ## Examples + + <.modal id="confirm-modal"> + This is a modal. + + + JS commands may be passed to the `:on_cancel` to configure + the closing/cancel event, for example: + + <.modal id="confirm" on_cancel={JS.navigate(~p"/posts")}> + This is another modal. + + + """ + attr :id, :string, required: true + attr :show, :boolean, default: false + attr :on_cancel, JS, default: %JS{} + slot :inner_block, required: true + + def modal(assigns) do + ~H""" + + """ + end + + def input(%{type: "select"} = assigns) do + ~H""" +
+ <.label for={@id}>{@label} + + <.error :for={msg <- @errors}>{msg} +
+ """ + end + + def input(%{type: "textarea"} = assigns) do + ~H""" +
+ <.label for={@id}>{@label} + + <.error :for={msg <- @errors}>{msg} +
+ """ + end + + # All other inputs text, datetime-local, url, password, etc. are handled here... + def input(assigns) do + ~H""" +
+ <.label for={@id}>{@label} + + <.error :for={msg <- @errors}>{msg} +
+ """ + end + + @doc """ + Renders a label. + """ + attr :for, :string, default: nil + slot :inner_block, required: true + + def label(assigns) do + ~H""" + + """ + end + + @doc """ + Generates a generic error message. + """ + slot :inner_block, required: true + + def error(assigns) do + ~H""" +

+ <.icon name="hero-exclamation-circle-mini" class="mt-0.5 h-5 w-5 flex-none" /> + {render_slot(@inner_block)} +

+ """ + end + + @doc """ + Renders a header with title. + """ + attr :class, :string, default: nil + + slot :inner_block, required: true + slot :subtitle + slot :actions + + def header(assigns) do + ~H""" +
+
+

+ {render_slot(@inner_block)} +

+

+ {render_slot(@subtitle)} +

+
+
{render_slot(@actions)}
+
+ """ + end + + @doc ~S""" + Renders a table with generic styling. + + ## Examples + + <.table id="users" rows={@users}> + <:col :let={user} label="id">{user.id} + <:col :let={user} label="username">{user.username} + + """ + attr :id, :string, required: true + attr :rows, :list, required: true + attr :row_id, :any, default: nil, doc: "the function for generating the row id" + attr :row_click, :any, default: nil, doc: "the function for handling phx-click on each row" + + attr :row_item, :any, + default: &Function.identity/1, + doc: "the function for mapping each row before calling the :col and :action slots" + + slot :col, required: true do + attr :label, :string + end + + slot :action, doc: "the slot for showing user actions in the last table column" + + def table(assigns) do + assigns = + with %{rows: %Phoenix.LiveView.LiveStream{}} <- assigns do + assign(assigns, row_id: assigns.row_id || fn {id, _item} -> id end) + end + + ~H""" +
+ + + + + + + + + + + + + +
{col[:label]} + {gettext("Actions")} +
+
+ + + {render_slot(col, @row_item.(row))} + +
+
+
+ + + {render_slot(action, @row_item.(row))} + +
+
+
+ """ + end + + @doc """ + Renders a data list. + + ## Examples + + <.list> + <:item title="Title">{@post.title} + <:item title="Views">{@post.views} + + """ + slot :item, required: true do + attr :title, :string, required: true + end + + def list(assigns) do + ~H""" +
+
+
+
{item.title}
+
{render_slot(item)}
+
+
+
+ """ + end + + @doc """ + Renders a back navigation link. + + ## Examples + + <.back navigate={~p"/posts"}>Back to posts + """ + attr :navigate, :any, required: true + slot :inner_block, required: true + + def back(assigns) do + ~H""" +
+ <.link + navigate={@navigate} + class="text-sm font-semibold leading-6 text-zinc-900 hover:text-zinc-700" + > + <.icon name="hero-arrow-left-solid" class="h-3 w-3" /> + {render_slot(@inner_block)} + +
+ """ + end + + @doc """ + Renders a [Heroicon](https://heroicons.com). + + Heroicons come in three styles – outline, solid, and mini. + By default, the outline style is used, but solid and mini may + be applied by using the `-solid` and `-mini` suffix. + + You can customize the size and colors of the icons by setting + width, height, and background color classes. + + Icons are extracted from the `deps/heroicons` directory and bundled within + your compiled app.css by the plugin in your `assets/tailwind.config.js`. + + ## Examples + + <.icon name="hero-x-mark-solid" /> + <.icon name="hero-arrow-path" class="ml-1 w-3 h-3 animate-spin" /> + """ + attr :name, :string, required: true + attr :class, :string, default: nil + + def icon(%{name: "hero-" <> _} = assigns) do + ~H""" + + """ + end + + ## JS Commands + + def show(js \\ %JS{}, selector) do + JS.show(js, + to: selector, + time: 300, + transition: + {"transition-all transform ease-out duration-300", + "opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95", + "opacity-100 translate-y-0 sm:scale-100"} + ) + end + + def hide(js \\ %JS{}, selector) do + JS.hide(js, + to: selector, + time: 200, + transition: + {"transition-all transform ease-in duration-200", + "opacity-100 translate-y-0 sm:scale-100", + "opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"} + ) + end + + def show_modal(js \\ %JS{}, id) when is_binary(id) do + js + |> JS.show(to: "##{id}") + |> JS.show( + to: "##{id}-bg", + time: 300, + transition: {"transition-all transform ease-out duration-300", "opacity-0", "opacity-100"} + ) + |> show("##{id}-container") + |> JS.add_class("overflow-hidden", to: "body") + |> JS.focus_first(to: "##{id}-content") + end + + def hide_modal(js \\ %JS{}, id) do + js + |> JS.hide( + to: "##{id}-bg", + transition: {"transition-all transform ease-in duration-200", "opacity-100", "opacity-0"} + ) + |> hide("##{id}-container") + |> JS.hide(to: "##{id}", transition: {"block", "block", "hidden"}) + |> JS.remove_class("overflow-hidden", to: "body") + |> JS.pop_focus() + end + + @doc """ + Translates an error message using gettext. + """ + def translate_error({msg, opts}) do + # When using gettext, we typically pass the strings we want + # to translate as a static argument: + # + # # Translate the number of files with plural rules + # dngettext("errors", "1 file", "%{count} files", count) + # + # However the error messages in our forms and APIs are generated + # dynamically, so we need to translate them by calling Gettext + # with our gettext backend as first argument. Translations are + # available in the errors.po file (as we use the "errors" domain). + if count = opts[:count] do + Gettext.dngettext(NullaWeb.Gettext, "errors", msg, msg, count, opts) + else + Gettext.dgettext(NullaWeb.Gettext, "errors", msg, opts) + end + end + + @doc """ + Translates the errors for a field from a keyword list of errors. + """ + def translate_errors(errors, field) when is_list(errors) do + for {^field, {msg, opts}} <- errors, do: translate_error({msg, opts}) + end +end diff --git a/lib/nulla_web/components/layouts.ex b/lib/nulla_web/components/layouts.ex new file mode 100644 index 0000000..0e335fa --- /dev/null +++ b/lib/nulla_web/components/layouts.ex @@ -0,0 +1,14 @@ +defmodule NullaWeb.Layouts do + @moduledoc """ + This module holds different layouts used by your application. + + See the `layouts` directory for all templates available. + The "root" layout is a skeleton rendered as part of the + application router. The "app" layout is set as the default + layout on both `use NullaWeb, :controller` and + `use NullaWeb, :live_view`. + """ + use NullaWeb, :html + + embed_templates "layouts/*" +end diff --git a/lib/nulla_web/components/layouts/app.html.heex b/lib/nulla_web/components/layouts/app.html.heex new file mode 100644 index 0000000..3b3b607 --- /dev/null +++ b/lib/nulla_web/components/layouts/app.html.heex @@ -0,0 +1,32 @@ +
+
+
+ + + +

+ v{Application.spec(:phoenix, :vsn)} +

+
+ +
+
+
+
+ <.flash_group flash={@flash} /> + {@inner_content} +
+
diff --git a/lib/nulla_web/components/layouts/root.html.heex b/lib/nulla_web/components/layouts/root.html.heex new file mode 100644 index 0000000..ea6a36d --- /dev/null +++ b/lib/nulla_web/components/layouts/root.html.heex @@ -0,0 +1,17 @@ + + + + + + + <.live_title default="Nulla" suffix=" · Phoenix Framework"> + {assigns[:page_title]} + + + + + + {@inner_content} + + diff --git a/lib/nulla_web/controllers/error_html.ex b/lib/nulla_web/controllers/error_html.ex new file mode 100644 index 0000000..1b0e1c8 --- /dev/null +++ b/lib/nulla_web/controllers/error_html.ex @@ -0,0 +1,24 @@ +defmodule NullaWeb.ErrorHTML do + @moduledoc """ + This module is invoked by your endpoint in case of errors on HTML requests. + + See config/config.exs. + """ + use NullaWeb, :html + + # If you want to customize your error pages, + # uncomment the embed_templates/1 call below + # and add pages to the error directory: + # + # * lib/nulla_web/controllers/error_html/404.html.heex + # * lib/nulla_web/controllers/error_html/500.html.heex + # + # embed_templates "error_html/*" + + # The default is to render a plain text page based on + # the template name. For example, "404.html" becomes + # "Not Found". + def render(template, _assigns) do + Phoenix.Controller.status_message_from_template(template) + end +end diff --git a/lib/nulla_web/controllers/error_json.ex b/lib/nulla_web/controllers/error_json.ex new file mode 100644 index 0000000..be9d1c3 --- /dev/null +++ b/lib/nulla_web/controllers/error_json.ex @@ -0,0 +1,21 @@ +defmodule NullaWeb.ErrorJSON do + @moduledoc """ + This module is invoked by your endpoint in case of errors on JSON requests. + + See config/config.exs. + """ + + # If you want to customize a particular status code, + # you may add your own clauses, such as: + # + # def render("500.json", _assigns) do + # %{errors: %{detail: "Internal Server Error"}} + # end + + # By default, Phoenix returns the status message from + # the template name. For example, "404.json" becomes + # "Not Found". + def render(template, _assigns) do + %{errors: %{detail: Phoenix.Controller.status_message_from_template(template)}} + end +end diff --git a/lib/nulla_web/controllers/page_controller.ex b/lib/nulla_web/controllers/page_controller.ex new file mode 100644 index 0000000..3daa035 --- /dev/null +++ b/lib/nulla_web/controllers/page_controller.ex @@ -0,0 +1,9 @@ +defmodule NullaWeb.PageController do + use NullaWeb, :controller + + def home(conn, _params) do + # The home page is often custom made, + # so skip the default app layout. + render(conn, :home, layout: false) + end +end diff --git a/lib/nulla_web/controllers/page_html.ex b/lib/nulla_web/controllers/page_html.ex new file mode 100644 index 0000000..fbb6c6e --- /dev/null +++ b/lib/nulla_web/controllers/page_html.ex @@ -0,0 +1,10 @@ +defmodule NullaWeb.PageHTML do + @moduledoc """ + This module contains pages rendered by PageController. + + See the `page_html` directory for all templates available. + """ + use NullaWeb, :html + + embed_templates "page_html/*" +end diff --git a/lib/nulla_web/controllers/page_html/home.html.heex b/lib/nulla_web/controllers/page_html/home.html.heex new file mode 100644 index 0000000..d72b03c --- /dev/null +++ b/lib/nulla_web/controllers/page_html/home.html.heex @@ -0,0 +1,222 @@ +<.flash_group flash={@flash} /> + +
+
+ +

+ Phoenix Framework + + v{Application.spec(:phoenix, :vsn)} + +

+

+ Peace of mind from prototype to production. +

+

+ Build rich, interactive web applications quickly, with less code and fewer moving parts. Join our growing community of developers using Phoenix to craft APIs, HTML5 apps and more, for fun or at scale. +

+ +
+
diff --git a/lib/nulla_web/endpoint.ex b/lib/nulla_web/endpoint.ex new file mode 100644 index 0000000..ca59adb --- /dev/null +++ b/lib/nulla_web/endpoint.ex @@ -0,0 +1,53 @@ +defmodule NullaWeb.Endpoint do + use Phoenix.Endpoint, otp_app: :nulla + + # The session will be stored in the cookie and signed, + # this means its contents can be read but not tampered with. + # Set :encryption_salt if you would also like to encrypt it. + @session_options [ + store: :cookie, + key: "_nulla_key", + signing_salt: "/+24FZnt", + same_site: "Lax" + ] + + socket "/live", Phoenix.LiveView.Socket, + websocket: [connect_info: [session: @session_options]], + longpoll: [connect_info: [session: @session_options]] + + # Serve at "/" the static files from "priv/static" directory. + # + # You should set gzip to true if you are running phx.digest + # when deploying your static files in production. + plug Plug.Static, + at: "/", + from: :nulla, + gzip: false, + only: NullaWeb.static_paths() + + # Code reloading can be explicitly enabled under the + # :code_reloader configuration of your endpoint. + if code_reloading? do + socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket + plug Phoenix.LiveReloader + plug Phoenix.CodeReloader + plug Phoenix.Ecto.CheckRepoStatus, otp_app: :nulla + end + + plug Phoenix.LiveDashboard.RequestLogger, + param_key: "request_logger", + cookie_key: "request_logger" + + plug Plug.RequestId + plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint] + + plug Plug.Parsers, + parsers: [:urlencoded, :multipart, :json], + pass: ["*/*"], + json_decoder: Phoenix.json_library() + + plug Plug.MethodOverride + plug Plug.Head + plug Plug.Session, @session_options + plug NullaWeb.Router +end diff --git a/lib/nulla_web/gettext.ex b/lib/nulla_web/gettext.ex new file mode 100644 index 0000000..45a98d5 --- /dev/null +++ b/lib/nulla_web/gettext.ex @@ -0,0 +1,25 @@ +defmodule NullaWeb.Gettext do + @moduledoc """ + A module providing Internationalization with a gettext-based API. + + By using [Gettext](https://hexdocs.pm/gettext), your module compiles translations + that you can use in your application. To use this Gettext backend module, + call `use Gettext` and pass it as an option: + + use Gettext, backend: NullaWeb.Gettext + + # Simple translation + gettext("Here is the string to translate") + + # Plural translation + ngettext("Here is the string to translate", + "Here are the strings to translate", + 3) + + # Domain-based translation + dgettext("errors", "Here is the error message to translate") + + See the [Gettext Docs](https://hexdocs.pm/gettext) for detailed usage. + """ + use Gettext.Backend, otp_app: :nulla +end diff --git a/lib/nulla_web/router.ex b/lib/nulla_web/router.ex new file mode 100644 index 0000000..95706c9 --- /dev/null +++ b/lib/nulla_web/router.ex @@ -0,0 +1,44 @@ +defmodule NullaWeb.Router do + use NullaWeb, :router + + pipeline :browser do + plug :accepts, ["html"] + plug :fetch_session + plug :fetch_live_flash + plug :put_root_layout, html: {NullaWeb.Layouts, :root} + plug :protect_from_forgery + plug :put_secure_browser_headers + end + + pipeline :api do + plug :accepts, ["json"] + end + + scope "/", NullaWeb do + pipe_through :browser + + get "/", PageController, :home + end + + # Other scopes may use custom stacks. + # scope "/api", NullaWeb do + # pipe_through :api + # end + + # Enable LiveDashboard and Swoosh mailbox preview in development + if Application.compile_env(:nulla, :dev_routes) do + # If you want to use the LiveDashboard in production, you should put + # it behind authentication and allow only admins to access it. + # If your application does not have an admins-only section yet, + # you can use Plug.BasicAuth to set up some basic authentication + # as long as you are also using SSL (which you should anyway). + import Phoenix.LiveDashboard.Router + + scope "/dev" do + pipe_through :browser + + live_dashboard "/dashboard", metrics: NullaWeb.Telemetry + forward "/mailbox", Plug.Swoosh.MailboxPreview + end + end +end diff --git a/lib/nulla_web/telemetry.ex b/lib/nulla_web/telemetry.ex new file mode 100644 index 0000000..0b8fc96 --- /dev/null +++ b/lib/nulla_web/telemetry.ex @@ -0,0 +1,93 @@ +defmodule NullaWeb.Telemetry do + use Supervisor + import Telemetry.Metrics + + def start_link(arg) do + Supervisor.start_link(__MODULE__, arg, name: __MODULE__) + end + + @impl true + def init(_arg) do + children = [ + # Telemetry poller will execute the given period measurements + # every 10_000ms. Learn more here: https://hexdocs.pm/telemetry_metrics + {:telemetry_poller, measurements: periodic_measurements(), period: 10_000} + # Add reporters as children of your supervision tree. + # {Telemetry.Metrics.ConsoleReporter, metrics: metrics()} + ] + + Supervisor.init(children, strategy: :one_for_one) + end + + def metrics do + [ + # Phoenix Metrics + summary("phoenix.endpoint.start.system_time", + unit: {:native, :millisecond} + ), + summary("phoenix.endpoint.stop.duration", + unit: {:native, :millisecond} + ), + summary("phoenix.router_dispatch.start.system_time", + tags: [:route], + unit: {:native, :millisecond} + ), + summary("phoenix.router_dispatch.exception.duration", + tags: [:route], + unit: {:native, :millisecond} + ), + summary("phoenix.router_dispatch.stop.duration", + tags: [:route], + unit: {:native, :millisecond} + ), + summary("phoenix.socket_connected.duration", + unit: {:native, :millisecond} + ), + sum("phoenix.socket_drain.count"), + summary("phoenix.channel_joined.duration", + unit: {:native, :millisecond} + ), + summary("phoenix.channel_handled_in.duration", + tags: [:event], + unit: {:native, :millisecond} + ), + + # Database Metrics + summary("nulla.repo.query.total_time", + unit: {:native, :millisecond}, + description: "The sum of the other measurements" + ), + summary("nulla.repo.query.decode_time", + unit: {:native, :millisecond}, + description: "The time spent decoding the data received from the database" + ), + summary("nulla.repo.query.query_time", + unit: {:native, :millisecond}, + description: "The time spent executing the query" + ), + summary("nulla.repo.query.queue_time", + unit: {:native, :millisecond}, + description: "The time spent waiting for a database connection" + ), + summary("nulla.repo.query.idle_time", + unit: {:native, :millisecond}, + description: + "The time the connection spent waiting before being checked out for the query" + ), + + # VM Metrics + summary("vm.memory.total", unit: {:byte, :kilobyte}), + summary("vm.total_run_queue_lengths.total"), + summary("vm.total_run_queue_lengths.cpu"), + summary("vm.total_run_queue_lengths.io") + ] + end + + defp periodic_measurements do + [ + # A module, function and arguments to be invoked periodically. + # This function must call :telemetry.execute/3 and a metric must be added above. + # {NullaWeb, :count_users, []} + ] + end +end -- cgit v1.2.3