Skip to content

Commit

Permalink
Merge branch 'release/0.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ledermann committed Aug 7, 2024
2 parents 5c7b765 + 3db9928 commit 2709e88
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ INFLUX_BUCKET=my-bucket
INFLUX_MEASUREMENT=my_power_splitter
INFLUX_INTERVAL=5

##### Redis
REDIS_URL=redis://localhost:6379/1

# Sensor mapping: Map to Measurement/Field in InfluxDB
INFLUX_SENSOR_GRID_IMPORT_POWER=SENEC:grid_power_plus
INFLUX_SENSOR_HOUSE_POWER=SENEC:house_power
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ jobs:
test:
runs-on: ubuntu-latest

services:
redis:
image: redis:7-alpine
ports: ['6379:6379']

env:
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
CI: true
Expand Down
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ gem 'base64'
# A toolkit of support libraries and Ruby core extensions extracted from the Rails framework. (https://rubyonrails.org)
gem 'activesupport'

# A Ruby client library for Redis (https://github.com/redis/redis-rb)
gem 'redis'

group :development do
# Guard gem for RSpec (https://github.com/guard/guard-rspec)
gem 'guard-rspec', require: false
Expand Down
21 changes: 13 additions & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ GEM
rexml
csv (3.3.0)
diff-lcs (1.5.1)
docile (1.4.0)
docile (1.4.1)
dotenv (3.1.2)
drb (2.2.1)
ffi (1.17.0)
Expand All @@ -44,7 +44,7 @@ GEM
guard (~> 2.1)
guard-compat (~> 1.1)
rspec (>= 2.99.0, < 4.0)
hashdiff (1.1.0)
hashdiff (1.1.1)
i18n (1.14.5)
concurrent-ruby (~> 1.0)
influxdb-client (3.1.0)
Expand All @@ -69,14 +69,18 @@ GEM
coderay (~> 1.1)
method_source (~> 1.0)
public_suffix (6.0.1)
racc (1.8.0)
racc (1.8.1)
rainbow (3.1.1)
rake (13.2.1)
rb-fsevent (0.11.2)
rb-inotify (0.11.1)
ffi (~> 1.0)
redis (5.2.0)
redis-client (>= 0.22.0)
redis-client (0.22.2)
connection_pool
regexp_parser (2.9.2)
rexml (3.3.2)
rexml (3.3.4)
strscan
rspec (3.13.0)
rspec-core (~> 3.13.0)
Expand All @@ -91,7 +95,7 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.1)
rubocop (1.65.0)
rubocop (1.65.1)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
Expand All @@ -102,14 +106,14 @@ GEM
rubocop-ast (>= 1.31.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.31.3)
rubocop-ast (1.32.0)
parser (>= 3.3.1.0)
rubocop-performance (1.21.1)
rubocop (>= 1.48.1, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-rake (0.6.0)
rubocop (~> 1.0)
rubocop-rspec (3.0.3)
rubocop-rspec (3.0.4)
rubocop (~> 1.61)
rubocop-thread_safety (0.5.1)
rubocop (>= 0.90.0)
Expand Down Expand Up @@ -144,6 +148,7 @@ DEPENDENCIES
guard-rspec
influxdb-client
rake
redis
rspec
rubocop
rubocop-performance
Expand All @@ -155,4 +160,4 @@ DEPENDENCIES
webmock

BUNDLED WITH
2.5.16
2.5.17
2 changes: 2 additions & 0 deletions lib/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Config # rubocop:disable Metrics/ClassLength
:influx_bucket,
:influx_measurement,
:influx_interval,
:redis_url,
:time_zone

def initialize(env, logger: NullLogger.new)
Expand All @@ -30,6 +31,7 @@ def initialize(env, logger: NullLogger.new)
logger.info "Accessing InfluxDB at #{influx_url}, bucket #{influx_bucket}"

@time_zone = env.fetch('TZ', 'Europe/Berlin')
@redis_url = env.fetch('REDIS_URL', nil)

init_sensors(env)
validate_sensors!
Expand Down
3 changes: 3 additions & 0 deletions lib/loop.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'influx_push'
require 'influx_pull'
require 'processor'
require 'redis_cache'

class Loop
def initialize(config:)
Expand Down Expand Up @@ -53,6 +54,8 @@ def process_historical_data
day += 1.day
end

RedisCache.new(config:).flush

config.logger.info '--- Processing historical data successfully finished'
end

Expand Down
30 changes: 30 additions & 0 deletions lib/redis_cache.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'redis'

class RedisCache
def initialize(config:)
@config = config
end
attr_reader :config

def flush
unless redis
config.logger.warn 'REDIS_URL not set, skipping Redis cache flush'
return
end

result = redis.flushall
if result == 'OK'
config.logger.info 'Redis cache flushed'
else
config.logger.error "Flushing Redis cache failed: #{result}"
end
rescue StandardError => e
config.logger.error "Flushing Redis cache failed: #{e.message}"
end

def redis
return unless config.redis_url

@redis ||= Redis.new(url: config.redis_url)
end
end
64 changes: 64 additions & 0 deletions spec/lib/redis_cache_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require 'redis_cache'

describe RedisCache do
subject(:redis_cache) { described_class.new(config:) }

let(:config) { Config.new(ENV.to_h, logger:) }
let(:logger) { MemoryLogger.new }

describe '#flush' do
subject(:flush) { redis_cache.flush }

context 'when Redis is available' do
it 'writes info message into log' do
flush

expect(logger.info_messages).to include('Redis cache flushed')
end
end

context 'when Redis is not available' do
before do
allow(config).to receive(:redis_url).and_return(
'redis://localhost:1234',
)
end

it 'writes error message into log' do
flush

expect(logger.error_messages).to include(
'Flushing Redis cache failed: Connection refused - connect(2) for 127.0.0.1:1234 (redis://localhost:1234)',
)
end
end

context 'when Redis cannot flush' do
before do
allow(Redis).to receive(:new).and_return(
instance_double(Redis, flushall: 'ERROR'),
)
end

it 'writes error message into log' do
flush

expect(logger.error_messages).to include(
'Flushing Redis cache failed: ERROR',
)
end
end

context 'when REDIS_URL missing' do
before { allow(config).to receive(:redis_url).and_return(nil) }

it 'writes warn message into log' do
flush

expect(logger.warn_messages).to include(
'REDIS_URL not set, skipping Redis cache flush',
)
end
end
end
end

0 comments on commit 2709e88

Please sign in to comment.