(w http.ResponseWriter, r *http.Request, userID string)
| 911 | } |
| 912 | |
| 913 | func (a *userAPI) handleNodeUsage(w http.ResponseWriter, r *http.Request, userID string) { |
| 914 | if r.Method != http.MethodGet { |
| 915 | writeJSON(w, http.StatusMethodNotAllowed, map[string]any{"error": "method not allowed"}) |
| 916 | return |
| 917 | } |
| 918 | rawUsage, err := a.users.ListUserNodeUsage(userID) |
| 919 | if err != nil { |
| 920 | internalError(w, r, err) |
| 921 | return |
| 922 | } |
| 923 | nodeList, _ := a.nodes.List() |
| 924 | nodeNames := make(map[string]string, len(nodeList)) |
| 925 | for _, n := range nodeList { |
| 926 | nodeNames[n.ID] = n.Name |
| 927 | } |
| 928 | type nodeUsageItem struct { |
| 929 | NodeID string `json:"node_id"` |
| 930 | NodeName string `json:"node_name"` |
| 931 | UploadBytes int64 `json:"upload_bytes"` |
| 932 | DownloadBytes int64 `json:"download_bytes"` |
| 933 | TotalBytes int64 `json:"total_bytes"` |
| 934 | } |
| 935 | items := make([]nodeUsageItem, 0, len(rawUsage)) |
| 936 | for _, u := range rawUsage { |
| 937 | t := u.UploadBytes + u.DownloadBytes |
| 938 | if t == 0 { |
| 939 | continue |
| 940 | } |
| 941 | name := nodeNames[u.NodeID] |
| 942 | if name == "" { |
| 943 | name = u.NodeID |
| 944 | } |
| 945 | items = append(items, nodeUsageItem{ |
| 946 | NodeID: u.NodeID, |
| 947 | NodeName: name, |
| 948 | UploadBytes: u.UploadBytes, |
| 949 | DownloadBytes: u.DownloadBytes, |
| 950 | TotalBytes: t, |
| 951 | }) |
| 952 | } |
| 953 | writeJSON(w, http.StatusOK, map[string]any{"usage": items}) |
| 954 | } |
no test coverage detected