This commit is contained in:
@@ -17,6 +17,9 @@ var validStatsSections = map[string]string{
|
|||||||
"commandes": "stats_reset_commandes_at",
|
"commandes": "stats_reset_commandes_at",
|
||||||
"revenus": "stats_reset_revenus_at",
|
"revenus": "stats_reset_revenus_at",
|
||||||
"produits": "stats_reset_produits_at",
|
"produits": "stats_reset_produits_at",
|
||||||
|
"heures": "stats_reset_heures_at",
|
||||||
|
"jours": "stats_reset_jours_at",
|
||||||
|
"doses": "stats_reset_doses_at",
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResetAdminStats réinitialise une section précise des statistiques.
|
// ResetAdminStats réinitialise une section précise des statistiques.
|
||||||
@@ -65,33 +68,24 @@ func GetAdminStats(c *gin.Context) {
|
|||||||
resetCmd := readResetAt(database, "stats_reset_commandes_at")
|
resetCmd := readResetAt(database, "stats_reset_commandes_at")
|
||||||
resetRev := readResetAt(database, "stats_reset_revenus_at")
|
resetRev := readResetAt(database, "stats_reset_revenus_at")
|
||||||
resetProd := readResetAt(database, "stats_reset_produits_at")
|
resetProd := readResetAt(database, "stats_reset_produits_at")
|
||||||
|
resetHeure := readResetAt(database, "stats_reset_heures_at")
|
||||||
|
resetJour := readResetAt(database, "stats_reset_jours_at")
|
||||||
|
resetDoses := readResetAt(database, "stats_reset_doses_at")
|
||||||
|
|
||||||
// whereCmd : filtre commandes non annulées depuis resetCmd
|
filterClause := func(base string, t time.Time) string {
|
||||||
whereCmd := func() string {
|
if !t.IsZero() {
|
||||||
base := "status != 'cancelled'"
|
return base + " AND created_at >= '" + t.Format(time.RFC3339) + "'"
|
||||||
if !resetCmd.IsZero() {
|
|
||||||
base += " AND created_at >= '" + resetCmd.Format(time.RFC3339) + "'"
|
|
||||||
}
|
|
||||||
return base
|
|
||||||
}
|
|
||||||
// whereRev : filtre commandes approuvées depuis resetRev
|
|
||||||
whereRev := func() string {
|
|
||||||
base := "status = 'approved'"
|
|
||||||
if !resetRev.IsZero() {
|
|
||||||
base += " AND created_at >= '" + resetRev.Format(time.RFC3339) + "'"
|
|
||||||
}
|
|
||||||
return base
|
|
||||||
}
|
|
||||||
// whereProd : filtre commandes non annulées depuis resetProd (pour les jointures sur c.)
|
|
||||||
whereProd := func() string {
|
|
||||||
base := "status != 'cancelled'"
|
|
||||||
if !resetProd.IsZero() {
|
|
||||||
base += " AND created_at >= '" + resetProd.Format(time.RFC3339) + "'"
|
|
||||||
}
|
}
|
||||||
return base
|
return base
|
||||||
}
|
}
|
||||||
|
|
||||||
// compatibilité : les anciennes clauses whereBase/whereApproved pointent sur commandes/revenus
|
whereCmd := func() string { return filterClause("status != 'cancelled'", resetCmd) }
|
||||||
|
whereRev := func() string { return filterClause("status = 'approved'", resetRev) }
|
||||||
|
whereProd := func() string { return filterClause("status != 'cancelled'", resetProd) }
|
||||||
|
whereHeure := func() string { return filterClause("status != 'cancelled'", resetHeure) }
|
||||||
|
whereJour := func() string { return filterClause("status != 'cancelled'", resetJour) }
|
||||||
|
whereDoses := func() string { return filterClause("status != 'cancelled'", resetDoses) }
|
||||||
|
|
||||||
whereBase := func(extra string) string {
|
whereBase := func(extra string) string {
|
||||||
base := whereCmd()
|
base := whereCmd()
|
||||||
if extra != "" {
|
if extra != "" {
|
||||||
@@ -106,7 +100,7 @@ func GetAdminStats(c *gin.Context) {
|
|||||||
gdb.Raw(`
|
gdb.Raw(`
|
||||||
SELECT EXTRACT(DOW FROM created_at)::int AS dow, COUNT(*) AS count
|
SELECT EXTRACT(DOW FROM created_at)::int AS dow, COUNT(*) AS count
|
||||||
FROM commandes
|
FROM commandes
|
||||||
WHERE ` + whereBase("") + `
|
WHERE ` + whereJour() + `
|
||||||
GROUP BY dow
|
GROUP BY dow
|
||||||
ORDER BY dow
|
ORDER BY dow
|
||||||
`).Scan(&wdRows)
|
`).Scan(&wdRows)
|
||||||
@@ -174,7 +168,7 @@ func GetAdminStats(c *gin.Context) {
|
|||||||
COUNT(*) AS count,
|
COUNT(*) AS count,
|
||||||
COALESCE(SUM(total_prix - COALESCE(referral_used, 0)), 0) AS revenue
|
COALESCE(SUM(total_prix - COALESCE(referral_used, 0)), 0) AS revenue
|
||||||
FROM commandes
|
FROM commandes
|
||||||
WHERE ` + whereBase("") + `
|
WHERE ` + whereHeure() + `
|
||||||
GROUP BY hour
|
GROUP BY hour
|
||||||
ORDER BY hour
|
ORDER BY hour
|
||||||
`).Scan(&hourRows)
|
`).Scan(&hourRows)
|
||||||
@@ -247,7 +241,7 @@ func GetAdminStats(c *gin.Context) {
|
|||||||
JOIN commandes c ON c.id = ci.command_id
|
JOIN commandes c ON c.id = ci.command_id
|
||||||
LEFT JOIN products p ON p.id = ci.product_id
|
LEFT JOIN products p ON p.id = ci.product_id
|
||||||
LEFT JOIN categories cat ON cat.name = p.category
|
LEFT JOIN categories cat ON cat.name = p.category
|
||||||
WHERE c.` + whereProd() + `
|
WHERE c.` + whereDoses() + `
|
||||||
GROUP BY ci.product_id, ci.produit, ci.quantite, cat.color
|
GROUP BY ci.product_id, ci.produit, ci.quantite, cat.color
|
||||||
ORDER BY ci.product_id, COUNT(DISTINCT ci.command_id) DESC
|
ORDER BY ci.product_id, COUNT(DISTINCT ci.command_id) DESC
|
||||||
`).Scan(&qtyRows)
|
`).Scan(&qtyRows)
|
||||||
@@ -335,6 +329,9 @@ func GetAdminStats(c *gin.Context) {
|
|||||||
"reset_at_commandes": dateFilter(resetCmd),
|
"reset_at_commandes": dateFilter(resetCmd),
|
||||||
"reset_at_revenus": dateFilter(resetRev),
|
"reset_at_revenus": dateFilter(resetRev),
|
||||||
"reset_at_produits": dateFilter(resetProd),
|
"reset_at_produits": dateFilter(resetProd),
|
||||||
|
"reset_at_heures": dateFilter(resetHeure),
|
||||||
|
"reset_at_jours": dateFilter(resetJour),
|
||||||
|
"reset_at_doses": dateFilter(resetDoses),
|
||||||
"by_weekday": byWeekday,
|
"by_weekday": byWeekday,
|
||||||
"by_day_30": byDay,
|
"by_day_30": byDay,
|
||||||
"by_day_revenue": byDayRevenue,
|
"by_day_revenue": byDayRevenue,
|
||||||
|
|||||||
Reference in New Issue
Block a user