Skip to content

Commit

Permalink
v0.2.0
Browse files Browse the repository at this point in the history
Changed layout when multiple servers are reporting metrics for one
project to a tabbed layout. This allows for easier navigation between
the different servers.

By default, the tabs will rotate every 10 seconds. This can be changed
by setting `?interval=4` in the URL. This will change the interval to 4
seconds between tab changes.
  • Loading branch information
kobaltz committed Feb 10, 2024
1 parent 4c8e99a commit 758058e
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
mission_control-servers (0.1.6)
mission_control-servers (0.2.0)
importmap-rails
rails (>= 7.1.3)
stimulus-rails
Expand Down
2 changes: 1 addition & 1 deletion app/assets/builds/mission_control_servers_application.css

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@
}
}

@layer components {
.tab-link {
@apply relative min-w-0 flex-1 overflow-hidden py-4 px-4 text-center text-sm font-medium focus:z-10;
@apply flex items-center justify-center;
@apply text-gray-100;
@apply bg-white hover:bg-gray-50;
@apply dark:bg-indigo-800 hover:bg-gray-50;
}
}

@layer components {
.progress-bar {
@apply bg-gray-300;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ def script_content
cron_job="* * * * * $(pwd)/metrics.sh"
(crontab -l 2>/dev/null | grep -v -F "$cron_job"; echo "$cron_job") | crontab -
echo "Sending first request to the server..."
$(pwd)/metrics.sh
echo
echo "████████████████████████████████████████████████████████████████████████████"
echo "█ █"
echo "█ ██╗███╗ ██╗███████╗████████╗ █████╗ ██╗ ██╗ ███████╗██████╗ █"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { Controller } from "@hotwired/stimulus";

export default class extends Controller {
static targets = ["link", "content"]
static values = { refreshInterval: 10000 }
index = 0;

connect() {
this.showInitialTab();
this.loopThroughTabs();
console.log(this.refreshIntervalValue);
}

showInitialTab() {
const initialTab = this.linkTargets[0];
if (initialTab) {
this.showTab(initialTab.dataset.target);
}
}

loopThroughTabs() {
this.showTab(this.linkTargets[this.index].dataset.target);

this.interval = setInterval(() => {
this.index = (this.index + 1) % this.linkTargets.length;
this.showTab(this.linkTargets[this.index].dataset.target);
}, this.refreshIntervalValue);
}

showTab(targetId) {
this.resetTabs();
const activeTab = this.linkTargets.find(link => link.dataset.target === targetId);
const activeContent = this.contentTargets.find(content => content.dataset.target === targetId);

if (activeTab && activeContent) {
activeTab.classList.add("text-xl", "text-gray-100", 'bg-indigo-500', 'transition', 'duration-300', 'ease-in-out');

activeContent.classList.remove('absolute', 'top-0', 'left-full', 'w-0', 'h-0');
activeContent.classList.add('relative');
}
}

resetTabs() {
this.linkTargets.forEach(link => {
link.classList.add('transition', 'duration-300', 'ease-in-out');
link.classList.remove("text-xl", "text-gray-100", 'bg-indigo-500');
});
this.contentTargets.forEach(content => {
content.classList.add('absolute', 'top-0', 'left-full', 'w-0', 'h-0');
content.classList.remove('relative');
});
}


changeTab(event) {
event.preventDefault();
const targetId = event.target.dataset.target;
const newIndex = this.linkTargets.findIndex(link => link.dataset.target === targetId);
if (newIndex >= 0) {
this.index = newIndex;
}
this.showTab(targetId);

clearInterval(this.interval);
this.loopThroughTabs();
}

disconnect() {
clearInterval(this.interval);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
</div>
<div class="flex flex-col bg-gray-400/5 p-2">
<dt class="line-chart">
<canvas data-controller="line-chart"
<canvas class="chart"
data-controller="line-chart"
data-line-chart-label-value="CPU History"
data-line-chart-label-array-value="<%= @cpu_history_data[:created_at_times] %>"
data-line-chart-data-array-value="<%= @cpu_history_data[:cpu_usages] %>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
</div>
<div class="flex flex-col items-center justify-center bg-gray-400/5 p-8">
<dt class="pie-chart">
<canvas data-controller="pie-chart"
<canvas class="chart"
data-controller="pie-chart"
data-pie-chart-label-value="CPU Usage"
data-pie-chart-idle-value="<%= 100.0 - @service.cpu %>"
data-pie-chart-idle-label-value="Idle"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
</div>
<div class="flex flex-col items-center justify-center bg-gray-400/5 p-8">
<dt class="pie-chart">
<canvas data-controller="pie-chart"
<canvas class="chart"
data-controller="pie-chart"
data-pie-chart-label-value="Memory Usage"
data-pie-chart-idle-value="<%= @service.mem_free %>"
data-pie-chart-idle-label-value="Idle"
Expand Down
105 changes: 61 additions & 44 deletions app/views/mission_control/servers/projects/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -42,52 +42,69 @@
</div>
</div>

<% @services.each do |hostname, services| %>
<div class="py-4 sm:py-8">
<div class="mx-auto max-w-7xl px-6 lg:px-8">
<div class="mx-auto max-w-2xl lg:max-w-none">
<div class="text-center">
<h2 class="text-3xl font-bold tracking-tight text-gray-900 sm:text-4xl"><%= hostname %></h2>
</div>
<dl class="mt-8 grid grid-cols-1 gap-0.5 overflow-hidden rounded-2xl text-center sm:grid-cols-2 lg:grid-cols-4">
<%= turbo_frame_tag dom_id(@project, [hostname, :cpu_usage].join('-')),
src: project_dashboards_cpu_usage_path(project_id: @project.token, hostname: hostname),
"data-controller": "refresh",
"data-refresh-src-value": project_dashboards_cpu_usage_path(project_id: @project.token, hostname: hostname),
"data-refresh-interval-value": 30000 %>
<%= tag.div data: { controller: "tabs", "tabs-refresh-interval-value": (params[:interval].to_i * 1000 || 10_000 ) } do %>
<div class="mt-3">
<div class="">
<!-- Current: "text-gray-900", Default: "text-gray-500 hover:text-gray-700" -->
<nav class="isolate flex divide-x divide-gray-200 dark:divide-gray-500 rounded-lg shadow" aria-label="Tabs">
<% @services.each_with_index do |(hostname, services), index| %>
<% first_tab_class = index.zero? ? "rounded-l-lg" : "" %>
<% last_tab_class = index == @services.length - 1 ? "rounded-r-lg" : "" %>
<%= tag.a "#",
"data-action": "click->tabs#changeTab",
"data-target": hostname,
"data-tabs-target": "link",
class: "tab-link flex items-center justify-center #{first_tab_class} #{last_tab_class}" do %>
<%= hostname %>
<% end %>
<% end %>
</nav>
</div>
</div>

<% @services.each do |hostname, services| %>
<%= tag.div id: [hostname, "tab-content"].join("-"), class: "py-4 sm:py-8 tab-content", "data-target": hostname, "data-tabs-target": "content" do %>
<div class="mx-auto max-w-7xl px-6 lg:px-8">
<div class="mx-auto max-w-2xl lg:max-w-none">
<dl class="grid grid-cols-1 gap-0.5 overflow-hidden rounded-2xl text-center sm:grid-cols-2 lg:grid-cols-4">
<%= turbo_frame_tag dom_id(@project, [hostname, :cpu_usage].join('-')),
src: project_dashboards_cpu_usage_path(project_id: @project.token, hostname: hostname),
"data-controller": "refresh",
"data-refresh-src-value": project_dashboards_cpu_usage_path(project_id: @project.token, hostname: hostname),
"data-refresh-interval-value": 30000 %>

<%= turbo_frame_tag dom_id(@project, [hostname, :memory_usage].join('-')),
src: project_dashboards_memory_usage_path(project_id: @project.token, hostname: hostname),
"data-controller": "refresh",
"data-refresh-src-value": project_dashboards_memory_usage_path(project_id: @project.token, hostname: hostname),
"data-refresh-interval-value": 30000 %>
<%= turbo_frame_tag dom_id(@project, [hostname, :memory_usage].join('-')),
src: project_dashboards_memory_usage_path(project_id: @project.token, hostname: hostname),
"data-controller": "refresh",
"data-refresh-src-value": project_dashboards_memory_usage_path(project_id: @project.token, hostname: hostname),
"data-refresh-interval-value": 30000 %>

<%= turbo_frame_tag dom_id(@project, [hostname, :disk_free].join('-')),
src: project_dashboards_disk_free_path(project_id: @project.token, hostname: hostname),
"data-controller": "refresh",
"data-refresh-src-value": project_dashboards_disk_free_path(project_id: @project.token, hostname: hostname),
"data-refresh-interval-value": 60000 %>
<%= turbo_frame_tag dom_id(@project, [hostname, :disk_free].join('-')),
src: project_dashboards_disk_free_path(project_id: @project.token, hostname: hostname),
"data-controller": "refresh",
"data-refresh-src-value": project_dashboards_disk_free_path(project_id: @project.token, hostname: hostname),
"data-refresh-interval-value": 60000 %>

<%= turbo_frame_tag dom_id(@project, [hostname, :last_seen].join('-')),
src: project_dashboards_last_seen_path(project_id: @project.token, hostname: hostname),
"data-controller": "refresh",
"data-refresh-src-value": project_dashboards_last_seen_path(project_id: @project.token, hostname: hostname),
"data-refresh-interval-value": 60000 %>
</dl>
<dl class="mt-8 grid grid-cols-1 gap-0.5 overflow-hidden rounded-2xl text-center sm:grid-cols-1 lg:grid-cols-2">
<%= turbo_frame_tag dom_id(@project, [hostname, :cpu_history].join('-')),
src: project_dashboards_cpu_history_path(project_id: @project.token, hostname: hostname),
"data-controller": "refresh",
"data-refresh-src-value": project_dashboards_cpu_history_path(project_id: @project.token, hostname: hostname),
"data-refresh-interval-value": 60000 %>
<%= turbo_frame_tag dom_id(@project, [hostname, :memory_history].join('-')),
src: project_dashboards_memory_history_path(project_id: @project.token, hostname: hostname),
"data-controller": "refresh",
"data-refresh-src-value": project_dashboards_memory_history_path(project_id: @project.token, hostname: hostname),
"data-refresh-interval-value": 60000 %>
</dl>
<%= turbo_frame_tag dom_id(@project, [hostname, :last_seen].join('-')),
src: project_dashboards_last_seen_path(project_id: @project.token, hostname: hostname),
"data-controller": "refresh",
"data-refresh-src-value": project_dashboards_last_seen_path(project_id: @project.token, hostname: hostname),
"data-refresh-interval-value": 60000 %>
</dl>
<dl class="mt-8 grid grid-cols-1 gap-0.5 overflow-hidden rounded-2xl text-center sm:grid-cols-1 lg:grid-cols-2">
<%= turbo_frame_tag dom_id(@project, [hostname, :cpu_history].join('-')),
src: project_dashboards_cpu_history_path(project_id: @project.token, hostname: hostname),
"data-controller": "refresh",
"data-refresh-src-value": project_dashboards_cpu_history_path(project_id: @project.token, hostname: hostname),
"data-refresh-interval-value": 60000 %>
<%= turbo_frame_tag dom_id(@project, [hostname, :memory_history].join('-')),
src: project_dashboards_memory_history_path(project_id: @project.token, hostname: hostname),
"data-controller": "refresh",
"data-refresh-src-value": project_dashboards_memory_history_path(project_id: @project.token, hostname: hostname),
"data-refresh-interval-value": 60000 %>
</dl>
</div>
</div>
</div>
</div>
<% end %>
<% end %>
<% end %>

2 changes: 1 addition & 1 deletion lib/mission_control/servers/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module MissionControl
module Servers
VERSION = "0.1.6"
VERSION = "0.2.0"
end
end

0 comments on commit 758058e

Please sign in to comment.