Module: Familia::Features::Quantization::ModelClassMethods
- Defined in:
- lib/familia/features/quantization.rb
Overview
Familia::Quantization::ModelClassMethods
Instance Method Summary collapse
-
#in_bucket?(timestamp, quantum, bucket_time) ⇒ Boolean
Check if a timestamp falls within a quantized bucket.
-
#qstamp(quantum = nil, pattern: nil, time: nil) ⇒ Integer, String
Generates a quantized timestamp based on the given parameters.
-
#qstamp_range(start_time, end_time, quantum, pattern: nil) ⇒ Array
Generate multiple quantized timestamps for a time range.
Instance Method Details
#in_bucket?(timestamp, quantum, bucket_time) ⇒ Boolean
Check if a timestamp falls within a quantized bucket
348 349 350 351 352 353 354 355 |
# File 'lib/familia/features/quantization.rb', line 348 def in_bucket?(, quantum, bucket_time) = .to_i if .respond_to?(:to_i) bucket_time = bucket_time.to_i if bucket_time.respond_to?(:to_i) bucket_start = qstamp(quantum, time: Time.at(bucket_time)) bucket_end = bucket_start + quantum - 1 .between?(bucket_start, bucket_end) end |
#qstamp(quantum = nil, pattern: nil, time: nil) ⇒ Integer, String
Generates a quantized timestamp based on the given parameters
This method rounds the current time to the nearest quantum and optionally formats it according to the given pattern. It's useful for creating time-based buckets or keys with reduced granularity.
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/familia/features/quantization.rb', line 288 def qstamp(quantum = nil, pattern: nil, time: nil) # Handle array input format: [quantum, pattern] quantum, pattern = quantum if quantum.is_a?(Array) # Use default quantum if none specified # Priority: provided quantum > class default_expiration > 10.minutes fallback quantum ||= default_expiration || 10.minutes # Validate quantum value unless quantum.is_a?(Numeric) && quantum.positive? raise ArgumentError, "Quantum must be positive (#{quantum.inspect} given)" end # Delegate to Familia.qstamp for the actual calculation Familia.qstamp(quantum, pattern: pattern, time: time) end |
#qstamp_range(start_time, end_time, quantum, pattern: nil) ⇒ Array
Generate multiple quantized timestamps for a time range
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/familia/features/quantization.rb', line 319 def qstamp_range(start_time, end_time, quantum, pattern: nil) = [] current = qstamp(quantum, time: start_time) end_bucket = qstamp(quantum, time: end_time) while current <= end_bucket << if pattern Time.at(current).strftime(pattern) else current end current += quantum end end |