Skip to content

Commit

Permalink
Collect uptime data in daily heartbeat from connect-ng
Browse files Browse the repository at this point in the history
  • Loading branch information
cjainsuse committed Jan 12, 2024
1 parent 9145722 commit 6ee0b51
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
13 changes: 13 additions & 0 deletions app/controllers/api/connect/v3/systems/systems_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ class Api::Connect::V3::Systems::SystemsController < Api::Connect::BaseControlle
before_action :authenticate_system

def update
if params[:online_at].present?
for online_at in params[:online_at]
begin
dthours = online_at.split(":")
@system_uptime = SystemUptime.create!(system_id: @system.id, online_at_day: dthours[0], online_at_hours: dthours[1])
logger.info(N_("Added uptime information for system '%s'") % @system.id)
rescue ActiveRecord::RecordNotUnique
logger.info(N_("Uptime information existing for system '%s'") % @system.id)
end
end
SystemUptime.where("online_at_day < '#{90.days.ago}'").delete_all
end

@system.hostname = params[:hostname]

# Since the payload is handled by rails all values are converted to string
Expand Down
4 changes: 4 additions & 0 deletions app/models/system_uptime.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class SystemUptime < ApplicationRecord
belongs_to :system, foreign_key: 'system_id'

end
19 changes: 19 additions & 0 deletions db/migrate/20240111200053_create_system_uptimes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class CreateSystemUptimes < ActiveRecord::Migration[6.1]
def change
safety_assured do
create_table :system_uptimes, id: false do |t|
t.bigint :system_id, null:false
t.date :online_at_day, null: false
t.column :online_at_hours, "binary(24)", null: false
t.timestamps
end

commit_db_transaction

add_index :system_uptimes, %i[system_id online_at_day online_at_hours], unique: true, name: 'id_online_day_hours'

add_foreign_key :system_uptimes, :systems, column: :system_id, validate: false

end
end
end

0 comments on commit 6ee0b51

Please sign in to comment.