demo_extras.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // demo_extras.go — routerExtras canónicos + series sintéticas deterministas
  2. // (port de routerExtras.ts vía dataset.js, SPEC §7.1).
  3. package adapters
  4. import (
  5. "fmt"
  6. "math"
  7. "time"
  8. )
  9. // demoBandSplit es el {band24, band5, band6, cable} de los extras demo y del
  10. // desglose de clientes live por router (issue #645). Band6 existe para cuando
  11. // un dispositivo reporte clientes en 6 GHz.
  12. type demoBandSplit struct {
  13. Band24 int `json:"band24"`
  14. Band5 int `json:"band5"`
  15. Band6 int `json:"band6"`
  16. Cable int `json:"cable"`
  17. }
  18. // demoBackhaul es el objeto backhaul de los extras demo ({kind, headline, latencyMs}).
  19. type demoBackhaul struct {
  20. Kind string `json:"kind"`
  21. Headline string `json:"headline"`
  22. LatencyMs float64 `json:"latencyMs"`
  23. }
  24. // demoPortState es una entrada de extras.ports ({name, up, speed, role}).
  25. type demoPortState struct {
  26. Name string `json:"name"`
  27. Up bool `json:"up"`
  28. Speed string `json:"speed"`
  29. Role string `json:"role"`
  30. }
  31. // demoExtras es el objeto routerExtras por router (shape JSON exacto de
  32. // routerExtras.ts). Claves opcionales ausentes con omitempty (paridad
  33. // `undefined`): firmwareBase, firmwareAvailable, gatewayLatencyMs, backhaul.
  34. type demoExtras struct {
  35. MAC string `json:"mac"`
  36. Firmware string `json:"firmware"`
  37. FirmwareBase string `json:"firmwareBase,omitempty"`
  38. FirmwareUpdated bool `json:"firmwareUpdated"`
  39. FirmwareAvailable string `json:"firmwareAvailable,omitempty"`
  40. LastReboot string `json:"lastReboot"`
  41. Soc string `json:"soc"`
  42. Flash string `json:"flash"`
  43. RamMb int `json:"ramMb"`
  44. BandSplit demoBandSplit `json:"bandSplit"`
  45. TrafficNow float64 `json:"trafficNow"`
  46. GatewayLatencyMs *float64 `json:"gatewayLatencyMs,omitempty"`
  47. GatewayLatencySpark []float64 `json:"gatewayLatencySpark"`
  48. Backhaul *demoBackhaul `json:"backhaul,omitempty"`
  49. BackhaulSignal []float64 `json:"backhaulSignal"`
  50. Radios []Radio `json:"radios"`
  51. Ports []demoPortState `json:"ports"`
  52. EthPorts []EthPort `json:"ethPorts"`
  53. }
  54. func fptr(v float64) *float64 { return &v }
  55. // canonRouterExtras: port literal de routerExtras (dataset.js:661-781).
  56. func canonRouterExtras() map[string]*demoExtras {
  57. return map[string]*demoExtras{
  58. "flint2": {
  59. MAC: "94:83:C4:2A:7F:10", Firmware: "GL 4.7.0", FirmwareBase: "OpenWrt 21.02",
  60. FirmwareUpdated: true, LastReboot: "12 nov, 03:12 (mantenimiento)",
  61. Soc: "MediaTek MT7986A", Flash: "8 GB eMMC", RamMb: 512,
  62. BandSplit: demoBandSplit{Band24: 4, Band5: 10, Cable: 3},
  63. TrafficNow: 84.2,
  64. // GL-MT6000: 1× WAN 2.5G + 4× LAN 1G + 1× LAN 2.5G.
  65. // SPEC-CANON D4: lan1/lan2 = uplinks LLDP (Salón/Estudio),
  66. // lan3 = switch tonto inferido, lan4 = NAS, lan5 = pve 2.5G.
  67. GatewayLatencySpark: []float64{},
  68. BackhaulSignal: []float64{},
  69. Radios: []Radio{},
  70. Ports: []demoPortState{},
  71. EthPorts: []EthPort{
  72. {ID: "wan", Label: "WAN", Up: true, Speed: "2.5 Gbps", ConnectedTo: "ONT fibra · Digi", Detail: "84.122.x.x · full duplex"},
  73. {ID: "lan1", Label: "LAN 1", Up: true, Speed: "1 Gbps", ConnectedTo: "Salón · AX3000T", Detail: "Uplink AP Salón (192.168.8.2)"},
  74. {ID: "lan2", Label: "LAN 2", Up: true, Speed: "1 Gbps", ConnectedTo: "Estudio · NanoPi R4S", Detail: "Uplink AP Estudio (192.168.8.3)"},
  75. {ID: "lan3", Label: "LAN 3", Up: true, Speed: "1 Gbps", ConnectedTo: "Switch sin gestión", Detail: "Inferido por FDB · 8 MACs"},
  76. {ID: "lan4", Label: "LAN 4", Up: true, Speed: "1 Gbps", ConnectedTo: "NAS Synology", Detail: "192.168.8.10 · cable"},
  77. {ID: "lan5", Label: "LAN 5", Up: true, Speed: "2.5 Gbps", ConnectedTo: "Proxmox pve", Detail: "192.168.8.5 · hipervisor · 10 CT"},
  78. },
  79. },
  80. "living": {
  81. MAC: "50:D2:F5:11:8C:2E", Firmware: "OpenWrt 23.05.5",
  82. FirmwareUpdated: true, LastReboot: "12 nov, 03:12 (mantenimiento)",
  83. Soc: "MediaTek MT7981B", Flash: "128 MB NAND", RamMb: 256,
  84. BandSplit: demoBandSplit{Band24: 6, Band5: 10, Cable: 2},
  85. TrafficNow: 51.7, GatewayLatencyMs: fptr(1),
  86. GatewayLatencySpark: []float64{1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 2, 2, 1, 1, 1, 1, 1},
  87. Backhaul: &demoBackhaul{Kind: "cable", Headline: "Cable · 1 Gbps · full duplex", LatencyMs: 1},
  88. BackhaulSignal: []float64{58, 59, 60, 59, 58, 58, 60, 61, 60, 59, 60, 60, 61, 60, 59, 60, 61, 62, 61, 60, 60, 59, 60, 60},
  89. Radios: []Radio{
  90. {Name: "2.4 GHz", Channel: 6, WidthMhz: 20, PowerDbm: 20, Clients: 6},
  91. {Name: "5 GHz", Channel: 36, WidthMhz: 80, PowerDbm: 23, Clients: 10},
  92. },
  93. Ports: []demoPortState{
  94. {Name: "br-lan", Up: true, Speed: "—", Role: "Bridge LAN"},
  95. {Name: "eth0", Up: true, Speed: "1 Gbps", Role: "Uplink al Gateway"},
  96. {Name: "lan1", Up: true, Speed: "1 Gbps", Role: "PS5"},
  97. {Name: "phy0-ap0", Up: true, Speed: "—", Role: "Radio 2.4 GHz"},
  98. {Name: "phy1-ap0", Up: true, Speed: "—", Role: "Radio 5 GHz"},
  99. },
  100. // AX3000T: 1× WAN + 3× LAN (SPEC-CANON D4: lan1 = PS5,
  101. // lan3 = GS308E UP — el switch gestionado, dist-living-lan3)
  102. EthPorts: []EthPort{
  103. {ID: "wan", Label: "WAN", Up: true, Speed: "1 Gbps", ConnectedTo: "Uplink → Gateway", Detail: "Gateway · LAN 1 (192.168.8.1)"},
  104. {ID: "lan1", Label: "LAN 1", Up: true, Speed: "1 Gbps", ConnectedTo: "PS5", Detail: "192.168.8.31 · cable"},
  105. {ID: "lan2", Label: "LAN 2", Up: false},
  106. {ID: "lan3", Label: "LAN 3", Up: true, Speed: "1 Gbps", ConnectedTo: "GS308E", Detail: "Switch gestionado · 192.168.8.13 · LLDP"},
  107. },
  108. },
  109. "estudio": {
  110. MAC: "7A:1B:9C:03:F4:62", Firmware: "OpenWrt 23.05.5",
  111. FirmwareUpdated: false, FirmwareAvailable: "24.10.1", LastReboot: "2 dic, 21:40 (actualización)",
  112. Soc: "Rockchip RK3399", Flash: "32 GB microSD", RamMb: 1024,
  113. BandSplit: demoBandSplit{Band24: 4, Band5: 4, Cable: 1},
  114. TrafficNow: 9.4, GatewayLatencyMs: fptr(1),
  115. GatewayLatencySpark: []float64{1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1},
  116. Backhaul: &demoBackhaul{Kind: "cable", Headline: "Cable · 1 Gbps · full duplex", LatencyMs: 1},
  117. BackhaulSignal: []float64{55, 56, 56, 55, 55, 56, 57, 56, 55, 56, 56, 57, 56, 56, 55, 56, 57, 57, 56, 56, 56, 55, 56, 56},
  118. Radios: []Radio{
  119. {Name: "2.4 GHz", Channel: 11, WidthMhz: 20, PowerDbm: 18, Clients: 4},
  120. {Name: "5 GHz", Channel: 44, WidthMhz: 80, PowerDbm: 21, Clients: 4},
  121. },
  122. Ports: []demoPortState{
  123. {Name: "br-lan", Up: true, Speed: "—", Role: "Bridge LAN"},
  124. {Name: "eth0", Up: true, Speed: "1 Gbps", Role: "Uplink al Gateway"},
  125. {Name: "eth1", Up: true, Speed: "1 Gbps", Role: "Switch sin gestión"},
  126. {Name: "phy0-ap0", Up: true, Speed: "—", Role: "Radio 2.4 GHz"},
  127. {Name: "phy1-ap0", Up: true, Speed: "—", Role: "Radio 5 GHz"},
  128. },
  129. // NanoPi R4S: 2× 1G (WAN + LAN). SPEC-CANON D4: lan1 = switch sin
  130. // gestión — SOLO panel; la topología no lo muestra (los nodos
  131. // "inferred" solo se anclan en el gateway, ver topology.go).
  132. EthPorts: []EthPort{
  133. {ID: "wan", Label: "WAN", Up: true, Speed: "1 Gbps", ConnectedTo: "Uplink → Gateway", Detail: "Gateway · LAN 2 (192.168.8.1)"},
  134. {ID: "lan1", Label: "LAN 1", Up: true, Speed: "1 Gbps", ConnectedTo: "Switch sin gestión", Detail: "Switch 8 puertos · 4 en uso"},
  135. },
  136. },
  137. "patio": {
  138. MAC: "C0:4A:00:9B:51:8D", Firmware: "OpenWrt 23.05.5",
  139. FirmwareUpdated: false, FirmwareAvailable: "24.10.1", LastReboot: "9 dic, 14:05 (corte eléctrico)",
  140. Soc: "Qualcomm QCA9563", Flash: "16 MB SPI", RamMb: 128,
  141. BandSplit: demoBandSplit{Band24: 5, Band5: 1, Cable: 0},
  142. TrafficNow: 1.8, GatewayLatencyMs: fptr(2),
  143. GatewayLatencySpark: []float64{2, 2, 3, 2, 2, 2, 3, 2, 2, 3, 2, 2, 3, 3, 2, 2, 3, 4, 3, 2, 2, 2, 2, 2},
  144. Backhaul: &demoBackhaul{Kind: "wireless", Headline: "−58 dBm · 866 Mbps PHY", LatencyMs: 2},
  145. BackhaulSignal: []float64{-60, -61, -61, -62, -61, -60, -59, -60, -61, -60, -59, -58, -59, -60, -60, -59, -58, -57, -58, -59, -60, -59, -58, -58},
  146. Radios: []Radio{
  147. {Name: "2.4 GHz", Channel: 1, WidthMhz: 20, PowerDbm: 20, Clients: 5, Congested: true},
  148. {Name: "5 GHz", Channel: 149, WidthMhz: 80, PowerDbm: 22, Clients: 1},
  149. },
  150. Ports: []demoPortState{
  151. {Name: "br-lan", Up: true, Speed: "—", Role: "Bridge LAN"},
  152. {Name: "eth0", Up: false, Speed: "—", Role: "Puerto LAN (sin uso)"},
  153. {Name: "phy0-ap0", Up: true, Speed: "—", Role: "Radio 2.4 GHz"},
  154. {Name: "phy1-sta0", Up: true, Speed: "866 Mbps", Role: "Uplink wireless al Gateway"},
  155. },
  156. // EAP225: 1× 1G — sin uso (uplink inalámbrico mesh, ver backhaul)
  157. EthPorts: []EthPort{
  158. {ID: "lan1", Label: "LAN", Up: false, Detail: "Uplink por WiFi mesh"},
  159. },
  160. },
  161. }
  162. }
  163. // ---------------------------------------------------------------------------
  164. // perfSeries (dataset.js:816-845) — deterministas, terminan en el valor actual
  165. // ---------------------------------------------------------------------------
  166. // noise replica el ruido determinista 0..1 del JS.
  167. func noise(i int, seed int) float64 {
  168. x := math.Sin(float64(i)*127.1+float64(seed)*311.7) * 43758.5453
  169. return x - math.Floor(x)
  170. }
  171. // hourRangeLabels: etiquetas 1h — puntos cada 3 minutos terminando "ahora".
  172. func hourRangeLabels(n int) []string {
  173. labels := make([]string, 0, n)
  174. now := time.Now()
  175. for i := n - 1; i >= 0; i-- {
  176. d := now.Add(-time.Duration(i) * 3 * time.Minute)
  177. labels = append(labels, fmt.Sprintf("%02d:%02d", d.Hour(), d.Minute()))
  178. }
  179. return labels
  180. }
  181. var dayLabels = []string{"Lun", "Mar", "Mié", "Jue", "Vie", "Sáb", "Dom"}
  182. // canonRouterIDs es el orden canónico (seed = índice + 1).
  183. var canonRouterIDs = []string{"flint2", "living", "estudio", "patio"}
  184. // perfSeries replica perfSeries(router, range): range ∈ "1h"|"24h"|"7d".
  185. // cpu/ram/temp son los valores ACTUALES del router (canon o caminados).
  186. func perfSeries(routerID string, cpu, ram, temp int, rang string) []PerfPoint {
  187. seed := 1
  188. for i, id := range canonRouterIDs {
  189. if id == routerID {
  190. seed = i + 1
  191. }
  192. }
  193. isGateway := routerID == "flint2"
  194. var n int
  195. var labels []string
  196. switch rang {
  197. case "1h":
  198. n = 20
  199. labels = hourRangeLabels(n)
  200. case "24h":
  201. n = 24
  202. labels = make([]string, n)
  203. for i := range labels {
  204. labels[i] = fmt.Sprintf("%02d:00", i)
  205. }
  206. default: // "7d"
  207. n = 7
  208. labels = dayLabels
  209. }
  210. peakIdx := 4 // 7d
  211. if rang == "24h" {
  212. peakIdx = 21
  213. } else if rang == "1h" {
  214. peakIdx = 15
  215. }
  216. cpuPeak := cpu + 34
  217. if cpuPeak > 88 {
  218. cpuPeak = 88
  219. }
  220. if isGateway {
  221. cpuPeak = 61
  222. }
  223. tempPeak := temp + 4
  224. if isGateway {
  225. tempPeak = 58
  226. }
  227. points := make([]PerfPoint, 0, n)
  228. for i := 0; i < n; i++ {
  229. var dayShape float64
  230. if rang == "7d" {
  231. dayShape = 0.7 + 0.3*(float64(i)/float64(n-1))
  232. } else {
  233. s := math.Sin((float64(i)-6)/float64(n)*math.Pi*2 - math.Pi/2)
  234. dayShape = 0.55 + 0.45*s*s
  235. }
  236. wob := noise(i, seed)
  237. c := math.Max(2, math.Round(float64(cpu)*dayShape+wob*6))
  238. r := math.Max(10, math.Round(float64(ram)-6+wob*10+(float64(i)/float64(n))*4))
  239. t := math.Max(30, math.Round(float64(temp)-5+wob*4+dayShape*4))
  240. if i == peakIdx {
  241. c = float64(cpuPeak)
  242. t = float64(tempPeak)
  243. }
  244. points = append(points, PerfPoint{T: labels[i], CPU: c, RAM: r, Temp: t})
  245. }
  246. // La serie termina SIEMPRE en el valor actual del router.
  247. points[n-1] = PerfPoint{T: labels[n-1], CPU: float64(cpu), RAM: float64(ram), Temp: float64(temp)}
  248. return points
  249. }
  250. // ---------------------------------------------------------------------------
  251. // Latencia WAN 24h (canon: pico 34 ms a las 21:00)
  252. // ---------------------------------------------------------------------------
  253. func canonWANLatency24h() []float64 {
  254. return []float64{9, 8, 8, 7, 7, 8, 9, 10, 11, 12, 11, 12, 13, 12, 11, 12, 14, 16, 19, 24, 29, 34, 14, 8}
  255. }
  256. func canonWANLatencyStats() WANLatencyStats {
  257. return WANLatencyStats{AvgMs: 11, JitterMs: 2.1, LossPct: 0}
  258. }
  259. // ---------------------------------------------------------------------------
  260. // adguardSeries24h (buildAdGuardSeries, dataset.js:862-884): suma exacta del
  261. // canon (84312 / 15687), punto 21:00 fijado a 5412/1031, drift en la hora 20.
  262. // ---------------------------------------------------------------------------
  263. func buildAdGuardSeries() []AdGuardHour {
  264. weights := []float64{0.35, 0.22, 0.15, 0.12, 0.12, 0.18, 0.35, 0.6, 0.8, 0.9, 1, 1.05, 1.1, 1.05, 1, 1.05, 1.15, 1.3, 1.5, 1.7, 1.9, 2.1, 1.4, 0.7}
  265. const totalQ, totalB = 84312, 15687
  266. sumW := 0.0
  267. for _, w := range weights {
  268. sumW += w
  269. }
  270. q := make([]int64, len(weights))
  271. for i, w := range weights {
  272. q[i] = int64(math.Round(w / sumW * totalQ))
  273. }
  274. q[21] = 5412
  275. var sumQ int64
  276. for _, v := range q {
  277. sumQ += v
  278. }
  279. q[20] += totalQ - sumQ // drift absorbido en la hora 20
  280. b := make([]int64, len(weights))
  281. for i, v := range q {
  282. b[i] = int64(math.Round(float64(v) * (float64(totalB) / float64(totalQ))))
  283. }
  284. b[21] = 1031
  285. var sumB int64
  286. for _, v := range b {
  287. sumB += v
  288. }
  289. b[20] += totalB - sumB
  290. out := make([]AdGuardHour, len(weights))
  291. for i := range weights {
  292. out[i] = AdGuardHour{T: fmt.Sprintf("%02d:00", i), Permitidas: q[i] - b[i], Bloqueadas: b[i]}
  293. }
  294. return out
  295. }
  296. // ---------------------------------------------------------------------------
  297. // WireGuard extras (wgPeerExtras + WG_TOTALS_30D)
  298. // ---------------------------------------------------------------------------
  299. // wgPeerExtra es {endpoint, allowedIps, lastIp} por peer (shape routerExtras.ts).
  300. type wgPeerExtra struct {
  301. Endpoint string `json:"endpoint"`
  302. AllowedIPs string `json:"allowedIps"`
  303. LastIP string `json:"lastIp"`
  304. }
  305. func canonWGPeerExtras() map[string]wgPeerExtra {
  306. return map[string]wgPeerExtra{
  307. "pixel-8-pro": {Endpoint: "5.224.x.x:51820", AllowedIPs: "10.0.0.2/32", LastIP: "5.224.x.x"},
  308. "macbook-air": {Endpoint: "92.58.x.x:51820", AllowedIPs: "10.0.0.3/32", LastIP: "92.58.x.x"},
  309. "ipad-air": {Endpoint: "—", AllowedIPs: "10.0.0.4/32", LastIP: "81.34.x.x"},
  310. "portatil-trabajo": {Endpoint: "—", AllowedIPs: "10.0.0.5/32", LastIP: "80.102.x.x"},
  311. "casa-familia": {Endpoint: "—", AllowedIPs: "10.0.0.6/32", LastIP: "95.60.x.x"},
  312. }
  313. }