Skip to content

Commit

Permalink
[price] add option to clear cache
Browse files Browse the repository at this point in the history
  • Loading branch information
ananthakumaran committed Dec 8, 2023
1 parent 251b6b8 commit 5fa6292
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 12 deletions.
14 changes: 14 additions & 0 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cache

import (
"github.com/ananthakumaran/paisa/internal/accounting"
"github.com/ananthakumaran/paisa/internal/prediction"
"github.com/ananthakumaran/paisa/internal/service"
)

func Clear() {
service.ClearInterestCache()
service.ClearPriceCache()
accounting.ClearCache()
prediction.ClearCache()
}
2 changes: 1 addition & 1 deletion internal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func SyncCommodities(db *gorm.DB) error {
return fmt.Errorf("Failed to fetch price for %s: %w", name, err)
}

price.UpsertAllByTypeAndID(db, commodity.Type, code, prices)
price.UpsertAllByTypeNameAndID(db, commodity.Type, name, code, prices)
}
return nil
}
Expand Down
13 changes: 11 additions & 2 deletions internal/model/price/price.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,21 @@ func (p Price) Less(o btree.Item) bool {
return p.Date.Before(o.(Price).Date)
}

func UpsertAllByTypeAndID(db *gorm.DB, commodityType config.CommodityType, commodityID string, prices []*Price) {
func DeleteAll(db *gorm.DB) error {
err := db.Exec("DELETE FROM prices").Error
if err != nil {
return err
}
return nil
}

func UpsertAllByTypeNameAndID(db *gorm.DB, commodityType config.CommodityType, commodityName string, commodityID string, prices []*Price) {
err := db.Transaction(func(tx *gorm.DB) error {
err := tx.Delete(&Price{}, "commodity_type = ? and commodity_id = ?", commodityType, commodityID).Error
err := tx.Delete(&Price{}, "commodity_type = ? and (commodity_id = ? or commodity_name = ?)", commodityType, commodityID, commodityName).Error
if err != nil {
return err
}

for _, price := range prices {
err := tx.Create(price).Error
if err != nil {
Expand Down
18 changes: 18 additions & 0 deletions internal/server/price.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"github.com/samber/lo"
log "github.com/sirupsen/logrus"

"github.com/ananthakumaran/paisa/internal/cache"
"github.com/ananthakumaran/paisa/internal/config"
"github.com/ananthakumaran/paisa/internal/model"
"github.com/ananthakumaran/paisa/internal/model/posting"
"github.com/ananthakumaran/paisa/internal/model/price"
"github.com/ananthakumaran/paisa/internal/scraper"
Expand Down Expand Up @@ -50,6 +52,22 @@ func GetPriceProviders(db *gorm.DB) gin.H {

}

func ClearPriceCache(db *gorm.DB) gin.H {
err := price.DeleteAll(db)
if err != nil {
return gin.H{"success": false, "message": err.Error()}
}

cache.Clear()

message, err := model.SyncJournal(db)
if err != nil {
return gin.H{"success": false, "message": message}
}

return gin.H{"success": true}
}

func ClearPriceProviderCache(db *gorm.DB, code string) gin.H {
provider := scraper.GetProviderByCode(code)
provider.ClearCache(db)
Expand Down
8 changes: 8 additions & 0 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ func Build(db *gorm.DB, enableCompression bool) *gin.Engine {
router.GET("/api/ledger", func(c *gin.Context) {
c.JSON(200, GetLedger(db))
})
router.POST("/api/price/delete", func(c *gin.Context) {
if config.GetConfig().Readonly {
c.JSON(200, gin.H{"success": true})
return
}

c.JSON(200, ClearPriceCache(db))
})
router.GET("/api/price", func(c *gin.Context) {
c.JSON(200, GetPrices(db))
})
Expand Down
9 changes: 2 additions & 7 deletions internal/server/sync.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package server

import (
"github.com/ananthakumaran/paisa/internal/accounting"
"github.com/ananthakumaran/paisa/internal/cache"
"github.com/ananthakumaran/paisa/internal/model"
"github.com/ananthakumaran/paisa/internal/prediction"
"github.com/ananthakumaran/paisa/internal/service"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
Expand All @@ -16,10 +14,7 @@ type SyncRequest struct {
}

func Sync(db *gorm.DB, request SyncRequest) gin.H {
service.ClearInterestCache()
service.ClearPriceCache()
accounting.ClearCache()
prediction.ClearCache()
cache.Clear()

if request.Journal {
message, err := model.SyncJournal(db)
Expand Down
5 changes: 5 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,11 @@ export function ajax(
options?: RequestInit
): Promise<{ file: LedgerFile }>;

export function ajax(
route: "/api/price/delete",
options?: RequestInit
): Promise<{ success: boolean; message: string }>;

export function ajax(
route: "/api/sync",
options?: RequestInit
Expand Down
43 changes: 41 additions & 2 deletions src/routes/ledger/price/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import Toggleable from "$lib/components/Toggleable.svelte";
import ValueChange from "$lib/components/ValueChange.svelte";
import { ajax, formatCurrency, type Price } from "$lib/utils";
import { toast } from "bulma-toast";
import _ from "lodash";
import { onMount } from "svelte";
import VirtualList from "svelte-tiny-virtual-list";
Expand All @@ -25,15 +26,53 @@
return null;
}
onMount(async () => {
async function clearPriceCache() {
const { success, message } = await ajax("/api/price/delete", { method: "POST" });
if (!success) {
toast({
message: `Failed to clear price cache. reason: ${message}`,
type: "is-danger",
duration: 10000
});
} else {
toast({
message: "Price cache cleared.",
type: "is-success"
});
}
await fetchPrice();
}
async function fetchPrice() {
({ prices: prices } = await ajax("/api/price"));
prices = _.omitBy(prices, (v) => v.length === 0);
}
onMount(async () => {
await fetchPrice();
});
</script>

<section class="section tab-price">
<div class="container is-fluid">
<div class="columns">
<div class="columns flex-wrap">
<div class="column is-12">
<div class="box p-3">
<div class="field has-addons mb-0">
<p class="control">
<button
class="button is-small is-link invertable is-light is-danger"
on:click={(_e) => clearPriceCache()}
>
<span class="icon is-small">
<i class="fas fa-trash" />
</span>
<span>Clear Price Cache</span>
</button>
</p>
</div>
</div>
</div>
<div class="column is-12">
<div class="box overflow-x-auto">
<table class="table is-narrow is-fullwidth is-light-border is-hoverable">
Expand Down

0 comments on commit 5fa6292

Please sign in to comment.