import axios from 'axios';
axios.defaults.headers.common["X-Requested-With"] = "XMLHttpRequest";
axios.defaults.timeout = 2000;
const axiosChecker = axios.create();
const axiosApi = axios.create({
baseURL: "http://monitoring.local/api"
});
class Checker {
static checkForAvailability(project) {
return axiosChecker.get(`${project.url}/status`)
.then((response) => {
if ("OK" !== response.data) {
throw new Error(project);
}
project.status = false;
})
.catch((error) => {
project.status = true;
})
.then(() => project);
}
static sendStats(project) {
return axiosApi.post("/statistics", {
project_id: project.id,
status: project.status,
}).catch((error) => {
//
}).then(() => project);
}
static run(projectID) {
let task = new Promise((resolve, reject) => {
axiosApi.get(`/projects/${projectID}`)
.then((response) => response.data)
.then((projects) => resolve(projects))
.catch((error) => {
//
})
});
task
.then((projects) => {
return Promise.all(projects.map(project => this.checkForAvailability(project)));
})
.then((projects) => {
return Promise.all(projects.map(project => this.sendStats(project)));
})
.then(() => {
this.run(projectID);
});
}
}
window.Checker = Checker;