-
Notifications
You must be signed in to change notification settings - Fork 10
Add beacon sync status reporting endpoint #661
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
app/controllers/api/v1/beacons/sync_statuses_controller.rb
This file contains hidden or 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,43 @@ | ||
| module Api | ||
| module V1 | ||
| module Beacons | ||
| class SyncStatusesController < Beacons::BaseController | ||
| def create | ||
| result = recorder.call(sync_status_params) | ||
|
|
||
| if result.success | ||
| render json: { status: "accepted" }, status: :ok | ||
| else | ||
| render json: { errors: result.errors }, status: :unprocessable_entity | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def recorder | ||
| ::Beacons::SyncStatusRecorder.new(Current.beacon) | ||
| end | ||
|
|
||
| def sync_status_params | ||
| { | ||
| status: params[:status], | ||
| manifest_version: params[:manifest_version], | ||
| manifest_checksum: params[:manifest_checksum], | ||
| synced_at: params[:synced_at], | ||
| files_count: params[:files_count], | ||
| total_size_bytes: params[:total_size_bytes], | ||
| error_message: params[:error_message], | ||
| device_info: extract_device_info, | ||
| } | ||
| end | ||
|
|
||
| def extract_device_info | ||
| value = params[:device_info] | ||
| return nil if value.blank? | ||
|
|
||
| value.respond_to?(:to_unsafe_h) ? value.to_unsafe_h : value | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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,57 @@ | ||
| module Beacons | ||
| class SyncStatusRecorder | ||
| Result = Data.define(:success, :errors) | ||
|
|
||
| def initialize(beacon, clock: Time) | ||
| @beacon = beacon | ||
| @clock = clock | ||
| end | ||
|
|
||
| def call(payload) | ||
| attrs = normalize(payload) | ||
| return Result.new(success: false, errors: [ "status is invalid" ]) unless valid_status?(attrs[:sync_status]) | ||
|
|
||
| beacon.update!(attrs) | ||
| Result.new(success: true, errors: []) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| attr_reader :beacon, :clock | ||
|
|
||
| def normalize(payload) | ||
| now = clock.current | ||
| status = payload[:status].to_s | ||
|
|
||
| { | ||
| sync_status: status, | ||
| last_seen_at: now, | ||
| last_sync_at: last_sync_at_for(status, payload[:synced_at]), | ||
| reported_manifest_version: payload[:manifest_version], | ||
| reported_manifest_checksum: payload[:manifest_checksum], | ||
| reported_files_count: payload[:files_count], | ||
| reported_total_size_bytes: payload[:total_size_bytes], | ||
| last_sync_error: status == "error" ? payload[:error_message] : nil, | ||
| device_info: payload[:device_info], | ||
| } | ||
| end | ||
|
|
||
| def last_sync_at_for(status, synced_at) | ||
| return beacon.last_sync_at unless status == "synced" | ||
|
|
||
| parse_time(synced_at) || clock.current | ||
| end | ||
|
|
||
| def parse_time(value) | ||
| return nil if value.blank? | ||
|
|
||
| Time.iso8601(value.to_s) | ||
| rescue ArgumentError | ||
| nil | ||
| end | ||
|
|
||
| def valid_status?(status) | ||
| Beacon::SYNC_STATUSES.include?(status) | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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,16 @@ | ||
| class AddSyncStatusToBeacons < ActiveRecord::Migration[8.0] | ||
| def change | ||
| add_column :beacons, :sync_status, :string | ||
| add_column :beacons, :last_seen_at, :datetime | ||
| add_column :beacons, :last_sync_at, :datetime | ||
| add_column :beacons, :reported_manifest_version, :string | ||
| add_column :beacons, :reported_manifest_checksum, :string | ||
| add_column :beacons, :reported_files_count, :integer | ||
| add_column :beacons, :reported_total_size_bytes, :bigint | ||
| add_column :beacons, :last_sync_error, :text | ||
| add_column :beacons, :device_info, :jsonb | ||
|
|
||
| add_index :beacons, :sync_status | ||
| add_index :beacons, :last_seen_at | ||
| end | ||
| end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is so cool; I didn't know about this trick.