Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to simplecache #573

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 23 additions & 43 deletions SCC-OUTPUT-REPORT.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
</tr></thead>
<tbody><tr>
<th>Go</th>
<th>27</th>
<th>9589</th>
<th>1464</th>
<th>447</th>
<th>7678</th>
<th>1413</th>
<th>256080</th>
<th>4138</th>
<th>25</th>
<th>9335</th>
<th>1417</th>
<th>431</th>
<th>7487</th>
<th>1375</th>
<th>251113</th>
<th>4029</th>
</tr><tr>
<td>processor/workers_test.go</td>
<td></td>
Expand Down Expand Up @@ -93,13 +93,13 @@
</tr><tr>
<td>cmd/badges/main.go</td>
<td></td>
<td>375</td>
<td>60</td>
<td>384</td>
<td>62</td>
<td>17</td>
<td>298</td>
<td>59</td>
<td>9660</td>
<td>240</td>
<td>305</td>
<td>58</td>
<td>9762</td>
<td>246</td>
</tr><tr>
<td>processor/workers_tokei_test.go</td>
<td></td>
Expand Down Expand Up @@ -170,16 +170,6 @@
<td>50</td>
<td>3766</td>
<td>99</td>
</tr><tr>
<td>cmd/badges/simplecache.go</td>
<td></td>
<td>161</td>
<td>28</td>
<td>13</td>
<td>120</td>
<td>20</td>
<td>3070</td>
<td>94</td>
</tr><tr>
<td>processor/processor_test.go</td>
<td></td>
Expand All @@ -190,16 +180,6 @@
<td>21</td>
<td>2573</td>
<td>66</td>
</tr><tr>
<td>cmd/badges/simplecache_test.go</td>
<td></td>
<td>102</td>
<td>21</td>
<td>3</td>
<td>78</td>
<td>17</td>
<td>1999</td>
<td>53</td>
</tr><tr>
<td>processor/structs_test.go</td>
<td></td>
Expand Down Expand Up @@ -293,16 +273,16 @@
</tr></tbody>
<tfoot><tr>
<th>Total</th>
<th>27</th>
<th>9589</th>
<th>1464</th>
<th>447</th>
<th>7678</th>
<th>1413</th>
<th>256080</th>
<th>4138</th>
<th>25</th>
<th>9335</th>
<th>1417</th>
<th>431</th>
<th>7487</th>
<th>1375</th>
<th>251113</th>
<th>4029</th>
</tr>
<tr>
<th colspan="9">Estimated Cost to Develop (organic) $229,670<br>Estimated Schedule Effort (organic) 7.86 months<br>Estimated People Required (organic) 2.59<br></th>
<th colspan="9">Estimated Cost to Develop (organic) $223,675<br>Estimated Schedule Effort (organic) 7.79 months<br>Estimated People Required (organic) 2.55<br></th>
</tr></tfoot>
</table></body></html>
43 changes: 26 additions & 17 deletions cmd/badges/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,30 @@ import (
"time"

"github.com/boyter/scc/v3/processor"
"github.com/boyter/simplecache"
jsoniter "github.com/json-iterator/go"
"github.com/rs/zerolog/log"
)

var (
uniqueCode = "unique_code"
cache = NewSimpleCache(1000, 86400)
uniqueCode = "unique_code"
cache = simplecache.New[[]processor.LanguageSummary](simplecache.Option{
MaxItems: intPtr(1000),
MaxAge: timePtr(time.Hour * 72),
})
countingSemaphore = make(chan bool, 1)
tmpDir = os.TempDir()
json = jsoniter.ConfigCompatibleWithStandardLibrary
)

func intPtr(i int) *int {
return &i
}

func timePtr(t time.Duration) *time.Duration {
return &t
}

func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
loc, err := processUrlPath(r.URL.Path)
Expand All @@ -37,23 +49,14 @@ func main() {
return
}

data, err := process(1, loc)
res, err := process(1, loc)
if err != nil {
log.Error().Str(uniqueCode, "03ec75c3").Err(err).Str("loc", loc.String()).Send()
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte("something bad happened sorry"))
return
}

var res []processor.LanguageSummary
err = json.Unmarshal(data, &res)
if err != nil {
log.Error().Str(uniqueCode, "9192cad8").Err(err).Send()
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte("something bad happened sorry"))
return
}

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)
Expand Down Expand Up @@ -250,7 +253,7 @@ func formatCount(count float64) string {
return fmt.Sprintf("%v", math.Round(count))
}

func process(id int, s location) ([]byte, error) {
func process(id int, s location) ([]processor.LanguageSummary, error) {
countingSemaphore <- true
defer func() {
<-countingSemaphore // remove one to free up concurrency
Expand All @@ -277,7 +280,7 @@ func process(id int, s location) ([]byte, error) {
cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
_, err := cmd.Output()

if ctx.Err() == context.DeadlineExceeded {
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
return nil, err
}

Expand Down Expand Up @@ -307,11 +310,17 @@ func process(id int, s location) ([]byte, error) {
return nil, err
}

file, err := os.ReadFile(filePath)
data, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}

var res []processor.LanguageSummary
err = json.Unmarshal(data, &res)
if err != nil {
return nil, err
}
cache.Add(s.String(), file)
_ = cache.Set(s.String(), res)

// Cleanup
if err := os.RemoveAll(filePath); err != nil {
Expand All @@ -322,7 +331,7 @@ func process(id int, s location) ([]byte, error) {
return nil, err
}

return file, nil
return res, nil
}

func processPath(s string) string {
Expand Down
161 changes: 0 additions & 161 deletions cmd/badges/simplecache.go

This file was deleted.

Loading
Loading