Module: Familia::Features::Quantization::ClassMethods
- Included in:
- Familia::Features::Quantization
- Defined in:
- lib/familia/features/quantization.rb
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
342 343 344 345 346 347 348 349 |
# File 'lib/familia/features/quantization.rb', line 342 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 >= 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.
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/familia/features/quantization.rb', line 280 def qstamp(quantum = nil, pattern: nil, time: nil) # Handle array input format: [quantum, pattern] if quantum.is_a?(Array) quantum, pattern = quantum end # 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
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/familia/features/quantization.rb', line 313 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 |