From 7c5486707b8c0d144d185016cbd8dbf28338b5a8 Mon Sep 17 00:00:00 2001 From: Ritesh Kumar Date: Sun, 28 Jan 2024 16:50:21 -0500 Subject: [PATCH] Cleanups --- docs/gita_cloning.livemd | 207 ------------------ ...hwork.livemd => initial_db_helpers.livemd} | 39 +--- lib/vyasa/written.ex | 46 ---- lib/vyasa/written/verse.ex | 1 - .../live/source_live/chapter/index.html.heex | 2 +- .../live/source_live/chapter/show_verse.ex | 3 - .../source_live/chapter/show_verse.html.heex | 2 +- .../live/source_live/chapter_index.ex | 91 -------- .../live/source_live/chapter_index.html.heex | 13 -- lib/vyasa_web/live/source_live/index.ex | 6 +- .../live/source_live/index.html.heex | 11 +- lib/vyasa_web/live/source_live/show.html.heex | 6 +- scripts/verses.livemd | 4 +- 13 files changed, 26 insertions(+), 405 deletions(-) delete mode 100644 docs/gita_cloning.livemd rename docs/{ritesh_roughwork.livemd => initial_db_helpers.livemd} (91%) delete mode 100644 lib/vyasa_web/live/source_live/chapter_index.ex delete mode 100644 lib/vyasa_web/live/source_live/chapter_index.html.heex diff --git a/docs/gita_cloning.livemd b/docs/gita_cloning.livemd deleted file mode 100644 index a9da84ad..00000000 --- a/docs/gita_cloning.livemd +++ /dev/null @@ -1,207 +0,0 @@ -# Ritesh's Rough Work on Vyasa - -## Root Section -- Common Utils - -```elixir -defmodule R do - def recompile() do - Mix.Task.reenable("app.start") - Mix.Task.reenable("compile") - Mix.Task.reenable("compile.all") - compilers = Mix.compilers() - Enum.each(compilers, &Mix.Task.reenable("compile.#{&1}")) - Mix.Task.run("compile.all") - end -end -``` - -### Supports Recompilation from within Livebook - - - -## 2) Cloning Gita by Creating a Schema for it - -```elixir -alias Vyasa.Written.{Source, Chapter, Verse, Translation, Transliteration, Transcript, Medium} - -verse = %Verse{ - num: 77, - body: "तच्च संस्मृत्य संस्मृत्य रूपमत्यद्भुतं हरेः।\n\nविस्मयो मे महान् राजन् हृष्यामि च पुनः पुनः।।18.77।।\n " -} - -verse_2 = %Verse{ - num: 78, - body: "तच्च संस्मृत्य संस्मृत्य रूपमत्यद्भुतं हरेः।\n\nविस्मयो मे महान् राजन् हृष्यामि च पुनः पुनः।।19.78।।\n " -} - -chapter = %Chapter{num: 1, body: "Chapter 1 Body", verses: [verse, verse_2]} - -source = %Source{ - title: "testTitle", - id: Ecto.UUID.generate(), - verses: [verse, verse_2], - chapters: [chapter] -} - -# verse = Ecto.build_assoc(source, :verses, verse) -# medium = %Medium{id: Ecto.UUID.generate()} -# transcript = %Transcript{id: Ecto.UUID.generate()} - -verse_2 = %Verse{ - verse_2 - | translations: [ - %Translation{language: "Urdu", body: "Urdu translationnnnn", meaning: "meaning in urdu"} - ] -} -``` - -Convert json --> struct --> using changeset --> insert into repo - -```elixir -defmodule Gita do - @root "/Users/ritesh/Projects/vyasa" - @gita_sub_dir "#{@root}/priv/static/corpus/gita" - - @verses "#{@gita_sub_dir}/verse.json" - @chapters "#{@gita_sub_dir}/chapters.json" - - alias Vyasa.Written.{Chapter} - - - def get_source(chapters, verses) do - - %{title: "Gita", chapters: chapters, verses: verses} - # Source.changeset(%Source{}, %{title: "Gita"}) - end - - - @doc """ - example input: 17 => [ - %{ - "chapter_number" => 17, - "chapter_summary" => "The seventeenth chapter of the Bhagavad Gita is \"Sraddhatraya Vibhaga Yoga\". In this chapter, Krishna describes the three types of faith corresponding to the three modes of the material nature. Lord Krishna further reveals that it is the nature of faith that determines the quality of life and the character of living entities. Those who have faith in passion and ignorance perform actions that yield temporary, material results while those who have faith in goodness perform actions in accordance with scriptural instructions and hence their hearts get further purified.", - "chapter_summary_hindi" => "भगवद गीता का सत्रहवा अध्याय श्रद्धात्रयविभागयोग है। इस अध्याय में, कृष्ण भौतिक प्रकृति के तीन गुणों से संबंधित तीन प्रकार के विश्वासों का वर्णन करते हैं। भगवान कृष्ण आगे बताते हैं कि यह विश्वास की प्रकृति है जो जीवन की गुणवत्ता और जीवित संस्थाओं के चरित्र को निर्धारित करती है। जो लोग लालसा और अज्ञानता में विश्वास रखते हैं, वे ऐसे कार्य करते हैं जो कि अस्थायी और भौतिक फल देते हैं परन्तु जो लोग अच्छाई में विश्वास रखते हैं वे शास्त्रपूर्ण निर्देशों के अनुसार कार्य करते हैं और इसलिए उन्हें स्थायी और अधिक प्रवीण फल प्राप्त होते हैं जो मन को और भी शुद्ध करते हैं। ", - "id" => 17, - "image_name" => "shraddha-traya-vibhaga-yoga", - "name" => "श्रद्धात्रयविभागयोग", - "name_meaning" => "Yoga through Discerning the Three Divisions of Faith", - "name_translation" => "Sraddhatraya Vibhaga Yoga", - "name_transliterated" => "Śhraddhā Traya Vibhāg Yog", - "verses_count" => 28 - } - """ - def create_changeset(chapter) do - # IO.inspect(chapter["chapter_number"]) - IO.inspect(chapter) - %{ - "chapter_number" => num, - "chapter_summary" => body, - } = chapter - - changeset = Chapter.changeset(%Chapter{}, %{num: num, body: body}) - - changeset - - - # %{chapter_number: num} = chapter - # num - # chapter[:chapter_number] - # IO.inspect(chapter_val["#{chapter}"]) - # num - end - - - - - def get_chapters() do - _changesets = @chapters - |> File.read!() - |> Jason.decode!() - - # |> Enum.group_by(& &1["chapter_number"]) - # |> Enum.take(1) - # |> Enum.map(fn chapter -> Gita.create_changeset(chapter) end) - # # |> Enum.map(fn {_k, v} -> Gita.create_changeset(v) end) - # |> Enum.take(1) - end - - - - @doc """ - { - "chapter_id": 18, - "chapter_number": 18, - "externalId": 696, - "id": 696, - "text": "अर्जुन उवाचनष्टो मोहः स्मृतिर्लब्धा त्वत्प्रसादान्मयाच्युत।स्थितोऽस्मि गतसन्देहः करिष्ये वचनं तव।।18.73।।", - "title": "Verse 696", - "verse_number": 73, - "verse_order": 696, - "transliteration": "arjuna uvācha\nnaṣhṭo mohaḥ smṛitir labdhā tvat-prasādān mayāchyuta\nsthito ‘smi gata-sandehaḥ kariṣhye vachanaṁ tava\n", - "word_meanings": "arjunaḥ uvācha—Arjun said; naṣhṭaḥ—dispelled; mohaḥ—illusion; smṛitiḥ—memory; labdhā—regained; tvat-prasādāt—by your grace; mayā—by me; achyuta—Shree Krishna, the infallible one; sthitaḥ—situated; asmi—I am; gata-sandehaḥ—free from doubts; kariṣhye—I shall act; vachanam—instructions; tava—your\n" - }, - - """ - - def create_verse(verse) do - %{ - "verse_number" => num, - "text" => body, - "transliteration" => transliteration, - "word_meanings" => meaning, - } = verse - - - end - - - def get_verses() do - @verses - |> File.read!() - |> Jason.decode!() - |> Enum.map(fn verse -> create_verse(verse)) - end -end - - -# get all as structs, then compose them properly before creating changesets? -verses = Gita.get_verses() -# chapters = Gita.get_chapters() -# source = Gita.get_source(chapters, verses) -# source_changeset = Gita.get_source() -# chapter_changesets = Gita.get_chapters() - - -``` - - - -## 1) Understanding the Schema - -#### Session Creation - -```elixir -## ref to schema @ lib/vyasa/sangh/session.ex to see the schema. The changeset is the one that shows how the creation works. - -{:ok, session} = - %{id: Ecto.UUID.generate()} - |> Vyasa.Sangh.create_session() -``` - -#### Creating Text - -```elixir -{:ok, text} = %{title: "gita"} |> Vyasa.Written.create_text() -``` - -#### Creating Comments - -```elixir -%{ - id: Ecto.UUID.generate(), - type: "null", - body: "My Comment Body", - session_id: session.id -} -|> Vyasa.Sangh.create_comment() -``` diff --git a/docs/ritesh_roughwork.livemd b/docs/initial_db_helpers.livemd similarity index 91% rename from docs/ritesh_roughwork.livemd rename to docs/initial_db_helpers.livemd index c68d92f5..5e4476b2 100644 --- a/docs/ritesh_roughwork.livemd +++ b/docs/initial_db_helpers.livemd @@ -1,4 +1,4 @@ -# Ritesh's Rough Work on Vyasa +# 2024 - An Ecto Odessy [Initial DB Helpers] ## Root Section -- Common Utils @@ -149,15 +149,6 @@ defmodule G do end ``` -```elixir -# improving on the get-* functions - -sid = Ecto.UUID.generate() - -verses = G.get_verses(sid) -chapters = G.get_chapters(verses, sid) -``` - #### DB Convenience Actions * purging of db @@ -212,20 +203,13 @@ DBHelper.purge_db() DBHelper.seed_db() ``` -```elixir -R.recompile()sr -alias Vyasa.Written - -Written.get_source_by_title("Gita") -``` - ## Exploration ```elixir -DBHelper.purge_db() -DBHelper.seed_db() +# DBHelper.purge_db() +# DBHelper.seed_db() alias Vyasa.Written sources = Written.list_sources() |> Repo.preload([:chapters, :verses]) @@ -233,15 +217,10 @@ s = hd(sources) v = hd(s.verses) c = hd(s.chapters) Map.keys(c) -c # Written.get_source!(hd(sources).id) ``` -```elixir -DBHelper.seed_db() -``` - ```elixir R.recompile() @@ -258,15 +237,11 @@ Written.get_chapter!(2, id) ## 2) Cloning Gita by Creating a Schema for it -### Actually creating entries: +### Check Queries found in the Written context module -#### Improving the ingestion to include missed attributes - - - -### Check Queries +### ```elixir R.recompile() @@ -290,8 +265,4 @@ chapter.verses |> Repo.preload([:source, :chapter]) ``` -```elixir - -``` - #### Session Creation diff --git a/lib/vyasa/written.ex b/lib/vyasa/written.ex index a6db419d..b036c43f 100644 --- a/lib/vyasa/written.ex +++ b/lib/vyasa/written.ex @@ -30,14 +30,12 @@ defmodule Vyasa.Written do [%Source{}, ...] """ - def list_sources do Repo.all(Source) |> Repo.preload([:chapters, :verses]) end - @doc """ Gets a single text. @@ -79,22 +77,11 @@ defmodule Vyasa.Written do end - # def get_chapter(no, source_id) do - # Repo.get_by!(Chapter, no: no, source_id: source_id) - # |> Repo.preload([:verses]) - # end - - # def get_chapter!(no, source_id) do - # Repo.get_by!(Chapter, no: no, source_id: source_id) - # |> Repo.preload([:verses]) - # end - def get_chapter(no, source_title) do src = list_sources() |>Enum.find(fn src -> src.title == source_title end) Repo.get_by(Chapter, no: no, source_id: src.id) |> Repo.preload([:verses]) - end def get_verses_in_chapter(no, source_id) do @@ -102,46 +89,15 @@ defmodule Vyasa.Written do |> Repo.preload([:verses]) chapter.verses - end def get_verse_via_url_params(verse_no, chap_no, source_title) do - # chapter = Repo.get_by(Chapter, no: chap_no, source_id: source_id) - # |> Repo.preload([:verses]) - - # chapter.verses - # |> Enum.find(fn verse -> verse.no === verse_no end) - - # IO.puts("get_verse_via_url_params") - # IO.inspect(verse_no) - - # # get_chapter(chap_no, source_id) - # get_verses_in_chapter(chap_no, source_id) - # |> Enum.find(fn verse -> verse.no == verse_no end) - - # chapter = Repo.get_by(Chapter, no: chap_no, source_id: source_id) - # |> Repo.preload([:verses, :source]) - chapter = get_chapter(chap_no, source_title) |> Repo.preload([:verses, :source]) chapter.verses |> Enum.find(fn verse -> verse.no == verse_no end) |> Repo.preload([:chapter, :source]) - - - - - - - - - - - # get_verses_in_chapter(chap_no, source_id) - # |> Enum.find(fn verse -> verse.no === verse_no - # end) - end @doc """ @@ -200,10 +156,8 @@ defmodule Vyasa.Written do Returns an `%Ecto.Changeset{}` for tracking text changes. ## Examples - iex> change_text(text) %Ecto.Changeset{data: %Text{}} - """ def change_text(%Text{} = text, attrs \\ %{}) do Text.changeset(text, attrs) diff --git a/lib/vyasa/written/verse.ex b/lib/vyasa/written/verse.ex index d38a9e59..6cea984a 100644 --- a/lib/vyasa/written/verse.ex +++ b/lib/vyasa/written/verse.ex @@ -8,7 +8,6 @@ defmodule Vyasa.Written.Verse do schema "verses" do field :no, :integer field :body, :string - # QQ: compiler was looking for this but not sure what made it look for this field since the migration file doesn't use this name belongs_to :source, Source, type: Ecto.UUID belongs_to :chapter, Chapter, type: :integer, references: :no, foreign_key: :chapter_no diff --git a/lib/vyasa_web/live/source_live/chapter/index.html.heex b/lib/vyasa_web/live/source_live/chapter/index.html.heex index c8fb1e21..a7726ebd 100644 --- a/lib/vyasa_web/live/source_live/chapter/index.html.heex +++ b/lib/vyasa_web/live/source_live/chapter/index.html.heex @@ -19,4 +19,4 @@ - <.back navigate={~p"/explore/#{@source_title}"}>Back to <%= @source_title %> + <.back navigate={~p"/explore/#{@source_title}"}>Back to <%= @source_title %> Chapters diff --git a/lib/vyasa_web/live/source_live/chapter/show_verse.ex b/lib/vyasa_web/live/source_live/chapter/show_verse.ex index be79bb09..26795d0d 100644 --- a/lib/vyasa_web/live/source_live/chapter/show_verse.ex +++ b/lib/vyasa_web/live/source_live/chapter/show_verse.ex @@ -1,9 +1,6 @@ defmodule VyasaWeb.SourceLive.Chapter.ShowVerse do use VyasaWeb, :live_view alias Vyasa.Written - # alias Vyasa.Corpus.Gita - # alias VyasaWeb.GitaLive.ImageGenerator - # alias Vyasa.Adapters.OgAdapter @impl true def mount(_params, _session, socket) do diff --git a/lib/vyasa_web/live/source_live/chapter/show_verse.html.heex b/lib/vyasa_web/live/source_live/chapter/show_verse.html.heex index dc9d3a3a..1dfe99a0 100644 --- a/lib/vyasa_web/live/source_live/chapter/show_verse.html.heex +++ b/lib/vyasa_web/live/source_live/chapter/show_verse.html.heex @@ -46,7 +46,7 @@ <.back navigate={~p"/explore/#{@source_title}/#{@chap_no}"}> Back to <%= @source_title %> Chapter <%= @verse.chapter_no %> - <.back navigate={~p"/explore/#{@source_title}"}>Back to <%= @source_title %> + <.back navigate={~p"/explore/#{@source_title}"}>Back to <%= @source_title %> Chapters
source_title, - "chap_no" => chap_no, - } = _params) do - - source = Written.get_source_by_title(source_title) - chapter = source.chapters - |> Enum.find(fn chap -> chap.no == chap_no end) - - inspect(chapter) - - %Chapter{ - verses: verses, - title: title, - body: body, - } = chapter - - - socket - |> stream(:verses, verses) - |> assign(:source_id, source.id) - |> assign(:source_tile, source.title) - |> assign(:chap_no, chap_no) - |> assign(:chap_body, body) - |> assign(:chap_title, title) - |> assign(:page_title, "Sources to explore") - |> assign(:text, nil) - |> assign_meta() - end - - defp assign_meta(socket) do - assign(socket, :meta, %{ - title: "Sources", - description: "The wealth of knowledge", - type: "website", - url: url(socket, ~p"/explore/"), - }) - end - @doc """ - Renders a clickable verse list. - - ## Examples - <.verse_list> - <:item title="Title" navigate={~p"/myPath"}><%= @post.title %> - - """ - slot :item, required: true do - attr :title, :string - attr :navigate, :any, required: false - end - - def verse_list(assigns) do - ~H""" -
-
-
-
- <.link - navigate={item[:navigate]} - class="text-sm font-semibold leading-6 text-zinc-900 hover:text-zinc-700" - > - <%= item.title %> - -
-
<%= render_slot(item) %>
-
-
-
- """ - end - -end diff --git a/lib/vyasa_web/live/source_live/chapter_index.html.heex b/lib/vyasa_web/live/source_live/chapter_index.html.heex deleted file mode 100644 index 87e0e5a2..00000000 --- a/lib/vyasa_web/live/source_live/chapter_index.html.heex +++ /dev/null @@ -1,13 +0,0 @@ - <.header> - <%= @chap_no%> - <:subtitle> <%= @chap_body %> - - - <.verse_list :for={{_dom_id, verse} <- @streams.verses}> - <:item title={"#{verse.chapter_no}. #{verse.no}"} - navigate={~p"/explore/#{@source_title}/#{@chap_no}/"} > -

<%= verse.body |> String.split("।।") |> List.first() %>

- - - - <.back navigate={~p"/explore/#{@source_id}"}>Back to Source diff --git a/lib/vyasa_web/live/source_live/index.ex b/lib/vyasa_web/live/source_live/index.ex index d370037c..a7644611 100644 --- a/lib/vyasa_web/live/source_live/index.ex +++ b/lib/vyasa_web/live/source_live/index.ex @@ -15,8 +15,8 @@ defmodule VyasaWeb.SourceLive.Index do defp apply_action(socket, :index, _params) do socket - |> assign(:page_title, "Sources to explore") - |> assign(:text, nil) + |> assign(:page_title, "Sources") + |> assign(:text, "hello world") |> assign_meta() end @@ -24,7 +24,7 @@ defmodule VyasaWeb.SourceLive.Index do defp assign_meta(socket) do assign(socket, :meta, %{ title: "Sources", - description: "The wealth of knowledge", + description: "The wealth of knowledge, distilled into words", type: "website", url: url(socket, ~p"/explore/"), }) diff --git a/lib/vyasa_web/live/source_live/index.html.heex b/lib/vyasa_web/live/source_live/index.html.heex index 5745f10f..696b5273 100644 --- a/lib/vyasa_web/live/source_live/index.html.heex +++ b/lib/vyasa_web/live/source_live/index.html.heex @@ -1,5 +1,8 @@ <.header> - <%= @page_title %> + +
+ <%= @page_title %> +
<.table @@ -7,6 +10,10 @@ rows={@streams.sources} row_click={fn {_id, source} -> JS.navigate(~p"/explore/#{source.title}/") end} > - <:col :let={{_id, source}} label=""><%= source.title %> + <:col :let={{_id, source}} label=""> +
+ <%= source.title %> +
+ diff --git a/lib/vyasa_web/live/source_live/show.html.heex b/lib/vyasa_web/live/source_live/show.html.heex index 9871ce76..30290e9a 100644 --- a/lib/vyasa_web/live/source_live/show.html.heex +++ b/lib/vyasa_web/live/source_live/show.html.heex @@ -1,6 +1,8 @@ -<.header> - Title: <%= @title%> +<.header > +
+ <%= @title%> +
diff --git a/scripts/verses.livemd b/scripts/verses.livemd index 7c524cb9..577e322f 100644 --- a/scripts/verses.livemd +++ b/scripts/verses.livemd @@ -1,7 +1,9 @@ # verses ```elixir -Ja +Mix.install([ + {:jason, "~> 1.4"} +]) ``` ## Section