<commit_before>package server

import (
	"encoding/json"
	"errors"
	log "github.com/Sirupsen/logrus"
	gkmetrics "github.com/go-kit/kit/metrics"
	gkprometheus "github.com/go-kit/kit/metrics/prometheus"
	"github.com/gorilla/mux"
	"github.com/gorilla/schema"
	"github.com/oxfeeefeee/appgo"
	"github.com/oxfeeefeee/appgo/auth"
	"github.com/oxfeeefeee/appgo/toolkit/strutil"
	stdprometheus "github.com/prometheus/client_golang/prometheus"
	"github.com/unrolled/render"
	"net/http"
	"reflect"
	"time"
)

const (
	UserIdFieldName      = "UserId__"
	AdminUserIdFieldName = "AdminUserId__"
	ResIdFieldName       = "ResourceId__"
	ContentFieldName     = "Content__"
	RequestFieldName     = "Request__"

	maxVersion = 99
)

const (
	_ HandlerType = iota
	HandlerTypeJson
	HandlerTypeHtml
)

var decoder = schema.NewDecoder()

var metrics_req_dur gkmetrics.Histogram

type HandlerType int

type httpFunc struct {
	requireAuth    bool
	requireAdmin   bool
	hasResId       bool
	hasContent     bool
	hasRequest     bool
	dummyInput     bool
	allowAnonymous bool
	inputType      reflect.Type
	contentType    reflect.Type
	funcValue      reflect.Value
}

type handler struct {
	htype    HandlerType
	path     string
	template string
	funcs    map[string]*httpFunc
	supports []string
	ts       TokenStore
	renderer *render.Render
}

func init() {
	decoder.IgnoreUnknownKeys(true)

	if appgo.Conf.Prometheus.Enable {
		metrics_req_dur = gkprometheus.NewSummary(stdprometheus.SummaryOpts{
			Namespace: "appgo",
			Subsystem: "api",
			Name:      "request_duration_seconds",
			Help:      "Total time spent serving requests.",
		}, []string{})
	}
}

func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	defer func(begin time.Time) {
		if appgo.Conf.Prometheus.Enable {
			metrics_req_dur.Observe(int64(time.Since(begin).Seconds()))
		}
	}(time.Now())

	method := r.Method
	ver := apiVersionFromHeader(r)
	if ver > 1 && ver <= maxVersion {<commit_msg>change time unit to microsecond<commit_after>package server

import (
	"encoding/json"
	"errors"
	log "github.com/Sirupsen/logrus"
	gkmetrics "github.com/go-kit/kit/metrics"
	gkprometheus "github.com/go-kit/kit/metrics/prometheus"
	"github.com/gorilla/mux"
	"github.com/gorilla/schema"
	"github.com/oxfeeefeee/appgo"
	"github.com/oxfeeefeee/appgo/auth"
	"github.com/oxfeeefeee/appgo/toolkit/strutil"
	stdprometheus "github.com/prometheus/client_golang/prometheus"
	"github.com/unrolled/render"
	"net/http"
	"reflect"
	"time"
)

const (
	UserIdFieldName      = "UserId__"
	AdminUserIdFieldName = "AdminUserId__"
	ResIdFieldName       = "ResourceId__"
	ContentFieldName     = "Content__"
	RequestFieldName     = "Request__"

	maxVersion = 99
)

const (
	_ HandlerType = iota
	HandlerTypeJson
	HandlerTypeHtml
)

var decoder = schema.NewDecoder()

var metrics_req_dur gkmetrics.Histogram

type HandlerType int

type httpFunc struct {
	requireAuth    bool
	requireAdmin   bool
	hasResId       bool
	hasContent     bool
	hasRequest     bool
	dummyInput     bool
	allowAnonymous bool
	inputType      reflect.Type
	contentType    reflect.Type
	funcValue      reflect.Value
}

type handler struct {
	htype    HandlerType
	path     string
	template string
	funcs    map[string]*httpFunc
	supports []string
	ts       TokenStore
	renderer *render.Render
}

func init() {
	decoder.IgnoreUnknownKeys(true)

	if appgo.Conf.Prometheus.Enable {
		metrics_req_dur = gkprometheus.NewSummary(stdprometheus.SummaryOpts{
			Namespace: "appgo",
			Subsystem: "api",
			Name:      "request_duration_microseconds",
			Help:      "Total time spent serving requests.",
		}, []string{})
	}
}

func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	defer func(begin time.Time) {
		if appgo.Conf.Prometheus.Enable {
			metrics_req_dur.Observe(int64(time.Since(begin) / time.Microsecond))
		}
	}(time.Now())

	method := r.Method
	ver := apiVersionFromHeader(r)
	if ver > 1 && ver <= maxVersion {
{'fraction_non_alphanumeric': 0.10458015267175573, 'fraction_numerical': 0.002544529262086514, 'mean_word_length': 3.901496259351621, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1630900', 'n_tokens_mistral': 1453, 'n_tokens_neox': 1384, 'n_words': 269}