Skip to content

Commit

Permalink
Added pghero:clean_space_stats rake task [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Feb 2, 2023
1 parent 1127fdb commit 947961b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 3.2.0 (unreleased)

- Added `pghero:clean_space_stats` rake task
- Added support for specifying retention period with `clean_query_stats`
- Removed reset button when historical query stats are enabled

Expand Down
4 changes: 2 additions & 2 deletions lib/pghero.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ def clean_query_stats(before: nil)
end
end

def clean_space_stats
def clean_space_stats(before: nil)
each_database do |database|
database.clean_space_stats
database.clean_space_stats(before: before)
end
end

Expand Down
5 changes: 3 additions & 2 deletions lib/pghero/methods/space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ def capture_space_stats
insert_stats("pghero_space_stats", columns, values) if values.any?
end

def clean_space_stats
PgHero::SpaceStats.where(database: id).where("captured_at < ?", 90.days.ago).delete_all
def clean_space_stats(before: nil)
before ||= 90.days.ago
PgHero::SpaceStats.where(database: id).where("captured_at < ?", before).delete_all
end

def space_stats_enabled?
Expand Down
8 changes: 8 additions & 0 deletions lib/tasks/pghero.rake
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@ namespace :pghero do
options[:before] = Integer(ENV["KEEP_DAYS"]).days.ago if ENV["KEEP_DAYS"].present?
PgHero.clean_query_stats(**options)
end

desc "Remove old space stats"
task clean_space_stats: :environment do
puts "Deleting old space stats..."
options = {}
options[:before] = Integer(ENV["KEEP_DAYS"]).days.ago if ENV["KEEP_DAYS"].present?
PgHero.clean_space_stats(**options)
end
end

0 comments on commit 947961b

Please sign in to comment.