snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s

This commit is contained in:
upstream-mirror
2026-09-18 01:22:23 +05:00
commit 7e385fc126
8391 changed files with 1766236 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
package profiler
import "net/http"
func StartHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
// add some info to context
next.ServeHTTP(rw, r)
})
}
func FinishHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
next.ServeHTTP(rw, r)
})
}

View File

@@ -0,0 +1,57 @@
package profiler
import (
"encoding/base64"
"fmt"
"time"
h "github.com/cortezaproject/corteza/server/pkg/http"
)
type (
Hit struct {
ID string
Status int
Route uint64
R *h.Request
Ts *time.Time
Tf *time.Time
D *time.Duration
}
Hits map[string][]*Hit
)
func (h *Hit) generateID() {
h.ID = base64.URLEncoding.EncodeToString([]byte(fmt.Sprintf("%s_%d", h.R.URL.Path, h.Ts.UnixNano())))
}
func (s Hits) Filter(fn func(k string, v *Hit) bool) Hits {
ss := Hits{}
for k, v := range s {
for _, vv := range v {
if !fn(k, vv) {
continue
}
ss[k] = append(ss[k], vv)
}
}
return ss
}
func (h Hits) Len() int {
return h.Len()
}
func (h Hits) Less(i, j int) bool {
return false
}
func (h Hits) Swap(i, j int) {
return
}

View File

@@ -0,0 +1,90 @@
package profiler
import (
"net/http"
"sync"
"time"
h "github.com/cortezaproject/corteza/server/pkg/http"
)
type (
Profiler struct {
mux sync.RWMutex
l Hits
}
)
func New() *Profiler {
return &Profiler{l: make(Hits)}
}
func (p *Profiler) Hit(r *h.Request) (h *Hit) {
var (
n = time.Now()
)
h = &Hit{"", http.StatusOK, 0, r, &n, nil, nil}
h.generateID()
return
}
func (p *Profiler) Push(h *Hit) (id string) {
p.mux.Lock()
defer p.mux.Unlock()
if h.Tf == nil {
n := time.Now()
d := n.Sub(*h.Ts)
h.Tf = &n
h.D = &d
}
h.generateID()
id = p.id(h.R)
p.l[id] = append(p.l[id], h)
return
}
func (p *Profiler) Hits(s Sort) Hits {
p.mux.RLock()
defer p.mux.RUnlock()
ll := p.l.Filter(func(k string, v *Hit) bool {
var b bool = true
if s.Path != "" && v.R.URL.Path != s.Path {
b = false
}
if s.Hit != "" && v.ID != s.Hit {
b = false
}
return b
})
return ll
}
func (p *Profiler) Purge(f *PurgeFilter) {
p.mux.Lock()
defer p.mux.Unlock()
if f.RouteID == 0 {
p.l = make(Hits, 0)
return
}
p.l = p.l.Filter(func(k string, v *Hit) bool {
return v.Route != f.RouteID
})
}
func (p *Profiler) id(r *h.Request) string {
return r.URL.Path
}

View File

@@ -0,0 +1,163 @@
package profiler
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
h "github.com/cortezaproject/corteza/server/pkg/http"
"github.com/stretchr/testify/require"
)
const (
day = time.Hour * 24
)
func Test_ApigwProfiler_newHit(t *testing.T) {
var (
p = New()
req = require.New(t)
rr, err = h.NewRequest(httptest.NewRequest("POST", "/foo", strings.NewReader(`foo`)))
hit = p.Hit(rr)
)
req.NoError(err)
req.Equal(hit.Status, http.StatusOK)
req.NotNil(hit.Ts)
req.NotNil(hit.ID)
}
func Test_ApigwProfiler_push(t *testing.T) {
var (
p = New()
req = require.New(t)
rr, err = h.NewRequest(httptest.NewRequest("POST", "/foo", strings.NewReader(`foo`)))
hit = p.Hit(rr)
id = p.Push(hit)
)
_, found := p.l[id]
req.NoError(err)
req.NotEmpty(id)
req.Len(p.l, 1)
req.True(found)
}
func Test_ApigwProfiler_filterPath(t *testing.T) {
var (
pp = New()
req = require.New(t)
now = time.Date(2022, time.March, 1, 1, 1, 1, 0, time.UTC)
then = now.Add(-1 * day)
later = now.Add(day)
hr, _ = h.NewRequest(httptest.NewRequest("POST", "/foo", strings.NewReader(`foo`)))
hr2, _ = h.NewRequest(httptest.NewRequest("GET", "/bar", strings.NewReader(`foo`)))
hr3, _ = h.NewRequest(httptest.NewRequest("GET", "/baz", strings.NewReader(`foo`)))
)
pp.Push(&Hit{R: hr, Ts: &then})
pp.Push(&Hit{R: hr, Ts: &then})
pp.Push(&Hit{R: hr2, Ts: &now})
pp.Push(&Hit{R: hr3, Ts: &later})
list := pp.Hits(Sort{
Path: hr3.RequestURI,
})
_, found := list[hr3.RequestURI]
req.True(found)
req.Len(list[hr3.RequestURI], 1)
}
func Test_ApigwProfiler_filterHit(t *testing.T) {
var (
pp = New()
req = require.New(t)
now = time.Date(2022, time.March, 1, 1, 1, 1, 0, time.UTC)
then = now.Add(-1 * day)
later = now.Add(day)
hr, _ = h.NewRequest(httptest.NewRequest("POST", "/foo", strings.NewReader(`foo`)))
hr2, _ = h.NewRequest(httptest.NewRequest("GET", "/bar", strings.NewReader(`foo`)))
hr3, _ = h.NewRequest(httptest.NewRequest("GET", "/baz", strings.NewReader(`foo`)))
)
pp.Push(&Hit{R: hr, Ts: &then})
pp.Push(&Hit{R: hr, Ts: &now})
pp.Push(&Hit{R: hr2, Ts: &now})
pp.Push(&Hit{R: hr2, Ts: &later})
pp.Push(&Hit{R: hr2, Ts: &later})
h := pp.Hit(hr3)
h.Ts = &later
id := pp.Push(h)
list := pp.Hits(Sort{
Hit: h.ID,
})
_, found := list[id]
req.True(found)
req.Len(list[id], 1)
}
func Test_ApigwProfiler_purgeAll(t *testing.T) {
var (
pp = New()
req = require.New(t)
now = time.Date(2022, time.March, 1, 1, 1, 1, 0, time.UTC)
then = now.Add(-1 * day)
later = now.Add(day)
hr, _ = h.NewRequest(httptest.NewRequest("POST", "/foo", strings.NewReader(`foo`)))
hr2, _ = h.NewRequest(httptest.NewRequest("GET", "/bar", strings.NewReader(`foo`)))
)
pp.Push(&Hit{R: hr, Ts: &then})
pp.Push(&Hit{R: hr, Ts: &now})
pp.Push(&Hit{R: hr2, Ts: &now})
pp.Push(&Hit{R: hr2, Ts: &later})
pp.Push(&Hit{R: hr2, Ts: &later})
req.Len(pp.l, 2)
pp.Purge(&PurgeFilter{})
req.Len(pp.l, 0)
}
func Test_ApigwProfiler_purgeRoute(t *testing.T) {
var (
pp = New()
req = require.New(t)
now = time.Date(2022, time.March, 1, 1, 1, 1, 0, time.UTC)
then = now.Add(-1 * day)
later = now.Add(day)
hr, _ = h.NewRequest(httptest.NewRequest("POST", "/foo", strings.NewReader(`foo`)))
hr2, _ = h.NewRequest(httptest.NewRequest("GET", "/bar", strings.NewReader(`foo`)))
)
pp.Push(&Hit{Route: 1, R: hr, Ts: &then})
pp.Push(&Hit{Route: 1, R: hr, Ts: &now})
pp.Push(&Hit{Route: 2, R: hr2, Ts: &now})
pp.Push(&Hit{Route: 2, R: hr2, Ts: &later})
pp.Push(&Hit{Route: 2, R: hr2, Ts: &later})
req.Len(pp.l, 2)
pp.Purge(&PurgeFilter{RouteID: 2})
req.Len(pp.l, 1)
}

View File

@@ -0,0 +1,7 @@
package profiler
type (
PurgeFilter struct {
RouteID uint64
}
)

View File

@@ -0,0 +1,8 @@
package profiler
type (
Sort struct {
Hit string
Path string
}
)