From 0acfa1a70fe0181f45d1602ca19bf9d6cfc42319 Mon Sep 17 00:00:00 2001 From: Charu Jain Date: Fri, 12 Jan 2024 18:03:40 +0000 Subject: [PATCH] Collect uptime data in daily heartbeat from connect-ng --- .../connect/v3/systems/systems_controller.rb | 11 +++++ app/models/system.rb | 1 + app/models/system_uptime.rb | 7 ++++ .../20240111200053_create_system_uptimes.rb | 18 +++++++++ spec/models/system_uptime_spec.rb | 7 ++++ .../v3/systems/systems_controller_spec.rb | 40 +++++++++++++++++++ 6 files changed, 84 insertions(+) create mode 100644 app/models/system_uptime.rb create mode 100644 db/migrate/20240111200053_create_system_uptimes.rb create mode 100644 spec/models/system_uptime_spec.rb diff --git a/app/controllers/api/connect/v3/systems/systems_controller.rb b/app/controllers/api/connect/v3/systems/systems_controller.rb index 08c60f105..1f6a1eff0 100644 --- a/app/controllers/api/connect/v3/systems/systems_controller.rb +++ b/app/controllers/api/connect/v3/systems/systems_controller.rb @@ -3,6 +3,17 @@ class Api::Connect::V3::Systems::SystemsController < Api::Connect::BaseControlle before_action :authenticate_system def update + if params[:online_at].present? + params[:online_at].each do |online_at| + 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 + SystemUptime.where("online_at_day < '#{90.days.ago.to_date}'").delete_all + end + @system.hostname = params[:hostname] # Since the payload is handled by rails all values are converted to string diff --git a/app/models/system.rb b/app/models/system.rb index 1e10bbc4c..0883f7de0 100644 --- a/app/models/system.rb +++ b/app/models/system.rb @@ -6,6 +6,7 @@ class System < ApplicationRecord has_many :services, through: :activations has_many :repositories, -> { distinct }, through: :services has_many :products, -> { distinct }, through: :services + has_many :system_uptimes, dependent: :destroy validates :system_token, uniqueness: { scope: %i[login password], case_sensitive: false } diff --git a/app/models/system_uptime.rb b/app/models/system_uptime.rb new file mode 100644 index 000000000..f6bf1087b --- /dev/null +++ b/app/models/system_uptime.rb @@ -0,0 +1,7 @@ +class SystemUptime < ApplicationRecord + belongs_to :system + + validates :system_id, presence: true + validates :online_at_day, presence: true + validates :online_at_hours, presence: true +end diff --git a/db/migrate/20240111200053_create_system_uptimes.rb b/db/migrate/20240111200053_create_system_uptimes.rb new file mode 100644 index 000000000..9c7b001f0 --- /dev/null +++ b/db/migrate/20240111200053_create_system_uptimes.rb @@ -0,0 +1,18 @@ +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 diff --git a/spec/models/system_uptime_spec.rb b/spec/models/system_uptime_spec.rb new file mode 100644 index 000000000..84d2c2f68 --- /dev/null +++ b/spec/models/system_uptime_spec.rb @@ -0,0 +1,7 @@ +require 'rails_helper' + +RSpec.describe SystemUptime, type: :model do + it { is_expected.to validate_presence_of(:system_id) } + it { is_expected.to validate_presence_of(:online_at_day) } + it { is_expected.to validate_presence_of(:online_at_hours) } +end diff --git a/spec/requests/api/connect/v3/systems/systems_controller_spec.rb b/spec/requests/api/connect/v3/systems/systems_controller_spec.rb index f029c5dff..0a927dba9 100644 --- a/spec/requests/api/connect/v3/systems/systems_controller_spec.rb +++ b/spec/requests/api/connect/v3/systems/systems_controller_spec.rb @@ -18,6 +18,8 @@ } end let(:payload) { { hostname: 'test', hwinfo: hwinfo } } + let(:systemuptime) { system.system_uptimes.first } + let(:online_hours) { ':111111111111111111111111' } describe '#update' do subject(:update_action) { put url, params: payload, headers: headers } @@ -45,6 +47,44 @@ expect(information[:cpus]).to eq('16') end end + + context 'when uptime data provided' do + let(:payload) { { hostname: 'test', hwinfo: hwinfo, online_at: [1.day.ago.to_date.to_s << online_hours] } } + + it do + update_action + + expect(systemuptime.system_id).to eq(system.reload.id) + expect(systemuptime.online_at_day.to_date).to eq(1.day.ago.to_date) + expect(systemuptime.online_at_hours.to_s).to eq('111111111111111111111111') + end + end + + context 'when uptime data older than 90 days deleted' do + let(:payload) { { hostname: 'test', hwinfo: hwinfo, online_at: [1.day.ago.to_date.to_s << online_hours, 91.days.ago.to_date.to_s << online_hours] } } + + it do + update_action + + expect(system.system_uptimes.count).to eq(1) + expect(systemuptime.system_id).to eq(system.reload.id) + expect(systemuptime.online_at_day.to_date).to eq(1.day.ago.to_date) + expect(systemuptime.online_at_hours.to_s).to eq('111111111111111111111111') + end + end + + context 'when same uptime data added twice' do + let(:payload) { { hostname: 'test', hwinfo: hwinfo, online_at: [1.day.ago.to_date.to_s << online_hours, 1.day.ago.to_date.to_s << online_hours] } } + + it do + update_action + + expect(system.system_uptimes.count).to eq(1) + expect(systemuptime.system_id).to eq(system.reload.id) + expect(systemuptime.online_at_day.to_date).to eq(1.day.ago.to_date) + expect(systemuptime.online_at_hours.to_s).to eq('111111111111111111111111') + end + end end context 'when hostname is not provided' do