Skip to content

Commit

Permalink
various tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
boyter committed Aug 4, 2023
1 parent 2d1bdb2 commit 929e73e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 21 deletions.
1 change: 1 addition & 0 deletions cmd/badges/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
badges
37 changes: 22 additions & 15 deletions cmd/badges/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import (
)

var uniqueCode = "unique_code"
var cache = NewSimpleCache(1000)
var cache = NewSimpleCache(100)
var countingSemaphore = make(chan bool, 5)

func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -48,12 +49,12 @@ func main() {
}

category := strings.TrimSpace(strings.ToLower(r.URL.Query().Get("category")))
wage := tryParseInt(strings.TrimSpace(strings.ToLower(r.URL.Query().Get("avg-wage"))), 56286)
title, value := calculate(category, wage, res)

title, value := calculate(category, res)

textLength := "250"
s := formatCount(float64(value))

textLength := "250"
if len(s) <= 3 {
textLength = "200"
}
Expand All @@ -63,10 +64,12 @@ func main() {
_, _ = w.Write([]byte(`<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100" height="20"><linearGradient id="b" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="a"><rect width="100" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#a)"><path fill="#555" d="M0 0h69v20H0z"/><path fill="#4c1" d="M69 0h31v20H69z"/><path fill="url(#b)" d="M0 0h100v20H0z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="110"> <text x="355" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="590">` + title + `</text><text x="355" y="140" transform="scale(.1)" textLength="590">` + title + `</text><text x="835" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="` + textLength + `">` + s + `</text><text x="835" y="140" transform="scale(.1)" textLength="` + textLength + `">` + s + `</text></g> </svg>`))
})

http.ListenAndServe(":8080", nil).Error()
addr := ":8080"
log.Info().Str(uniqueCode, "1876ce1e").Str("addr", addr).Msg("serving")
http.ListenAndServe(addr, nil).Error()
}

func calculate(category string, res []processor.LanguageSummary) (string, int64) {
func calculate(category string, wage int, res []processor.LanguageSummary) (string, int64) {
title := ""
var value int64

Expand Down Expand Up @@ -98,16 +101,7 @@ func calculate(category string, res []processor.LanguageSummary) (string, int64)
value += x.Code
}

wage := 56286
//if 'avg-wage' in event['queryStringParameters']:
//wage = event['queryStringParameters']['avg-wage']

value = int64(estimateCost(value, wage))
//if wage.isdigit():
//s = format_count(estimate_cost(sum([x['Code'] for x in j]), int(wage)))
//else:
//s = format_count(estimate_cost(sum([x['Code'] for x in j])))

case "lines": // lines is the default
fallthrough
case "line": // lines is the default
Expand Down Expand Up @@ -186,6 +180,11 @@ func formatCount(count float64) string {
}

func process(id int, s location) ([]byte, error) {
countingSemaphore <- true
defer func() {
<-countingSemaphore // remove one to free up concurrency
}()

val, ok := cache.Get(s.String())
if ok {
return val, nil
Expand Down Expand Up @@ -317,3 +316,11 @@ func estimateEffort(codeCount int64) float64 {
func estimateCost(codeCount int64, averageWage int) float64 {
return estimateEffort(codeCount) * (float64(averageWage) / 12) * 1.8
}

func tryParseInt(s string, def int) int {
i, err := strconv.Atoi(s)
if err != nil {
return def
}
return i
}
27 changes: 25 additions & 2 deletions cmd/badges/simplecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"math"
"sync"
"time"
)

type cacheEntry struct {
Expand All @@ -17,13 +18,35 @@ type SimpleCache struct {
}

func NewSimpleCache(maxItems int) *SimpleCache {
cache := SimpleCache{
simpleCache := SimpleCache{
maxItems: maxItems,
items: map[string]cacheEntry{},
lock: sync.Mutex{},
}
simpleCache.runAgeItems()
return &simpleCache
}

return &cache
func (cache *SimpleCache) runAgeItems() {
go func() {
for {
// maps are randomly ordered, so only decrementing 50 at a time should be acceptable
count := 50
cache.lock.Lock()
for k, v := range cache.items {
if v.hits > 0 {
v.hits--
cache.items[k] = v
}
count--
if count <= 0 {
break
}
}
cache.lock.Unlock()
time.Sleep(10 * time.Second)
}
}()
}

func (cache *SimpleCache) Add(cacheKey string, entry []byte) {
Expand Down
8 changes: 4 additions & 4 deletions cmd/badges/simplecache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ func TestSimpleCache_Multiple(t *testing.T) {
}

func TestSimpleCache_MultipleLarge(t *testing.T) {
simpleCache := NewSimpleCache(10)
simpleCache := NewSimpleCache(1000)

for i := 0; i < 5000; i++ {
for i := 0; i < 500000; i++ {
simpleCache.Add(fmt.Sprintf("%d", i), []byte{})
simpleCache.Add("10", []byte{})
simpleCache.Get(fmt.Sprintf("%d", i))
simpleCache.Get("10")
simpleCache.Get("10")
}

if len(simpleCache.items) != 9 {
t.Errorf("expected 9 items got %v", len(simpleCache.items))
if len(simpleCache.items) != 999 {
t.Errorf("expected 999 items got %v", len(simpleCache.items))
}
}

0 comments on commit 929e73e

Please sign in to comment.