Skip to content

Commit

Permalink
data stream to client working! #17
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Jan 7, 2025
1 parent 653be14 commit 76eb6a5
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 35 deletions.
31 changes: 12 additions & 19 deletions lib/app/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ defmodule App.User do
def changeset(user, attrs) do
user
|> cast(attrs, [:id, :login, :avatar_url, :name, :company, :bio, :blog, :location, :email, :created_at, :hireable, :two_factor_authentication, :public_repos, :followers, :following])
|> validate_required([:id, :login, :avatar_url, :name, :created_at, :followers, :following])
|> validate_required([:id, :login, :avatar_url, :created_at, :followers, :following])
end

@doc """
Expand All @@ -40,27 +40,12 @@ defmodule App.User do
|> Repo.insert(on_conflict: :replace_all, conflict_target: [:id])
end

# Envar.require_env_file(".env")

def get_org_members_from_api(org_name) do
token = Envar.get("GH_PERSONAL_ACCESS_TOKEN")

client = Tentacat.Client.new(%{access_token: token})
{200, data, _res} = Tentacat.Organizations.Members.list(client, org_name)
# dbg(data)
Useful.atomize_map_keys(data)
end

def get_user_from_api(username) do
token = Envar.get("GH_PERSONAL_ACCESS_TOKEN")
client = Tentacat.Client.new(%{access_token: token})
{200, data, _res} = Tentacat.Users.find(client, username)
{:ok, entry} = Useful.atomize_map_keys(data)
|> dbg
{:ok, data} = App.GitHub.user(username)
|> map_github_user_fields_to_table()
|> create()

entry
data
end

# Next: get list of org members
Expand All @@ -74,7 +59,7 @@ defmodule App.User do
avatar_url: String.split(u.avatar_url, "?") |> List.first,
bio: u.bio,
blog: u.blog,
company: String.replace(u.company, "@", ""),
company: clean_company(u.company),
created_at: u.created_at,
email: u.email,
followers: u.followers,
Expand All @@ -89,5 +74,13 @@ defmodule App.User do
}
end

def clean_company(company) do
# avoid `String.replace(nil, "@", "", [])` error
if company == nil do
""
else
String.replace(company, "@", "")
end
end

end
83 changes: 80 additions & 3 deletions lib/app_web/live/app_live.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,84 @@
defmodule AppWeb.AppLive do
use AppWeb, :live_view

def mount(_params, _session, socket) do
{:ok, socket}
@topic "live"
@img "https://avatars.githubusercontent.com/u/"

def mount(_session, _params, socket) do
if connected?(socket) do
AppWeb.Endpoint.subscribe(@topic) # subscribe to the channel
end
p = %{id: 1, login: "Alex", avatar_url: "#{@img}1"}
{:ok, assign(socket, %{data: p})}
end

def handle_event("inc", _value, socket) do
dbg(socket.assigns)
val = socket.assigns.data.id + 1
p = %{id: val, login: "Alex #{val}", avatar_url: "#{@img}#{val}"}
new_state = assign(socket, %{data: p})
broadcast("inc", new_state.assigns)

{:noreply, new_state}
end

def handle_event("dec", _, socket) do
val = socket.assigns.val - 1
p = %{id: val, login: "Alex", avatar_url: "#{@img}#{val}"}
new_state = assign(socket, %{data: p})
broadcast("dec", new_state.assigns)
{:noreply, new_state}
end

def handle_event("sync", _value, socket) do
IO.inspect("sync")
sync(socket)
# val = socket.assigns.val + 1
# p = %{login: "Alex", avatar_url: "#{@img}#{val}"}
# new_state = assign(socket, %{val: val, data: p})
# broadcast("inc", new_state.assigns)

# :timer.apply_interval(1000, IO, :puts, ["weeeee"])

{:noreply, socket}
end

def handle_event("update", _value, socket) do
IO.inspect("- - - - - - - - - - - - - - - - - - - handle_event: update")
{:noreply, socket}
end

# update `data` by broadcasting it as the profiles are crawled:
def sync(socket) do

dbg(socket.assigns)
list = App.GitHub.org_user_list("dwyl")
# dbg(list)
# Iterate through the list of people
# Enum.map(list, Task.async( fn u ->
# dbg(u)
# end))
list
|> Stream.with_index
|> Enum.map(fn {u, i} ->
IO.inspect("- - - Enum.map u.login: #{i}: #{u.login}")
data = App.User.get_user_from_api(u.login)
data = AuthPlug.Helpers.strip_struct_metadata(data)
new_state = assign(socket, %{data: data})
Task.start(fn ->
:timer.sleep(300 + 100 * i)
AppWeb.Endpoint.broadcast(@topic, "update", new_state.assigns)
end)
end)

{:noreply, socket}
end

def handle_info(msg, socket) do
{:noreply, assign(socket, data: msg.payload.data)}
end

def broadcast(msg, assigns) do
AppWeb.Endpoint.broadcast_from(self(), @topic, msg, assigns)
end
end
end
26 changes: 25 additions & 1 deletion lib/app_web/live/app_live.html.heex
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
<h1 class="text-3xl text-white py-3 text-center
bg-gradient-to-r from-green-400 to-blue-500 rounded-lg shadow-lg">
hello david
</h1>
</h1>

<div>
<h1 class="text-4xl font-bold text-center">
The count is: <%= @data.id %> </h1>

<p class="text-center">
<button phx-click="dec"
class="w-20 bg-red-500 hover:bg-red-600">-</button>
<button phx-click="inc"
class="w-20 bg-green-500 hover:bg-green-600">+</button>
</p>
</div>

<h1>{ @data.login }</h1>

<img style="height:auto;" src={@data.avatar_url}
width="260" height="260"
class="rounded-full">


<button phx-click="sync"
class="w-20 bg-green-600 hover:bg-green-800 text-white font-bold py-2 px-4 rounded">
Sync
</button>
2 changes: 1 addition & 1 deletion lib/app_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule AppWeb.Router do

scope "/", AppWeb do
pipe_through :browser

live "/", AppLive
end

Expand Down
4 changes: 2 additions & 2 deletions priv/repo/migrations/20221005213110_create_users.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ defmodule App.Repo.Migrations.CreateUsers do
add :email, :string
add :followers, :integer
add :following, :integer
add :hireable, :boolean, default: false, null: false
add :hireable, :boolean, default: false
add :location, :string
add :login, :string
add :name, :string
add :public_repos, :integer
add :two_factor_authentication, :boolean, default: false, null: false
add :two_factor_authentication, :boolean, default: false

timestamps()
end
Expand Down
6 changes: 3 additions & 3 deletions test/app/github_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ defmodule App.GitHubTest do
assert user.public_repos > 30
end

test "App.org_user_list/1" do
orgname = "dwyl"
test "App.GitHub.org_user_list/1" do
orgname = "ideaq"
list = GitHub.org_user_list(orgname)
assert length(list) > 500
assert length(list) > 2
end
end
7 changes: 1 addition & 6 deletions test/app/user_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,8 @@ defmodule App.UserTest do
assert inserted_user.name == user.name
end

test "get_org_members_from_api/1" do
App.User.get_org_members_from_api("dwyl") # |> dbg
assert true == true
end

test "get_user_from_api/1" do
data = App.User.get_user_from_api("iteles") |> dbg
data = App.User.get_user_from_api("iteles") # |> dbg
assert data.public_repos > 30
end
end
Expand Down

0 comments on commit 76eb6a5

Please sign in to comment.