Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shields for Uptime Robot #947

Merged
merged 8 commits into from
Apr 20, 2017
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ Thumbs.db
.Spotlight-V100
.Trashes

# Jetbrains
/.idea

# Created by https://www.gitignore.io/api/node

### Node ###
Expand Down
8 changes: 8 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,14 @@ <h3 id="miscellaneous"> Miscellaneous </h3>
<td><img src='https://img.shields.io/swagger/valid/2.0/https/bitbucket.org/api/swagger.json.svg?maxAge=2592000' alt=''/></td>
<td><code>https://img.shields.io/swagger/valid/2.0/https/bitbucket.org/api/swagger.json.svg</code></td>
</tr>
<tr><th> Uptime Robot status: </th>
<td><img src='https://img.shields.io/uptimerobot/status/m778918918-3e92c097147760ee39d02d36.svg?maxAge=2592000' alt=''/></td>
<td><code>https://img.shields.io/uptimerobot/status/m778918918-3e92c097147760ee39d02d36.svg</code></td>
</tr>
<tr><th> Uptime Robot ratio (30 days): </th>
<td><img src='https://img.shields.io/uptimerobot/ratio/30/m778918918-3e92c097147760ee39d02d36.svg?maxAge=2592000' alt=''/></td>
<td><code>https://img.shields.io/uptimerobot/ratio/30/m778918918-3e92c097147760ee39d02d36.svg</code></td>
</tr>
</tbody></table>

<h3 id="miscellaneous"> Longer Miscellaneous </h3>
Expand Down
127 changes: 127 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6030,6 +6030,133 @@ cache(function(data, match, sendBadge, request) {
});
}));

// Uptime Robot status integration.
// API documentation : https://uptimerobot.com/api
camp.route(/^\/uptimerobot\/status\/(.*)\.(svg|png|gif|jpg|json)$/,
cache(function(data, match, sendBadge, request) {
var monitorApiKey = match[1]; // eg, m778918918-3e92c097147760ee39d02d36
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

var format = match[2];
var badgeData = getBadgeData('status', data);
var options = {
method: 'POST',
json: true,
body: {
"api_key": monitorApiKey,
"format": "json"
},
uri: 'https://api.uptimerobot.com/v2/getMonitors'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you include a link to the API docs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

};
// A monitor API key must start with "m"
if (monitorApiKey.substring(0, "m".length) !== "m") {
badgeData.text[1] = 'must use a monitor key';
sendBadge(format, badgeData);
return;
}
request(options, function(err, res, json) {
if (err !== null || res.statusCode >= 500 || typeof json !== 'object') {
badgeData.text[1] = 'inaccessible';
sendBadge(format, badgeData);
return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a "monitor not found" branch?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If an incorrect api key is entered for a monitor the following json is returned :

{
	"stat": "fail",
	"error": {
		"type": "invalid_parameter",
		"parameter_name": "api_key",
		"passed_value": "u956-afus321g565fghr519",
		"message": "api_key not found."
	}
}

I can add this case if this is what you want with something like that :

...
if (json.stat === 'fail') {
    badgeData.text[1] = 'unknown error';
    if (json.error) {
        badgeData.text[1] = json.error.message;
    }
    badgeData.colorscheme = 'lightgrey';
    sendBadge(format, badgeData);
    return;
}
var status = json.monitors[0].status;
if (status === 0) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding that. Looks great. Though, I think it would be better to check that json.error.message is a string. Otherwise if error is an empty object you could end up with undefined on the badge.

if (json.error && typeof json.error.message === 'string') {
  badgeData.text[1] = json.error.message;
} else {
  badgeData.text[1] = 'vendor error';
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 91f69ae

try {
if (json.stat === 'fail') {
badgeData.text[1] = 'vendor error';
if (json.error && typeof json.error.message === 'string') {
badgeData.text[1] = json.error.message;
}
badgeData.colorscheme = 'lightgrey';
sendBadge(format, badgeData);
return;
}
var status = json.monitors[0].status;
if (status === 0) {
badgeData.text[1] = 'paused';
badgeData.colorscheme = 'yellow';
} else if (status === 1) {
badgeData.text[1] = 'not checked yet';
badgeData.colorscheme = 'yellowgreen';
} else if (status === 2) {
badgeData.text[1] = 'up';
badgeData.colorscheme = 'brightgreen';
} else if (status === 8) {
badgeData.text[1] = 'seems down';
badgeData.colorscheme = 'orange';
} else if (status === 9) {
badgeData.text[1] = 'down';
badgeData.colorscheme = 'red';
} else {
badgeData.text[1] = 'invalid';
badgeData.colorscheme = 'lightgrey';
}
sendBadge(format, badgeData);
} catch(e) {
badgeData.text[1] = 'invalid';
sendBadge(format, badgeData);
}
});
}));

// Uptime Robot ratio integration.
// API documentation : https://uptimerobot.com/api
camp.route(/^\/uptimerobot\/ratio\/(.*)\/(.*)\.(svg|png|gif|jpg|json)$/,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In testing this out, I found it was easy to miss the numberOfDays parameter. What do you think about moving it before the key, so it's less easily missed, and making it optional?

http://img.shields.io/uptimerobot/ratio/m778918918-3e92c097147760ee39d02d36.svg
http://img.shields.io/uptimerobot/ratio/30/m778918918-3e92c097147760ee39d02d36.svg

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you're right 1bd8766

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still required.

Copy link
Contributor Author

@crazy-max crazy-max Apr 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oups forgot the optional part...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 3ed5543

cache(function(data, match, sendBadge, request) {
var numberOfDays = match[1]; // eg, 30
var monitorApiKey = match[2]; // eg, m778918918-3e92c097147760ee39d02d36
var format = match[3];
var badgeData = getBadgeData('uptime', data);
var options = {
method: 'POST',
json: true,
body: {
"api_key": monitorApiKey,
"custom_uptime_ratios": numberOfDays,
"format": "json"
},
uri: 'https://api.uptimerobot.com/v2/getMonitors'
};
// A monitor API key must start with "m"
if (monitorApiKey.substring(0, "m".length) !== "m") {
badgeData.text[1] = 'must use a monitor key';
sendBadge(format, badgeData);
return;
}
request(options, function(err, res, json) {
if (err !== null) {
badgeData.text[1] = 'inaccessible';
sendBadge(format, badgeData);
return;
}
try {
if (json.stat === 'fail') {
badgeData.text[1] = 'vendor error';
if (json.error && typeof json.error.message === 'string') {
badgeData.text[1] = json.error.message;
}
badgeData.colorscheme = 'lightgrey';
sendBadge(format, badgeData);
return;
}
var percent = parseFloat(json.monitors[0].custom_uptime_ratio);
badgeData.text[1] = percent + '%';
if (percent <= 10) {
badgeData.colorscheme = 'red';
} else if (percent <= 30) {
badgeData.colorscheme = 'yellow';
} else if (percent <= 50) {
badgeData.colorscheme = 'yellowgreen';
} else if (percent <= 70) {
badgeData.colorscheme = 'green';
} else {
badgeData.colorscheme = 'brightgreen';
}
sendBadge(format, badgeData);
} catch (e) {
badgeData.text[1] = 'invalid';
sendBadge(format, badgeData);
}
});
}));

// Any badge.
camp.route(/^\/(:|badge\/)(([^-]|--)*?)-(([^-]|--)*)-(([^-]|--)+)\.(svg|png|gif|jpg)$/,
function(data, match, end, ask) {
Expand Down
8 changes: 8 additions & 0 deletions try.html
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,14 @@ <h3 id="miscellaneous"> Miscellaneous </h3>
<td><img src='/swagger/valid/2.0/https/bitbucket.org/api/swagger.json.svg' alt=''/></td>
<td><code>https://img.shields.io/swagger/valid/2.0/https/bitbucket.org/api/swagger.json.svg</code></td>
</tr>
<tr><th> Uptime Robot status: </th>
<td><img src='/uptimerobot/status/m778918918-3e92c097147760ee39d02d36.svg' alt=''/></td>
<td><code>https://img.shields.io/uptimerobot/status/m778918918-3e92c097147760ee39d02d36.svg</code></td>
</tr>
<tr><th> Uptime Robot ratio (30 days): </th>
<td><img src='/uptimerobot/ratio/30/m778918918-3e92c097147760ee39d02d36.svg' alt=''/></td>
<td><code>https://img.shields.io/uptimerobot/ratio/30/m778918918-3e92c097147760ee39d02d36.svg</code></td>
</tr>
</tbody></table>

<h3 id="miscellaneous"> Longer Miscellaneous </h3>
Expand Down