A pretty dumb approach for getting plot data. Will make it better later
This serves my currenr necessity, will add features later
This commit is contained in:
@@ -9,3 +9,4 @@ linters:
|
|||||||
- wsl_v5
|
- wsl_v5
|
||||||
- exhaustruct
|
- exhaustruct
|
||||||
- gochecknoglobals
|
- gochecknoglobals
|
||||||
|
- nlreturn
|
||||||
|
|||||||
@@ -0,0 +1,115 @@
|
|||||||
|
{{ define "analysis" }}
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<canvas id="monthlyTotal"></canvas>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<canvas id="monthlyBalance"></canvas>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script src="/assets/js/chart.js"></script>
|
||||||
|
<script src="/assets/js/chartjs-plugin-datalabels.js"></script>
|
||||||
|
<script>
|
||||||
|
const mBalanceChart = document.getElementById('monthlyBalance');
|
||||||
|
const monthlyBalanceData = {{ .plotData.mBalance | toJson }};
|
||||||
|
|
||||||
|
const labels = monthlyBalanceData.map(entry =>
|
||||||
|
new Date(2000, entry.month, 1).toLocaleString('en', {month: 'short'})
|
||||||
|
);
|
||||||
|
const values = monthlyBalanceData.map(entry => Number(entry.total));
|
||||||
|
|
||||||
|
// TODO - make the fucking folds work
|
||||||
|
new Chart(mBalanceChart, {
|
||||||
|
type: 'bar',
|
||||||
|
data: {
|
||||||
|
labels: labels,
|
||||||
|
datasets: [{
|
||||||
|
label: 'Monthly Balance',
|
||||||
|
data: values,
|
||||||
|
backgroundColor: (ctx) => {
|
||||||
|
const value = ctx.raw; // the data point value
|
||||||
|
return value < 0 ? 'rgb(231 144 234 / 50%)' : 'rgb(67 149 84 / 50%)';
|
||||||
|
},
|
||||||
|
borderColor: (ctx) => {
|
||||||
|
const value = ctx.raw;
|
||||||
|
return value < 0 ? 'rgb(231 144 234 / 100%)' : 'rgb(67 149 84 / 100%)';
|
||||||
|
},
|
||||||
|
borderWidth: 1
|
||||||
|
}]
|
||||||
|
},
|
||||||
|
options: {
|
||||||
|
plugins: {
|
||||||
|
datalabels: {
|
||||||
|
// anchor: (ctx) => ctx.raw > 0 ? 'start' : 'end', // below bar for negative
|
||||||
|
// align: (ctx) => ctx.raw > 0 ? 'end' : 'start',
|
||||||
|
anchor: "end",
|
||||||
|
align: "end",
|
||||||
|
formatter: (value) => value,
|
||||||
|
color: (context) => {
|
||||||
|
const value = context.dataset.data[context.dataIndex];
|
||||||
|
return value < 0 ? 'rgb(231 144 234 / 100%)' : 'rgb(67 149 84 / 100%)';
|
||||||
|
},
|
||||||
|
font: {
|
||||||
|
weight: 'bold'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
scales: {
|
||||||
|
y: {beginAtZero: true}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
plugins: [ChartDataLabels]
|
||||||
|
});
|
||||||
|
|
||||||
|
// Total Chart
|
||||||
|
const mTotalChart = document.getElementById('monthlyTotal');
|
||||||
|
const mTotalData = {{ .plotData.mTotal | toJson }};
|
||||||
|
|
||||||
|
const mTotalLabels = mTotalData.map(entry =>
|
||||||
|
new Date(2000, entry.month, 1).toLocaleString('en', {month: 'short'})
|
||||||
|
);
|
||||||
|
const mTotalValues = mTotalData.map(entry => Number(entry.total));
|
||||||
|
new Chart(mTotalChart, {
|
||||||
|
type: 'line',
|
||||||
|
data: {
|
||||||
|
labels: mTotalLabels,
|
||||||
|
datasets: [{
|
||||||
|
label: 'Monthly Total',
|
||||||
|
data: mTotalValues,
|
||||||
|
backgroundColor: (ctx) => {
|
||||||
|
const value = ctx.raw; // the data point value
|
||||||
|
return value < 0 ? 'rgb(231 144 234 / 50%)' : 'rgb(67 149 84 / 50%)';
|
||||||
|
},
|
||||||
|
borderColor: (ctx) => {
|
||||||
|
const value = ctx.raw;
|
||||||
|
return value < 0 ? 'rgb(231 144 234 / 100%)' : 'rgb(67 149 84 / 100%)';
|
||||||
|
},
|
||||||
|
borderWidth: 1
|
||||||
|
}]
|
||||||
|
},
|
||||||
|
options: {
|
||||||
|
plugins: {
|
||||||
|
datalabels: {
|
||||||
|
// anchor: (ctx) => ctx.raw > 0 ? 'start' : 'end', // below bar for negative
|
||||||
|
// align: (ctx) => ctx.raw > 0 ? 'end' : 'start',
|
||||||
|
anchor: "end",
|
||||||
|
align: "end",
|
||||||
|
formatter: (value) => value,
|
||||||
|
color: (context) => {
|
||||||
|
const value = context.dataset.data[context.dataIndex];
|
||||||
|
return value < 0 ? 'rgb(231 144 234 / 100%)' : 'rgb(67 149 84 / 100%)';
|
||||||
|
},
|
||||||
|
font: {
|
||||||
|
weight: 'bold'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
scales: {
|
||||||
|
y: {beginAtZero: true}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
plugins: [ChartDataLabels]
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
{{ end }}
|
||||||
@@ -1,4 +1,9 @@
|
|||||||
{{ define "expenses" }}
|
{{ define "expenses" }}
|
||||||
|
<fieldset>
|
||||||
|
<legend>FILTERS</legend>
|
||||||
|
<form action="">
|
||||||
|
</form>
|
||||||
|
</fieldset>
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<th>ID</th>
|
<th>ID</th>
|
||||||
|
|||||||
@@ -40,6 +40,13 @@
|
|||||||
hx-replace-url="true"
|
hx-replace-url="true"
|
||||||
id="overview"
|
id="overview"
|
||||||
class="nav-button {{ if eq .page "overview" }}selected{{ end }}">OVERVIEW</button>
|
class="nav-button {{ if eq .page "overview" }}selected{{ end }}">OVERVIEW</button>
|
||||||
|
<button
|
||||||
|
hx-post="/analysis"
|
||||||
|
hx-target="#content"
|
||||||
|
hx-swap="innerHTML"
|
||||||
|
hx-replace-url="true"
|
||||||
|
id="overview"
|
||||||
|
class="nav-button {{ if eq .page "analysis" }}selected{{ end }}">ANALYSIS</button>
|
||||||
</nav>
|
</nav>
|
||||||
<br>
|
<br>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
@@ -61,6 +68,8 @@
|
|||||||
{{ template "category" . }}
|
{{ template "category" . }}
|
||||||
{{ else if eq .content "overview" }}
|
{{ else if eq .content "overview" }}
|
||||||
{{ template "overview" . }}
|
{{ template "overview" . }}
|
||||||
|
{{ else if eq .content "analysis" }}
|
||||||
|
{{ template "analysis" . }}
|
||||||
{{ else if eq .content "stores" }}
|
{{ else if eq .content "stores" }}
|
||||||
{{ template "stores" . }}
|
{{ template "stores" . }}
|
||||||
{{ else if eq .content "store" }}
|
{{ else if eq .content "store" }}
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+14
-2
@@ -1,9 +1,12 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
"github.com/ESilva15/expenses/api"
|
"github.com/ESilva15/expenses/api"
|
||||||
"github.com/ESilva15/expenses/config"
|
"github.com/ESilva15/expenses/config"
|
||||||
"github.com/ESilva15/expenses/expenses/auth"
|
"github.com/ESilva15/expenses/expenses/auth"
|
||||||
|
val "github.com/ESilva15/expenses/expenses/values"
|
||||||
"github.com/ESilva15/expenses/pages"
|
"github.com/ESilva15/expenses/pages"
|
||||||
|
|
||||||
"github.com/gin-contrib/sessions"
|
"github.com/gin-contrib/sessions"
|
||||||
@@ -11,7 +14,6 @@ import (
|
|||||||
"github.com/shopspring/decimal"
|
"github.com/shopspring/decimal"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"fmt"
|
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -30,7 +32,16 @@ var (
|
|||||||
return t.Format("02-Jan-2006")
|
return t.Format("02-Jan-2006")
|
||||||
},
|
},
|
||||||
"formatPrice": func(v decimal.Decimal) string {
|
"formatPrice": func(v decimal.Decimal) string {
|
||||||
return fmt.Sprintf("%s", v.Round(2).StringFixed(2))
|
return v.Round(val.DecRange).StringFixed(val.DecRange)
|
||||||
|
},
|
||||||
|
"toJson": func(v any) template.JS {
|
||||||
|
a, err := json.Marshal(v)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("failed to marshal json data:", err)
|
||||||
|
return template.JS("{}")
|
||||||
|
}
|
||||||
|
|
||||||
|
return template.JS(a)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@@ -61,6 +72,7 @@ func setRoutes(router *gin.RouterGroup, eng *gin.Engine) {
|
|||||||
pages.RoutePayments(router)
|
pages.RoutePayments(router)
|
||||||
pages.RouteShares(router)
|
pages.RouteShares(router)
|
||||||
pages.RouteOverview(router)
|
pages.RouteOverview(router)
|
||||||
|
pages.RouteAnalysis(router)
|
||||||
|
|
||||||
// This are for the engine, I really need to rethink this
|
// This are for the engine, I really need to rethink this
|
||||||
pages.RouteLogin(eng)
|
pages.RouteLogin(eng)
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
// Package values has useful values for us
|
||||||
|
package values
|
||||||
|
|
||||||
|
const (
|
||||||
|
// MonthsPerYear is the number of months in a year.
|
||||||
|
MonthsPerYear = 12
|
||||||
|
// DecRange is the number of decimal places.
|
||||||
|
DecRange = 2
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// Months is the list with the months names in English.
|
||||||
|
Months = []string{
|
||||||
|
"January", "February", "March", "April", "May", "June",
|
||||||
|
"July", "August", "September", "October", "November", "December",
|
||||||
|
}
|
||||||
|
)
|
||||||
@@ -0,0 +1,150 @@
|
|||||||
|
package pages
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
exp "github.com/ESilva15/expenses/expenses"
|
||||||
|
mod "github.com/ESilva15/expenses/expenses/models"
|
||||||
|
val "github.com/ESilva15/expenses/expenses/values"
|
||||||
|
dec "github.com/shopspring/decimal"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MonthlyTotal holds the data for the MonthlyTotal plot.
|
||||||
|
type MonthlyTotal struct {
|
||||||
|
Month int `json:"month"`
|
||||||
|
Total dec.Decimal `json:"total"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func getMonthlyBalance(exp mod.Expenses) ([]MonthlyTotal, error) {
|
||||||
|
monthlyBalance := make([]MonthlyTotal, val.MonthsPerYear)
|
||||||
|
for k := range val.MonthsPerYear {
|
||||||
|
monthlyBalance[k].Month = k
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, e := range exp {
|
||||||
|
month := e.Date.Month() - 1
|
||||||
|
t := e.Type.TypeName
|
||||||
|
|
||||||
|
// Get the current expenses value and make it negative if its an expense
|
||||||
|
value := e.Value
|
||||||
|
if t == "Despesa" {
|
||||||
|
value = value.Neg()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store the data on the montly balance list
|
||||||
|
monthlyBalance[month].Total = monthlyBalance[month].Total.Add(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
return monthlyBalance, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getMonthlyTotal(exp mod.Expenses) ([]MonthlyTotal, error) {
|
||||||
|
mTotal := make([]MonthlyTotal, val.MonthsPerYear)
|
||||||
|
for k := range mTotal {
|
||||||
|
mTotal[k].Month = k
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 1: net total for each month
|
||||||
|
for _, e := range exp {
|
||||||
|
month := int(e.Date.Month()) - 1 // Jan = 0
|
||||||
|
value := e.Value
|
||||||
|
if e.Type.TypeName == "Despesa" {
|
||||||
|
value = value.Neg()
|
||||||
|
}
|
||||||
|
mTotal[month].Total = mTotal[month].Total.Add(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 2: cumulative balance
|
||||||
|
for i := 1; i < len(mTotal); i++ {
|
||||||
|
mTotal[i].Total = mTotal[i].Total.Add(mTotal[i-1].Total)
|
||||||
|
}
|
||||||
|
|
||||||
|
return mTotal, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getAnalysisDataWG(c *gin.Context) (map[string]any, error) {
|
||||||
|
ctx, err := getLoggedInUserCTX(c)
|
||||||
|
if err != nil {
|
||||||
|
return map[string]any{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
filter, err := expenseFilterFromQuery(c)
|
||||||
|
if err != nil {
|
||||||
|
return map[string]any{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
queriedExpenses, err := exp.App.GetAllExpenses(ctx, filter)
|
||||||
|
if err != nil {
|
||||||
|
return map[string]any{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
mBalance []MonthlyTotal
|
||||||
|
mTotal []MonthlyTotal
|
||||||
|
bErr error
|
||||||
|
tErr error
|
||||||
|
wg sync.WaitGroup
|
||||||
|
)
|
||||||
|
|
||||||
|
wg.Add(2)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
mBalance, bErr = getMonthlyBalance(queriedExpenses)
|
||||||
|
}()
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
mTotal, tErr = getMonthlyTotal(queriedExpenses)
|
||||||
|
}()
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
if tErr != nil {
|
||||||
|
return map[string]any{}, tErr
|
||||||
|
}
|
||||||
|
if bErr != nil {
|
||||||
|
return map[string]any{}, bErr
|
||||||
|
}
|
||||||
|
|
||||||
|
plotData := map[string]any{
|
||||||
|
"mBalance": mBalance,
|
||||||
|
"mTotal": mTotal,
|
||||||
|
}
|
||||||
|
|
||||||
|
return plotData, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func analysisPartialPage(c *gin.Context) {
|
||||||
|
// TODO - this is repeated code with the analysisPage one, figure something
|
||||||
|
data, err := getAnalysisDataWG(c)
|
||||||
|
if err != nil {
|
||||||
|
ServerErrorView(c, "failed to compiled analysis data")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.HTML(http.StatusOK, "analysis", gin.H{
|
||||||
|
"plotData": data,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func analysisPage(c *gin.Context) {
|
||||||
|
data, err := getAnalysisDataWG(c)
|
||||||
|
if err != nil {
|
||||||
|
ServerErrorView(c, "failed to compiled analysis data")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.HTML(http.StatusOK, "terminal", gin.H{
|
||||||
|
"page": "analysis",
|
||||||
|
"renderNavBar": true,
|
||||||
|
"content": "analysis",
|
||||||
|
"plotData": data,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// RouteAnalysis routes the endpoints for analysis stuff.
|
||||||
|
func RouteAnalysis(router *gin.RouterGroup) {
|
||||||
|
router.GET("/analysis", analysisPage)
|
||||||
|
router.POST("/analysis", analysisPartialPage)
|
||||||
|
}
|
||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ESilva15/expenses/expenses"
|
expenses "github.com/ESilva15/expenses/expenses"
|
||||||
mod "github.com/ESilva15/expenses/expenses/models"
|
mod "github.com/ESilva15/expenses/expenses/models"
|
||||||
"github.com/ESilva15/expenses/expenses/repo"
|
"github.com/ESilva15/expenses/expenses/repo"
|
||||||
|
|
||||||
@@ -18,8 +18,9 @@ func expenseFilterFromQuery(c *gin.Context) (repo.ExpFilter, error) {
|
|||||||
var start, end time.Time
|
var start, end time.Time
|
||||||
filter := repo.NewExpFilter()
|
filter := repo.NewExpFilter()
|
||||||
|
|
||||||
startDate := c.Query("range-start") + " 00:00:00"
|
startDate := c.Query("range-start")
|
||||||
if startDate != "" {
|
if startDate != "" {
|
||||||
|
startDate += " 00:00:00"
|
||||||
start, err = time.ParseInLocation("02-Jan-2006 15:04:05", startDate, time.UTC)
|
start, err = time.ParseInLocation("02-Jan-2006 15:04:05", startDate, time.UTC)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error startDate: %v", err)
|
log.Printf("error startDate: %v", err)
|
||||||
@@ -28,8 +29,9 @@ func expenseFilterFromQuery(c *gin.Context) (repo.ExpFilter, error) {
|
|||||||
filter.Start = &start
|
filter.Start = &start
|
||||||
}
|
}
|
||||||
|
|
||||||
endDate := c.Query("range-end") + " 23:59:59"
|
endDate := c.Query("range-end")
|
||||||
if endDate != "" {
|
if endDate != "" {
|
||||||
|
endDate += " 00:00:00"
|
||||||
end, err = time.ParseInLocation("02-Jan-2006 15:04:05", endDate, time.UTC)
|
end, err = time.ParseInLocation("02-Jan-2006 15:04:05", endDate, time.UTC)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error endDate: %v", err)
|
log.Printf("error endDate: %v", err)
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ test: remove-dev-container
|
|||||||
--name $(container)-dev \
|
--name $(container)-dev \
|
||||||
--env-file .env \
|
--env-file .env \
|
||||||
-v ./pgsql-data-dev/:/var/lib/postgresql/data/ \
|
-v ./pgsql-data-dev/:/var/lib/postgresql/data/ \
|
||||||
-it $(image)
|
-it $(registry)/$(repo)/expensesdb:$(tag)
|
||||||
|
|
||||||
build_image:
|
build_image:
|
||||||
make -C ./
|
make -C ./
|
||||||
|
|||||||
+1
-1
@@ -5,4 +5,4 @@ REQUIRED_VARS := registry repo
|
|||||||
$(foreach v,$(REQUIRED_VARS),\
|
$(foreach v,$(REQUIRED_VARS),\
|
||||||
$(if $(value $(v)),,$(error Variable '$(v)' is required but empty)))
|
$(if $(value $(v)),,$(error Variable '$(v)' is required but empty)))
|
||||||
|
|
||||||
tag = v0.0.91-alpha
|
tag = v0.0.92-alpha
|
||||||
|
|||||||
Reference in New Issue
Block a user