[JavaScript] Found piece of code that works, but now needs to be more object-oriented? via /r/learnprogramming

[JavaScript] Found piece of code that works, but now needs to be more object-oriented?

My project: Build a dashboard that monitors OS-level metrics of servers like if servers are online, their response time, what processes are running, what services are running, etc. Found a code that helps me display a green checkmark if a server is reading "online" in the JSON array, and a red x-mark if it is offline:

 <!-- If/Else script that checks online/offline -->  //define a function that will fetch status and set icon URL function setServerStatusserver123() { var url = "server123report.json"; var online = "green.svg"; var offline = "red.svg"; var $img = $("#server123-status-image"); //get a reference to your  $.getJSON(url, function(data) { if(data && data.status && data.status.toLowerCase() === "online") { $img.attr("src", online); } else { $img.attr("src", offline); } }); } //special jQuery syntax to run when document is ready (like onload but better) $(function() { setServerStatusserver123(); });  

What I'm using:

  • Frontend: HTML/CSS/JS
  • Backend: PowerShell queries that export information via JSON array

Problem: I am trying to reuse this logic and code to check on processes running. There are anywhere from 5 processes to over 20 processes on a single server.

As the code stands right now above, it literally checks data.status and looks for a string match of "online" and displays the green.svg if it's there, and red.svg if it is "offline".

If I want to check the string exactly, I would have to check spotify.exe exactly, meanwhile duplicating the code again for chrome.exe and so forth.

As you can see, it becomes a huge copy and paste onslaught when you're trying to check for a list of processes and it begs the question: How can I make this more object-oriented?

I don't know where to start, I understand OO from a super fundamental perspective, but now I'm in the gauntlet and need to actually do it and am totally lost.


Link for JSON example to test against:


Submitted July 10, 2017 at 04:16PM by networkhappi
via reddit http://ift.tt/2u1vSLJ


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s