Skip to content

Commit

Permalink
actually drop old cache metrics via timestamp.
Browse files Browse the repository at this point in the history
  • Loading branch information
featheredtoast committed Nov 11, 2024
1 parent b8c35a6 commit 56a6682
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
12 changes: 2 additions & 10 deletions frmcache/src/app/cache_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,11 @@ func (c *CacheWorker) cacheMetricsWithHistory(metric string, data []string) (err
}
}

//720 = 1 hour, 5 second increments. retain that many rows for every data.
keep := 720 * len(data)

delete := `delete from cache_with_history where
metric = $1 AND
url = $2 AND session_name = $3 AND
id NOT IN (
select id from "cache_with_history" where metric = $1
AND url = $2 AND session_name = $3
order by id desc
limit $4
);`
_, err = tx.Exec(delete, metric, c.frmBaseUrl, c.sessionName, keep)
time < $4;`
_, err = tx.Exec(delete, metric, c.frmBaseUrl, c.sessionName, c.now.Add(time.Duration(-1) * time.Hour))
return
}

Expand Down
3 changes: 2 additions & 1 deletion frmcache/src/app/cache_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"
)

func setupServer(expectedResponse string) *httptest.Server {
Expand Down Expand Up @@ -51,7 +52,7 @@ func TestCacheHistory(t *testing.T) {
mock.ExpectBegin()
mock.ExpectExec(`^insert into cache_with_history (.+)`).WithArgs("drone", `{"ID":"0","VehicleType":"Drone"}`, now, frm.URL, sessionName).WillReturnResult(sql.NewResult(1, 1))
mock.ExpectExec(`^insert into cache_with_history (.+)`).WithArgs("drone", `{"ID":"1","VehicleType":"Drone"}`, now, frm.URL, sessionName).WillReturnResult(sql.NewResult(1, 1))
mock.ExpectExec(`^delete from cache_with_history (.+)`).WithArgs("drone", frm.URL, sessionName, 720*2).WillReturnResult(sql.NewResult(1, 1))
mock.ExpectExec(`^delete from cache_with_history (.+)`).WithArgs("drone", frm.URL, sessionName, now.Add(time.Duration(-1)*time.Hour)).WillReturnResult(sql.NewResult(1, 1))
mock.ExpectCommit()

c := NewCacheWorker(frm.URL, db)
Expand Down
12 changes: 12 additions & 0 deletions frmcache/src/db/007_add_cache_history_metric_index.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Write your migrate up statements here

CREATE INDEX cache_with_history_metric ON cache_with_history(metric);
CREATE INDEX cache_with_history_time ON cache_with_history(time);

---- create above / drop below ----

-- Write your migrate down statements here. If this migration is irreversible
-- Then delete the separator line above.

DROP INDEX if exists cache_with_history_metric;
DROP INDEX if exists cache_with_history_time;

0 comments on commit 56a6682

Please sign in to comment.