Skip to content

Commit

Permalink
Merge pull request #95 from lescuer97/styling_css
Browse files Browse the repository at this point in the history
Styling css
  • Loading branch information
lescuer97 authored Sep 13, 2024
2 parents 585466d + b68da52 commit 541cfd9
Show file tree
Hide file tree
Showing 28 changed files with 961 additions and 613 deletions.
2 changes: 2 additions & 0 deletions cmd/nutmix/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ func main() {
log.Panicf("os.OpenFile(pathToProjectLogFile, os.O_RDWR|os.O_CREATE, 0764) %+v", err)
}

defer logFile.Close()

w := io.MultiWriter(os.Stdout, logFile)

opts := &slog.HandlerOptions{
Expand Down
7 changes: 5 additions & 2 deletions internal/database/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ func GetMintMeltBalanceByTime(pool *pgxpool.Pool, time int64) (MintMeltBalance,
var mintMeltBalance MintMeltBalance
// change the paid status of the quote
batch := pgx.Batch{}
batch.Queue("SELECT quote, request, request_paid, expiry, unit, minted, state, seen_at FROM mint_request WHERE seen_at >= $1", time)
batch.Queue("SELECT quote, request, amount, request_paid, expiry, unit, melted, fee_reserve, state, payment_preimage, seen_at FROM melt_request WHERE seen_at >= $1", time)
batch.Queue("SELECT quote, request, request_paid, expiry, unit, minted, state, seen_at FROM mint_request WHERE seen_at >= $1 AND (state = 'ISSUED' OR state = 'PAID') ", time)
batch.Queue("SELECT quote, request, amount, request_paid, expiry, unit, melted, fee_reserve, state, payment_preimage, seen_at FROM melt_request WHERE seen_at >= $1 AND (state = 'ISSUED' OR state = 'PAID')", time)

results := pool.SendBatch(context.Background(), &batch)

defer results.Close()

mintRows, err := results.Query()
if err != nil {
if err == pgx.ErrNoRows {
Expand All @@ -47,6 +49,7 @@ func GetMintMeltBalanceByTime(pool *pgxpool.Pool, time int64) (MintMeltBalance,
}
return mintMeltBalance, databaseError(fmt.Errorf(" results.Query(): %w", err))
}
defer meltRows.Close()
meltRequest, err := pgx.CollectRows(meltRows, pgx.RowToStructByName[cashu.MeltRequestDB])

if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions internal/database/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func CheckListOfProofs(pool *pgxpool.Pool, CList []string, SecretList []string)

var proofList []cashu.Proof

rows, err := pool.Query(context.Background(), "SELECT amount, id, secret, c, y, witness FROM proofs WHERE C = ANY($1) OR secret = ANY($2)", CList, SecretList)
rows, err := pool.Query(context.Background(), "SELECT amount, id, secret, c, y, witness, seen_at FROM proofs WHERE C = ANY($1) OR secret = ANY($2)", CList, SecretList)

if err != nil {
if err == pgx.ErrNoRows {
Expand Down Expand Up @@ -394,9 +394,10 @@ func CheckListOfProofsBySecretCurve(pool *pgxpool.Pool, Ys []string) ([]cashu.Pr

var proofList []cashu.Proof

rows, err := pool.Query(context.Background(), "SELECT amount, id, secret, c, y, witness FROM proofs WHERE Y = ANY($1)", Ys)
rows, err := pool.Query(context.Background(), `SELECT amount, id, secret, c, y, witness, seen_at FROM proofs WHERE y = ANY($1)`, Ys)

if err != nil {

if err == pgx.ErrNoRows {
return proofList, nil
}
Expand Down
4 changes: 3 additions & 1 deletion internal/routes/admin/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ func AuthMiddleware(ctx context.Context) gin.HandlerFunc {
return
default:
c.Redirect(http.StatusTemporaryRedirect, "/admin/login")
c.Next()
c.Header("HX-Location", "/admin/login")
c.Abort()
c.JSON(200, nil)

}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/routes/admin/keysets.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func KeysetsPage(ctx context.Context, pool *pgxpool.Pool, mint *mint.Mint) gin.H

return func(c *gin.Context) {

c.HTML(200, "keysets-page", nil)
c.HTML(200, "keysets.html", nil)
}
}
func KeysetsLayoutPage(ctx context.Context, pool *pgxpool.Pool, mint *mint.Mint) gin.HandlerFunc {
Expand Down
90 changes: 75 additions & 15 deletions internal/routes/admin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log/slog"
"os"
"slices"
"time"

"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v5/pgxpool"
Expand Down Expand Up @@ -35,34 +36,92 @@ func AdminRoutes(ctx context.Context, r *gin.Engine, pool *pgxpool.Pool, mint *m

adminRoute.Use(AuthMiddleware(ctx))

// PAGES SETUP
// This is /admin
adminRoute.GET("", InitPage(ctx, pool, mint))
adminRoute.GET("/keysets", KeysetsPage(ctx, pool, mint))
adminRoute.GET("/settings", MintSettingsPage(ctx, pool, mint))
adminRoute.GET("/login", LoginPage(ctx, pool, mint))
adminRoute.POST("/login", Login(ctx, pool, mint))
adminRoute.GET("/bolt11", LightningNodePage(ctx, pool, mint))

// partial template routes
adminRoute.GET("/mintsettings", MintInfoTab(ctx, pool, mint))
adminRoute.POST("/mintsettings", MintInfoPost(ctx, pool, mint))

adminRoute.GET("/bolt11", Bolt11Tab(ctx, pool, mint))
// change routes
adminRoute.POST("/login", Login(ctx, pool, mint))
adminRoute.POST("/mintsettings", MintSettingsForm(ctx, pool, mint))
adminRoute.POST("/bolt11", Bolt11Post(ctx, pool, mint))

adminRoute.GET("/keysets", KeysetsPage(ctx, pool, mint))

adminRoute.POST("/rotate/sats", RotateSatsSeed(ctx, pool, mint))
adminRoute.GET("/keysets-layout", KeysetsLayoutPage(ctx, pool, mint))

// fractional html components
adminRoute.GET("/keysets-layout", KeysetsLayoutPage(ctx, pool, mint))
adminRoute.GET("/lightningdata", LightningDataFormFields(ctx, pool, mint))

adminRoute.GET("/mintactivity", MintActivityTab(ctx, pool, mint))
adminRoute.GET("/mint-balance", MintBalance(ctx, pool, mint))
adminRoute.GET("/mint-melt", MintMeltActivity(ctx, pool, mint))

adminRoute.GET("/mint-melt-summary", MintMeltSummary(ctx, pool, mint))
adminRoute.GET("/mint-melt-list", MintMeltList(ctx, pool, mint))
adminRoute.GET("/logs", LogsTab(ctx))

}

type TIME_REQUEST string

var (
h24 TIME_REQUEST = "24h"
h48 TIME_REQUEST = "48h"
h72 TIME_REQUEST = "72h"
d7 TIME_REQUEST = "7D"
ALL TIME_REQUEST = "all"
)

func ParseToTimeRequest(str string) TIME_REQUEST {

switch str {
case "24h":
return h24
case "48h":
return h48
case "72h":
return h72
case "7d":
return d7
case "all":
return ALL
default:
return h24
}

}

// return 24 hours by default
func (t TIME_REQUEST) RollBackFromNow() time.Time {

rollBackHour := time.Now()

switch t {
case h24:
duration := time.Duration(24) * time.Hour
return rollBackHour.Add(-duration)
case h48:
duration := time.Duration(48) * time.Hour
return rollBackHour.Add(-duration)
case h72:
duration := time.Duration(72) * time.Hour
return rollBackHour.Add(-duration)
case d7:
duration := time.Duration((7 * 24)) * time.Hour
return rollBackHour.Add(-duration)
case ALL:
return time.Unix(1, 0)
}
duration := time.Duration(24) * time.Hour
return rollBackHour.Add(-duration)
}

func LogsTab(ctx context.Context) gin.HandlerFunc {

return func(c *gin.Context) {

timeHeader := c.GetHeader("time")

timeRequestDuration := ParseToTimeRequest(timeHeader)

// read logs
logsdir, err := utils.GetLogsDirectory()

Expand All @@ -71,6 +130,7 @@ func LogsTab(ctx context.Context) gin.HandlerFunc {
}

file, err := os.Open(logsdir + "/" + mint.LogFileName)
defer file.Close()
if err != nil {

errorMessage := ErrorNotif{
Expand All @@ -81,7 +141,7 @@ func LogsTab(ctx context.Context) gin.HandlerFunc {
return
}

logs := utils.ParseLogFileByLevel(file, []slog.Level{slog.LevelWarn, slog.LevelError, slog.LevelInfo})
logs := utils.ParseLogFileByLevelAndTime(file, []slog.Level{slog.LevelWarn, slog.LevelError, slog.LevelInfo, slog.LevelDebug}, timeRequestDuration.RollBackFromNow())

slices.Reverse(logs)

Expand Down
103 changes: 86 additions & 17 deletions internal/routes/admin/mint-activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"fmt"
"log"
"sort"
"time"

"github.com/btcsuite/btcd/btcutil"
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v5/pgxpool"
Expand All @@ -15,16 +15,10 @@ import (
"github.com/lightningnetwork/lnd/zpay32"
)

func MintActivityTab(ctx context.Context, pool *pgxpool.Pool, mint *mint.Mint) gin.HandlerFunc {

return func(c *gin.Context) {
c.HTML(200, "mint-activity", nil)
}
}

func MintBalance(ctx context.Context, pool *pgxpool.Pool, mint *mint.Mint) gin.HandlerFunc {

return func(c *gin.Context) {

if mint.Config.MINT_LIGHTNING_BACKEND == comms.FAKE_WALLET {
c.HTML(200, "fake-wallet-balance", nil)
return
Expand All @@ -46,13 +40,14 @@ func MintBalance(ctx context.Context, pool *pgxpool.Pool, mint *mint.Mint) gin.H
}
}

func MintMeltActivity(ctx context.Context, pool *pgxpool.Pool, mint *mint.Mint) gin.HandlerFunc {
func MintMeltSummary(ctx context.Context, pool *pgxpool.Pool, mint *mint.Mint) gin.HandlerFunc {

return func(c *gin.Context) {
duration := time.Duration(24) * time.Hour
previous24hours := time.Now().Add(-duration).Unix()
timeHeader := c.GetHeader("time")

timeRequestDuration := ParseToTimeRequest(timeHeader)

mintMeltBalance, err := database.GetMintMeltBalanceByTime(pool, previous24hours)
mintMeltBalance, err := database.GetMintMeltBalanceByTime(pool, timeRequestDuration.RollBackFromNow().Unix())

if err != nil {
log.Println(err)
Expand All @@ -65,8 +60,8 @@ func MintMeltActivity(ctx context.Context, pool *pgxpool.Pool, mint *mint.Mint)
return
}

mintMeltTotal := make(map[string]float64)

mintMeltTotal := make(map[string]int64)
mintMeltTotal["Mint"] += 0
// sum up mint
for _, mintRequest := range mintMeltBalance.Mint {
invoice, err := zpay32.Decode(mintRequest.Request, &mint.Network)
Expand All @@ -82,19 +77,93 @@ func MintMeltActivity(ctx context.Context, pool *pgxpool.Pool, mint *mint.Mint)
return
}

mintMeltTotal["Mint"] += invoice.MilliSat.ToSatoshis().ToUnit(btcutil.AmountSatoshi)
mintMeltTotal["Mint"] += int64( invoice.MilliSat.ToSatoshis().ToUnit(btcutil.AmountSatoshi))
}

// sum up melt amount
for _, meltRequest := range mintMeltBalance.Melt {

mintMeltTotal["Melt"] += float64(meltRequest.Amount)
mintMeltTotal["Melt"] += int64(meltRequest.Amount)
}
mintMeltTotal["Melt"] = mintMeltTotal["Melt"] * -1

// get net flows
mintMeltTotal["Net"] = mintMeltTotal["Mint"] - mintMeltTotal["Melt"]
mintMeltTotal["Net"] = mintMeltTotal["Mint"] + mintMeltTotal["Melt"]

c.HTML(200, "mint-melt-activity", mintMeltTotal)
}
}
func MintMeltList(ctx context.Context, pool *pgxpool.Pool, mint *mint.Mint) gin.HandlerFunc {
return func(c *gin.Context) {
timeHeader := c.GetHeader("time")
timeRequestDuration := ParseToTimeRequest(timeHeader)

mintMeltBalance, err := database.GetMintMeltBalanceByTime(pool, timeRequestDuration.RollBackFromNow().Unix())

if err != nil {
log.Println(err)
errorMessage := ErrorNotif{

Error: "There was an error getting mint activity",
}

c.HTML(200, "settings-error", errorMessage)
return
}

mintMeltRequestVisual := ListMintMeltVisual{}

// sum up mint
for _, mintRequest := range mintMeltBalance.Mint {
utc := time.Unix(mintRequest.SeenAt, 0).UTC().Format("2006-Jan-2 15:04:05 MST")

mintMeltRequestVisual = append(mintMeltRequestVisual, MintMeltRequestVisual{
Type: "Mint",
Unit: mintRequest.Unit,
Request: mintRequest.Request,
Status: string(mintRequest.State),
SeenAt: utc,
})

}

// sum up melt amount
for _, meltRequest := range mintMeltBalance.Melt {
utc := time.Unix(meltRequest.SeenAt, 0).UTC().Format("2006-Jan-2 15:04:05 MST")

mintMeltRequestVisual = append(mintMeltRequestVisual, MintMeltRequestVisual{
Type: "Melt",
Unit: meltRequest.Unit,
Request: meltRequest.Request,
Status: string(meltRequest.State),
SeenAt: utc,
})
}

sort.Sort(mintMeltRequestVisual)

c.HTML(200, "mint-melt-list", mintMeltRequestVisual)
}
}

type MintMeltRequestVisual struct {
Type string
Unit string
Request string
Status string
SeenAt string
}

type ListMintMeltVisual []MintMeltRequestVisual

func (ms ListMintMeltVisual) Len() int {
return len(ms)
}

func (ms ListMintMeltVisual) Less(i, j int) bool {
return ms[i].SeenAt < ms[j].SeenAt
}

func (ms ListMintMeltVisual) Swap(i, j int) {
ms[i], ms[j] = ms[j], ms[i]
}
2 changes: 1 addition & 1 deletion internal/routes/admin/pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ func LoginPage(ctx context.Context, pool *pgxpool.Pool, mint *mint.Mint) gin.Han

func InitPage(ctx context.Context, pool *pgxpool.Pool, mint *mint.Mint) gin.HandlerFunc {
return func(c *gin.Context) {
c.HTML(200, "index.html", nil)
c.HTML(200, "mint_activity.html", nil)
}
}
Loading

0 comments on commit 541cfd9

Please sign in to comment.