add total revenue in header
This commit is contained in:
+105
-3
@@ -3,7 +3,9 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -18,8 +20,106 @@ import (
|
|||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"google.golang.org/grpc/credentials"
|
"google.golang.org/grpc/credentials"
|
||||||
"google.golang.org/grpc/metadata"
|
"google.golang.org/grpc/metadata"
|
||||||
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type MetricResponse struct {
|
||||||
|
Metrics []struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
Value float64 `json:"value"`
|
||||||
|
Unit string `json:"unit"`
|
||||||
|
Period string `json:"period"`
|
||||||
|
} `json:"metrics"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func getRevenue() string {
|
||||||
|
client := &http.Client{}
|
||||||
|
|
||||||
|
req, err := http.NewRequest("GET", "https://api.revenuecat.com/v2/projects/proj5b2f7356/metrics/overview", nil)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Sprintf("Error creating request: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add authorization header
|
||||||
|
req.Header.Add("Authorization", "Bearer sk_wxmjNCLtnDSUPmSXlbEWyuqkpTVTi")
|
||||||
|
|
||||||
|
response, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Sprintf("Error fetching revenue: %v", err)
|
||||||
|
}
|
||||||
|
defer response.Body.Close()
|
||||||
|
|
||||||
|
body, err := io.ReadAll(response.Body)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Sprintf("Error reading response: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var metricResp MetricResponse
|
||||||
|
err = json.Unmarshal(body, &metricResp)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Sprintf("Error parsing JSON: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a map for easy lookup
|
||||||
|
metrics := make(map[string]struct {
|
||||||
|
name string
|
||||||
|
value float64
|
||||||
|
unit string
|
||||||
|
})
|
||||||
|
|
||||||
|
for _, m := range metricResp.Metrics {
|
||||||
|
metrics[m.ID] = struct {
|
||||||
|
name string
|
||||||
|
value float64
|
||||||
|
unit string
|
||||||
|
}{
|
||||||
|
name: m.Name,
|
||||||
|
value: m.Value,
|
||||||
|
unit: m.Unit,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if m, ok := metrics["revenue"]; ok {
|
||||||
|
return fmt.Sprintf("%s%.0f", m.unit, m.value)
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
|
||||||
|
// Format the output
|
||||||
|
// var result strings.Builder
|
||||||
|
// result.WriteString("\n")
|
||||||
|
// result.WriteString("💰 Revenue Metrics\n")
|
||||||
|
// result.WriteString("═══════════════════════════════════════\n\n")
|
||||||
|
|
||||||
|
// Key metrics
|
||||||
|
// if m, ok := metrics["mrr"]; ok {
|
||||||
|
// result.WriteString(fmt.Sprintf("MRR: %s%.0f\n", m.unit, m.value))
|
||||||
|
// }
|
||||||
|
// if m, ok := metrics["revenue"]; ok {
|
||||||
|
// result.WriteString(fmt.Sprintf("Revenue (28d): %s%.0f\n", m.unit, m.value))
|
||||||
|
// }
|
||||||
|
// result.WriteString("\n")
|
||||||
|
//
|
||||||
|
// if m, ok := metrics["active_subscriptions"]; ok {
|
||||||
|
// result.WriteString(fmt.Sprintf("Active Subscriptions: %s%.0f\n", m.unit, m.value))
|
||||||
|
// }
|
||||||
|
// if m, ok := metrics["active_trials"]; ok {
|
||||||
|
// result.WriteString(fmt.Sprintf("Active Trials: %s%.0f\n", m.unit, m.value))
|
||||||
|
// }
|
||||||
|
// result.WriteString("\n")
|
||||||
|
//
|
||||||
|
// if m, ok := metrics["new_customers"]; ok {
|
||||||
|
// result.WriteString(fmt.Sprintf("New Customers (28d): %s%.0f\n", m.unit, m.value))
|
||||||
|
// }
|
||||||
|
// if m, ok := metrics["active_users"]; ok {
|
||||||
|
// result.WriteString(fmt.Sprintf("Active Users (28d): %s%.0f\n", m.unit, m.value))
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// result.WriteString("\n═══════════════════════════════════════\n")
|
||||||
|
//
|
||||||
|
// return result.String()
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
titleStyle = lipgloss.NewStyle().
|
titleStyle = lipgloss.NewStyle().
|
||||||
Bold(true).
|
Bold(true).
|
||||||
@@ -105,12 +205,14 @@ type model struct {
|
|||||||
terminalWidth int
|
terminalWidth int
|
||||||
terminalHeight int
|
terminalHeight int
|
||||||
focusOnDetail bool // true = detail panel has focus, false = list has focus
|
focusOnDetail bool // true = detail panel has focus, false = list has focus
|
||||||
|
revenue string
|
||||||
}
|
}
|
||||||
|
|
||||||
func initialModel(humans []*HumanData) model {
|
func initialModel(humans []*HumanData) model {
|
||||||
return model{
|
return model{
|
||||||
humans: humans,
|
humans: humans,
|
||||||
cursor: 0,
|
cursor: 0,
|
||||||
|
revenue: getRevenue(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -281,7 +383,7 @@ func (m model) View() string {
|
|||||||
pastSevenDaysSignupsCount++
|
pastSevenDaysSignupsCount++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
header := titleStyle.Render(fmt.Sprintf("👥 Humans | %d total | %d in the past 7 days", len(m.humans), pastSevenDaysSignupsCount))
|
header := titleStyle.Render(fmt.Sprintf("👥 Humans | %d total | %d in the past 7 days | Revenue: %s", len(m.humans), pastSevenDaysSignupsCount, m.revenue))
|
||||||
|
|
||||||
// Status bar showing current position
|
// Status bar showing current position
|
||||||
statusBar := statusStyle.Render(fmt.Sprintf(" %d/%d", m.cursor+1, len(m.humans)))
|
statusBar := statusStyle.Render(fmt.Sprintf(" %d/%d", m.cursor+1, len(m.humans)))
|
||||||
|
|||||||
Reference in New Issue
Block a user