82 lines
2.8 KiB
Java
82 lines
2.8 KiB
Java
package com.svnlog.web.service;
|
|
|
|
import java.io.IOException;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.time.Instant;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import com.svnlog.web.model.TaskInfo;
|
|
import com.svnlog.web.model.TaskStatus;
|
|
|
|
@Service
|
|
public class HealthService {
|
|
|
|
private final OutputFileService outputFileService;
|
|
private final SettingsService settingsService;
|
|
private final TaskService taskService;
|
|
|
|
public HealthService(OutputFileService outputFileService,
|
|
SettingsService settingsService,
|
|
TaskService taskService) {
|
|
this.outputFileService = outputFileService;
|
|
this.settingsService = settingsService;
|
|
this.taskService = taskService;
|
|
}
|
|
|
|
public Map<String, Object> basicHealth() {
|
|
final Map<String, Object> response = new HashMap<String, Object>();
|
|
response.put("status", "ok");
|
|
response.put("timestamp", Instant.now().toString());
|
|
return response;
|
|
}
|
|
|
|
public Map<String, Object> detailedHealth() throws IOException {
|
|
final Map<String, Object> result = new HashMap<String, Object>();
|
|
final Map<String, Object> settings = settingsService.getSettings();
|
|
final Path outputRoot = outputFileService.getOutputRoot();
|
|
|
|
final boolean outputDirWritable = ensureWritable(outputRoot);
|
|
int running = 0;
|
|
int failed = 0;
|
|
int cancelled = 0;
|
|
for (TaskInfo task : taskService.getTasks()) {
|
|
if (task.getStatus() == TaskStatus.RUNNING || task.getStatus() == TaskStatus.PENDING) {
|
|
running++;
|
|
}
|
|
if (task.getStatus() == TaskStatus.FAILED) {
|
|
failed++;
|
|
}
|
|
if (task.getStatus() == TaskStatus.CANCELLED) {
|
|
cancelled++;
|
|
}
|
|
}
|
|
|
|
result.put("status", "ok");
|
|
result.put("timestamp", Instant.now().toString());
|
|
result.put("outputDir", outputRoot.toString());
|
|
result.put("outputDirWritable", outputDirWritable);
|
|
result.put("apiKeyConfigured", Boolean.TRUE.equals(settings.get("apiKeyConfigured")));
|
|
result.put("taskTotal", taskService.getTasks().size());
|
|
result.put("taskRunning", running);
|
|
result.put("taskFailed", failed);
|
|
result.put("taskCancelled", cancelled);
|
|
return result;
|
|
}
|
|
|
|
private boolean ensureWritable(Path outputRoot) {
|
|
try {
|
|
Files.createDirectories(outputRoot);
|
|
final Path probe = outputRoot.resolve(".health-probe");
|
|
Files.write(probe, "ok".getBytes("UTF-8"));
|
|
Files.deleteIfExists(probe);
|
|
return true;
|
|
} catch (Exception e) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|