Skip to content

Commit

Permalink
Group time to 5min intervals
Browse files Browse the repository at this point in the history
  • Loading branch information
ledermann committed Aug 28, 2024
1 parent 4dfc8a4 commit fa0ba9a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 24 deletions.
20 changes: 11 additions & 9 deletions lib/flux/extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
module Flux
class Extractor < Flux::Reader
def records(day)
return [] if day.today? && Time.current.hour.zero?

query_string = <<~FLUX
#{from_bucket}
|> #{day_range(day)}
Expand All @@ -20,13 +18,17 @@ def records(day)
private

def day_range(day)
range(
start: day.beginning_of_day,
stop: [
day.beginning_of_day + 1.day,
Time.current.beginning_of_hour,
].min,
)
start = day.beginning_of_day

stop =
if day.today?
current_time = Time.current
current_time.beginning_of_minute - (current_time.min % 5).minutes
else
start + 1.day
end

range(start:, stop:)
end

def extract_and_transform_data(flux_tables)
Expand Down
2 changes: 1 addition & 1 deletion lib/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def group_by_5min(splitted)
.group_by { |item| (item[:time].to_i - 1.minute) / 5.minutes }
.map do |_interval, items|
{
time: items.first[:time].beginning_of_minute,
time: items.last[:time],
house_power_grid: sum(items, :house_power_grid),
wallbox_power_grid:
wallbox_present ? sum(items, :wallbox_power_grid) : nil,
Expand Down
51 changes: 37 additions & 14 deletions spec/lib/flux/extractor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
subject(:extractor) { described_class.new(config:) }

let(:config) { Config.new(ENV) }
let(:day) { Date.yesterday }

describe '#records', vcr: 'extractor' do
let(:time) { day.to_time.change(hour: 9, minute: 42).to_i }
subject(:day_records) { extractor.records(day) }

let(:time) { day.to_time.change(hour: 9, min: 42) }

before do
records = [
Expand Down Expand Up @@ -38,20 +39,42 @@

after { flux_delete_all }

it 'returns transformed data' do
records = extractor.records(day)
context 'when day is in the past' do
let(:day) { Date.yesterday }

it 'returns transformed data' do
expect(day_records).to be_an(Array).and all(be_a(Hash)).and all(
include(
'time',
'grid_power_plus',
'house_power',
'wallbox_charge_power',
'power',
),
)

expect(day_records.length).to eq(1440) # 24 hours * 60 records per hour (1m intervals)
end
end

context 'when day is today' do
let(:day) { Date.new(2024, 8, 28) }

before { travel_to(time) }

expect(records).to be_an(Array).and all(be_a(Hash)).and all(
include(
'time',
'grid_power_plus',
'house_power',
'wallbox_charge_power',
'power',
),
)
it 'returns transformed data' do
expect(day_records).to be_an(Array).and all(be_a(Hash)).and all(
include(
'time',
'grid_power_plus',
'house_power',
'wallbox_charge_power',
'power',
),
)

expect(records.length).to eq(1440) # 24 hours * 60 records per hour (1m intervals)
expect(day_records.length).to eq(1440) # 24 hours * 60 records per hour (1m intervals)
end
end
end
end
3 changes: 3 additions & 0 deletions spec/support/time_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require 'active_support/testing/time_helpers'

RSpec.configure { |config| config.include ActiveSupport::Testing::TimeHelpers }

0 comments on commit fa0ba9a

Please sign in to comment.