-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibs.js
More file actions
157 lines (144 loc) · 3.72 KB
/
libs.js
File metadata and controls
157 lines (144 loc) · 3.72 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const axios = require("axios");
const {
API_KEY,
BASE_URL,
} = process.env;
const token = API_KEY;
function config(workspace, notificationIds) {
async function add_new_monitor(name, site, monitor_type = "http") {
const headers = {
"User-Agent": "TianjiMonitor/1.0",
Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
Authorization: "Basic <base64-encoded-credentials>",
Host: "stats.pop.coop",
Referer: BASE_URL,
"Cache-Control": "no-cache",
};
let payload = {
url: site,
method: "get",
contentType: "application/json",
headers: JSON.stringify({
"User-Agent": "TianjiMonitor/1.0",
Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
Authorization: "Basic <base64-encoded-credentials>",
Host: "stats.pop.coop",
Referer: BASE_URL,
"Cache-Control": "no-cache",
}),
};
if (monitor_type !== "http") {
payload = {
hostname: site,
};
}
let data = JSON.stringify({
name,
type: monitor_type,
active: true,
interval: 60,
maxRetries: 2,
trendingMode: false,
notificationIds: notificationIds,
payload,
});
let config = {
method: "post",
maxBodyLength: Infinity,
url: `${BASE_URL}/open/workspace/${workspace}/monitor/upsert`,
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${token}`,
},
data: data,
};
axios
.request(config)
.then((response) => {
console.log(JSON.stringify(response.data));
})
.catch((error) => {
console.log(error);
});
}
async function delete_all() {
let config = {
method: "get",
maxBodyLength: Infinity,
url: `${BASE_URL}/open/workspace/${workspace}/monitor/all`,
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${token}`,
},
};
axios
.request(config)
.then((response) => {
if (response.data) {
Promise.all(
response.data.map(async (item) => {
console.log(`Delete ${item.name}`);
await delete_monitor(item.id);
})
);
}
})
.catch((error) => {
console.log(error);
});
}
async function delete_monitor(monitorId) {
let config = {
method: "delete",
maxBodyLength: Infinity,
url: `${BASE_URL}/open/workspace/${workspace}/monitor/${monitorId}/del`,
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${token}`,
},
};
axios
.request(config)
.then((response) => {
console.log(JSON.stringify(response.data));
})
.catch((error) => {
console.log(error);
});
}
async function list() {
let config = {
method: "get",
maxBodyLength: Infinity,
url: `${BASE_URL}/open/workspace/${workspace}/monitor/all`,
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${token}`,
},
};
axios
.request(config)
.then((response) => {
console.log(response.data);
response.data.map((item) => {
console.log(item.notifications);
});
})
.catch((error) => {
console.log(error);
});
}
return {
add_new_monitor,
delete_monitor,
delete_all,
list,
};
}
module.exports.config = config;