{{- $url := "" -}} {{- $report := dict -}} {{- if reflect.IsMap . -}} {{- with index . "URL" -}} {{- $url = . -}} {{- end -}} {{- with index . "Report" -}} {{- $report = . -}} {{- end -}} {{- end -}} {{- $entries := default (dict) (index $report "entries") -}} {{- $deadLinks := default (slice) (index $report "links") -}} {{- $host := "" -}} {{- with $url -}} {{- $host = . | replaceRE "^https?://([^/]+).*$" "$1" | replaceRE "^www\\." "" -}} {{- end -}} {{- $lookupURLs := slice -}} {{- with $url -}} {{- $lookupURLs = $lookupURLs | append . -}} {{- if strings.HasSuffix . "/" -}} {{- $withoutSlash := strings.TrimSuffix "/" . -}} {{- if ne $withoutSlash . -}} {{- $lookupURLs = $lookupURLs | append $withoutSlash -}} {{- end -}} {{- else -}} {{- $lookupURLs = $lookupURLs | append (printf "%s/" .) -}} {{- end -}} {{- end -}} {{- $entry := dict -}} {{- range $lookupURLs -}} {{- if and (eq (len $entry) 0) (isset $entries .) -}} {{- $entry = index $entries . -}} {{- end -}} {{- end -}} {{- $deadInfo := dict -}} {{- range $lookupURLs -}} {{- if eq (len $deadInfo) 0 -}} {{- $matches := where $deadLinks "url" . -}} {{- if gt (len $matches) 0 -}} {{- $deadInfo = index $matches 0 -}} {{- end -}} {{- end -}} {{- end -}} {{- $statusCode := 0 -}} {{- $statusRaw := "" -}} {{- $statusChecked := "" -}} {{- $statusErrorType := "" -}} {{- $statusTone := "unknown" -}} {{- $statusLabel := "État inconnu" -}} {{- $statusTitle := "" -}} {{- if gt (len $entry) 0 -}} {{- with index $entry "status" -}} {{- $statusString := printf "%v" . -}} {{- if gt (len (findRE "^[0-9]+$" $statusString)) 0 -}} {{- $statusCode = int $statusString -}} {{- else -}} {{- $statusRaw = $statusString -}} {{- end -}} {{- end -}} {{- with index $entry "checkedAt" -}} {{- $statusChecked = . -}} {{- end -}} {{- with index $entry "errorType" -}} {{- $statusErrorType = . -}} {{- end -}} {{- end -}} {{- if and (eq $statusCode 0) (eq $statusRaw "") (gt (len $deadInfo) 0) -}} {{- with index $deadInfo "status" -}} {{- $statusString := printf "%v" . -}} {{- if gt (len (findRE "^[0-9]+$" $statusString)) 0 -}} {{- $statusCode = int $statusString -}} {{- else -}} {{- $statusRaw = $statusString -}} {{- end -}} {{- end -}} {{- end -}} {{- if and (eq $statusErrorType "") (gt (len $deadInfo) 0) -}} {{- with index $deadInfo "errorType" -}} {{- $statusErrorType = . -}} {{- end -}} {{- end -}} {{- if gt $statusCode 0 -}} {{- if lt $statusCode 400 -}} {{- $statusTone = "ok" -}} {{- $statusLabel = printf "OK %d" $statusCode -}} {{- else if lt $statusCode 500 -}} {{- $statusTone = "warn" -}} {{- $statusLabel = printf "Erreur %d" $statusCode -}} {{- $statusTitle = printf "Réponse HTTP %d lors de la dernière vérification" $statusCode -}} {{- else -}} {{- $statusTone = "dead" -}} {{- $statusLabel = printf "Erreur %d" $statusCode -}} {{- $statusTitle = printf "Réponse HTTP %d lors de la dernière vérification" $statusCode -}} {{- end -}} {{- else if ne $statusRaw "" -}} {{- $statusTone = "dead" -}} {{- $statusLabel = "Erreur" -}} {{- $statusTitle = printf "Erreur %s lors de la dernière vérification" (upper $statusRaw) -}} {{- else if ne $statusErrorType "" -}} {{- $statusTone = "dead" -}} {{- $statusLabel = "Erreur" -}} {{- $statusTitle = printf "Erreur %s lors de la dernière vérification" (upper $statusErrorType) -}} {{- end -}} {{- if and (ne $statusTitle "") (ne $statusChecked "") -}} {{- $statusTitle = printf "%s (vérifié le %s)" $statusTitle ((time $statusChecked) | time.Format "02/01/2006") -}} {{- end -}} {{- return dict "Host" $host "Code" $statusCode "Raw" $statusRaw "CheckedAt" $statusChecked "ErrorType" $statusErrorType "Tone" $statusTone "Label" $statusLabel "Title" $statusTitle -}}