-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds the DynamoDB ORM dynamoid and switches all routes to use it. Implements the database structure from the design document
- Loading branch information
1 parent
b257fc0
commit ddbcf90
Showing
19 changed files
with
311 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...es/register/components/StatusDropdown.jsx → ...gistrations/components/StatusDropdown.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
class LaneFactory | ||
def self.competing_lane(event_ids = {}) | ||
competing_lane = Lane.new({}) | ||
competing_lane.name = "Competing" | ||
if event_ids != {} | ||
competing_lane.completed_steps = ["Event Registration"] | ||
competing_lane.step_details = { | ||
event_ids: event_ids, | ||
} | ||
end | ||
competing_lane | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
class Lane | ||
attr_accessor :name, :lane_state, :completed_steps, :step_details | ||
|
||
def initialize(args) | ||
@name = args["name"] | ||
@lane_state = args["lane_state"] || "waiting" | ||
@completed_steps = args["completed_steps"] || [] | ||
@step_details = args["step_details"] || {} | ||
end | ||
|
||
def dynamoid_dump | ||
self.to_json | ||
end | ||
|
||
def self.dynamoid_load(serialized_str) | ||
parsed = JSON.parse serialized_str | ||
Lane.new(parsed) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'lane' | ||
class Registrations | ||
include Dynamoid::Document | ||
|
||
# We autoscale dynamodb in production | ||
if ENV.fetch("CODE_ENVIRONMENT", "development") == "staging" | ||
table name: 'registrations-staging', read_capacity: 5, write_capacity: 5, key: :attendee_id | ||
else | ||
table name: "registrations", capacity_mode: nil, key: :attendee_id | ||
end | ||
|
||
# Fields | ||
field :user_id, :string | ||
field :competition_id, :string | ||
field :is_attending, :boolean | ||
field :hide_name_publicly, :boolean | ||
field :lane_states, :map | ||
field :lanes, :array, of: Lane | ||
|
||
global_secondary_index hash_key: :user_id, projected_attributes: :all | ||
global_secondary_index hash_key: :competition_id, projected_attributes: :all | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'aws-sdk-dynamodb' | ||
require 'dynamoid' | ||
require_relative '../helpers/lane_factory' | ||
|
||
class RegistrationProcessor | ||
def self.process_message(message) | ||
@dynamodb ||= if ENV['LOCALSTACK_ENDPOINT'] | ||
Aws::DynamoDB::Client.new(endpoint: ENV['LOCALSTACK_ENDPOINT']) | ||
else | ||
Aws::DynamoDB::Client.new | ||
end | ||
# implement your message processing logic here | ||
puts "Working on Message: #{message}" | ||
return unless message['step'] == 'Event Registration' | ||
|
||
registration = { | ||
competitor_id: message['competitor_id'], | ||
competition_id: message['competition_id'], | ||
event_ids: message['event_ids'], | ||
registration_status: 'waiting', | ||
} | ||
save_registration(registration) | ||
def initialize | ||
Dynamoid.configure do |config| | ||
config.region = ENV.fetch("AWS_REGION", 'us-west-2') | ||
config.namespace = nil | ||
if ENV.fetch("CODE_ENVIRONMENT", "development") == "development" | ||
config.endpoint = ENV.fetch('LOCALSTACK_ENDPOINT', nil) | ||
else | ||
config.credentials = Aws::ECSCredentials.new(retries: 3) | ||
end | ||
end | ||
# We have to require the model after we initialized dynamoid | ||
require_relative '../models/registrations' | ||
end | ||
|
||
def self.save_registration(registration) | ||
@dynamodb.put_item({ | ||
table_name: 'Registrations', | ||
item: registration, | ||
}) | ||
def process_message(message) | ||
puts "Working on Message: #{message}" | ||
if message['step'] == 'Lane Init' | ||
lane_init(message['competition_id'], message['user_id']) | ||
elsif message['step'] == 'Event Registration' | ||
event_registration(message['competition_id'], message['user_id'], message['step_details']['event_ids']) | ||
end | ||
end | ||
|
||
private | ||
|
||
def lane_init(competition_id, user_id) | ||
empty_registration = Registrations.new(attendee_id: "#{competition_id}-#{user_id}", competition_id: competition_id, user_id: user_id) | ||
empty_registration.save! | ||
end | ||
|
||
def event_registration(competition_id, user_id, event_ids) | ||
registration = Registrations.find("#{competition_id}-#{user_id}") | ||
competing_lane = LaneFactory.competing_lane(event_ids) | ||
if registration.lanes.nil? | ||
registration.update_attributes(lanes: [competing_lane]) | ||
else | ||
registration.update_attributes(lanes: registration.lanes.append(competing_lane)) | ||
end | ||
registration.save! | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'dynamoid' | ||
|
||
Dynamoid.configure do |config| | ||
config.region = ENV.fetch("AWS_REGION", 'us-west-2') | ||
config.namespace = nil | ||
if Rails.env.production? | ||
config.credentials = Aws::ECSCredentials.new(retries: 3) | ||
else | ||
config.endpoint = ENV.fetch('LOCALSTACK_ENDPOINT', nil) | ||
end | ||
end |
Oops, something went wrong.