summaryrefslogtreecommitdiff
path: root/htroot/js/IndexReIndexMonitor.js
blob: 787a45d9d6d88a18c95a5799fea08829f96fa0cd (plain)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * @licstart  The following is the entire license notice for the 
 * JavaScript code in this file.
 * 
 * Copyright (C) 2018 by luccioman; https://github.com/luccioman
 * 
 *         
 * This file is part of YaCy.
 * 
 * YaCy is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 * 
 * YaCy is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with YaCy.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @licend  The above is the entire license notice
 * for the JavaScript code in this file.
 */

/* This is JavaScript for the IndexReIndexMonitor_p.html page */

var REFRESH_DELAY = 5000;
var TERMINATED_STATUS = 2;
var xhr = null;
if (window.XMLHttpRequest) {
	if(window.JSON && window.JSON.parse) {
		xhr = new XMLHttpRequest();
	} else {
		console.warn("JSON parsing is not supported by the browser.");
	}
} else {
	console.warn("XMLHttpRequest is not supported by the browser.");
}

/**
 * Once DOM is fully loaded, handle eventual reindex and recrawl jobs reports
 * refreshing
 */
function domLoaded() {
	/* Get the DOM elements to be refreshed */
	var earlyRecrawlTerminationElem = document.getElementById("earlyRecrawlTermination");
	var recrawlStatusElem = document.getElementById("recrawlStatus");
	var recrawlQueryTextElem = document.getElementById("recrawlQueryText");
	var recrawlStartTimeElem = document.getElementById("recrawlStartTime");
	var recrawlEndTimeElem = document.getElementById("recrawlEndTime");
	var urlsToRecrawlCountElem = document.getElementById("urlsToRecrawlCount");
	var recrawledUrlsCountElem = document.getElementById("recrawledUrlsCount");
	var rejectedUrlsCountElem = document.getElementById("rejectedUrlsCount");
	var malformedUrlsCountElem = document.getElementById("malformedUrlsCount");
	var refreshingIcon = document.getElementById("refreshingIcon");
	var refreshFailureIcon = document.getElementById("refreshFailureIcon");
	var refreshBtn = document.getElementById("recrawlRefreshBtn");

	if (recrawlStatusElem != null && recrawlStatusElem.dataset != null) {
		/* Initialize status labels that may be translated in the html template */
		var recrawlStatusLabels = {};
		if(recrawlStatusElem.dataset.status0 != null) {
			recrawlStatusLabels["0"] = recrawlStatusElem.dataset.status0;
		}
		if(recrawlStatusElem.dataset.status1 != null) {
			recrawlStatusLabels["1"] = recrawlStatusElem.dataset.status1;
		}
		if(recrawlStatusElem.dataset.status2 != null) {
			recrawlStatusLabels["2"] = recrawlStatusElem.dataset.status2;
		}
		
		if (recrawlStatusElem.dataset.status != TERMINATED_STATUS) {
			/* Update a DOM element when it exists */
			var updateElemText = function(elem, newValue) {
				if (newValue != null && elem != null) {
					elem.innerText = newValue;
				}
			}
			
			/* Handle failure to fetch fresh report information */
			var handleFetchReportError = function() {
				/* Hide the icon marking automated refresh, but keep the manual refresh button */
				if(refreshingIcon != null) {
					refreshingIcon.className = "hidden";
				}
				/* Display the icon informing user about failure on automated report refresh */
				if(refreshFailureIcon != null && refreshFailureIcon.className != null) {
					refreshFailureIcon.className = refreshFailureIcon.className.replace("hidden", "");
				}
			}
			
			/* Update report DOM elements with the fetched report information */
			var updateReportElements = function(report) {
				recrawlStatusElem.dataset.status = report.status;

				if (recrawlStatusLabels[report.status] != null) {
					/* Use the eventually translated status label when available */
					updateElemText(recrawlStatusElem, recrawlStatusLabels[report.status]);
				} else if(report.statusLabel != null) {
					/* Otherwise use the default one provided in the json response */
					updateElemText(recrawlStatusElem, report.statusLabel);
				}
				if(report.earlyTerminated) {
					if(earlyRecrawlTerminationElem != null && earlyRecrawlTerminationElem.className != null) {
						earlyRecrawlTerminationElem.className = earlyRecrawlTerminationElem.className.replace("hidden", "");
					}
				}
				updateElemText(recrawlQueryTextElem, report.query);
				updateElemText(recrawlStartTimeElem,
						report.startTime);
				updateElemText(recrawlEndTimeElem, report.endTime);
				if (report.urlCounts != null) {
					updateElemText(urlsToRecrawlCountElem,
							report.urlCounts.toRecrawl);
					updateElemText(recrawledUrlsCountElem,
							report.urlCounts.recrawled);
					updateElemText(rejectedUrlsCountElem,
							report.urlCounts.rejected);
					updateElemText(malformedUrlsCountElem,
							report.urlCounts.malformed);
					if (report.urlCounts.malformedDeleted != null
							&& malformedUrlsCountElem != null) {
						malformedUrlsCountElem.title = malformedUrlsCountElem.title
								.replace(/\d+/,
										report.urlCounts.malformedDeleted);
					}
				}
			}

			/* Processing the response from IndexReIndexMonitor_p.json */
			var handleResponse = function() {
				if (xhr.readyState == 4 /* XMLHttpRequest.DONE */) {
					if (xhr.status != 200 || xhr.response == null) {
						handleFetchReportError();
						return;
					}
					var report = null;
					try {
						var jsonResponse = window.JSON.parse(xhr.response);
						if (jsonResponse != null) {
							report = jsonResponse.recrawlJob;
						}
					} catch(error) {
						console.error("JSON parsing error ", error);
					}
					if (report == null || report.status == null) {
						handleFetchReportError();
						return;
					}

					updateReportElements(report);

					if(report.status == TERMINATED_STATUS) {
						/* First hide the icon marking automated refresh and the manual refresh button */
						if(refreshingIcon != null) {
							refreshingIcon.className = "hidden";
						}
						if(refreshBtn != null) {
							refreshBtn.className = "hidden";
						}
						/*
						 * Then if the update fieldset is displayed, completely refresh the page to display again the
						 * new recrawl job fieldset
						 */
						if(document.getElementById("updateJobFieldset") != null) {
							window.location.href = "IndexReIndexMonitor_p.html";
						}
					} else {
						/*
						 * Continue refreshing while the job is not
						 * terminated
						 */
						window.setTimeout(function() {
							xhr.onreadystatechange = handleResponse;
							xhr.open("get", "IndexReIndexMonitor_p.json");
							xhr.send();
						}, REFRESH_DELAY);
					}
				}
			};
			
			/*
			 * We are here so JavaScript is enabled and required API are supported : 
			 * we can show the refreshing icon if the element is present
			 */
			if(refreshingIcon != null && refreshingIcon.className != null) {
				refreshingIcon.className = refreshingIcon.className.replace("hidden", "");
			}

			window.setTimeout(function() {
				xhr.onreadystatechange = handleResponse;
				xhr.open("get", "IndexReIndexMonitor_p.json");
				xhr.send();
			}, REFRESH_DELAY);
		}
	}
}
if (xhr != null) {
	window.onload = domLoaded;
}