Skip to content

Commit

Permalink
Tidy moduledoc
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Vaughan committed Oct 8, 2018
1 parent bd51f7d commit 211a80b
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 29 deletions.
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
# Slot Sync

* Syncs slot data from external provider
A tool for reading data from a third party via their API and publishing to a broker.

## Overview

* Currently sync's slot data from WIW
* Runs sync every 5 minutes (see SchedEx for :wiw_sync to change)
* Caches the data locally in Redis
* If remote data changed then publishes to Kafka
* If newly read data has changed from cached version it then publishes to Kafka

## Detail
```
(every 5 minutes)
|> SlotSync.Runner
|> SlotSync.WIW
|> 1 days worth of shifts from today
|> get slots on WIW
|> SlotSync.Dispatcher
|> each shift
|> SlotSync.Processor.Shift (GenServer)
|> if matches redis cache then next (cache expires each slot after 1 week)
|> if not then save in redis and publish
|> SlotSync.Publishers.Kafka
```
7 changes: 0 additions & 7 deletions lib/slot_sync.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,2 @@
defmodule SlotSync do
@moduledoc """
SlotSync 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
3 changes: 3 additions & 0 deletions lib/slot_sync/cache/redis.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
defmodule SlotSync.Cache.Redis do
@moduledoc """
A interface for caching shift data into redis
"""
use GenServer

use Confex, otp_app: :slot_sync
Expand Down
7 changes: 3 additions & 4 deletions lib/slot_sync/dogstatsd_config.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
defmodule SlotSync.DogStatsdConfig do
# require Logger

@moduledoc """
Helper to get DogStatsd setup correctly
"""
def configs do
# [:host, :port, :namespace, :tags, :max_buffer_size]

host = Confex.get_env(:slot_sync, SlotSync.Datadog)[:host]
port = Confex.get_env(:slot_sync, SlotSync.Datadog)[:port]
namespace = Confex.get_env(:slot_sync, SlotSync.Datadog)[:namespace]
Expand Down
4 changes: 4 additions & 0 deletions lib/slot_sync/kafka_config.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
defmodule SlotSync.KafkaConfig do
@moduledoc """
Helper to get Kafka setup correctly
"""

require Logger

def brokers do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,20 @@ defmodule SlotSync.Publishers.Kafka do
"""
use Confex, otp_app: :slot_sync

alias SlotSync.TrackingLocationsFormatter
alias KafkaEx.Protocol.Produce.Request
alias KafkaEx.Protocol.Produce.Message
require Logger

# @type key() :: String.t()
# @type event() :: String.t()
# @type topic_name() :: String.t() | atom()
# @type encoded_message() :: String.t()
@type key() :: String.t()
@type event() :: String.t()
@type encoded_message() :: String.t()

@doc """
This is the main function to trigger a message to be published to Kafka
## Examples
"""

# @type message_number() :: integer()
# @spec call(map(), integer(), topic_name()) :: {:ok, message_number()} | {:error, String.t()}
@type message_number() :: integer()
@spec call(map(), integer()) :: {:ok, message_number()} | {:error, String.t()}
def call(data, id) do
id
|> build_event_key()
Expand All @@ -33,8 +28,8 @@ defmodule SlotSync.Publishers.Kafka do

defp build_event_key(id), do: [{"id", id}]

# @spec encode_event_message(key, event, topic_name) ::
# {:encoded_key, encoded_message, encoded_message: encoded_message} | {:error, key}
@spec encode_event_message(key, event) ::
{:encoded_key, encoded_message, encoded_message: encoded_message} | {:error, key}
defp encode_event_message(key, event) do
with {:ok, encoded_key} <- encoder().call(key_schema_name(), key),
{:ok, encoded_message} <- encoder().call(value_schema_name(), transformed(event)) do
Expand All @@ -45,8 +40,6 @@ defmodule SlotSync.Publishers.Kafka do
end

defp transformed(event) do
event["linked_users"] |> IO.inspect(label: "linked_users")

[
{"account_id", event["account_id"]},
{"acknowledged", event["acknowledged"]},
Expand Down
2 changes: 1 addition & 1 deletion lib/slot_sync/runner.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule SlotSync.Runner do
@moduledoc """
Will schedule running covering a days worth of data on each call
Ensures sync peroid covers a rolling amount of data on each call
"""
@spec start() :: :ok
def start do
Expand Down
4 changes: 3 additions & 1 deletion lib/slot_sync/shift_processor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ defmodule SlotSync.Processor.Shift do
It compares incoming data to that in redis, and updates if changed.
And then also publishes to Kafka.
denotes defined on API
(others are not listed)
%{
"account_id" => 77967, x
"acknowledged" => 1, x
Expand All @@ -19,7 +21,7 @@ defmodule SlotSync.Processor.Shift do
"end_time" => "Mon, 08 Oct 2018 00:15:00 +0100",
"id" => 2076303948, x
"instances" => 0, x
"is_open" => false, z
"is_open" => false, x
"linked_users" => nil,
"location_id" => 3999871,
"notes" => "", x
Expand Down
2 changes: 2 additions & 0 deletions lib/slot_sync/wiw.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ defmodule SlotSync.WIW do
W-Token: abc123
https://api.wheniwork.com/2/shifts?start=2018-10-07&end=2018-10-08
http://dev.wheniwork.com/#shifts
"""

alias SlotSync.Dispatcher
Expand Down

0 comments on commit 211a80b

Please sign in to comment.