Resolve "Statistics per Instance level"

This commit is contained in:
Phil 2022-01-08 17:21:53 +01:00
parent 97fe8febfa
commit 151e9b9b7f
12 changed files with 148 additions and 7 deletions

23
db/AddLinks.go Normal file
View file

@ -0,0 +1,23 @@
package db
import (
"strconv"
)
func StatsIncreaseTotalLinks() (int, error) {
val, err := Client.Get("total-links").Result()
if err != nil {
err := Client.Set("total-links", "0", 0).Err()
if err != nil {
return 0, err
}
}
i, _ := strconv.Atoi(val)
i++
err = Client.Set("total-links", i, 0).Err()
if err != nil {
return 0, err
}
return i, nil
}

23
db/AddViews.go Normal file
View file

@ -0,0 +1,23 @@
package db
import (
"strconv"
)
func StatsIncreaseViewsLinks() (int, error) {
val, err := Client.Get("total-views").Result()
if err != nil {
err := Client.Set("total-views", "0", 0).Err()
if err != nil {
return 0, err
}
}
i, _ := strconv.Atoi(val)
i++
err = Client.Set("total-views", i, 0).Err()
if err != nil {
return 0, err
}
return i, nil
}

30
db/GetStats.go Normal file
View file

@ -0,0 +1,30 @@
package db
import (
"log"
"strconv"
)
func GetTotalLinks() int {
val1, err1 := Client.Get("total-links").Result()
if err1 != nil {
log.Fatal(err1)
}
val2, err2 := strconv.Atoi(val1)
if err2 != nil {
log.Fatal(err1)
}
return val2
}
func GetTotalViews() int {
val1, err1 := Client.Get("total-views").Result()
if err1 != nil {
log.Fatal(err1)
}
val2, err2 := strconv.Atoi(val1)
if err2 != nil {
log.Fatal(err1)
}
return val2
}

View file

@ -1,14 +1,15 @@
version: "3"
services:
web:
# build: . # Only if you want to Build the image on your own Server!
# build: . # Only if you want to Build the image on your own Server!
image: registry.ucode.space/phil/goshorly:latest
environment:
- HOST=example.org # Domain or IP-Adress
- PORT=3000 # The Port you want to use
- HTTPS=true # If you want to use HTTPS
- PROXY=true # If you want to use a Reverse Proxy
- REDIS_URI=redis # The Redis-URI (name of redis container OOTB)
- HOST=127.0.0.1 # Domain or IP-Adress
- PORT=3000 # The Port you want to use
- HTTPS=true # If you want to use HTTPS
- PROXY=true # If you want to use a Reverse Proxy
- REDIS_URI=redis # The Redis-URI (name of redis container OOTB)
- ENABLE_STATS=true # Enables the /stats public frontend
depends_on:
- redis
restart: always

View file

@ -4,11 +4,12 @@ services:
# build: . # Only if you want to Build the image on your own Server!
image: registry.ucode.space/phil/goshorly:latest
environment:
- HOST=your_domain_or_ip # Domain or IP-Adress
- HOST=127.0.0.1 # Domain or IP-Adress
- PORT=3000 # The Port you want to use
- HTTPS=false # If you want to use HTTPS
- PROXY=false # If you want to use a Reverse Proxy
- REDIS_URI=redis # The Redis-URI (name of redis container OOTB)
- ENABLE_STATS=true # Enables the /stats public frontend (Default: true)
ports:
- "3000:3000"
depends_on:

View file

@ -32,6 +32,10 @@ func main() {
app.Get("/", routes.Gethome)
if utils.ESTATS == "true" {
app.Get("/stats", routes.GetStats)
}
app.Get("/:id", routes.ID)
app.Use(limiter.New(utils.ConfigLimiter))

View file

@ -1,6 +1,7 @@
package routes
import (
"git.ucode.space/Phil/goshorly/db"
"git.ucode.space/Phil/goshorly/utils"
"github.com/gofiber/fiber/v2"
)
@ -10,5 +11,7 @@ func Gethome(c *fiber.Ctx) error {
"GitCommitShort": utils.GitCommitShort,
"GitBranch": utils.GitBranch,
"GitBuild": utils.GitBuild,
"TotalLinks": db.GetTotalLinks(),
"TotalViews": db.GetTotalViews(),
})
}

View file

@ -1,6 +1,8 @@
package routes
import (
"log"
"git.ucode.space/Phil/goshorly/db"
"git.ucode.space/Phil/goshorly/utils"
"github.com/gofiber/fiber/v2"
@ -16,6 +18,10 @@ func ID(c *fiber.Ctx) error {
"url": "URL not found",
})
} else {
_, err = db.StatsIncreaseViewsLinks()
if err != nil {
log.Fatal(err.Error())
}
return c.Status(301).JSON(&fiber.Map{
"error": false,
"url": val,
@ -28,5 +34,10 @@ func ID(c *fiber.Ctx) error {
"BASEURL": utils.URL,
})
}
_, err = db.StatsIncreaseViewsLinks()
if err != nil {
log.Fatal(err.Error())
}
return c.Redirect(val)
}

View file

@ -1,6 +1,7 @@
package routes
import (
"log"
"regexp"
"time"
@ -31,6 +32,8 @@ func Posthome(c *fiber.Ctx) error {
"GitCommitShort": utils.GitCommitShort,
"GitBranch": utils.GitBranch,
"GitBuild": utils.GitBuild,
"TotalLinks": db.GetTotalLinks(),
"TotalViews": db.GetTotalViews(),
})
}
@ -48,6 +51,8 @@ func Posthome(c *fiber.Ctx) error {
"GitCommitShort": utils.GitCommitShort,
"GitBranch": utils.GitBranch,
"GitBuild": utils.GitBuild,
"TotalLinks": db.GetTotalLinks(),
"TotalViews": db.GetTotalViews(),
})
}
@ -67,6 +72,8 @@ func Posthome(c *fiber.Ctx) error {
"GitCommitShort": utils.GitCommitShort,
"GitBranch": utils.GitBranch,
"GitBuild": utils.GitBuild,
"TotalLinks": db.GetTotalLinks(),
"TotalViews": db.GetTotalViews(),
})
}
@ -85,11 +92,20 @@ func Posthome(c *fiber.Ctx) error {
"GitCommitShort": utils.GitCommitShort,
"GitBranch": utils.GitBranch,
"GitBuild": utils.GitBuild,
"TotalLinks": db.GetTotalLinks(),
"TotalViews": db.GetTotalViews(),
})
}
fURL := utils.URL + id
// Increase Total Links into the DB
_, err = db.StatsIncreaseTotalLinks()
if err != nil {
log.Fatalln(err.Error())
}
if u.CLI {
return c.Status(201).JSON(&fiber.Map{
"success": true,
@ -102,5 +118,7 @@ func Posthome(c *fiber.Ctx) error {
"GitCommitShort": utils.GitCommitShort,
"GitBranch": utils.GitBranch,
"GitBuild": utils.GitBuild,
"TotalLinks": db.GetTotalLinks(),
"TotalViews": db.GetTotalViews(),
})
}

16
routes/stats.go Normal file
View file

@ -0,0 +1,16 @@
package routes
import (
"time"
"git.ucode.space/Phil/goshorly/db"
"github.com/gofiber/fiber/v2"
)
func GetStats(c *fiber.Ctx) error {
return c.JSON(&fiber.Map{
"Timestamp": time.Now(),
"Totallinks": db.GetTotalLinks(),
"Totalviews": db.GetTotalViews(),
})
}

View file

@ -12,6 +12,7 @@ var (
PROXY bool
URL string
REDIS_URI string
ESTATS string
)
func Init_env_vars() {
@ -48,6 +49,13 @@ func Init_env_vars() {
REDIS_URI = "redis"
}
UESTATS, _ := os.LookupEnv("ENABLE_STATS")
if UESTATS != "false" {
ESTATS = "true"
} else {
ESTATS = "false"
}
create_string()
}

View file

@ -35,6 +35,9 @@
<p>An dead simple & fast URL shortener. <br>
All shorten URLs will be available for 30days!
</p>
{{ if and .TotalLinks .TotalViews }}
<p>This Instance has served over {{ .TotalLinks }} URLs and {{ .TotalViews }} total Views!</p>
{{ end }}
<hr>
<form method="post" action="#">
<fieldset>