This repository has been archived by the owner on May 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
112 lines (107 loc) · 3.34 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
var fs = require("fs");
var request = require('sync-request');
var knex = require('knex')({
client: 'mysql',
connection: {
host : getVar("DB_HOST"),
user : getVar("DB_USER"),
password : getVar("DB_PASS"),
database : getVar("DB_BASE")
}
});
var last_count = ".last_count";
function getVar(name) {
if (process.env[name]) {
return process.env[name];
} else {
console.error("fatal: missing variable " + name + ".");
process.exit(1);
}
return undefined;
}
function sendMsg(payload) {
return request("POST", webhookURL, {
body: JSON.stringify(payload)
});
}
var webhookURL = getVar("SLACK_WEBHOOK_URL");
var descriptions = ["Ha-ha, an error.", "Wasn't me.", "You should probably fix this.", "I blame <@robotxlabs>", "Maybe <https://www.youtube.com/watch?v=dQw4w9WgXcQ|this> will help you?", "<@thatoddmailbox>...", "Ugh, wat du nao?"];
var old_count = parseInt(fs.readFileSync(last_count));
knex("errors").count("*").then(function(data) {
var new_count = data[0]["count(*)"];
if (old_count == new_count) {
console.log("No new errors.");
process.exit(0);
}
var new_errors = new_count - old_count;
sendMsg({text: (new_errors == 1 ? "There is 1 new error." : "There are " + new_errors + " new errors.") });
knex("errors").orderBy('errorId', 'desc').limit(new_errors).select("*").then(function(data) {
var results = data.reverse();
var c = 1;
for (var i in results) {
var row = results[i];
var color = "danger";
if (row.status === "404") {
color = "warning";
} else if (row.msg === "Yay an error"){
color = "good";
}
var payload = {
"attachments": [
{
"fallback": "Uh-oh! An error has occurred, but we don't seem able to display it to you.",
"color": color,
"author_name": "MyHomeworkSpace Error Tracker",
"title": "Error #" + c,
"text": descriptions[Math.floor(Math.random() * descriptions.length)],
"fields": [
{
"title": "Username",
"value": row.username,
"short": true
},
{
"title": "HTTP Status",
"value": row.status,
"short": true
},
{
"title": "URL",
"value": row.url,
"short": false
},
{
"title": "Error Message",
"value": row.msg,
"short": false
},
{
"title": "User-Agent",
"value": JSON.parse(row.headers)['user-agent'],
"short": false
}
],
"footer": "MyHomeworkSpace",
"footer_icon": "https://avatars1.githubusercontent.com/u/15315494",
"ts": new Date(row.timestamp).valueOf() / 1000
}
]
};
if (row.url === "/robots.txt"){
payload = {
attachments: [
{
color: "good",
text: "A bot had a hard time finding the robots.txt file"
}
]
}
};
sendMsg(payload);
c++;
}
fs.writeFileSync(last_count, new_count);
console.log("Done!");
process.exit(0);
});
});