Skip to content

Commit

Permalink
Rewrite: delete obsolete skills
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikKalkoken committed Sep 26, 2024
1 parent 9c28855 commit 4a955d9
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 38 deletions.
2 changes: 1 addition & 1 deletion FyneApp.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Website = "https://github.com/ErikKalkoken/evebuddy"
Icon = "icon.png"
Name = "EVE Buddy"
ID = "io.github.erikkalkoken.evebuddy"
Version = "0.2.2"
Version = "0.2.3"
Build = 2

[Release]
Expand Down
20 changes: 11 additions & 9 deletions internal/app/character/characterasset.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ func (s *CharacterService) updateCharacterAssetsESI(ctx context.Context, arg Upd
},
func(ctx context.Context, characterID int32, data any) error {
assets := data.([]esiCharacterAssetPlus)
itemIDs := set.New[int64]()
incomingIDs := set.New[int64]()
for _, ca := range assets {
itemIDs.Add(ca.ItemId)
incomingIDs.Add(ca.ItemId)
}
typeIDs := set.New[int32]()
locationIDs := set.New[int64]()
for _, ca := range assets {
typeIDs.Add(ca.TypeId)
if !itemIDs.Contains(ca.LocationId) {
if !incomingIDs.Contains(ca.LocationId) {
locationIDs.Add(ca.LocationId) // location IDs that are not referencing other itemIDs are locations
}
}
Expand All @@ -107,13 +107,13 @@ func (s *CharacterService) updateCharacterAssetsESI(ctx context.Context, arg Upd
if err := s.EveUniverseService.AddMissingEveTypes(ctx, typeIDs.ToSlice()); err != nil {
return err
}
ids, err := s.st.ListCharacterAssetIDs(ctx, characterID)
x, err := s.st.ListCharacterAssetIDs(ctx, characterID)
if err != nil {
return err
}
existingIDs := set.NewFromSlice(ids)
currentIDs := set.NewFromSlice(x)
for _, a := range assets {
if existingIDs.Contains(a.ItemId) {
if currentIDs.Contains(a.ItemId) {
arg := storage.UpdateCharacterAssetParams{
CharacterID: characterID,
ItemID: a.ItemId,
Expand Down Expand Up @@ -143,9 +143,11 @@ func (s *CharacterService) updateCharacterAssetsESI(ctx context.Context, arg Upd
return err
}
}
obsoleteIDs := existingIDs.Difference(itemIDs)
if err := s.st.DeleteCharacterAssets(ctx, characterID, obsoleteIDs.ToSlice()); err != nil {
return err
obsoleteIDs := currentIDs.Difference(incomingIDs)
if obsoleteIDs.Size() > 0 {
if err := s.st.DeleteCharacterAssets(ctx, characterID, obsoleteIDs.ToSlice()); err != nil {
return err
}
}
}
return nil
Expand Down
1 change: 1 addition & 0 deletions internal/app/character/charactermailupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func (s *CharacterService) updateCharacterMailsESI(ctx context.Context, arg Upda
return err
}
}
// TODO: Delete obsolete mail labels and list
// if err := s.st.DeleteObsoleteCharacterMailLabels(ctx, characterID); err != nil {
// return err
// }
Expand Down
17 changes: 13 additions & 4 deletions internal/app/character/characterskills.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/ErikKalkoken/evebuddy/internal/app"
"github.com/ErikKalkoken/evebuddy/internal/app/storage"
"github.com/ErikKalkoken/evebuddy/internal/optional"
"github.com/ErikKalkoken/evebuddy/internal/set"
"github.com/antihax/goesi/esi"
)

Expand Down Expand Up @@ -38,9 +39,14 @@ func (s *CharacterService) updateCharacterSkillsESI(ctx context.Context, arg Upd
if err := s.st.UpdateCharacterSkillPoints(ctx, characterID, total, unallocated); err != nil {
return err
}
var existingSkills []int32
x, err := s.st.ListCharacterSkillIDs(ctx, characterID)
if err != nil {
return err
}
currentSkillIDs := set.NewFromSlice(x)
incomingSkillIDs := set.New[int32]()
for _, o := range skills.Skills {
existingSkills = append(existingSkills, o.SkillId)
incomingSkillIDs.Add(o.SkillId)
_, err := s.EveUniverseService.GetOrCreateEveTypeESI(ctx, o.SkillId)
if err != nil {
return err
Expand All @@ -57,8 +63,11 @@ func (s *CharacterService) updateCharacterSkillsESI(ctx context.Context, arg Upd
return err
}
}
if err := s.st.DeleteExcludedCharacterSkills(ctx, characterID, existingSkills); err != nil {
return err
obsoleteSkillIDs := currentSkillIDs.Difference(incomingSkillIDs)
if obsoleteSkillIDs.Size() > 0 {
if err := s.st.DeleteCharacterSkills(ctx, characterID, obsoleteSkillIDs.ToSlice()); err != nil {
return err
}
}
return nil
})
Expand Down
17 changes: 12 additions & 5 deletions internal/app/character/characterskills_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,16 @@ func TestUpdateCharacterSkillsESI(t *testing.T) {
httpmock.Reset()
c := factory.CreateCharacter()
factory.CreateCharacterToken(app.CharacterToken{CharacterID: c.ID})
// old := factory.CreateCharacterSkill(storage.UpdateOrCreateCharacterSkillParams{CharacterID: c.ID})
factory.CreateEveType(storage.CreateEveTypeParams{ID: 41})
factory.CreateEveType(storage.CreateEveTypeParams{ID: 42})
factory.CreateCharacterSkill(storage.UpdateOrCreateCharacterSkillParams{
CharacterID: c.ID,
EveTypeID: 41,
})
factory.CreateCharacterSkill(storage.UpdateOrCreateCharacterSkillParams{
CharacterID: c.ID,
EveTypeID: 42,
})
data := map[string]any{
"skills": []map[string]any{
{
Expand All @@ -109,10 +116,10 @@ func TestUpdateCharacterSkillsESI(t *testing.T) {
// then
if assert.NoError(t, err) {
assert.True(t, changed)
_, err := st.GetCharacterSkill(ctx, c.ID, 42)
assert.Error(t, err, storage.ErrNotFound)
_, err = st.GetCharacterSkill(ctx, c.ID, 41)
assert.NoError(t, err)
ids, err := st.ListCharacterSkillIDs(ctx, c.ID)
if assert.NoError(t, err) {
assert.ElementsMatch(t, []int32{41}, ids)
}
}
})

Expand Down
14 changes: 11 additions & 3 deletions internal/app/storage/characterskill.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
"github.com/ErikKalkoken/evebuddy/internal/app/storage/queries"
)

func (st *Storage) DeleteExcludedCharacterSkills(ctx context.Context, characterID int32, eveTypeIDs []int32) error {
arg := queries.DeleteExcludedCharacterSkillsParams{
func (st *Storage) DeleteCharacterSkills(ctx context.Context, characterID int32, eveTypeIDs []int32) error {
arg := queries.DeleteCharacterSkillsParams{
CharacterID: int64(characterID),
EveTypeIds: convertNumericSlice[int32, int64](eveTypeIDs),
}
err := st.q.DeleteExcludedCharacterSkills(ctx, arg)
err := st.q.DeleteCharacterSkills(ctx, arg)
if err != nil {
return err
}
Expand All @@ -38,6 +38,14 @@ func (st *Storage) GetCharacterSkill(ctx context.Context, characterID int32, typ
return t2, nil
}

func (st *Storage) ListCharacterSkillIDs(ctx context.Context, characterID int32) ([]int32, error) {
ids, err := st.q.ListCharacterSkillIDs(ctx, int64(characterID))
if err != nil {
return nil, fmt.Errorf("failed to list skill IDs for character %d: %w", characterID, err)
}
return convertNumericSlice[int64, int32](ids), nil
}

func (st *Storage) ListCharacterSkillProgress(ctx context.Context, characterID, eveGroupID int32) ([]app.ListCharacterSkillProgress, error) {
arg := queries.ListCharacterSkillProgressParams{
CharacterID: int64(characterID),
Expand Down
31 changes: 24 additions & 7 deletions internal/app/storage/characterskill_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,37 @@ func TestCharacterSkill(t *testing.T) {
}
}
})
t.Run("can list skill IDs", func(t *testing.T) {
// given
testutil.TruncateTables(db)
c := factory.CreateCharacter()
o1 := factory.CreateCharacterSkill(storage.UpdateOrCreateCharacterSkillParams{
CharacterID: c.ID,
})
o2 := factory.CreateCharacterSkill(storage.UpdateOrCreateCharacterSkillParams{
CharacterID: c.ID,
})
// when
ids, err := r.ListCharacterSkillIDs(ctx, c.ID)
// then
if assert.NoError(t, err) {
assert.ElementsMatch(t, []int32{o1.EveType.ID, o2.EveType.ID}, ids)
}
})
t.Run("can delete excluded skills", func(t *testing.T) {
// given
testutil.TruncateTables(db)
c := factory.CreateCharacter()
o1 := factory.CreateCharacterSkill(storage.UpdateOrCreateCharacterSkillParams{CharacterID: c.ID})
o2 := factory.CreateCharacterSkill(storage.UpdateOrCreateCharacterSkillParams{CharacterID: c.ID})
x1 := factory.CreateCharacterSkill(storage.UpdateOrCreateCharacterSkillParams{CharacterID: c.ID})
x2 := factory.CreateCharacterSkill(storage.UpdateOrCreateCharacterSkillParams{CharacterID: c.ID})
// when
err := r.DeleteExcludedCharacterSkills(ctx, c.ID, []int32{o2.EveType.ID})
err := r.DeleteCharacterSkills(ctx, c.ID, []int32{x2.EveType.ID})
// then
if assert.NoError(t, err) {
_, err := r.GetCharacterSkill(ctx, c.ID, o1.EveType.ID)
assert.Error(t, err, storage.ErrNotFound)
_, err = r.GetCharacterSkill(ctx, c.ID, o2.EveType.ID)
assert.NoError(t, err)
ids, err := r.ListCharacterSkillIDs(ctx, c.ID)
if assert.NoError(t, err) {
assert.ElementsMatch(t, []int32{x1.EveType.ID}, ids)
}
}
})
}
Expand Down
9 changes: 7 additions & 2 deletions internal/app/storage/queries/character_skills.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ JOIN eve_categories ON eve_categories.id = eve_groups.eve_category_id
WHERE character_id = ?
AND eve_type_id = ?;

-- name: DeleteExcludedCharacterSkills :exec
-- name: DeleteCharacterSkills :exec
DELETE FROM character_skills
WHERE character_id = ?
AND eve_type_id NOT IN (sqlc.slice('eve_type_ids'));
AND eve_type_id IN (sqlc.slice('eve_type_ids'));

-- name: ListCharacterSkillIDs :many
SELECT eve_type_id
FROM character_skills
WHERE character_id = ?;

-- name: ListCharacterShipsAbilities :many
SELECT DISTINCT ss2.ship_type_id as type_id, et.name as type_name, eg.id as group_id, eg.name as group_name,
Expand Down
39 changes: 34 additions & 5 deletions internal/app/storage/queries/character_skills.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import (
"log"
"log/slog"
"net/http"
_ "net/http/pprof"
"os"
"path/filepath"
"runtime/debug"
"strings"
"time"

"fyne.io/fyne/v2/app"
"github.com/antihax/goesi"
"github.com/juju/mutex/v2"
"gopkg.in/natefinch/lumberjack.v2"

"github.com/ErikKalkoken/evebuddy/internal/app/character"
Expand All @@ -30,8 +33,6 @@ import (
"github.com/ErikKalkoken/evebuddy/internal/httptransport"
"github.com/ErikKalkoken/evebuddy/internal/sso"
"github.com/ErikKalkoken/evebuddy/internal/uninstall"
"github.com/antihax/goesi"
"github.com/juju/mutex/v2"
)

const (
Expand Down Expand Up @@ -69,6 +70,7 @@ var (
levelFlag logLevelFlag
debugFlag = flag.Bool("debug", false, "Show additional debug information")
uninstallFlag = flag.Bool("uninstall", false, "Uninstalls the app by deleting all user files")
pprofFlag = flag.Bool("pprof", false, "Enable pprof web server")
)

func init() {
Expand Down Expand Up @@ -198,6 +200,13 @@ func main() {
u.StatusCacheService = sc
u.Init()

// start pprof web server
if *pprofFlag {
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
}

// Start app
u.ShowAndRun()
}

0 comments on commit 4a955d9

Please sign in to comment.