Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(page-threads): make script that hits thread hit each page of a t… #40

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/epochtalk_server/smf_query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,9 @@ defmodule EpochtalkServer.SmfQuery do
{:ok, [], _} ->
{:error, "Posts not found for thread_id: #{id}"}

{:error, :page_does_not_exist} ->
{:error, :page_does_not_exist}

{:ok, posts, data} ->
return_tuple(posts, data)
end
Expand Down
5 changes: 4 additions & 1 deletion lib/epochtalk_server_web/controllers/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ defmodule EpochtalkServerWeb.Controllers.Post do
defp proxy_by_thread(conn, attrs) do
with thread_id <- Validate.cast(attrs, "thread_id", :integer, required: true),
page <- Validate.cast(attrs, "page", :integer, default: 1),
limit <- Validate.cast(attrs, "limit", :integer, default: 5),
limit <- Validate.cast(attrs, "limit", :integer, default: 25),
user <- Guardian.Plug.current_resource(conn),
user_priority <- ACL.get_user_priority(conn),
:ok <- ACL.allow!(conn, "posts.byThread"),
Expand All @@ -602,6 +602,9 @@ defmodule EpochtalkServerWeb.Controllers.Post do
pagination_data: data
})
else
{:error, :page_does_not_exist} ->
ErrorHelpers.render_json_error(conn, 400, "Error, page does not exist")

_ ->
ErrorHelpers.render_json_error(conn, 400, "Error, cannot get posts by thread")
end
Expand Down
28 changes: 16 additions & 12 deletions lib/epochtalk_server_web/helpers/proxy_pagination.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,23 @@ defmodule EpochtalkServerWeb.Helpers.ProxyPagination do

total_pages = total_pages(total_records, per_page)

result = records(query, page, total_pages, per_page)

pagination_data = %{
next: page < total_pages,
prev: page > 1,
page: page,
per_page: per_page,
total_records: total_records,
total_pages: total_pages,
desc: desc
}
if page > total_pages do
{:error, :page_does_not_exist}
else
result = records(query, page, total_pages, per_page)

pagination_data = %{
next: page < total_pages,
prev: page > 1,
page: page,
per_page: per_page,
total_records: total_records,
total_pages: total_pages,
desc: desc
}

{:ok, result, pagination_data}
{:ok, result, pagination_data}
end
end

def page_next_prev(query, page, per_page: per_page, desc: desc) do
Expand Down
25 changes: 20 additions & 5 deletions parse_by_thread.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,35 @@ defmodule ParseByThread do

@start_id 5_485_059
@end_id 1
@max_page 999_999

def run do
for id <- @start_id..@end_id//-1 do
case HTTPoison.get("http://localhost:4000/api/posts?thread_id=#{id}") do
def run,
do: for(id <- @start_id..@end_id//-1, do: process_thread(id))

def process_thread(id) do
Enum.reduce_while(1..@max_page, nil, fn page, _acc ->
case HTTPoison.get("http://localhost:4000/api/posts?thread_id=#{id}&page=#{page}") do
{:ok, %HTTPoison.Response{status_code: 200, body: _body}} ->
Logger.info("Successfully parsed thread with id (#{id})")
{:cont, nil}

{:ok,
%HTTPoison.Response{
status_code: 400,
body:
"{\"error\":\"Bad Request\",\"message\":\"Error, page does not exist\",\"status\":400}"
}} ->
Logger.info("Successfully parsed #{page - 1} page(s) of thread with id (#{id})")
{:halt, nil}

{:ok, %HTTPoison.Response{status_code: status_code}} ->
Logger.error("Thread with id (#{id}) received response with status code #{status_code}")
{:halt, nil}

{:error, %HTTPoison.Error{reason: reason}} ->
Logger.error("Thread with id (#{id}) HTTP request failed: #{reason}")
{:halt, nil}
end
end
end)
end
end

Expand Down
Loading