Add a rules controller

This commit is contained in:
Dan Milne
2025-11-04 09:47:11 +11:00
parent 5ff166613e
commit c72d83acda
14 changed files with 272 additions and 42 deletions

View File

@@ -9,15 +9,18 @@ module Api
# GET /api/:public_key/rules/version
# Quick version check - returns latest updated_at timestamp
def version
current_sampling = HubLoad.current_sampling
response.headers['X-Sample-Rate'] = current_sampling[:allowed_requests].to_s
render json: {
version: Rule.latest_version,
count: Rule.active.count,
sampling: HubLoad.current_sampling
sampling: current_sampling
}
end
# GET /api/:public_key/rules?since=2024-11-03T12:00:00.000Z
# Incremental sync - returns rules updated since timestamp
# GET /api/:public_key/rules?since=1730646186272060
# Incremental sync - returns rules updated since timestamp (microsecond Unix timestamp)
# GET /api/:public_key/rules
# Full sync - returns all active rules
def index
@@ -30,9 +33,12 @@ module Api
Rule.active.sync_order
end
current_sampling = HubLoad.current_sampling
response.headers['X-Sample-Rate'] = current_sampling[:allowed_requests].to_s
render json: {
version: Rule.latest_version,
sampling: HubLoad.current_sampling,
sampling: current_sampling,
rules: rules.map(&:to_agent_format)
}
rescue ArgumentError => e
@@ -59,9 +65,17 @@ module Api
end
def parse_timestamp(timestamp_str)
Time.parse(timestamp_str)
# Parse microsecond Unix timestamp
unless timestamp_str.match?(/^\d+$/)
raise ArgumentError, "Invalid timestamp format. Expected microsecond Unix timestamp (e.g., 1730646186272060)"
end
total_microseconds = timestamp_str.to_i
seconds = total_microseconds / 1_000_000
microseconds = total_microseconds % 1_000_000
Time.at(seconds, microseconds)
rescue ArgumentError => e
raise ArgumentError, "Invalid timestamp format. Expected ISO8601 format (e.g., 2024-11-03T12:00:00.000Z)"
raise ArgumentError, "Invalid timestamp format: #{e.message}. Use microsecond Unix timestamp (e.g., 1730646186272060)"
end
end
end