Compare commits
6 Commits
cache
...
background
| Author | SHA1 | Date | |
|---|---|---|---|
| cc103043b4 | |||
|
|
cabc153ee8 | ||
| 557a6f4ba8 | |||
| 813ced8bb0 | |||
| 4402ecb674 | |||
| 44f4c2df6a |
24
LICENSE
Normal file
24
LICENSE
Normal file
@@ -0,0 +1,24 @@
|
||||
This is free and unencumbered software released into the public domain.
|
||||
|
||||
Anyone is free to copy, modify, publish, use, compile, sell, or
|
||||
distribute this software, either in source code form or as a compiled
|
||||
binary, for any purpose, commercial or non-commercial, and by any
|
||||
means.
|
||||
|
||||
In jurisdictions that recognize copyright laws, the author or authors
|
||||
of this software dedicate any and all copyright interest in the
|
||||
software to the public domain. We make this dedication for the benefit
|
||||
of the public at large and to the detriment of our heirs and
|
||||
successors. We intend this dedication to be an overt act of
|
||||
relinquishment in perpetuity of all present and future rights to this
|
||||
software under copyright law.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
For more information, please refer to <https://unlicense.org>
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
################################################################################
|
||||
# Create a stage for building the application.
|
||||
ARG GO_VERSION=1.23.4
|
||||
ARG GO_VERSION=1.24.0
|
||||
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} AS build
|
||||
WORKDIR /src
|
||||
|
||||
|
||||
@@ -33,6 +33,8 @@ func main() {
|
||||
relay.Info.PubKey = getEnv("RELAY_PUBKEY", "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798")
|
||||
relay.Info.Description = getEnv("RELAY_DESCRIPTION", "LUMINA Relay")
|
||||
relay.Info.Icon = getEnv("RELAY_ICON", "https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fliquipedia.net%2Fcommons%2Fimages%2F3%2F35%2FSCProbe.jpg&f=1&nofb=1&ipt=0cbbfef25bce41da63d910e86c3c343e6c3b9d63194ca9755351bb7c2efa3359&ipo=images")
|
||||
relay.Info.Software = "lumina-relay"
|
||||
relay.Info.Version = "0.1.0"
|
||||
|
||||
// Print relay information
|
||||
fmt.Printf("Name: %s\n", relay.Info.Name)
|
||||
@@ -46,6 +48,10 @@ func main() {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Initialize trending system to start background calculations
|
||||
fmt.Println("Initializing trending system...")
|
||||
trending.Initialize(db.DB.DB)
|
||||
|
||||
relay.StoreEvent = append(relay.StoreEvent, db.SaveEvent)
|
||||
relay.QueryEvents = append(relay.QueryEvents, db.QueryEvents)
|
||||
relay.DeleteEvent = append(relay.DeleteEvent, db.DeleteEvent)
|
||||
|
||||
@@ -3,6 +3,7 @@ package trending
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.highperfocused.tech/highperfocused/lumina-relay/relay/cache"
|
||||
@@ -19,16 +20,48 @@ type Post struct {
|
||||
}
|
||||
|
||||
var (
|
||||
trendingCache = cache.New()
|
||||
cacheDuration = 5 * time.Minute
|
||||
trendingCache = cache.New()
|
||||
cacheDuration = 35 * time.Minute // Slightly longer than update interval
|
||||
updateInterval = 30 * time.Minute
|
||||
)
|
||||
|
||||
// Initialize sets up the background updating of trending calculations
|
||||
// This should be called once at application startup
|
||||
func Initialize(db *sql.DB) {
|
||||
// Perform initial calculation
|
||||
if _, err := calculateTrendingKind20(db); err != nil {
|
||||
fmt.Printf("Error in initial trending calculation: %v\n", err)
|
||||
}
|
||||
|
||||
// Set up periodic updates
|
||||
go func() {
|
||||
ticker := time.NewTicker(updateInterval)
|
||||
defer ticker.Stop()
|
||||
|
||||
for range ticker.C {
|
||||
if _, err := calculateTrendingKind20(db); err != nil {
|
||||
fmt.Printf("Error updating trending data: %v\n", err)
|
||||
} else {
|
||||
fmt.Printf("Successfully updated trending data at %s\n", time.Now().Format(time.RFC3339))
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// GetTrendingKind20 returns the top 20 trending posts of kind 20 from the last 24 hours
|
||||
// It returns cached results that are updated periodically in the background
|
||||
func GetTrendingKind20(db *sql.DB) ([]Post, error) {
|
||||
if cached, ok := trendingCache.Get("trending_kind_20"); ok {
|
||||
return cached.([]Post), nil
|
||||
}
|
||||
|
||||
// If cache is empty (which shouldn't happen with background updates),
|
||||
// calculate on demand
|
||||
return calculateTrendingKind20(db)
|
||||
}
|
||||
|
||||
// calculateTrendingKind20 performs the actual calculation and updates the cache
|
||||
func calculateTrendingKind20(db *sql.DB) ([]Post, error) {
|
||||
query := `
|
||||
WITH reactions AS (
|
||||
SELECT
|
||||
|
||||
Reference in New Issue
Block a user