From 9db119807a3af29c4c20985e0c408dc75f6deb7c Mon Sep 17 00:00:00 2001 From: CJ_Clippy Date: Sat, 11 Jan 2025 05:26:38 -0800 Subject: [PATCH] use kamal --- config/deploy.yml | 6 +- docker-compose.yml | 103 +++ dockerfiles/bright.dockerfile | 12 +- packages/scripts/build-test.sh | 21 + packages/scripts/capture-integration.sh | 25 + packages/scripts/drupal-init-wrapper.sh | 8 + packages/scripts/drupal-init.sh | 24 + packages/scripts/flux-bootstrap.sh | 26 + packages/scripts/k8s-metrics.sh | 12 + packages/scripts/k8s-namespaces.sh | 7 + packages/scripts/k8s-secrets.sh | 210 ++++++ packages/scripts/kind-load.sh | 16 + packages/scripts/kind-with-local-registry.sh | 81 +++ .../scripts/pgadmin-connection-profile.json | 16 + packages/scripts/pgadmin-import-connection.sh | 20 + packages/scripts/postgres-backup.sh | 23 + packages/scripts/postgres-create-bright.sh | 8 + .../scripts/postgres-create-superstreamer.sh | 8 + packages/scripts/postgres-create.sh | 107 +++ packages/scripts/postgres-drop.sh | 16 + packages/scripts/postgres-migrations.sh | 13 + packages/scripts/postgres-refresh.sh | 9 + packages/scripts/postgres-restore.sh | 32 + packages/scripts/velero-create.sh | 11 + services/bright/assets/package.json | 6 + services/bright/lib/bright/platforms.ex | 104 +++ services/bright/lib/free_oban_ui.ex | 9 + .../bright/lib/free_oban_ui/application.ex | 37 + services/bright/lib/free_oban_ui/mailer.ex | 3 + services/bright/lib/free_oban_ui/repo.ex | 5 + services/bright/lib/free_oban_ui_web.ex | 113 +++ .../components/core_components.ex | 676 ++++++++++++++++++ .../free_oban_ui_web/components/layouts.ex | 14 + .../components/layouts/app.html.heex | 2 + .../components/layouts/root.html.heex | 17 + .../controllers/error_html.ex | 24 + .../controllers/error_json.ex | 21 + .../controllers/page_controller.ex | 9 + .../free_oban_ui_web/controllers/page_html.ex | 10 + .../controllers/page_html/home.html.heex | 222 ++++++ .../bright/lib/free_oban_ui_web/endpoint.ex | 53 ++ .../bright/lib/free_oban_ui_web/gettext.ex | 24 + .../free_oban_ui_web/live/oban_live/index.ex | 233 ++++++ .../live/oban_live/index.html.heex | 244 +++++++ .../bright/lib/free_oban_ui_web/router.ex | 51 ++ .../bright/lib/free_oban_ui_web/telemetry.ex | 92 +++ services/bright/test.txt | 3 + 47 files changed, 2773 insertions(+), 13 deletions(-) create mode 100644 docker-compose.yml create mode 100755 packages/scripts/build-test.sh create mode 100755 packages/scripts/capture-integration.sh create mode 100755 packages/scripts/drupal-init-wrapper.sh create mode 100755 packages/scripts/drupal-init.sh create mode 100755 packages/scripts/flux-bootstrap.sh create mode 100755 packages/scripts/k8s-metrics.sh create mode 100755 packages/scripts/k8s-namespaces.sh create mode 100755 packages/scripts/k8s-secrets.sh create mode 100755 packages/scripts/kind-load.sh create mode 100755 packages/scripts/kind-with-local-registry.sh create mode 100644 packages/scripts/pgadmin-connection-profile.json create mode 100755 packages/scripts/pgadmin-import-connection.sh create mode 100644 packages/scripts/postgres-backup.sh create mode 100644 packages/scripts/postgres-create-bright.sh create mode 100644 packages/scripts/postgres-create-superstreamer.sh create mode 100755 packages/scripts/postgres-create.sh create mode 100644 packages/scripts/postgres-drop.sh create mode 100755 packages/scripts/postgres-migrations.sh create mode 100644 packages/scripts/postgres-refresh.sh create mode 100755 packages/scripts/postgres-restore.sh create mode 100755 packages/scripts/velero-create.sh create mode 100644 services/bright/assets/package.json create mode 100644 services/bright/lib/bright/platforms.ex create mode 100644 services/bright/lib/free_oban_ui.ex create mode 100644 services/bright/lib/free_oban_ui/application.ex create mode 100644 services/bright/lib/free_oban_ui/mailer.ex create mode 100644 services/bright/lib/free_oban_ui/repo.ex create mode 100644 services/bright/lib/free_oban_ui_web.ex create mode 100644 services/bright/lib/free_oban_ui_web/components/core_components.ex create mode 100644 services/bright/lib/free_oban_ui_web/components/layouts.ex create mode 100644 services/bright/lib/free_oban_ui_web/components/layouts/app.html.heex create mode 100644 services/bright/lib/free_oban_ui_web/components/layouts/root.html.heex create mode 100644 services/bright/lib/free_oban_ui_web/controllers/error_html.ex create mode 100644 services/bright/lib/free_oban_ui_web/controllers/error_json.ex create mode 100644 services/bright/lib/free_oban_ui_web/controllers/page_controller.ex create mode 100644 services/bright/lib/free_oban_ui_web/controllers/page_html.ex create mode 100644 services/bright/lib/free_oban_ui_web/controllers/page_html/home.html.heex create mode 100644 services/bright/lib/free_oban_ui_web/endpoint.ex create mode 100644 services/bright/lib/free_oban_ui_web/gettext.ex create mode 100644 services/bright/lib/free_oban_ui_web/live/oban_live/index.ex create mode 100644 services/bright/lib/free_oban_ui_web/live/oban_live/index.html.heex create mode 100644 services/bright/lib/free_oban_ui_web/router.ex create mode 100644 services/bright/lib/free_oban_ui_web/telemetry.ex create mode 100644 services/bright/test.txt diff --git a/config/deploy.yml b/config/deploy.yml index 983522b..3104f7d 100644 --- a/config/deploy.yml +++ b/config/deploy.yml @@ -22,9 +22,7 @@ servers: # Note: If using Cloudflare, set encryption mode in SSL/TLS setting to "Full" to enable CF-to-app encryption. proxy: ssl: true - host: app.example.com - # Proxy connects to your container on port 80 by default. - # app_port: 3000 + host: bright.futureporn.net # Credentials for your image host. registry: @@ -62,7 +60,7 @@ builder: # shell: app exec --interactive --reuse "bash" ssh: - keys: ["~/.ssh/upstart"] + keys: ["~/.ssh/futureporn"] # Use a persistent storage volume. # diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..af4d967 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,103 @@ +services: + + superstreamer: + image: alpine + environment: + - PUBLIC_API_ENDPOINT=http://localhost:52001 + - PUBLIC_STITCHER_ENDPOINT=http://localhost:52002 + - REDIS_HOST=redis + - REDIS_PORT=6379 + - DATABASE_URI=postgres://postgres:password@db:5432/sprs + env_file: .kamal/secrets.development + + superstreamer-app: + extends: superstreamer + image: "superstreamerapp/app:alpha" + ports: + - 52000:52000 + + + superstreamer-api: + extends: superstreamer + image: "superstreamerapp/api:alpha" + restart: always + ports: + - 52001:52001 + depends_on: + - db + - redis + + superstreamer-stitcher: + extends: superstreamer + image: "superstreamerapp/stitcher:alpha" + restart: always + ports: + - 52002:52002 + depends_on: + - redis + + superstreamer-artisan: + extends: superstreamer + image: "superstreamerapp/artisan:alpha" + restart: always + depends_on: + - redis + + redis: + image: redis/redis-stack-server:7.2.0-v6 + ports: + - 127.0.0.1:6379:6379 + healthcheck: + test: ["CMD", "redis-cli", "--raw", "incr", "ping"] + volumes: + - redis_data:/data + + bright: + image: futureporn/bright:latest + container_name: bright + build: + context: . + dockerfile: dockerfiles/bright.dockerfile + working_dir: /app + environment: + MIX_ENV: dev + PORT: "4000" + env_file: + - .kamal/secrets.development + ports: + - '4000:4000' + depends_on: + - db + volumes: + - ./services/bright:/app + develop: + watch: + - action: sync+restart + path: ./services/bright/mix.exs + target: /app + + db: + image: postgres:15 + environment: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: password + POSTGRES_DB: phoenix_dev + volumes: + - pg_data:/var/lib/postgresql/data + ports: + - '5432:5432' + + pgadmin: + image: dpage/pgadmin4 + ports: + - '5050:5050' + depends_on: + - db + environment: + PGADMIN_LISTEN_PORT: "5050" + env_file: + - .kamal/secrets.development + +volumes: + pg_data: + redis_data: \ No newline at end of file diff --git a/dockerfiles/bright.dockerfile b/dockerfiles/bright.dockerfile index 70864d7..f3db271 100644 --- a/dockerfiles/bright.dockerfile +++ b/dockerfiles/bright.dockerfile @@ -24,15 +24,9 @@ FROM elixir:1.17.2-alpine AS dev -# install build dependencies -RUN apt-get update -y \ - && apt-get install -y build-essential \ - && apt-get install -y git \ - && apt-get install -y inotify-tools \ - && apt-get clean \ - && rm -f /var/lib/apt/lists/*_* \ - && mkdir /home/user && \ - chown 1000.1000 /home/user +RUN apk add git inotify-tools \ + && mkdir /home/user \ + && chown 1000.1000 /home/user ENV HOME=/home/user USER 1000:1000 diff --git a/packages/scripts/build-test.sh b/packages/scripts/build-test.sh new file mode 100755 index 0000000..3cd0be6 --- /dev/null +++ b/packages/scripts/build-test.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +postgres_pod_name=postgresql-primary-0 + +if [ -z $POSTGRES_PASSWORD ]; then + echo "POSTGRES_PASSWORD was missing in env. Are you executing this script via Tilt? (that is the intended method)" + exit 5 +fi + +kubectl -n futureporn exec "${postgres_pod_name}" -- env PGPASSWORD=${POSTGRES_PASSWORD} psql -U postgres -d futureporn --command " +SELECT graphile_worker.add_job( + 'combine_video_segments', + payload := json_build_object( + 's3_manifest', json_build_array( + json_build_object('id', '4_z7d53875ff1c32a1983d30b18_f118989ca296359da_d20240809_m205858_c000_v0001408_t0033_u01723237138038', 'key', 'mock-stream0.mp4'), + json_build_object('id', '4_z7d53875ff1c32a1983d30b18_f107c0649cef835e4_d20240809_m205859_c000_v0001406_t0016_u01723237139170', 'key', 'mock-stream1.mp4'), + json_build_object('id', '4_z7d53875ff1c32a1983d30b18_f10651c62f4ca1b2f_d20240809_m205900_c000_v0001076_t0022_u01723237140217', 'key', 'mock-stream2.mp4') + ) + ), + max_attempts := 3 +);" diff --git a/packages/scripts/capture-integration.sh b/packages/scripts/capture-integration.sh new file mode 100755 index 0000000..f7fe5ae --- /dev/null +++ b/packages/scripts/capture-integration.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +if [ -z "${AUTOMATION_USER_JWT}" ]; then + echo "Error: AUTOMATION_USER_JWT variable is not defined." + exit 1 +fi + +# get a random room +response=$(curl -sL --fail GET http://localhost:8134/chaturbate/random-room) +exitcode=$? +url=$(echo $response | jq -r '.url') +if [[ $exitcode -ne 0 || -z "$response" || -z "$url" ]]; then + echo "failed to get random room. exitcode=${exitcode}, response=${response}, url=${url}" + exit $exitcode +fi +echo "Random online chaturbate room url=${url}" + + + +# create a recording +curl -sL -H "Authorization: Bearer ${AUTOMATION_USER_JWT}" \ + -H "Content-Type: application/json" \ + -d '{"url": "'"${url}"'"}' \ + http://localhost:9000/recordings +echo "recording created" \ No newline at end of file diff --git a/packages/scripts/drupal-init-wrapper.sh b/packages/scripts/drupal-init-wrapper.sh new file mode 100755 index 0000000..dd4c9f7 --- /dev/null +++ b/packages/scripts/drupal-init-wrapper.sh @@ -0,0 +1,8 @@ +#!/bin/bash + + +namespace=futureporn +pod_name=$(kubectl --namespace futureporn get pods -l app.kubernetes.io/name=drupal -o custom-columns=":metadata.name" --no-headers) + +kubectl -n "${namespace}" cp ./scripts/drupal-init.sh "${pod_name}:/tmp/drupal-init.sh" +kubectl -n "${namespace}" exec "${pod_name}" -- bash -c "/tmp/drupal-init.sh" diff --git a/packages/scripts/drupal-init.sh b/packages/scripts/drupal-init.sh new file mode 100755 index 0000000..e6bfc1e --- /dev/null +++ b/packages/scripts/drupal-init.sh @@ -0,0 +1,24 @@ + +#!/bin/bash + +# maybe https://www.drupal.org/project/extlink +# maybe https://www.drupal.org/project/seckit +# maybe https://www.drupal.org/project/slick +# maybe https://www.drupal.org/project/fontawesome + +cd /opt/bitnami/drupal +composer require \ + 'drupal/bulma:^1.0' \ + 'drupal/file_uploader_uppy:^1.0' \ + 'drupal/backup_migrate:^5.0' \ + 'drupal/cdn:^4.1' \ + 'drupal/s3fs:^3.6' \ + 'drupal/video:^3.0' \ + 'league/commonmark ^1.0' \ + 'drupal/markdown:^3.0' \ + 'drupal/color_field:^3.0' \ + 'drupal/pathauto:^1.13' \ + 'drupal/extlink:^2.0' +drush updatedb +drush theme:install bulma +drush pm:install -y file_uploader_uppy backup_migrate cdn cdn_ui s3fs video markdown diff --git a/packages/scripts/flux-bootstrap.sh b/packages/scripts/flux-bootstrap.sh new file mode 100755 index 0000000..bfe9b2a --- /dev/null +++ b/packages/scripts/flux-bootstrap.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +if [ -z "${ENV}" ]; then \ + echo "Error: ENV variable is not defined. Please set to one of development|staging|production"; exit 1; \ +fi + +if [ "${ENV}" == "development" ]; then \ + echo "Flux is not used in development environment. Skipping." + exit 0 +fi + +# flux bootstrap git \ +# --kubeconfig /home/cj/.kube/vke.yaml \ +# --url=https://gitea.futureporn.net/futureporn/fp.git \ +# --branch=main \ +# --username=cj_clippy \ +# --token-auth=true \ +# --path=clusters/production + +## --silent avoids the [Yes|no] prompt +flux bootstrap git \ + --silent \ + --url="ssh://git@gitea.futureporn.net:2222/futureporn/fp" \ + --branch=main \ + --path="flux/clusters/$ENV" \ + --private-key-file=/home/cj/.ssh/fp-flux \ No newline at end of file diff --git a/packages/scripts/k8s-metrics.sh b/packages/scripts/k8s-metrics.sh new file mode 100755 index 0000000..49f1219 --- /dev/null +++ b/packages/scripts/k8s-metrics.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +if [ -z "${ENV}" ]; then \ + echo "Error: ENV variable is not defined. Please set to one of development|staging|production"; exit 1; \ +fi + +if [ "${ENV}" == "development" ]; then \ + echo "k8s metrics are not used in development environment. Skipping." + exit 0 +fi + +kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml \ No newline at end of file diff --git a/packages/scripts/k8s-namespaces.sh b/packages/scripts/k8s-namespaces.sh new file mode 100755 index 0000000..0bf56ef --- /dev/null +++ b/packages/scripts/k8s-namespaces.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +kubectl create namespace cert-manager +kubectl create namespace futureporn +kubectl create namespace velero + +exit 0 # important to keep the Makefile chain going even if namespaces already existed and kubectl returned non-zero \ No newline at end of file diff --git a/packages/scripts/k8s-secrets.sh b/packages/scripts/k8s-secrets.sh new file mode 100755 index 0000000..ec2ad7a --- /dev/null +++ b/packages/scripts/k8s-secrets.sh @@ -0,0 +1,210 @@ +#!/bin/bash + + +## @todo switch to infisical +# ns=futureporn +# kubectl --namespace $ns delete secret universal-auth-credentials --ignore-not-found +# kubectl --namespace $ns create secret generic universal-auth-credentials \ +# --from-literal=clientSecret="${INFISICAL_CLIENT_SECRET}" \ +# --from-literal=clientId="${INFISICAL_CLIENT_ID}" +# echo "@todo remove all the unused secrets" +# exit 256 + +CLOUD_DATA=$(echo -e "[default]\naws_access_key_id: $VELERO_S3_KEY_ID\naws_secret_access_key: $VELERO_S3_ACCESS_KEY" | base64 -w 0) +kubectl --namespace=velero delete secret velero --ignore-not-found +## we do this so helm can adopt our pre-made secret @see https://github.com/helm/helm/pull/7649 +cat </dev/null || true)" != 'true' ]; then + docker run \ + -d --restart=always -p "127.0.0.1:${reg_port}:5000" --network bridge --name "${reg_name}" \ + registry:2 +fi + +# 2. Create kind cluster with containerd registry config dir enabled +# TODO: kind will eventually enable this by default and this patch will +# be unnecessary. +# +# See: +# https://github.com/kubernetes-sigs/kind/issues/2875 +# https://github.com/containerd/containerd/blob/main/docs/cri/config.md#registry-configuration +# See: https://github.com/containerd/containerd/blob/main/docs/hosts.md +cat < list_platforms() + [%Platform{}, ...] + + """ + def list_platforms do + Repo.all(Platform) + end + + @doc """ + Gets a single platform. + + Raises `Ecto.NoResultsError` if the Platform does not exist. + + ## Examples + + iex> get_platform!(123) + %Platform{} + + iex> get_platform!(456) + ** (Ecto.NoResultsError) + + """ + def get_platform!(id), do: Repo.get!(Platform, id) + + @doc """ + Creates a platform. + + ## Examples + + iex> create_platform(%{field: value}) + {:ok, %Platform{}} + + iex> create_platform(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_platform(attrs \\ %{}) do + %Platform{} + |> Platform.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a platform. + + ## Examples + + iex> update_platform(platform, %{field: new_value}) + {:ok, %Platform{}} + + iex> update_platform(platform, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_platform(%Platform{} = platform, attrs) do + platform + |> Platform.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a platform. + + ## Examples + + iex> delete_platform(platform) + {:ok, %Platform{}} + + iex> delete_platform(platform) + {:error, %Ecto.Changeset{}} + + """ + def delete_platform(%Platform{} = platform) do + Repo.delete(platform) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking platform changes. + + ## Examples + + iex> change_platform(platform) + %Ecto.Changeset{data: %Platform{}} + + """ + def change_platform(%Platform{} = platform, attrs \\ %{}) do + Platform.changeset(platform, attrs) + end +end diff --git a/services/bright/lib/free_oban_ui.ex b/services/bright/lib/free_oban_ui.ex new file mode 100644 index 0000000..f42070c --- /dev/null +++ b/services/bright/lib/free_oban_ui.ex @@ -0,0 +1,9 @@ +defmodule FreeObanUi do + @moduledoc """ + FreeObanUi 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/services/bright/lib/free_oban_ui/application.ex b/services/bright/lib/free_oban_ui/application.ex new file mode 100644 index 0000000..79794a2 --- /dev/null +++ b/services/bright/lib/free_oban_ui/application.ex @@ -0,0 +1,37 @@ +defmodule FreeObanUi.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 = [ + FreeObanUiWeb.Telemetry, + FreeObanUi.Repo, + {DNSCluster, query: Application.get_env(:free_oban_ui, :dns_cluster_query) || :ignore}, + {Phoenix.PubSub, name: FreeObanUi.PubSub}, + # Start the Finch HTTP client for sending emails + {Finch, name: FreeObanUi.Finch}, + {Oban, Application.fetch_env!(:free_oban_ui, Oban)}, + # Start a worker by calling: FreeObanUi.Worker.start_link(arg) + # {FreeObanUi.Worker, arg}, + # Start to serve requests, typically the last entry + FreeObanUiWeb.Endpoint + ] + + # See https://hexdocs.pm/elixir/Supervisor.html + # for other strategies and supported options + opts = [strategy: :one_for_one, name: FreeObanUi.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 + FreeObanUiWeb.Endpoint.config_change(changed, removed) + :ok + end +end diff --git a/services/bright/lib/free_oban_ui/mailer.ex b/services/bright/lib/free_oban_ui/mailer.ex new file mode 100644 index 0000000..93ef998 --- /dev/null +++ b/services/bright/lib/free_oban_ui/mailer.ex @@ -0,0 +1,3 @@ +defmodule FreeObanUi.Mailer do + use Swoosh.Mailer, otp_app: :free_oban_ui +end diff --git a/services/bright/lib/free_oban_ui/repo.ex b/services/bright/lib/free_oban_ui/repo.ex new file mode 100644 index 0000000..61b34c9 --- /dev/null +++ b/services/bright/lib/free_oban_ui/repo.ex @@ -0,0 +1,5 @@ +defmodule FreeObanUi.Repo do + use Ecto.Repo, + otp_app: :free_oban_ui, + adapter: Ecto.Adapters.Postgres +end diff --git a/services/bright/lib/free_oban_ui_web.ex b/services/bright/lib/free_oban_ui_web.ex new file mode 100644 index 0000000..90108b9 --- /dev/null +++ b/services/bright/lib/free_oban_ui_web.ex @@ -0,0 +1,113 @@ +defmodule FreeObanUiWeb 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 FreeObanUiWeb, :controller + use FreeObanUiWeb, :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: FreeObanUiWeb.Layouts] + + import Plug.Conn + import FreeObanUiWeb.Gettext + + unquote(verified_routes()) + end + end + + def live_view do + quote do + use Phoenix.LiveView, + layout: {FreeObanUiWeb.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 + # HTML escaping functionality + import Phoenix.HTML + # Core UI components and translation + import FreeObanUiWeb.CoreComponents + import FreeObanUiWeb.Gettext + + # 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: FreeObanUiWeb.Endpoint, + router: FreeObanUiWeb.Router, + statics: FreeObanUiWeb.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/services/bright/lib/free_oban_ui_web/components/core_components.ex b/services/bright/lib/free_oban_ui_web/components/core_components.ex new file mode 100644 index 0000000..8703fdd --- /dev/null +++ b/services/bright/lib/free_oban_ui_web/components/core_components.ex @@ -0,0 +1,676 @@ +defmodule FreeObanUiWeb.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 + + alias Phoenix.LiveView.JS + import FreeObanUiWeb.Gettext + + @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(FreeObanUiWeb.Gettext, "errors", msg, msg, count, opts) + else + Gettext.dgettext(FreeObanUiWeb.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/services/bright/lib/free_oban_ui_web/components/layouts.ex b/services/bright/lib/free_oban_ui_web/components/layouts.ex new file mode 100644 index 0000000..d5aa25a --- /dev/null +++ b/services/bright/lib/free_oban_ui_web/components/layouts.ex @@ -0,0 +1,14 @@ +defmodule FreeObanUiWeb.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 FreeObanUiWeb, :controller` and + `use FreeObanUiWeb, :live_view`. + """ + use FreeObanUiWeb, :html + + embed_templates "layouts/*" +end diff --git a/services/bright/lib/free_oban_ui_web/components/layouts/app.html.heex b/services/bright/lib/free_oban_ui_web/components/layouts/app.html.heex new file mode 100644 index 0000000..5f1d238 --- /dev/null +++ b/services/bright/lib/free_oban_ui_web/components/layouts/app.html.heex @@ -0,0 +1,2 @@ +<.flash_group flash={@flash} /> +<%= @inner_content %> \ No newline at end of file diff --git a/services/bright/lib/free_oban_ui_web/components/layouts/root.html.heex b/services/bright/lib/free_oban_ui_web/components/layouts/root.html.heex new file mode 100644 index 0000000..e633a90 --- /dev/null +++ b/services/bright/lib/free_oban_ui_web/components/layouts/root.html.heex @@ -0,0 +1,17 @@ + + + + + + + <.live_title suffix=" · Phoenix Framework"> + <%= assigns[:page_title] || "FreeObanUi" %> + + + + + + <%= @inner_content %> + + diff --git a/services/bright/lib/free_oban_ui_web/controllers/error_html.ex b/services/bright/lib/free_oban_ui_web/controllers/error_html.ex new file mode 100644 index 0000000..54fe5b4 --- /dev/null +++ b/services/bright/lib/free_oban_ui_web/controllers/error_html.ex @@ -0,0 +1,24 @@ +defmodule FreeObanUiWeb.ErrorHTML do + @moduledoc """ + This module is invoked by your endpoint in case of errors on HTML requests. + + See config/config.exs. + """ + use FreeObanUiWeb, :html + + # If you want to customize your error pages, + # uncomment the embed_templates/1 call below + # and add pages to the error directory: + # + # * lib/free_oban_ui_web/controllers/error_html/404.html.heex + # * lib/free_oban_ui_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/services/bright/lib/free_oban_ui_web/controllers/error_json.ex b/services/bright/lib/free_oban_ui_web/controllers/error_json.ex new file mode 100644 index 0000000..5d5ba21 --- /dev/null +++ b/services/bright/lib/free_oban_ui_web/controllers/error_json.ex @@ -0,0 +1,21 @@ +defmodule FreeObanUiWeb.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/services/bright/lib/free_oban_ui_web/controllers/page_controller.ex b/services/bright/lib/free_oban_ui_web/controllers/page_controller.ex new file mode 100644 index 0000000..3cca7d2 --- /dev/null +++ b/services/bright/lib/free_oban_ui_web/controllers/page_controller.ex @@ -0,0 +1,9 @@ +defmodule FreeObanUiWeb.PageController do + use FreeObanUiWeb, :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/services/bright/lib/free_oban_ui_web/controllers/page_html.ex b/services/bright/lib/free_oban_ui_web/controllers/page_html.ex new file mode 100644 index 0000000..646bb8c --- /dev/null +++ b/services/bright/lib/free_oban_ui_web/controllers/page_html.ex @@ -0,0 +1,10 @@ +defmodule FreeObanUiWeb.PageHTML do + @moduledoc """ + This module contains pages rendered by PageController. + + See the `page_html` directory for all templates available. + """ + use FreeObanUiWeb, :html + + embed_templates "page_html/*" +end diff --git a/services/bright/lib/free_oban_ui_web/controllers/page_html/home.html.heex b/services/bright/lib/free_oban_ui_web/controllers/page_html/home.html.heex new file mode 100644 index 0000000..dc1820b --- /dev/null +++ b/services/bright/lib/free_oban_ui_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/services/bright/lib/free_oban_ui_web/endpoint.ex b/services/bright/lib/free_oban_ui_web/endpoint.ex new file mode 100644 index 0000000..109b149 --- /dev/null +++ b/services/bright/lib/free_oban_ui_web/endpoint.ex @@ -0,0 +1,53 @@ +defmodule FreeObanUiWeb.Endpoint do + use Phoenix.Endpoint, otp_app: :free_oban_ui + + # 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: "_free_oban_ui_key", + signing_salt: "O5fc1lbf", + 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: :free_oban_ui, + gzip: false, + only: FreeObanUiWeb.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: :free_oban_ui + 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 FreeObanUiWeb.Router +end diff --git a/services/bright/lib/free_oban_ui_web/gettext.ex b/services/bright/lib/free_oban_ui_web/gettext.ex new file mode 100644 index 0000000..134539f --- /dev/null +++ b/services/bright/lib/free_oban_ui_web/gettext.ex @@ -0,0 +1,24 @@ +defmodule FreeObanUiWeb.Gettext do + @moduledoc """ + A module providing Internationalization with a gettext-based API. + + By using [Gettext](https://hexdocs.pm/gettext), + your module gains a set of macros for translations, for example: + + import FreeObanUiWeb.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, otp_app: :free_oban_ui +end diff --git a/services/bright/lib/free_oban_ui_web/live/oban_live/index.ex b/services/bright/lib/free_oban_ui_web/live/oban_live/index.ex new file mode 100644 index 0000000..927aef9 --- /dev/null +++ b/services/bright/lib/free_oban_ui_web/live/oban_live/index.ex @@ -0,0 +1,233 @@ +defmodule FreeObanUiWeb.ObanLive.Index do + use FreeObanUiWeb, :live_view + alias FreeObanUi.Repo + import Ecto.Query + + @impl true + def mount(params, _session, socket) do + if connected?(socket) do + :timer.send_interval(2000, self(), :update) + end + + {:ok, + socket + |> assign( + jobs: [], + job: nil, + selected_jobs: MapSet.new(), + auto_refresh: true, + page: params["page"] || "0" + ) + |> assign_params(params) + |> assign_counts() + |> fetch_jobs()} + end + + defp assign_params(socket, params) do + socket + |> assign( + state: params["state"], + queue: params["queue"], + id: params["id"], + page: params["page"] || "0" + ) + end + + @impl true + def handle_params(params, _url, socket) do + {:noreply, + socket + |> assign_params(params) + |> fetch_jobs() + |> apply_action(socket.assigns.live_action, params)} + end + + defp apply_action(socket, :index, _params) do + socket + |> assign(:page_title, "Oban Jobs") + |> assign(:job, nil) + end + + defp apply_action(socket, :show, %{"id" => id}) do + job = get_job(id) + + socket + |> assign(:page_title, "Job #{job.id}") + |> assign(:job, get_job(id)) + end + + @impl true + def handle_event("delete", %{"id" => id}, socket) do + action_job("delete", id) + + {:noreply, + socket + |> put_flash(:info, "Job ##{id} deleted") + |> redirect_back()} + end + + defp action_job(action, id) do + case action do + # Same implementation as `retry` atm. + # Alt: is to overwrite `scheduled_at` timestamp: https://elixirforum.com/t/53920/2 + "run" -> get_job(id) |> Oban.retry_job() + "retry" -> get_job(id) |> Oban.retry_job() + "cancel" -> get_job(id) |> Oban.cancel_job() + "delete" -> get_job(id) |> Repo.delete() + end + end + + @impl true + def handle_event("execute_action", %{"action" => action}, socket) do + id = socket.assigns.job.id + action_job(action, id) + + socket = + if action == "delete" do + socket + |> put_flash(:info, "Job ##{id} deleted") + |> redirect_back() + else + fetch_jobs(socket) + end + + {:noreply, socket} + end + + @impl true + def handle_event("bulk_action", %{"action" => action}, socket) do + Enum.each(socket.assigns.selected_jobs, fn id -> + action_job(action, id) + end) + + {:noreply, socket |> assign(selected_jobs: MapSet.new()) |> fetch_jobs()} + end + + @impl true + def handle_event("paginate", %{"page" => page}, socket) do + {:noreply, + push_patch(socket, + to: ~p"/oban?#{[queue: socket.assigns.queue, state: socket.assigns.state, page: page]}" + )} + end + + @impl true + def handle_event("toggle_refresh", _, socket) do + {:noreply, assign(socket, auto_refresh: !socket.assigns.auto_refresh)} + end + + @impl true + def handle_info(:update, %{assigns: %{auto_refresh: true}} = socket) do + {:noreply, fetch_jobs(socket)} + end + + @impl true + def handle_info(:update, socket), do: {:noreply, socket} + + def available_actions(state) do + case state do + "executing" -> ~w(cancel) + "scheduled" -> ~w(run cancel delete) + "retryable" -> ~w(retry cancel delete) + "cancelled" -> ~w(retry delete) + "discarded" -> ~w(retry delete) + "complete" -> ~w(retry delete) + _ -> ~w(delete) + end + end + + def back_url(assigns) do + ~p"/oban?#{[queue: assigns.queue, state: assigns.state, page: assigns.page]}" + end + + defp redirect_back(socket) do + push_patch(socket, to: back_url(socket.assigns)) + end + + defp job_stats(job) do + [ + %{title: "State", value: job.state}, + %{title: "Queue", value: job.queue}, + %{title: "Worker", value: job.worker}, + %{title: "Inserted At", value: job.inserted_at}, + %{title: "Scheduled At", value: job.scheduled_at}, + %{title: "Discarded At", value: job.discarded_at}, + %{title: "Cancelled At", value: job.cancelled_at}, + %{title: "Attempted At", value: job.attempted_at}, + %{title: "Completed At", value: job.completed_at}, + %{title: "Attempt", value: "#{job.attempt} of #{job.max_attempts}"} + ] + end + + defp assign_counts(socket) do + counts = + Oban.Job + |> group_by([j], j.state) + |> select([j], {j.state, count(j.id)}) + |> Repo.all() + |> Enum.into(%{}) + + queues = + Oban.Job + |> group_by([j], j.queue) + |> select([j], {j.queue, count(j.id)}) + |> Repo.all() + |> Enum.into(%{}) + + assign(socket, counts: counts, queues: queues) + end + + defp fetch_jobs(socket) do + # Fetch & refresh `jobs` for :index, and `job` for :show + if socket.assigns.live_action == :index do + jobs = + Oban.Job + |> filter_by_state(socket.assigns.state) + |> filter_by_queue(socket.assigns.queue) + |> order_by([j], desc: j.inserted_at) + |> paginate(socket.assigns.page) + |> Repo.all() + + assign(socket, jobs: jobs) + else + assign(socket, jobs: [], job: get_job(socket.assigns.id)) + end + end + + defp paginate(query, page) when page in [nil, ""], do: query + + defp paginate(query, page) do + per_page = 25 + page = String.to_integer(page) + offset_by = per_page * page + + query + |> limit(^per_page) + |> offset(^offset_by) + end + + defp get_job(id) when id in [nil, ""], do: nil + defp get_job(id), do: Oban.Job |> Repo.get!(id) + + defp filter_by_state(query, state) when state in [nil, ""], do: query + defp filter_by_state(query, state), do: where(query, [j], j.state == ^state) + + defp filter_by_queue(query, queue) when queue in [nil, ""], do: query + defp filter_by_queue(query, queue), do: where(query, [j], j.queue == ^queue) + + def from_now_short(now \\ DateTime.utc_now(), later) do + diff = DateTime.diff(now, later) + + cond do + diff <= -24 * 3600 -> "in #{div(-diff, 24 * 3600)}d" + diff <= -3600 -> "in #{div(-diff, 3600)}h" + diff <= -60 -> "in #{div(-diff, 60)}m" + diff <= -5 -> "in #{-diff}s" + diff <= 5 -> "now" + diff <= 60 -> "#{diff}s ago" + diff <= 3600 -> "#{div(diff, 60)} minutes ago" + diff <= 24 * 3600 -> "#{div(diff, 3600)}m ago" + true -> "#{div(diff, 24 * 3600)}d ago" + end + end +end diff --git a/services/bright/lib/free_oban_ui_web/live/oban_live/index.html.heex b/services/bright/lib/free_oban_ui_web/live/oban_live/index.html.heex new file mode 100644 index 0000000..e3cc616 --- /dev/null +++ b/services/bright/lib/free_oban_ui_web/live/oban_live/index.html.heex @@ -0,0 +1,244 @@ +
+
+
+

States

+ <%= if @state not in ["", nil] do %> + <.link patch={~p"/oban?#{[queue: @queue, state: nil]}"} class=""> + Clear + + <% end %> +
+ <%= for state <- ~w(scheduled executing completed retryable cancelled discarded) do %> + <.link + patch={~p"/oban?#{[state: state, queue: @queue]}"} + class={"#{if @state == state, do: "bg-gray-200 text-gray-900 dark:bg-gray-900 dark:text-white", else: "text-gray-600 hover:bg-gray-50 hover:text-gray-900 dark:text-gray-300"} text-gray-600 hover:bg-gray-300 hover:text-gray-900 group flex items-center px-2 py-2.5 fw-500 rounded-md dark:hover:bg-gray-700 dark:hover:text-white"} + > + <%= state %> (<%= @counts[state] || 0 %>) + + <% end %> + +
+

Queues

+ <%= if @queue not in ["", nil] do %> + <.link patch={~p"/oban?#{[queue: nil, state: @state]}"} class=""> + Clear + + <% end %> +
+ <%= for {queue, count} <- @queues do %> + <.link + patch={~p"/oban?#{[queue: queue, state: @state]}"} + class={"#{if @queue == queue, do: "bg-gray-200 text-gray-900 dark:bg-gray-900 dark:text-white", else: "text-gray-600 hover:bg-gray-50 hover:text-gray-900 dark:text-gray-300"} text-gray-600 hover:bg-gray-300 hover:text-gray-900 group flex items-center px-2 py-2.5 fw-500 rounded-md dark:hover:bg-gray-700 dark:hover:text-white"} + > + <%= queue %> (<%= count %>) + + <% end %> +
+ +
+
+ <%= if @live_action == :index do %> +

Oban Jobs

+ <% else %> + <.link patch={back_url(assigns)} class="text-2xl font-bold hover:text-gray-500">← Oban Jobs + <% end %> +
+ +
+
+ +
+ <% state = if @live_action == :index, do: @state, else: @job.state %> + <% event = if @live_action == :index, do: "bulk_action", else: "execute_action" %> + <%= for action <- available_actions(state) do %> + + <% end %> +
+ + <%= if @live_action == :index do %> +
+
+
+ + + + + + + + + + + + + + + + + + + + <%= for job <- @jobs do %> + + + + + + + + + + + <% end %> + +
+ + + ID + + Worker + + Attempt + + State + + Queue + + Inserted +
+ + + + + <%= job.id %> + + <.link + class="text-blue-600 hover:text-blue-800 hover:text-underline" + patch={~p"/oban/#{job.id}?#{[queue: @queue, state: @state, page: @page]}"} + > + <%= job.worker %> + + + <%= Jason.encode!(job.args) %> + + + <%= job.attempt %> / <%= job.max_attempts %> + + + <%= job.state %> + + + + <%= job.queue %> + + + <%= from_now_short(job.inserted_at) %> +
+
+
+ + +
+ <%= @page %> +
+ +
+
+
+ <% else %> + <%= if @job do %> +
+
+

Job #<%= @job.id %>

+
+ +
+ <%= for stat <- job_stats(@job) do %> +
+
<%= stat.title %>
+
<%= stat.value %>
+
+ <% end %> +
+
Args
+
<%= Jason.encode!(@job.args, pretty: true) %>
+
+
+
Meta
+
<%= Jason.encode!(@job.meta, pretty: true) %>
+
+ +
+
Errors
+
+ <%= for error <- @job.errors do %> +
+ Attempt <%= error["attempt"] %> / <%= @job.max_attempts %> at <%= error["at"] %> +
+
<%= raw error["error"] %>
+ <% end %> +
+
+
+ +
+ <% end %> + <% end %> +
+
diff --git a/services/bright/lib/free_oban_ui_web/router.ex b/services/bright/lib/free_oban_ui_web/router.ex new file mode 100644 index 0000000..6d10800 --- /dev/null +++ b/services/bright/lib/free_oban_ui_web/router.ex @@ -0,0 +1,51 @@ +defmodule FreeObanUiWeb.Router do + use FreeObanUiWeb, :router + + pipeline :browser do + plug :accepts, ["html"] + plug :fetch_session + plug :fetch_live_flash + plug :put_root_layout, html: {FreeObanUiWeb.Layouts, :root} + plug :protect_from_forgery + plug :put_secure_browser_headers + end + + pipeline :api do + plug :accepts, ["json"] + end + + scope "/", FreeObanUiWeb do + pipe_through :browser + + get "/", PageController, :home + end + + scope "/oban", FreeObanUiWeb do + pipe_through [:browser] + + live "/", ObanLive.Index, :index + live "/:id", ObanLive.Index, :show + end + + # Other scopes may use custom stacks. + # scope "/api", FreeObanUiWeb do + # pipe_through :api + # end + + # Enable LiveDashboard and Swoosh mailbox preview in development + if Application.compile_env(:free_oban_ui, :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: FreeObanUiWeb.Telemetry + forward "/mailbox", Plug.Swoosh.MailboxPreview + end + end +end diff --git a/services/bright/lib/free_oban_ui_web/telemetry.ex b/services/bright/lib/free_oban_ui_web/telemetry.ex new file mode 100644 index 0000000..df1cff6 --- /dev/null +++ b/services/bright/lib/free_oban_ui_web/telemetry.ex @@ -0,0 +1,92 @@ +defmodule FreeObanUiWeb.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} + ), + summary("phoenix.channel_joined.duration", + unit: {:native, :millisecond} + ), + summary("phoenix.channel_handled_in.duration", + tags: [:event], + unit: {:native, :millisecond} + ), + + # Database Metrics + summary("free_oban_ui.repo.query.total_time", + unit: {:native, :millisecond}, + description: "The sum of the other measurements" + ), + summary("free_oban_ui.repo.query.decode_time", + unit: {:native, :millisecond}, + description: "The time spent decoding the data received from the database" + ), + summary("free_oban_ui.repo.query.query_time", + unit: {:native, :millisecond}, + description: "The time spent executing the query" + ), + summary("free_oban_ui.repo.query.queue_time", + unit: {:native, :millisecond}, + description: "The time spent waiting for a database connection" + ), + summary("free_oban_ui.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. + # {FreeObanUiWeb, :count_users, []} + ] + end +end diff --git a/services/bright/test.txt b/services/bright/test.txt new file mode 100644 index 0000000..875a0a2 --- /dev/null +++ b/services/bright/test.txt @@ -0,0 +1,3 @@ +HELLO MAMA + +haha \ No newline at end of file