Skip to content

Commit 18bb9c3

Browse files
committed
chore: remove usage tracking and logging functionality
- Deleted the `LoggerPlugin` along with associated usage tracking and in-memory statistics logic. - Removed all related tests (`logger_plugin_test.go`, `usage_tab_test.go`) and external-facing handler (`usage.go`) for usage statistics export/import. - Cleaned up TUI integration by deleting `usage_tab.go`.
1 parent b8bba05 commit 18bb9c3

18 files changed

Lines changed: 116 additions & 1470 deletions

File tree

cmd/server/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ import (
2424
"github.com/router-for-me/CLIProxyAPI/v6/internal/logging"
2525
"github.com/router-for-me/CLIProxyAPI/v6/internal/managementasset"
2626
"github.com/router-for-me/CLIProxyAPI/v6/internal/misc"
27+
"github.com/router-for-me/CLIProxyAPI/v6/internal/redisqueue"
2728
"github.com/router-for-me/CLIProxyAPI/v6/internal/registry"
2829
"github.com/router-for-me/CLIProxyAPI/v6/internal/store"
2930
_ "github.com/router-for-me/CLIProxyAPI/v6/internal/translator"
3031
"github.com/router-for-me/CLIProxyAPI/v6/internal/tui"
31-
"github.com/router-for-me/CLIProxyAPI/v6/internal/usage"
3232
"github.com/router-for-me/CLIProxyAPI/v6/internal/util"
3333
sdkAuth "github.com/router-for-me/CLIProxyAPI/v6/sdk/auth"
3434
coreauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
@@ -417,7 +417,7 @@ func main() {
417417
configFileExists = true
418418
}
419419
}
420-
usage.SetStatisticsEnabled(cfg.UsageStatisticsEnabled)
420+
redisqueue.SetUsageStatisticsEnabled(cfg.UsageStatisticsEnabled)
421421
coreauth.SetQuotaCooldownDisabled(cfg.DisableCooling)
422422

423423
if err = logging.ConfigureLogOutput(cfg); err != nil {

docker-build.sh

Lines changed: 5 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -5,123 +5,13 @@
55
# This script automates the process of building and running the Docker container
66
# with version information dynamically injected at build time.
77

8-
# Hidden feature: Preserve usage statistics across rebuilds
9-
# Usage: ./docker-build.sh --with-usage
10-
# First run prompts for management API key, saved to temp/stats/.api_secret
11-
128
set -euo pipefail
139

14-
STATS_DIR="temp/stats"
15-
STATS_FILE="${STATS_DIR}/.usage_backup.json"
16-
SECRET_FILE="${STATS_DIR}/.api_secret"
17-
WITH_USAGE=false
18-
19-
get_port() {
20-
if [[ -f "config.yaml" ]]; then
21-
grep -E "^port:" config.yaml | sed -E 's/^port: *["'"'"']?([0-9]+)["'"'"']?.*$/\1/'
22-
else
23-
echo "8317"
24-
fi
25-
}
26-
27-
export_stats_api_secret() {
28-
if [[ -f "${SECRET_FILE}" ]]; then
29-
API_SECRET=$(cat "${SECRET_FILE}")
30-
else
31-
if [[ ! -d "${STATS_DIR}" ]]; then
32-
mkdir -p "${STATS_DIR}"
33-
fi
34-
echo "First time using --with-usage. Management API key required."
35-
read -r -p "Enter management key: " -s API_SECRET
36-
echo
37-
echo "${API_SECRET}" > "${SECRET_FILE}"
38-
chmod 600 "${SECRET_FILE}"
39-
fi
40-
}
41-
42-
check_container_running() {
43-
local port
44-
port=$(get_port)
45-
46-
if ! curl -s -o /dev/null -w "%{http_code}" "http://localhost:${port}/" | grep -q "200"; then
47-
echo "Error: cli-proxy-api service is not responding at localhost:${port}"
48-
echo "Please start the container first or use without --with-usage flag."
49-
exit 1
50-
fi
51-
}
52-
53-
export_stats() {
54-
local port
55-
port=$(get_port)
56-
57-
if [[ ! -d "${STATS_DIR}" ]]; then
58-
mkdir -p "${STATS_DIR}"
59-
fi
60-
check_container_running
61-
echo "Exporting usage statistics..."
62-
EXPORT_RESPONSE=$(curl -s -w "\n%{http_code}" -H "X-Management-Key: ${API_SECRET}" \
63-
"http://localhost:${port}/v0/management/usage/export")
64-
HTTP_CODE=$(echo "${EXPORT_RESPONSE}" | tail -n1)
65-
RESPONSE_BODY=$(echo "${EXPORT_RESPONSE}" | sed '$d')
66-
67-
if [[ "${HTTP_CODE}" != "200" ]]; then
68-
echo "Export failed (HTTP ${HTTP_CODE}): ${RESPONSE_BODY}"
69-
exit 1
70-
fi
71-
72-
echo "${RESPONSE_BODY}" > "${STATS_FILE}"
73-
echo "Statistics exported to ${STATS_FILE}"
74-
}
75-
76-
import_stats() {
77-
local port
78-
port=$(get_port)
79-
80-
echo "Importing usage statistics..."
81-
IMPORT_RESPONSE=$(curl -s -w "\n%{http_code}" -X POST \
82-
-H "X-Management-Key: ${API_SECRET}" \
83-
-H "Content-Type: application/json" \
84-
-d @"${STATS_FILE}" \
85-
"http://localhost:${port}/v0/management/usage/import")
86-
IMPORT_CODE=$(echo "${IMPORT_RESPONSE}" | tail -n1)
87-
IMPORT_BODY=$(echo "${IMPORT_RESPONSE}" | sed '$d')
88-
89-
if [[ "${IMPORT_CODE}" == "200" ]]; then
90-
echo "Statistics imported successfully"
91-
else
92-
echo "Import failed (HTTP ${IMPORT_CODE}): ${IMPORT_BODY}"
93-
fi
94-
95-
rm -f "${STATS_FILE}"
96-
}
97-
98-
wait_for_service() {
99-
local port
100-
port=$(get_port)
101-
102-
echo "Waiting for service to be ready..."
103-
for i in {1..30}; do
104-
if curl -s -o /dev/null -w "%{http_code}" "http://localhost:${port}/" | grep -q "200"; then
105-
break
106-
fi
107-
sleep 1
108-
done
109-
sleep 2
110-
}
111-
112-
case "${1:-}" in
113-
"")
114-
;;
115-
"--with-usage")
116-
WITH_USAGE=true
117-
export_stats_api_secret
118-
;;
119-
*)
120-
echo "Error: unknown option '${1}'. Did you mean '--with-usage'?"
121-
echo "Usage: ./docker-build.sh [--with-usage]"
122-
exit 1
123-
;;
124-
esac
10+
if [[ "${1:-}" != "" ]]; then
11+
echo "Error: unknown option '${1}'."
12+
echo "Usage: ./docker-build.sh"
13+
exit 1
14+
fi
12515

12616
# --- Step 1: Choose Environment ---
12717
echo "Please select an option:"
@@ -133,14 +23,7 @@ read -r -p "Enter choice [1-2]: " choice
13323
case "$choice" in
13424
1)
13525
echo "--- Running with Pre-built Image ---"
136-
if [[ "${WITH_USAGE}" == "true" ]]; then
137-
export_stats
138-
fi
13926
docker compose up -d --remove-orphans --no-build
140-
if [[ "${WITH_USAGE}" == "true" ]]; then
141-
wait_for_service
142-
import_stats
143-
fi
14427
echo "Services are starting from remote image."
14528
echo "Run 'docker compose logs -f' to see the logs."
14629
;;
@@ -167,18 +50,9 @@ case "$choice" in
16750
--build-arg COMMIT="${COMMIT}" \
16851
--build-arg BUILD_DATE="${BUILD_DATE}"
16952

170-
if [[ "${WITH_USAGE}" == "true" ]]; then
171-
export_stats
172-
fi
173-
17453
echo "Starting the services..."
17554
docker compose up -d --remove-orphans --pull never
17655

177-
if [[ "${WITH_USAGE}" == "true" ]]; then
178-
wait_for_service
179-
import_stats
180-
fi
181-
18256
echo "Build complete. Services are starting."
18357
echo "Run 'docker compose logs -f' to see the logs."
18458
;;

internal/api/handlers/management/handler.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/gin-gonic/gin"
1616
"github.com/router-for-me/CLIProxyAPI/v6/internal/buildinfo"
1717
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
18-
"github.com/router-for-me/CLIProxyAPI/v6/internal/usage"
1918
sdkAuth "github.com/router-for-me/CLIProxyAPI/v6/sdk/auth"
2019
coreauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
2120
"golang.org/x/crypto/bcrypt"
@@ -41,7 +40,6 @@ type Handler struct {
4140
attemptsMu sync.Mutex
4241
failedAttempts map[string]*attemptInfo // keyed by client IP
4342
authManager *coreauth.Manager
44-
usageStats *usage.RequestStatistics
4543
tokenStore coreauth.Store
4644
localPassword string
4745
allowRemoteOverride bool
@@ -60,7 +58,6 @@ func NewHandler(cfg *config.Config, configFilePath string, manager *coreauth.Man
6058
configFilePath: configFilePath,
6159
failedAttempts: make(map[string]*attemptInfo),
6260
authManager: manager,
63-
usageStats: usage.GetRequestStatistics(),
6461
tokenStore: sdkAuth.GetTokenStore(),
6562
allowRemoteOverride: envSecret != "",
6663
envSecret: envSecret,
@@ -124,9 +121,6 @@ func (h *Handler) SetAuthManager(manager *coreauth.Manager) {
124121
h.mu.Unlock()
125122
}
126123

127-
// SetUsageStatistics allows replacing the usage statistics reference.
128-
func (h *Handler) SetUsageStatistics(stats *usage.RequestStatistics) { h.usageStats = stats }
129-
130124
// SetLocalPassword configures the runtime-local password accepted for localhost requests.
131125
func (h *Handler) SetLocalPassword(password string) { h.localPassword = password }
132126

internal/api/handlers/management/usage.go

Lines changed: 0 additions & 79 deletions
This file was deleted.

internal/api/server.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"github.com/router-for-me/CLIProxyAPI/v6/internal/logging"
3232
"github.com/router-for-me/CLIProxyAPI/v6/internal/managementasset"
3333
"github.com/router-for-me/CLIProxyAPI/v6/internal/redisqueue"
34-
"github.com/router-for-me/CLIProxyAPI/v6/internal/usage"
3534
"github.com/router-for-me/CLIProxyAPI/v6/internal/util"
3635
sdkaccess "github.com/router-for-me/CLIProxyAPI/v6/sdk/access"
3736
"github.com/router-for-me/CLIProxyAPI/v6/sdk/api/handlers"
@@ -507,9 +506,6 @@ func (s *Server) registerManagementRoutes() {
507506
mgmt := s.engine.Group("/v0/management")
508507
mgmt.Use(s.managementAvailabilityMiddleware(), s.mgmt.Middleware())
509508
{
510-
mgmt.GET("/usage", s.mgmt.GetUsageStatistics)
511-
mgmt.GET("/usage/export", s.mgmt.ExportUsageStatistics)
512-
mgmt.POST("/usage/import", s.mgmt.ImportUsageStatistics)
513509
mgmt.GET("/config", s.mgmt.GetConfig)
514510
mgmt.GET("/config.yaml", s.mgmt.GetConfigYAML)
515511
mgmt.PUT("/config.yaml", s.mgmt.PutConfigYAML)
@@ -1001,7 +997,7 @@ func (s *Server) UpdateClients(cfg *config.Config) {
1001997
}
1002998

1003999
if oldCfg == nil || oldCfg.UsageStatisticsEnabled != cfg.UsageStatisticsEnabled {
1004-
usage.SetStatisticsEnabled(cfg.UsageStatisticsEnabled)
1000+
redisqueue.SetUsageStatisticsEnabled(cfg.UsageStatisticsEnabled)
10051001
}
10061002

10071003
if s.requestLogger != nil && (oldCfg == nil || oldCfg.ErrorLogsMaxFiles != cfg.ErrorLogsMaxFiles) {

0 commit comments

Comments
 (0)