Skip to content

Commit

Permalink
remove fibertype omitempty and replace srs algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
TomRomeo committed Feb 29, 2024
1 parent c11b671 commit dfa2542
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 41 deletions.
2 changes: 1 addition & 1 deletion backend/pkg/converter/fiberSerializerTypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type FiberGetGroupDecksResponseBody struct {
type FiberGetDueResponseBody struct {
DueCards int64 `json:"dueCards"`
NewCards int64 `json:"newCards"`
DueDecks int64 `json:"dueDecks,omitempty"`
DueDecks int64 `json:"dueDecks"`
}

type FiberGetUserGroupsResponseBody struct {
Expand Down
43 changes: 24 additions & 19 deletions backend/services/linearSrs/handler/linear-srs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handler
import (
"context"
"math"
"math/rand"
"sort"
"time"

Expand Down Expand Up @@ -74,38 +75,42 @@ func (e *LinearSrs) Push(ctx context.Context, req *pbCommon.SrsPushRequest, rsp

func (e *LinearSrs) Pull(ctx context.Context, req *pbCommon.DeckRequest, rsp *pbCommon.Card) error {
logger.Infof("Received LinearSrs.Pull request: %v", req)
if _, err := e.cardDeckService.AddUserActiveDeck(ctx, &pbCommon.DeckRequest{UserID: req.UserID, Deck: &pbCommon.Deck{DeckID: req.Deck.DeckID}}); err != nil {
dueCards, err := e.store.FindUserDeckDueCards(ctx, req.UserID, req.Deck.DeckID)
if err != nil {
return err
}
cards, err := e.store.FindUserDeckCards(
ctx, req.UserID,
req.Deck.DeckID,
)
// TODO: Implement carddeck service call here
targetNewCards := int64(5)

// get new cards learned today
currentNewCards, err := e.store.FindUserDeckNewCardsLearnedToday(ctx, req.UserID, req.Deck.DeckID)
if err != nil {
return err
}
var dueCards []*model.UserCardBinding
for _, card := range cards {
if card.Due <= time.Now().Unix() {
dueCards = append(dueCards, card)
}
newCards, err := e.store.FindUserDeckNewCards(ctx, req.UserID, req.Deck.DeckID)
if err != nil {
return err
}
// determine smartest card to return
// sort by oldest first
sort.Slice(dueCards, func(i, j int) bool {
return dueCards[i].Due < dueCards[j].Due
})
// if no more cards are due, return empty card
if len(dueCards) == 0 {
var returnedCard *pbCommon.Card
if len(dueCards) >= 5 || ((currentNewCards >= targetNewCards || len(newCards) == 0) && len(dueCards) > 0) {
// determine smartest card to return
// sort by oldest first
now := time.Now().Unix()
sort.Slice(dueCards, func(i, j int) bool {
return (math.Abs(float64(dueCards[i].Due - now))) > math.Abs(float64((dueCards[j].Due - now)))
})
returnedCard = &pbCommon.Card{CardID: dueCards[0].CardID}
} else if currentNewCards < targetNewCards && len(newCards) > 0 {
rand.Shuffle(len(newCards), func(i, j int) { newCards[i], newCards[j] = newCards[j], newCards[i] })
returnedCard = &pbCommon.Card{CardID: newCards[0].CardID}
} else {
*rsp = pbCommon.Card{
CardID: "",
Sides: nil,
}
return nil
}

returnedCard := &pbCommon.Card{CardID: dueCards[0].CardID}

// get content of card
cardWithContent, err := e.cardDeckService.GetCard(ctx, &pbCommon.CardRequest{
UserID: req.UserID,
Expand Down
2 changes: 1 addition & 1 deletion backend/services/srs/handler/srs.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (e *Srs) Pull(ctx context.Context, req *pbCommon.DeckRequest, rsp *pbCommon
return err
}
var returnedCard *pbCommon.Card
if len(dueCards) >= 5 || ((currentNewCards >= targetNewCards || len(newCards) == 0) && len(dueCards) == 0) {
if len(dueCards) >= 5 || ((currentNewCards >= targetNewCards || len(newCards) == 0) && len(dueCards) > 0) {
// determine smartest card to return
// sort by oldest first
now := time.Now().Unix()
Expand Down
44 changes: 25 additions & 19 deletions backend/services/staticSrs/handler/static-srs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package handler

import (
"context"
"math"
"math/rand"
"sort"
"time"

Expand Down Expand Up @@ -70,38 +72,42 @@ func (e *StaticSrs) Push(ctx context.Context, req *pbCommon.SrsPushRequest, rsp

func (e *StaticSrs) Pull(ctx context.Context, req *pbCommon.DeckRequest, rsp *pbCommon.Card) error {
logger.Infof("Received StaticSrs.Pull request: %v", req)
if _, err := e.cardDeckService.AddUserActiveDeck(ctx, &pbCommon.DeckRequest{UserID: req.UserID, Deck: &pbCommon.Deck{DeckID: req.Deck.DeckID}}); err != nil {
dueCards, err := e.store.FindUserDeckDueCards(ctx, req.UserID, req.Deck.DeckID)
if err != nil {
return err
}
cards, err := e.store.FindUserDeckCards(
ctx, req.UserID,
req.Deck.DeckID,
)
// TODO: Implement carddeck service call here
targetNewCards := int64(5)

// get new cards learned today
currentNewCards, err := e.store.FindUserDeckNewCardsLearnedToday(ctx, req.UserID, req.Deck.DeckID)
if err != nil {
return err
}
var dueCards []*model.UserCardBinding
for _, card := range cards {
if card.Due <= time.Now().Unix() {
dueCards = append(dueCards, card)
}
newCards, err := e.store.FindUserDeckNewCards(ctx, req.UserID, req.Deck.DeckID)
if err != nil {
return err
}
// determine smartest card to return
// sort by oldest first
sort.Slice(dueCards, func(i, j int) bool {
return dueCards[i].Due < dueCards[j].Due
})
// if no more cards are due, return empty card
if len(dueCards) == 0 {
var returnedCard *pbCommon.Card
if len(dueCards) >= 5 || ((currentNewCards >= targetNewCards || len(newCards) == 0) && len(dueCards) > 0) {
// determine smartest card to return
// sort by oldest first
now := time.Now().Unix()
sort.Slice(dueCards, func(i, j int) bool {
return (math.Abs(float64(dueCards[i].Due - now))) > math.Abs(float64((dueCards[j].Due - now)))
})
returnedCard = &pbCommon.Card{CardID: dueCards[0].CardID}
} else if currentNewCards < targetNewCards && len(newCards) > 0 {
rand.Shuffle(len(newCards), func(i, j int) { newCards[i], newCards[j] = newCards[j], newCards[i] })
returnedCard = &pbCommon.Card{CardID: newCards[0].CardID}
} else {
*rsp = pbCommon.Card{
CardID: "",
Sides: nil,
}
return nil
}

returnedCard := &pbCommon.Card{CardID: dueCards[0].CardID}

// get content of card
cardWithContent, err := e.cardDeckService.GetCard(ctx, &pbCommon.CardRequest{
UserID: req.UserID,
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/deck/Deck.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export const Deck = ({
</div>
</div>
<div className="flex flex-row space-x-2 overflow-hidden sm:space-x-3 md:space-x-3 lg:space-x-3">
{(deck.due?.dueCards || deck.due?.newCards) && (
{(!!deck.due?.dueCards || !!deck.due?.newCards) && (
<IconLabel
iconLabel={{
icon: "Activity",
Expand Down

0 comments on commit dfa2542

Please sign in to comment.