summaryrefslogblamecommitdiff
path: root/api/usage.go
blob: 378c6178d4eb83fb3910903cc2c48f62525d3d26 (plain) (tree)
1
2
3
4
5
6
7
8
9
10









                                       
                                      




































                                                                                             
package api

import (
	"context"
	"encoding/json"
	"log"
	"net"

	"github.com/dustin/go-humanize"
	"sinanmohd.com/redq/db"
	"sinanmohd.com/redq/bpf/usage"
)

type UsageStat struct {
	Ingress string `json:"ingress"`
	Egress  string `json:"egress"`
}

type UsageResp map[string]UsageStat

func handleUsage(conn net.Conn, u *usage.Usage, queries *db.Queries, ctxDb context.Context) {
	resp := make(UsageResp)

	fetchedUsage, err := queries.GetUsage(ctxDb)
	if err != nil {
		log.Printf("fetching from database: %s", err)
		return
	}

	u.Mutex.RLock()
	for _, value := range u.Data {
		fetchedUsage.Ingress += int64(value.Ingress)
		fetchedUsage.Egress += int64(value.Egress)
	}
	u.Mutex.RUnlock()
	resp["total"] = UsageStat{
		Ingress: humanize.Bytes(uint64(fetchedUsage.Ingress)),
		Egress:  humanize.Bytes(uint64(fetchedUsage.Egress)),
	}

	buf, err := json.Marshal(resp)
	if err != nil {
		log.Printf("marshaling json: %s", err)
		return
	}

	conn.Write(buf)
}