<commit_before>package routes

import (
	"encoding/json"
	"github.com/MarvinMenzerath/UpAndRunning2/lib"
	"github.com/julienschmidt/httprouter"
	"github.com/op/go-logging"
	"net/http"
	"strconv"
)

func ApiIndex(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	responseBytes, _ := json.Marshal(BasicResponse{true, "Welcome to UpAndRunning's API!"})
	w.Header().Set("Content-Type", "application/json")
	w.Write(responseBytes)
}

func ApiStatus(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	// Query the Database
	db := lib.GetDatabase()
	stmt, err := db.Prepare("SELECT id, name, protocol, url, status, time, lastFailStatus, lastFailTime, ups, downs, totalChecks, avgAvail FROM website WHERE url = ? AND enabled = 1;")
	if err != nil {
		logging.MustGetLogger("logger").Error("Unable to fetch Websites: ", err)
		SendJsonMessage(w, http.StatusInternalServerError, "Unable to process your Request.")
		return
	}

	var (
		id             int
		name           string
		protocol       string
		url            string
		status         string
		time           string
		lastFailStatus string
		lastFailTime   string
		ups            int
		downs          int
		totalChecks    int
		avgAvail       float64
	)

	err = stmt.QueryRow(ps.ByName("url")).Scan(&id, &name, &protocol, &url, &status, &time, &lastFailStatus, &lastFailTime, &ups, &downs, &totalChecks, &avgAvail)
	if err != nil {
		logging.MustGetLogger("logger").Error("Unable to fetch Websites: ", err)
		SendJsonMessage(w, http.StatusInternalServerError, "Unable to process your Request.")
		return
	}

	// Build Response
	responseJson := DetailedWebsiteResponse{true, WebsiteData{id, name, protocol + "://" + url}, WebsiteAvailability{ups, downs, totalChecks, strconv.FormatFloat(avgAvail, 'f', 2, 64) + "%"}, WebsiteCheckResult{status, time}, WebsiteCheckResult{lastFailStatus, lastFailTime}}

	// Send Response
	responseBytes, err := json.Marshal(responseJson)
	if err != nil {
		SendJsonMessage(w, http.StatusInternalServerError, "Unable to process your Request.")
		return
	}
	w.Header().Set("Content-Type", "application/json")
	w.Write(responseBytes)
}

func ApiWebsites(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	// Query the Database
	db := lib.GetDatabase()
	rows, err := db.Query("SELECT name, protocol, url, status FROM website WHERE enabled = 1 AND visible = 1;")
	if err != nil {
		logging.MustGetLogger("logger").Error("Unable to fetch Websites: ", err)
		SendJsonMessage(w, http.StatusInternalServerError, "Unable to process your Request.")
		return
	}
	defer rows.Close()

	// Add every Website
	websites := []BasicWebsite{}
	for rows.Next() {
		var row BasicWebsite
		err = rows.Scan(&row.Name, &row.Protocol, &row.Url, &row.Status)
		if err != nil {
			logging.MustGetLogger("logger").Error("Unable to read Website-Row: ", err)
			SendJsonMessage(w, http.StatusInternalServerError, "Unable to process your Request.")
			return
		}
		websites = append(websites, row)
	}

	// Check for Errors
	err = rows.Err()
	if err != nil {
		logging.MustGetLogger("logger").Error("Unable to read Website-Rows: ", err)
		SendJsonMessage(w, http.StatusInternalServerError, "Unable to process your Request.")
		return
	}

	// Send Response
	responseBytes, err := json.Marshal(WebsiteResponse{true, websites})
	if err != nil {
		SendJsonMessage(w, http.StatusInternalServerError, "Unable to process your Request.")
		return
	}
	w.Header().Set("Content-Type", "application/json")
	w.Write(responseBytes)
}
<commit_msg>Updated Status-API to tell about unknown Websites<commit_after>package routes

import (
	"encoding/json"
	"github.com/MarvinMenzerath/UpAndRunning2/lib"
	"github.com/julienschmidt/httprouter"
	"github.com/op/go-logging"
	"net/http"
	"strconv"
	"database/sql"
)

func ApiIndex(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	responseBytes, _ := json.Marshal(BasicResponse{true, "Welcome to UpAndRunning's API!"})
	w.Header().Set("Content-Type", "application/json")
	w.Write(responseBytes)
}

func ApiStatus(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	// Query the Database
	db := lib.GetDatabase()
	stmt, err := db.Prepare("SELECT id, name, protocol, url, status, time, lastFailStatus, lastFailTime, ups, downs, totalChecks, avgAvail FROM website WHERE url = ? AND enabled = 1;")
	if err != nil {
		logging.MustGetLogger("logger").Error("Unable to fetch Website-Status: ", err)
		SendJsonMessage(w, http.StatusInternalServerError, "Unable to process your Request.")
		return
	}

	var (
		id             int
		name           string
		protocol       string
		url            string
		status         string
		time           string
		lastFailStatus string
		lastFailTime   string
		ups            int
		downs          int
		totalChecks    int
		avgAvail       float64
	)

	err = stmt.QueryRow(ps.ByName("url")).Scan(&id, &name, &protocol, &url, &status, &time, &lastFailStatus, &lastFailTime, &ups, &downs, &totalChecks, &avgAvail)
	switch {
	case err == sql.ErrNoRows:
		SendJsonMessage(w, http.StatusNotFound, "Unable to find any data matching the given url.")
		return
	case err != nil:
		logging.MustGetLogger("logger").Error("Unable to fetch Website-Status: ", err)
		SendJsonMessage(w, http.StatusInternalServerError, "Unable to process your Request.")
		return
	}

	// Build Response
	responseJson := DetailedWebsiteResponse{true, WebsiteData{id, name, protocol + "://" + url}, WebsiteAvailability{ups, downs, totalChecks, strconv.FormatFloat(avgAvail, 'f', 2, 64) + "%"}, WebsiteCheckResult{status, time}, WebsiteCheckResult{lastFailStatus, lastFailTime}}

	// Send Response
	responseBytes, err := json.Marshal(responseJson)
	if err != nil {
		SendJsonMessage(w, http.StatusInternalServerError, "Unable to process your Request.")
		return
	}
	w.Header().Set("Content-Type", "application/json")
	w.Write(responseBytes)
}

func ApiWebsites(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	// Query the Database
	db := lib.GetDatabase()
	rows, err := db.Query("SELECT name, protocol, url, status FROM website WHERE enabled = 1 AND visible = 1;")
	if err != nil {
		logging.MustGetLogger("logger").Error("Unable to fetch Websites: ", err)
		SendJsonMessage(w, http.StatusInternalServerError, "Unable to process your Request.")
		return
	}
	defer rows.Close()

	// Add every Website
	websites := []BasicWebsite{}
	for rows.Next() {
		var row BasicWebsite
		err = rows.Scan(&row.Name, &row.Protocol, &row.Url, &row.Status)
		if err != nil {
			logging.MustGetLogger("logger").Error("Unable to read Website-Row: ", err)
			SendJsonMessage(w, http.StatusInternalServerError, "Unable to process your Request.")
			return
		}
		websites = append(websites, row)
	}

	// Check for Errors
	err = rows.Err()
	if err != nil {
		logging.MustGetLogger("logger").Error("Unable to read Website-Rows: ", err)
		SendJsonMessage(w, http.StatusInternalServerError, "Unable to process your Request.")
		return
	}

	// Send Response
	responseBytes, err := json.Marshal(WebsiteResponse{true, websites})
	if err != nil {
		SendJsonMessage(w, http.StatusInternalServerError, "Unable to process your Request.")
		return
	}
	w.Header().Set("Content-Type", "application/json")
	w.Write(responseBytes)
}

{'fraction_non_alphanumeric': 0.12922355537167288, 'fraction_numerical': 0.0024824162184526274, 'mean_word_length': 4.221022318214543, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5780254', 'n_tokens_mistral': 2394, 'n_tokens_neox': 2273, 'n_words': 597}