Implemented very rudimentary sync conflict detection and api endpoint /conflicts.

This commit is contained in:
Ryan Pandya 2022-08-30 13:10:23 -07:00
parent 51a7b8be93
commit 2b7f784951
3 changed files with 80 additions and 2 deletions

View File

@ -4,11 +4,11 @@ defmodule Logsrv do
""" """
def dir do def dir do
Application.get_env(:logsrv, :dir) Application.get_env(:logsrv, :dir) <> "/"
end end
def dir(subdir) do def dir(subdir) do
"#{dir()}/#{subdir}" "#{dir()}/#{subdir}/"
end end
def journals do def journals do
@ -21,4 +21,59 @@ defmodule Logsrv do
|> File.ls! |> File.ls!
end end
def compare(fd) do
sync_conflict_fd = fd
orig_fd = fd
|> String.replace(~r/\.sync.*/,"\.md")
sync_conflict = sync_conflict_fd |> File.read!
orig = orig_fd |> File.read!
# FOR NOW, just return the diff
diff = orig
|> String.myers_difference(sync_conflict)
diff
|> Kernel.inspect()
|> Jason.encode!
end
def conflicts do
{conflicts(journals()), conflicts(pages())}
end
def conflicts(list) do
list
|> Enum.filter( fn (fd) ->
fd |> String.match?(~r/.*sync.*/)
end)
end
def resolve_conflicts do
{journal_conflicts, pages_conflicts} = conflicts()
[
journal_conflicts
|> Enum.map( fn(fd) ->
%{
:type => :journal,
:date => fd |> Logsrv.Helpers.to_date,
:file => fd,
:diff => compare(dir(:journals) <> fd),
}
end),
pages_conflicts
|> Enum.map( fn(fd) ->
%{
:type => :page,
:file => fd,
:diff => compare(dir(:pages) <> fd),
} end)
]
|> List.flatten()
end
end end

View File

@ -0,0 +1,14 @@
defmodule Logsrv.Helpers do
def moot do
"moo"
end
def to_date(str) do
str
|> String.replace(~r/\..*/,"")
|> String.replace("_","-")
|> Date.from_iso8601!()
end
end

View File

@ -45,6 +45,15 @@ defmodule Logsrv.Router do
|> send_resp(200, pages) # Send a 200 OK response with the posts in the body |> send_resp(200, pages) # Send a 200 OK response with the posts in the body
end end
get "/conflicts" do
conflicts = Logsrv.resolve_conflicts
|> Jason.encode!
conn
|> put_resp_content_type("application/json")
|> send_resp(200, conflicts) # Send a 200 OK response with the conflicts
end
# Fallback handler when there was no match # Fallback handler when there was no match
match _ do match _ do
send_resp(conn, 404, "Not Found") send_resp(conn, 404, "Not Found")