<commit_before>package v2

import (
	"encoding/json"
	"net/http"
	"strconv"

	"github.com/SpectoLabs/hoverfly/core/handlers"
	"github.com/codegangsta/negroni"
	"github.com/go-zoo/bone"
)

type HoverflyLogs interface {
	GetLogsView() LogsView
	GetFilteredLogsView(int) LogsView
}

type LogsHandler struct {
	Hoverfly HoverflyLogs
}

func (this *LogsHandler) RegisterRoutes(mux *bone.Mux, am *handlers.AuthHandler) {
	mux.Get("/api/v2/logs", negroni.New(
		negroni.HandlerFunc(am.RequireTokenAuthentication),
		negroni.HandlerFunc(this.Get),
	))
}

func (this *LogsHandler) Get(w http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
	var logs LogsView

	queryParams := req.URL.Query()
	limitQuery, err := strconv.Atoi(queryParams.Get("limit"))
	if err == nil {
		logs = this.Hoverfly.GetFilteredLogsView(limitQuery)
	} else {
		logs = this.Hoverfly.GetLogsView()
	}

	bytes, _ := json.Marshal(logs)

	handlers.WriteResponse(w, bytes)
}<commit_msg>Added a websocket for logs at /api/v2/ws/logs<commit_after>package v2

import (
	"encoding/json"
	"errors"
	"net/http"
	"reflect"
	"strconv"

	"github.com/SpectoLabs/hoverfly/core/handlers"
	"github.com/codegangsta/negroni"
	"github.com/go-zoo/bone"
)

type HoverflyLogs interface {
	GetLogsView() LogsView
	GetFilteredLogsView(int) LogsView
}

type LogsHandler struct {
	Hoverfly HoverflyLogs
}

func (this *LogsHandler) RegisterRoutes(mux *bone.Mux, am *handlers.AuthHandler) {
	mux.Get("/api/v2/logs", negroni.New(
		negroni.HandlerFunc(am.RequireTokenAuthentication),
		negroni.HandlerFunc(this.Get),
	))

	mux.Get("/api/v2/ws/logs", http.HandlerFunc(this.GetWS))
}

func (this *LogsHandler) Get(w http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
	var logs LogsView

	queryParams := req.URL.Query()
	limitQuery, err := strconv.Atoi(queryParams.Get("limit"))
	if err == nil {
		logs = this.Hoverfly.GetFilteredLogsView(limitQuery)
	} else {
		logs = this.Hoverfly.GetLogsView()
	}

	bytes, _ := json.Marshal(logs)

	handlers.WriteResponse(w, bytes)
}

func (this *LogsHandler) GetWS(w http.ResponseWriter, r *http.Request) {

	var previousLogs LogsView

	handlers.NewWebsocket(func() ([]byte, error) {
		currentLogs := this.Hoverfly.GetLogsView()

		if !reflect.DeepEqual(currentLogs, previousLogs) {
			previousLogs = currentLogs
			return json.Marshal(currentLogs)
		}

		return nil, errors.New("No update needed")
	}, w, r)
}
{'fraction_non_alphanumeric': 0.13643926788685523, 'fraction_numerical': 0.0024958402662229617, 'mean_word_length': 4.938271604938271, 'pattern_counts': {'":': 0, '<': 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': '2247310', 'n_tokens_mistral': 955, 'n_tokens_neox': 921, 'n_words': 161}