Skip to content

Instantly share code, notes, and snippets.

@huynhbaoan
Last active October 24, 2024 14:07
Show Gist options
  • Save huynhbaoan/d41bdb07359165fcc0ad91a9e65b0242 to your computer and use it in GitHub Desktop.
Save huynhbaoan/d41bdb07359165fcc0ad91a9e65b0242 to your computer and use it in GitHub Desktop.
Various script. Awk, JS
grep -E '2024/(07|08|09)' $1 | awk -F, '
BEGIN {
sum_cpu_avg = 0;
sum_cpu_max = 0;
max_cpu_avg = 0;
max_cpu_max = 0;
count = 0;
}
{
sum_cpu_avg += $2;
sum_cpu_max += $3;
if ($2 > max_cpu_avg) max_cpu_avg = $2;
if ($3 > max_cpu_max) max_cpu_max = $3;
count++;
}
END {
# Print averages
printf "Average CPUUtilization Average: %.6f\n", sum_cpu_avg / count;
printf "Average CPUUtilization Maximum: %.6f\n", sum_cpu_max / count;
# Print maximums
printf "\nHighest CPUUtilization Average: %.6f\n", max_cpu_avg;
printf "Highest CPUUtilization Maximum: %.6f\n", max_cpu_max;
}'
grep -E '2024/(07|08|09)' yourfile.csv | awk -F, '
BEGIN {
sum_networkout_avg = 0;
sum_networkin_max = 0;
sum_networkin_avg = 0;
sum_networkout_max = 0;
max_networkout_avg = 0;
max_networkin_max = 0;
max_networkin_avg = 0;
max_networkout_max = 0;
count = 0;
}
{
sum_networkout_avg += $2;
sum_networkin_max += $3;
sum_networkin_avg += $4;
sum_networkout_max += $5;
if ($2 > max_networkout_avg) max_networkout_avg = $2;
if ($3 > max_networkin_max) max_networkin_max = $3;
if ($4 > max_networkin_avg) max_networkin_avg = $4;
if ($5 > max_networkout_max) max_networkout_max = $5;
count++;
}
END {
conversion_to_gbps = (8 / 1000000000); # Convert bytes to bits, then to Gbps
# Print averages in Gbps
printf "Average NetworkOut Average (Gbps): %.6f\n", (sum_networkout_avg / count) * conversion_to_gbps;
printf "Average NetworkIn Maximum (Gbps): %.6f\n", (sum_networkin_max / count) * conversion_to_gbps;
printf "Average NetworkIn Average (Gbps): %.6f\n", (sum_networkin_avg / count) * conversion_to_gbps;
printf "Average NetworkOut Maximum (Gbps): %.6f\n", (sum_networkout_max / count) * conversion_to_gbps;
# Print maximums in Gbps
printf "\nHighest NetworkOut Average (Gbps): %.6f\n", max_networkout_avg * conversion_to_gbps;
printf "Highest NetworkIn Maximum (Gbps): %.6f\n", max_networkin_max * conversion_to_gbps;
printf "Highest NetworkIn Average (Gbps): %.6f\n", max_networkin_avg * conversion_to_gbps;
printf "Highest NetworkOut Maximum (Gbps): %.6f\n", max_networkout_max * conversion_to_gbps;
}'
grep -E '2024/(07|08|09)' yourfile.csv | awk -F, '
BEGIN {
sum_networkout_avg = 0;
sum_networkin_max = 0;
sum_networkin_avg = 0;
sum_networkout_max = 0;
count = 0;
}
{
sum_networkout_avg += $2;
sum_networkin_max += $3;
sum_networkin_avg += $4;
sum_networkout_max += $5;
count++;
}
END {
print "Average NetworkOut Average: " sum_networkout_avg / count;
print "Average NetworkIn Maximum: " sum_networkin_max / count;
print "Average NetworkIn Average: " sum_networkin_avg / count;
print "Average NetworkOut Maximum: " sum_networkout_max / count;
}'
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Playlist with Multiple Videos and Error Handling</title>
</head>
<body>
<h1>YouTube Playlist</h1>
<h2 id="song-title"></h2> <!-- Song title will appear here -->
<div id="player"></div>
<button id="prevButton">Previous</button>
<button id="nextButton">Next</button>
<button id="shuffleButton">Shuffle</button>
<p id="error-message" style="color: red;"></p> <!-- Display error messages -->
<script>
// Load the YouTube IFrame Player API code
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
var playlist = [
//{videoId: "pS-AWIfuBNE", startSeconds: 0, endSeconds: 0, title: "Gimme! Gimme! Gimme! (ABBA) | EPIC ORCHESTRAL COVER"},
//{videoId: "5RtFA3IojOE", startSeconds: 0, endSeconds: 0, title: "The Final Countdown (Europe) | EPIC ORCHESTRAL COVER"},
//{videoId: "htekfsbqqwc", startSeconds: 0, endSeconds: 0, title: "SYMPHONIKA ON THE ROCK - The Final Countdown | Europe Cover - Rock Orchestra"},
{videoId: "pICAha0nsb0", startSeconds: 0, endSeconds: 0, title: "Two Steps From Hell - Star Sky"},
{videoId: "n-Dh3ftkRAs", startSeconds: 0, endSeconds: 0, title: "Two Steps From Hell - Flight of the Silverbird"},
{videoId: "RKmw9oS__MM", startSeconds: 0, endSeconds: 0, title: "Two Steps From Hell - Cannon in D Minor"},
{videoId: "hKRUPYrAQoE", startSeconds: 0, endSeconds: 0, title: "Two Steps From Hell - Victory"},
{videoId: "jIxas0a-KgM", startSeconds: 0, endSeconds: 0, title: "Two Steps From Hell - Strength of a Thousand Men (Archangel)"},
{videoId: "R7WFzpWQ6-c", startSeconds: 0, endSeconds: 0, title: "Two Steps From Hell - Archangel (Archangel)"},
{videoId: "vbttZVTSJRU", startSeconds: 5, endSeconds: 0, title: "Two Steps From Hell - Blackheart (SkyWorld)"},
{videoId: "qiB98Wbsdlo", startSeconds: 5, endSeconds: 0, title: "Two Steps From Hell - El Dorado (SkyWorld)"},
{videoId: "ibufSTLNQrU", startSeconds: 0, endSeconds: 0, title: "Thomas Bergersen - Mountain Call"},
{videoId: "GDWnpSR9Tas", startSeconds: 0, endSeconds: 0, title: "Rasputin (Boney M.) | EPIC ORCHESTRAL COVER"},
{videoId: "UUcnw1iNN-8", startSeconds: 0, endSeconds: 0, title: "Katyusha (Катюша) | Orchestral Performance"},
{videoId: "rlDoFUzFqho", startSeconds: 0, endSeconds: 0, title: "Moskau (Dschinghis Khan) | EPIC ORCHESTRAL COVER"},
{videoId: "BFpjbckH-hY", startSeconds: 0, endSeconds: 0, title: "Canon in D - Hungarian Dance Style (Pachelbel meets Brahms) | Epic Orchestral"},
{videoId: "", startSeconds: 0, endSeconds: 0, title: ""},
];
var currentIndex = 0;
var shuffledPlaylist = [];
var isShuffled = false;
// Function that loads the player
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '315',
width: '560',
videoId: playlist[currentIndex].videoId,
playerVars: {
'autoplay': 1,
'start': playlist[currentIndex].startSeconds
},
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange,
'onError': onPlayerError // Handle player errors
}
});
}
// Function to handle when the player is ready
function onPlayerReady(event) {
updateSongTitle();
player.seekTo(playlist[currentIndex].startSeconds, true);
}
// Function to handle video state changes
function onPlayerStateChange(event) {
if (event.data === YT.PlayerState.ENDED) {
playNext();
}
}
// Function to handle video loading errors
function onPlayerError(event) {
var errorMessage = document.getElementById('error-message');
var currentVideoTitle = getCurrentVideo().title; // Get the title of the video that caused the error
switch (event.data) {
case 2:
errorMessage.textContent = `Error with video: "${currentVideoTitle}". Invalid video ID.`;
break;
case 5:
errorMessage.textContent = `Error with video: "${currentVideoTitle}". The video is not available in HTML5.`;
break;
case 100:
errorMessage.textContent = `Error with video: "${currentVideoTitle}". Video not found.`;
break;
case 101:
case 150:
errorMessage.textContent = `Error with video: "${currentVideoTitle}". Embedding of this video has been disabled. Skipping to next video.`;
break;
default:
errorMessage.textContent = `Error with video: "${currentVideoTitle}". An unknown error occurred.`;
}
// Print the title of the video causing the error to the console
console.log(`Error loading video: "${currentVideoTitle}"`);
// Skip to the next video if there's an error
playNext();
}
// Function to update the song title
function updateSongTitle() {
var songTitleElement = document.getElementById('song-title');
songTitleElement.textContent = getCurrentVideo().title;
}
// Function to play the next track
function playNext() {
currentIndex++;
if (currentIndex >= getPlaylistLength()) {
currentIndex = 0;
}
loadVideo();
}
// Function to play the previous track
function playPrevious() {
currentIndex--;
if (currentIndex < 0) {
currentIndex = getPlaylistLength() - 1;
}
loadVideo();
}
// Function to load and play a video
function loadVideo() {
var currentVideo = getCurrentVideo();
if (currentVideo.videoId) {
player.loadVideoById({
videoId: currentVideo.videoId,
startSeconds: currentVideo.startSeconds
});
}
updateSongTitle();
}
// Function to shuffle the playlist
function shufflePlaylist() {
isShuffled = !isShuffled;
if (isShuffled && shuffledPlaylist.length === 0) { // Only shuffle once
shuffledPlaylist = [...playlist];
for (let i = shuffledPlaylist.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffledPlaylist[i], shuffledPlaylist[j]] = [shuffledPlaylist[j], shuffledPlaylist[i]];
}
}
currentIndex = 0;
loadVideo();
}
// Helper function to get the current video
function getCurrentVideo() {
return isShuffled ? shuffledPlaylist[currentIndex] : playlist[currentIndex];
}
// Helper function to get the length of the current playlist
function getPlaylistLength() {
return isShuffled ? shuffledPlaylist.length : playlist.length;
}
// Add event listeners to the buttons
document.getElementById('nextButton').addEventListener('click', playNext);
document.getElementById('prevButton').addEventListener('click', playPrevious);
document.getElementById('shuffleButton').addEventListener('click', shufflePlaylist);
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Playlist with Multiple Videos and Timed Switching</title>
</head>
<body>
<h1>YouTube Playlist</h1>
<h2 id="song-title"></h2> <!-- Song title will appear here -->
<div id="player"></div>
<button id="prevButton">Previous</button>
<button id="nextButton">Next</button>
<button id="shuffleButton">Shuffle</button>
<p id="error-message" style="color: red;"></p> <!-- Display error messages -->
<script>
// Load the YouTube IFrame Player API code
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
var timer; // Global variable to hold the timer
var remainingTime; // Variable to store remaining time when pausing
var isPlaying = false; // Track if the video is currently playing
var playlist = [
{videoId: "pS-AWIfuBNE", startSeconds: 0, endSeconds: 60, title: "Gimme! Gimme! Gimme! (ABBA) | EPIC ORCHESTRAL COVER"},
{videoId: "5RtFA3IojOE", startSeconds: 0, endSeconds: 90, title: "The Final Countdown (Europe) | EPIC ORCHESTRAL COVER"},
{videoId: "htekfsbqqwc", startSeconds: 0, endSeconds: 70, title: "SYMPHONIKA ON THE ROCK - The Final Countdown | Europe Cover - Rock Orchestra"},
{videoId: "pICAha0nsb0", startSeconds: 0, endSeconds: 60, title: "Two Steps From Hell - Star Sky"},
{videoId: "n-Dh3ftkRAs", startSeconds: 0, endSeconds: 80, title: "Two Steps From Hell - Flight of the Silverbird"},
{videoId: "RKmw9oS__MM", startSeconds: 0, endSeconds: 75, title: "Two Steps From Hell - Cannon in D Minor"},
{videoId: "hKRUPYrAQoE", startSeconds: 0, endSeconds: 85, title: "Two Steps From Hell - Victory"},
{videoId: "jIxas0a-KgM", startSeconds: 0, endSeconds: 90, title: "Two Steps From Hell - Strength of a Thousand Men (Archangel)"},
{videoId: "R7WFzpWQ6-c", startSeconds: 0, endSeconds: 90, title: "Two Steps From Hell - Archangel (Archangel)"},
{videoId: "vbttZVTSJRU", startSeconds: 5, endSeconds: 70, title: "Two Steps From Hell - Blackheart (SkyWorld)"},
{videoId: "qiB98Wbsdlo", startSeconds: 5, endSeconds: 70, title: "Two Steps From Hell - El Dorado (SkyWorld)"},
{videoId: "ibufSTLNQrU", startSeconds: 0, endSeconds: 80, title: "Thomas Bergersen - Mountain Call"},
{videoId: "GDWnpSR9Tas", startSeconds: 0, endSeconds: 90, title: "Rasputin (Boney M.) | EPIC ORCHESTRAL COVER"},
{videoId: "UUcnw1iNN-8", startSeconds: 0, endSeconds: 60, title: "Katyusha (Катюша) | Orchestral Performance"},
{videoId: "rlDoFUzFqho", startSeconds: 0, endSeconds: 60, title: "Moskau (Dschinghis Khan) | EPIC ORCHESTRAL COVER"},
{videoId: "BFpjbckH-hY", startSeconds: 0, endSeconds: 60, title: "Canon in D - Hungarian Dance Style (Pachelbel meets Brahms) | Epic Orchestral"},
{videoId: "", startSeconds: 0, endSeconds: 0, title: ""},
];
var currentIndex = 0;
var shuffledPlaylist = [];
var isShuffled = false;
// Function that loads the player
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '315',
width: '560',
videoId: playlist[currentIndex].videoId,
playerVars: {
'autoplay': 0, // Do not autoplay initially
'start': playlist[currentIndex].startSeconds
},
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange,
'onError': onPlayerError // Handle player errors
}
});
}
// Function to handle when the player is ready
function onPlayerReady(event) {
loadVideo();
}
// Function to handle video state changes
function onPlayerStateChange(event) {
var currentVideo = getCurrentVideo();
var duration = (currentVideo.endSeconds - currentVideo.startSeconds) * 1000;
if (event.data === YT.PlayerState.PLAYING && !isPlaying) {
// Start the timer or resume it when the video starts playing
isPlaying = true;
if (remainingTime) {
// If the video was paused, resume the remaining time
timer = setTimeout(function() {
playNext();
}, remainingTime);
} else {
// If the video is playing for the first time, start the timer
timer = setTimeout(function() {
playNext();
}, duration);
}
} else if (event.data === YT.PlayerState.PAUSED) {
// Pause the timer when the video is paused
clearExistingTimer();
remainingTime = (currentVideo.endSeconds - player.getCurrentTime()) * 1000; // Calculate the remaining time
isPlaying = false;
} else if (event.data === YT.PlayerState.ENDED) {
// Automatically go to the next video if the current video ends
playNext();
}
}
// Function to handle video loading errors
function onPlayerError(event) {
var errorMessage = document.getElementById('error-message');
var currentVideoTitle = getCurrentVideo().title; // Get the title of the video that caused the error
switch (event.data) {
case 2:
errorMessage.textContent = `Error with video: "${currentVideoTitle}". Invalid video ID.`;
break;
case 5:
errorMessage.textContent = `Error with video: "${currentVideoTitle}". The video is not available in HTML5.`;
break;
case 100:
errorMessage.textContent = `Error with video: "${currentVideoTitle}". Video not found.`;
break;
case 101:
case 150:
errorMessage.textContent = `Error with video: "${currentVideoTitle}". Embedding of this video has been disabled. Skipping to next video.`;
break;
default:
errorMessage.textContent = `Error with video: "${currentVideoTitle}". An unknown error occurred.`;
}
// Skip to the next video if there's an error
playNext();
}
// Function to load and play a video
function loadVideo() {
var currentVideo = getCurrentVideo();
remainingTime = null; // Reset remaining time when a new video is loaded
if (currentVideo.videoId) {
player.loadVideoById({
videoId: currentVideo.videoId,
startSeconds: currentVideo.startSeconds
});
// Clear any existing timer before starting a new one
clearExistingTimer();
}
updateSongTitle();
}
// Function to play the next track
function playNext() {
clearExistingTimer(); // Clear the current timer
currentIndex++;
if (currentIndex >= getPlaylistLength()) {
currentIndex = 0; // Loop back to the first song if it's the last one
}
loadVideo();
}
// Function to play the previous track
function playPrevious() {
clearExistingTimer(); // Clear the current timer
currentIndex--;
if (currentIndex < 0) {
currentIndex = getPlaylistLength() - 1; // Loop back to the last song if it's the first one
}
loadVideo();
}
// Function to clear any existing timer
function clearExistingTimer() {
if (timer) {
clearTimeout(timer); // Clear the timer if it exists
timer = null; // Reset the timer variable
}
}
// Function to update the song title
function updateSongTitle() {
var songTitleElement = document.getElementById('song-title');
songTitleElement.textContent = getCurrentVideo().title;
}
// Function to shuffle the playlist
function shufflePlaylist() {
isShuffled = !isShuffled;
if (isShuffled && shuffledPlaylist.length === 0) { // Only shuffle once
shuffledPlaylist = [...playlist];
for (let i = shuffledPlaylist.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffledPlaylist[i], shuffledPlaylist[j]] = [shuffledPlaylist[j], shuffledPlaylist[i]];
}
}
currentIndex = 0;
loadVideo();
}
// Helper function to get the current video
function getCurrentVideo() {
return isShuffled ? shuffledPlaylist[currentIndex] : playlist[currentIndex];
}
// Helper function to get the length of the current playlist
function getPlaylistLength() {
return isShuffled ? shuffledPlaylist.length : playlist.length;
}
// Add event listeners to the buttons
document.getElementById('nextButton').addEventListener('click', playNext);
document.getElementById('prevButton').addEventListener('click', playPrevious);
document.getElementById('shuffleButton').addEventListener('click', shufflePlaylist);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment