From 7b056a4a41247ff68f9e42ea6990da7fcdfec5b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Hells=C3=A9n?= Date: Sat, 17 Feb 2024 04:25:39 +0100 Subject: [PATCH] Get DB size from Postgres --- db/stats.sql.go | 21 +++++++++++++++++ handlers.go | 12 +++++++--- sql/queries/stats.sql | 2 ++ sqlc.yaml | 2 -- stats.go | 53 ++++++++++++------------------------------- 5 files changed, 46 insertions(+), 44 deletions(-) create mode 100644 db/stats.sql.go create mode 100644 sql/queries/stats.sql diff --git a/db/stats.sql.go b/db/stats.sql.go new file mode 100644 index 0000000..70dbed8 --- /dev/null +++ b/db/stats.sql.go @@ -0,0 +1,21 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.25.0 +// source: stats.sql + +package db + +import ( + "context" +) + +const dBSize = `-- name: DBSize :one +SELECT pg_size_pretty(pg_database_size(current_database())) +` + +func (q *Queries) DBSize(ctx context.Context) (string, error) { + row := q.db.QueryRow(ctx, dBSize) + var pg_size_pretty string + err := row.Scan(&pg_size_pretty) + return pg_size_pretty, err +} diff --git a/handlers.go b/handlers.go index 36e01f4..5834093 100644 --- a/handlers.go +++ b/handlers.go @@ -31,7 +31,7 @@ func FeedsHandler(w http.ResponseWriter, _ *http.Request) { Keywords: "RSS, Atom, Feed, Archive", Author: "TheLovinator", CanonicalURL: "http://localhost:8000/feeds", - Content: "

Here be

", + Content: "

No feeds yet.

", } html := FullHTML(htmlData) _, err := w.Write([]byte(html)) @@ -163,7 +163,10 @@ func IndexHandler(w http.ResponseWriter, _ *http.Request) { Content: content, } html := FullHTML(htmlData) - w.Write([]byte(html)) + _, err := w.Write([]byte(html)) + if err != nil { + log.Println("Error writing response:", err) + } } func UploadOpmlHandler(w http.ResponseWriter, r *http.Request) { @@ -218,5 +221,8 @@ func UploadOpmlHandler(w http.ResponseWriter, r *http.Request) { ParseResult: parseResult, } html := FullHTML(htmlData) - w.Write([]byte(html)) + _, err = w.Write([]byte(html)) + if err != nil { + log.Println("Error writing response:", err) + } } diff --git a/sql/queries/stats.sql b/sql/queries/stats.sql new file mode 100644 index 0000000..5ceca21 --- /dev/null +++ b/sql/queries/stats.sql @@ -0,0 +1,2 @@ +-- name: DBSize :one +SELECT pg_size_pretty(pg_database_size(current_database())); diff --git a/sqlc.yaml b/sqlc.yaml index 2f7727e..9170817 100644 --- a/sqlc.yaml +++ b/sqlc.yaml @@ -8,7 +8,5 @@ sql: out: "db" sql_package: "pgx/v5" emit_prepared_queries: true - emit_interface: true emit_empty_slices: true - emit_exported_queries: true emit_json_tags: true diff --git a/stats.go b/stats.go index ce06310..2d311f7 100644 --- a/stats.go +++ b/stats.go @@ -1,8 +1,7 @@ package main import ( - "fmt" - "os" + "context" "time" ) @@ -14,46 +13,22 @@ type Cache struct { var cache Cache func GetDBSize() (string, error) { - // If cache is less than 10 minutes old, return cached data - if time.Since(cache.timestamp).Minutes() < 10 { - return cache.data, nil + // If the cache is older than 5 minutes, get the database size from the database + if time.Since(cache.timestamp) > 5*time.Minute { + dbSize, err := getDBSizeFromDB() + if err != nil { + return "", err + } + cache = Cache{timestamp: time.Now(), data: dbSize} } + return cache.data, nil +} - // TODO: This should be read from the environment - fileInfo, err := os.Stat("feedvault.db") +func getDBSizeFromDB() (string, error) { + // Get database size from the PostgreSQL database + dbSize, err := DB.DBSize(context.Background()) if err != nil { return "", err } - - // Get the file size in bytes - fileSize := fileInfo.Size() - - // Convert to human readable size and append the unit (KiB, MiB, GiB, TiB) - var size float64 - - if fileSize < 1024*1024 { - // If the file size is less than 1 KiB, return the size in bytes - size = float64(fileSize) / 1024 - cache.data = fmt.Sprintf("%.2f KiB", size) - - } else if fileSize < 1024*1024*1024 { - // If the file size is less than 1 MiB, return the size in KiB - size = float64(fileSize) / (1024 * 1024) - cache.data = fmt.Sprintf("%.2f MiB", size) - - } else if fileSize < 1024*1024*1024*1024 { - // If the file size is less than 1 GiB, return the size in MiB - size = float64(fileSize) / (1024 * 1024 * 1024) - cache.data = fmt.Sprintf("%.2f GiB", size) - } else { - // If the file size is 1 GiB or more, return the size in TiB - size = float64(fileSize) / (1024 * 1024 * 1024 * 1024) - cache.data = fmt.Sprintf("%.2f TiB", size) - } - - // Update cache timestamp - cache.timestamp = time.Now() - - // Return the human readable size - return cache.data, nil + return dbSize, nil }