Skip to content

Commit

Permalink
update go modules, update to lcw v2
Browse files Browse the repository at this point in the history
  • Loading branch information
paskal committed May 1, 2024
1 parent d7e6fe8 commit 2e4d972
Show file tree
Hide file tree
Showing 287 changed files with 22,266 additions and 12,892 deletions.
20 changes: 2 additions & 18 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
linters-settings:
govet:
check-shadowing: true
golint:
min-confidence: 0.8
gocyclo:
min-complexity: 15
maligned:
suggest-new: true
dupl:
threshold: 100
goconst:
min-len: 2
min-occurrences: 2
misspell:
locale: US
lll:
line-length: 140
gocritic:
enabled-tags:
- performance
Expand All @@ -25,7 +10,6 @@ linters-settings:
- wrapperFunc
- hugeParam
- rangeValCopy
revive:

linters:
disable-all: true
Expand Down Expand Up @@ -54,12 +38,12 @@ linters:

run:
modules-download-mode: vendor
skip-dirs:
- vendor
concurrency: 4
tests: false

issues:
exclude-dirs:
- vendor
exclude-rules:
- text: "weak cryptographic primitive"
linters:
Expand Down
13 changes: 7 additions & 6 deletions app/bot/anecdot.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ import (
"strings"
"time"

"github.com/go-pkgz/lcw"
"github.com/go-pkgz/lcw/v2"
)

// Anecdote bot, returns from jokesrv.fermyon.app or chucknorris.io
type Anecdote struct {
client HTTPClient
categCache lcw.LoadingCache
categCache lcw.LoadingCache[[]string]
}

// NewAnecdote makes a bot for jokesrv.fermyon.app and chucknorris.io
func NewAnecdote(client HTTPClient) *Anecdote {
log.Printf("[INFO] anecdote bot with https://jokesrv.fermyon.app and https://api.chucknorris.io/jokes/random")
c, _ := lcw.NewExpirableCache(lcw.MaxKeys(100), lcw.TTL(time.Hour))
o := lcw.NewOpts[[]string]()
c, _ := lcw.NewExpirableCache(o.MaxKeys(100), o.TTL(time.Hour))
return &Anecdote{client: client, categCache: c}
}

Expand Down Expand Up @@ -59,7 +60,7 @@ func (a Anecdote) OnMessage(msg Message) (response Response) {
// get categorize from https://jokesrv.fermyon.app/categories and extend with / prefix and ! suffix
// to mach commands
func (a Anecdote) categories() ([]string, error) {
res, err := a.categCache.Get("categories", func() (interface{}, error) {
res, err := a.categCache.Get("categories", func() ([]string, error) {
var categories []string
req, err := http.NewRequest("GET", "https://jokesrv.fermyon.app/categories", http.NoBody)
if err != nil {
Expand All @@ -83,8 +84,8 @@ func (a Anecdote) categories() ([]string, error) {
return nil, err
}

cc := make([]string, 0, len(res.([]string)))
for _, c := range res.([]string) {
cc := make([]string, 0, len(res))
for _, c := range res {
cc = append(cc, c+"!")
}
return cc, nil
Expand Down
25 changes: 13 additions & 12 deletions app/bot/openai/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"sync"
"time"

"github.com/go-pkgz/lcw"
"github.com/go-pkgz/lcw/v2"
"github.com/go-pkgz/syncs"
tbapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
Expand All @@ -28,7 +28,7 @@ type Summarizer struct {

// Cache is using to optimize `Summary` calls (generating summary by link)
// In debug mode cache is dumped to local file
cache lcw.LoadingCache
cache lcw.LoadingCache[summaryItem]

threads int
debug bool
Expand All @@ -49,7 +49,8 @@ type openAISummary interface {
// NewSummarizer creates new summarizer object
// If debug is true, it loads cache from file
func NewSummarizer(openAISummary openAISummary, remark remarkCommentsGetter, uKeeper uKeeperGetter, threads int, debug bool) Summarizer {
cache, _ := lcw.NewExpirableCache(lcw.MaxKeys(100), lcw.TTL(time.Hour*12))
o := lcw.NewOpts[summaryItem]()
cache, _ := lcw.NewExpirableCache(o.MaxKeys(100), o.TTL(time.Hour*12))

if debug {
err := loadBackup(cache)
Expand Down Expand Up @@ -169,7 +170,7 @@ func (s Summarizer) GetSummariesByRemarkLink(remarkLink string) (messages []stri
// If debug is true, it saves cache to file
// Important: this isn't thread safe
func (s Summarizer) Summary(link string) (summary string, err error) {
item, err := s.cache.Get(link, func() (interface{}, error) { return s.summaryInternal(link) })
item, err := s.cache.Get(link, func() (summaryItem, error) { return s.summaryInternal(link) })
if err != nil {
return "", err
}
Expand All @@ -181,7 +182,7 @@ func (s Summarizer) Summary(link string) (summary string, err error) {
}
}

return item.(summaryItem).render(), nil
return item.render(), nil
}

func (s Summarizer) summaryInternal(link string) (item summaryItem, err error) {
Expand Down Expand Up @@ -226,7 +227,7 @@ func (s summaryItem) isEmpty() bool {
}

// loadBackup loads cache from local file in debug mode
func loadBackup(cache lcw.LoadingCache) error {
func loadBackup(cache lcw.LoadingCache[summaryItem]) error {
type LocalCache struct {
Summaries map[string]summaryItem `json:"Summaries"`
}
Expand All @@ -244,7 +245,7 @@ func loadBackup(cache lcw.LoadingCache) error {
}

for k := range lc.Summaries {
_, _ = cache.Get(k, func() (interface{}, error) {
_, _ = cache.Get(k, func() (summaryItem, error) {
return lc.Summaries[k], nil
})
}
Expand All @@ -253,7 +254,7 @@ func loadBackup(cache lcw.LoadingCache) error {
}

// saveBackup saves cache to local file in debug mode
func saveBackup(cache lcw.LoadingCache) error {
func saveBackup(cache lcw.LoadingCache[summaryItem]) error {
type LocalCache struct {
Summaries map[string]summaryItem `json:"Summaries"`
}
Expand All @@ -263,12 +264,12 @@ func saveBackup(cache lcw.LoadingCache) error {
}

for _, k := range cache.Keys() {
v, _ := cache.Get(k, func() (interface{}, error) {
return nil, nil
v, _ := cache.Get(k, func() (summaryItem, error) {
return summaryItem{}, nil
})

if v != nil {
lc.Summaries[k] = v.(summaryItem)
if v.isEmpty() {
lc.Summaries[k] = v
}
}

Expand Down
29 changes: 19 additions & 10 deletions app/bot/openai/summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"
"time"

"github.com/go-pkgz/lcw"
"github.com/go-pkgz/lcw/v2"
"github.com/radio-t/super-bot/app/bot/openai/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -43,7 +43,8 @@ func TestSummarizer_GetSummariesByMessage(t *testing.T) {
},
}

cache, err := lcw.NewExpirableCache(lcw.MaxKeys(10), lcw.TTL(time.Hour))
o := lcw.NewOpts[summaryItem]()
cache, err := lcw.NewExpirableCache(o.MaxKeys(10), o.TTL(time.Hour))
assert.NoError(t, err)
s := Summarizer{
openAISummary: os,
Expand Down Expand Up @@ -86,7 +87,8 @@ func TestSummarizer_GetSummariesByMessageRemark(t *testing.T) {
},
}

cache, err := lcw.NewExpirableCache(lcw.MaxKeys(10), lcw.TTL(time.Hour))
o := lcw.NewOpts[summaryItem]()
cache, err := lcw.NewExpirableCache(o.MaxKeys(10), o.TTL(time.Hour))
assert.NoError(t, err)
s := Summarizer{
openAISummary: os,
Expand Down Expand Up @@ -128,7 +130,8 @@ func TestSummarizer_GetSummariesByMessage_ErrorNoLink(t *testing.T) {
},
}

cache, err := lcw.NewExpirableCache(lcw.MaxKeys(10), lcw.TTL(time.Hour))
o := lcw.NewOpts[summaryItem]()
cache, err := lcw.NewExpirableCache(o.MaxKeys(10), o.TTL(time.Hour))
assert.NoError(t, err)
s := Summarizer{
openAISummary: os,
Expand Down Expand Up @@ -166,7 +169,8 @@ func TestSummarizer_GetSummariesByMessage_ErrorSummary(t *testing.T) {
},
}

cache, err := lcw.NewExpirableCache(lcw.MaxKeys(10), lcw.TTL(time.Hour))
o := lcw.NewOpts[summaryItem]()
cache, err := lcw.NewExpirableCache(o.MaxKeys(10), o.TTL(time.Hour))
assert.NoError(t, err)
s := Summarizer{
openAISummary: os,
Expand Down Expand Up @@ -204,7 +208,8 @@ func TestSummarizer_GetSummariesByMessage_ErrorBadRadiotLink(t *testing.T) {
},
}

cache, err := lcw.NewExpirableCache(lcw.MaxKeys(10), lcw.TTL(time.Hour))
o := lcw.NewOpts[summaryItem]()
cache, err := lcw.NewExpirableCache(o.MaxKeys(10), o.TTL(time.Hour))
assert.NoError(t, err)
s := Summarizer{
openAISummary: os,
Expand Down Expand Up @@ -242,7 +247,8 @@ func TestSummarizer_GetSummariesByMessage_ErrorGetComments(t *testing.T) {
},
}

cache, err := lcw.NewExpirableCache(lcw.MaxKeys(10), lcw.TTL(time.Hour))
o := lcw.NewOpts[summaryItem]()
cache, err := lcw.NewExpirableCache(o.MaxKeys(10), o.TTL(time.Hour))
assert.NoError(t, err)
s := Summarizer{
openAISummary: os,
Expand Down Expand Up @@ -280,7 +286,8 @@ func TestSummarizer_GetSummariesByMessage_ErrorGetContent(t *testing.T) {
},
}

cache, err := lcw.NewExpirableCache(lcw.MaxKeys(10), lcw.TTL(time.Hour))
o := lcw.NewOpts[summaryItem]()
cache, err := lcw.NewExpirableCache(o.MaxKeys(10), o.TTL(time.Hour))
assert.NoError(t, err)
s := Summarizer{
openAISummary: os,
Expand Down Expand Up @@ -320,7 +327,8 @@ func TestSummarizer_GetSummariesByRemarkLink(t *testing.T) {
},
}

cache, err := lcw.NewExpirableCache(lcw.MaxKeys(10), lcw.TTL(time.Hour))
o := lcw.NewOpts[summaryItem]()
cache, err := lcw.NewExpirableCache(o.MaxKeys(10), o.TTL(time.Hour))
assert.NoError(t, err)
s := Summarizer{
openAISummary: os,
Expand Down Expand Up @@ -379,7 +387,8 @@ func TestSummarizer_Summary(t *testing.T) {
},
}

cache, err := lcw.NewExpirableCache(lcw.MaxKeys(10), lcw.TTL(time.Hour))
o := lcw.NewOpts[summaryItem]()
cache, err := lcw.NewExpirableCache(o.MaxKeys(10), o.TTL(time.Hour))
assert.NoError(t, err)
s := Summarizer{
openAISummary: os,
Expand Down
40 changes: 20 additions & 20 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,42 @@ module github.com/radio-t/super-bot
go 1.21

require (
github.com/go-pkgz/lcw v1.0.2
github.com/go-pkgz/lcw/v2 v2.0.0
github.com/go-pkgz/lgr v0.11.1
github.com/go-pkgz/notify v1.0.0
github.com/go-pkgz/notify v1.1.0
github.com/go-pkgz/repeater v1.1.3
github.com/go-pkgz/requester v0.1.0
github.com/go-pkgz/requester v0.2.0
github.com/go-pkgz/syncs v1.3.2
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
github.com/jessevdk/go-flags v1.5.0
github.com/sandwich-go/gpt3-encoder v0.0.0-20230203030618-cd99729dd0dd
github.com/sashabaranov/go-openai v1.16.0
github.com/stretchr/testify v1.8.4
golang.org/x/text v0.13.0
golang.org/x/time v0.3.0
github.com/sashabaranov/go-openai v1.23.0
github.com/stretchr/testify v1.9.0
golang.org/x/text v0.14.0
golang.org/x/time v0.5.0
)

require (
github.com/aymerick/douceur v0.2.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dlclark/regexp2 v1.10.0 // indirect
github.com/go-pkgz/email v0.4.1 // indirect
github.com/go-redis/redis/v8 v8.11.5 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/dlclark/regexp2 v1.7.0 // indirect
github.com/go-pkgz/email v0.5.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/golang-lru v1.0.2 // indirect
github.com/microcosm-cc/bluemonday v1.0.26 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/microcosm-cc/bluemonday v1.0.25 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/samber/lo v1.38.1 // indirect
github.com/slack-go/slack v0.12.3 // indirect
github.com/stretchr/objx v0.5.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
github.com/redis/go-redis/v9 v9.4.0 // indirect
github.com/samber/lo v1.37.0 // indirect
github.com/slack-go/slack v0.12.2 // indirect
github.com/stretchr/objx v0.5.2 // indirect
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.11.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 2e4d972

Please sign in to comment.