<commit_before>package gop

import (
    "github.com/gorilla/mux"
    "github.com/gorilla/schema"
    "net/http"
    "fmt"
    "time"
    "os"
)

var decoder = schema.NewDecoder()       // Single-instance so struct info cached

func gopHandler(g *Req, w http.ResponseWriter, r *http.Request) {
    enabled, _ := g.Cfg.GetBool("gop", "enable_gop_urls", false)
    if !enabled {
        http.Error(w, "Not enabled", http.StatusNotFound)
        return
    }
    vars := mux.Vars(r)
    switch vars["action"] {
        case "request-status": {
            handleRequestStatus(g, w, r)
            return
        }
        case "test": {
            handleTest(g, w, r)
            return
        }
        default: {
            http.Error(w, "Not Found", http.StatusNotFound)
            return
        }
    }
}

func handleRequestStatus(g *Req, w http.ResponseWriter, r *http.Request) {
    reqChan := make(chan *Req)
    g.app.getReqs <- reqChan
    appDuration := time.Since(g.app.startTime).Seconds()
    fmt.Fprintf(w, "%s - %s PID %d up for %.3fs (%s)\n\n", g.app.ProjectName, g.app.AppName, os.Getpid(), appDuration, g.app.startTime)
    for req := range reqChan {<commit_msg>send mem stats in json<commit_after>package gop

import (
    "github.com/gorilla/mux"
    "github.com/gorilla/schema"
    "net/http"
    "fmt"
    "time"
    "os"
    "encoding/json"
    "runtime"
)

var decoder = schema.NewDecoder()       // Single-instance so struct info cached

func gopHandler(g *Req, w http.ResponseWriter, r *http.Request) {
    enabled, _ := g.Cfg.GetBool("gop", "enable_gop_urls", false)
    if !enabled {
        http.Error(w, "Not enabled", http.StatusNotFound)
        return
    }
    vars := mux.Vars(r)
    switch vars["action"] {
        case "request-status": {
            handleRequestStatus(g, w, r)
            return
        }
        case "mem": {
            handleMem(g, w, r)
            return
        }
        case "test": {
            handleTest(g, w, r)
            return
        }
        default: {
            http.Error(w, "Not Found", http.StatusNotFound)
            return
        }
    }
}

func handleMem(g *Req, w http.ResponseWriter, r *http.Request) {
    var memStats runtime.MemStats
    runtime.ReadMemStats(&memStats)
    json, err := json.Marshal(memStats)
    if err != nil {
        g.Error("Failed to encode memstats as json: %s", err.Error())
        http.Error(w, "Failed to get stats", http.StatusInternalServerError)
        return
    }
    w.Header().Set("Content-Type", "text/json")
    w.Write(json)
}

func handleRequestStatus(g *Req, w http.ResponseWriter, r *http.Request) {
    reqChan := make(chan *Req)
    g.app.getReqs <- reqChan
    appDuration := time.Since(g.app.startTime).Seconds()
    fmt.Fprintf(w, "%s - %s PID %d up for %.3fs (%s)\n\n", g.app.ProjectName, g.app.AppName, os.Getpid(), appDuration, g.app.startTime)
    for req := range reqChan {
{'fraction_non_alphanumeric': 0.14036288942143102, 'fraction_numerical': 0.0006846970215679561, 'mean_word_length': 2.2039473684210527, 'pattern_counts': {'":': 5, '<': 5, '<?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': '703364', 'n_tokens_mistral': 983, 'n_tokens_neox': 976, 'n_words': 225}