Skip to content

Commit

Permalink
Do some cleanup and renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
hschne committed Oct 23, 2024
1 parent 6d4354e commit 6818a3d
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 121 deletions.
4 changes: 4 additions & 0 deletions app/models/bandwidth.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class Bandwidth < ActiveRecord::Base
end
4 changes: 0 additions & 4 deletions app/models/bandwidth_usage.rb

This file was deleted.

4 changes: 4 additions & 0 deletions app/models/cpu_usage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class CpuUsage < ActiveRecord::Base
end
4 changes: 0 additions & 4 deletions app/models/load_average.rb

This file was deleted.

80 changes: 44 additions & 36 deletions app/puny_monitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,34 @@ class App < Sinatra::Base
200
end

get "/data/cpu" do
get "/data/cpu_usage" do
content_type :json
end_time = Time.now
start_time = end_time - 1.hour
cpu_loads = CpuLoad.where(created_at: start_time..end_time)
.group_by_minute(:created_at, n: 1, series: true)
.average(:load_average)
cpu_loads = CpuUsage.where(created_at: start_time..end_time)
.group_by_minute(:created_at, n: 1, series: true)
.average(:used_percent)
cpu_loads.to_json
end

get "/data/memory" do
get "/data/cpu_load" do
content_type :json
end_time = Time.now
start_time = end_time - 1.hour
[
{ name: "1 minute", data: CpuLoad.where(created_at: start_time..end_time)
.group_by_minute(:created_at, n: 1, series: true)
.average(:one_minute) },
{ name: "5 minutes", data: CpuLoad.where(created_at: start_time..end_time)
.group_by_minute(:created_at, n: 1, series: true)
.average(:five_minutes) },
{ name: "15 minutes", data: CpuLoad.where(created_at: start_time..end_time)
.group_by_minute(:created_at, n: 1, series: true)
.average(:fifteen_minutes) }
].to_json
end

get "/data/memory_usage" do
content_type :json
end_time = Time.now
start_time = end_time - 1.hour
Expand All @@ -46,7 +63,7 @@ class App < Sinatra::Base
memory_usage.to_json
end

get "/data/filesystem" do
get "/data/filesystem_usage" do
content_type :json
end_time = Time.now
start_time = end_time - 1.hour
Expand Down Expand Up @@ -75,47 +92,38 @@ class App < Sinatra::Base
end_time = Time.now
start_time = end_time - 1.hour
[
{ name: "Incoming Mbps", data: BandwidthUsage.where(created_at: start_time..end_time)
.group_by_minute(:created_at, n: 1, series: true)
.average(:incoming_mbps) },
{ name: "Outgoing Mbps", data: BandwidthUsage.where(created_at: start_time..end_time)
.group_by_minute(:created_at, n: 1, series: true)
.average(:outgoing_mbps) }
].to_json
end

get "/data/load_average" do
content_type :json
end_time = Time.now
start_time = end_time - 1.hour
[
{ name: "1 minute", data: LoadAverage.where(created_at: start_time..end_time)
.group_by_minute(:created_at, n: 1, series: true)
.average(:one_minute) },
{ name: "5 minutes", data: LoadAverage.where(created_at: start_time..end_time)
.group_by_minute(:created_at, n: 1, series: true)
.average(:five_minutes) },
{ name: "15 minutes", data: LoadAverage.where(created_at: start_time..end_time)
.group_by_minute(:created_at, n: 1, series: true)
.average(:fifteen_minutes) }
{ name: "Incoming Mbps", data: Bandwidth.where(created_at: start_time..end_time)
.group_by_minute(:created_at, n: 1, series: true)
.average(:incoming_mbps) },
{ name: "Outgoing Mbps", data: Bandwidth.where(created_at: start_time..end_time)
.group_by_minute(:created_at, n: 1, series: true)
.average(:outgoing_mbps) }
].to_json
end

@scheduler.every "5s" do
CpuLoad.create(load_average: SystemUtils.cpu_usage_percent)
CpuUsage.create(used_percent: SystemUtils.cpu_usage_percent)
cpu_load_averages = SystemUtils.cpu_load_average
LoadAverage.create(one_minute: cpu_load_averages[0],
five_minutes: cpu_load_averages[1],
fifteen_minutes: cpu_load_averages[2])

CpuLoad.create(one_minute: cpu_load_averages[0],
five_minutes: cpu_load_averages[1],
fifteen_minutes: cpu_load_averages[2])
MemoryUsage.create(used_percent: SystemUtils.memory_usage_percent)

FilesystemUsage.create(used_percent: SystemUtils.filesystem_usage_percent)

disk_io = SystemUtils.disk_io_stats
DiskIO.create(read_mb_per_sec: disk_io[:read_mb_per_sec], write_mb_per_sec: disk_io[:write_mb_per_sec])

bandwidth = SystemUtils.bandwidth_usage
BandwidthUsage.create(incoming_mbps: bandwidth[:incoming_mbps], outgoing_mbps: bandwidth[:outgoing_mbps])
Bandwidth.create(incoming_mbps: bandwidth[:incoming_mbps], outgoing_mbps: bandwidth[:outgoing_mbps])
end

@scheduler.every "1h" do
CpuUsage.where("created_at < ?", 1.month.ago).destroy_all
CpuLoad.where("created_at < ?", 1.month.ago).destroy_all
MemoryUsage.where("created_at < ?", 1.month.ago).destroy_all
FilesystemUsage.where("created_at < ?", 1.month.ago).destroy_all
DiskIO.where("created_at < ?", 1.month.ago).destroy_all
BandwidthUsage.where("created_at < ?", 1.month.ago).destroy_all
end
end
end
14 changes: 7 additions & 7 deletions app/views/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<div class="tile">
<h2>CPU Usage</h2>
<%= area_chart "/data/cpu",
<%= area_chart "/data/cpu_usage",
ytitle: "CPU Usage (%)",
min: 0,
max: 100,
Expand All @@ -15,8 +15,8 @@
</div>

<div class="tile">
<h2>Load Average</h2>
<%= line_chart "/data/load_average",
<h2>CPU Load</h2>
<%= line_chart "/data/cpu_load",
ytitle: "Load Average",
library: {
title: {
Expand All @@ -28,7 +28,7 @@

<div class="tile">
<h2>Memory Usage</h2>
<%= area_chart "/data/memory",
<%= area_chart "/data/memory_usage",
ytitle: "Memory Usage (%)",
min: 0,
max: 100,
Expand All @@ -42,7 +42,7 @@

<div class="tile">
<h2>Filesystem Usage</h2>
<%= area_chart "/data/filesystem",
<%= area_chart "/data/filesystem_usage",
ytitle: "Used Space (%)",
min: 0,
max: 100,
Expand All @@ -67,12 +67,12 @@
</div>

<div class="tile">
<h2>Bandwidth Usage</h2>
<h2>Bandwidth</h2>
<%= area_chart "/data/bandwidth",
ytitle: "Bandwidth (Mbps)",
library: {
title: {
text: "Bandwidth Usage",
text: "Bandwidth",
},
},
refresh: 5 %>
Expand Down
10 changes: 0 additions & 10 deletions db/migrate/20230515123456_create_cpu_loads.rb

This file was deleted.

10 changes: 0 additions & 10 deletions db/migrate/20230515123457_create_memory_usages.rb

This file was deleted.

10 changes: 0 additions & 10 deletions db/migrate/20230515123458_create_filesystem_usages.rb

This file was deleted.

11 changes: 0 additions & 11 deletions db/migrate/20230516000000_create_disk_ios.rb

This file was deleted.

11 changes: 0 additions & 11 deletions db/migrate/20230517000000_create_bandwidth_usages.rb

This file was deleted.

12 changes: 0 additions & 12 deletions db/migrate/20230518000000_create_load_averages.rb

This file was deleted.

10 changes: 4 additions & 6 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# frozen_string_literal: true

# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
Expand All @@ -13,15 +11,15 @@
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.2].define(version: 20_240_930_155_845) do
create_table "bandwidth_usages", force: :cascade do |t|
create_table "bandwidths", force: :cascade do |t|
t.float "incoming_mbps"
t.float "outgoing_mbps"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "cpu_loads", force: :cascade do |t|
t.float "load_average"
create_table "cpu_usages", force: :cascade do |t|
t.float "used_percent"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Expand All @@ -39,7 +37,7 @@
t.datetime "updated_at", null: false
end

create_table "load_averages", force: :cascade do |t|
create_table "cpu_loads", force: :cascade do |t|
t.float "one_minute"
t.float "five_minutes"
t.float "fifteen_minutes"
Expand Down
Empty file added db/seeds.rb
Empty file.

0 comments on commit 6818a3d

Please sign in to comment.