networkinsight.go 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. package opencost
  2. import (
  3. "fmt"
  4. "math"
  5. "strings"
  6. "sync"
  7. "time"
  8. "github.com/opencost/opencost/core/pkg/filter"
  9. "github.com/opencost/opencost/core/pkg/filter/ast"
  10. "github.com/opencost/opencost/core/pkg/filter/matcher"
  11. "github.com/opencost/opencost/core/pkg/log"
  12. "github.com/opencost/opencost/core/pkg/util/timeutil"
  13. )
  14. const (
  15. NetworkInsightsServiceUnknown = "unknownService"
  16. NetworkInsightsNamespace = "namespace"
  17. NetworkInsightsCluster = "cluster"
  18. NetworkInsightsPod = "pod"
  19. )
  20. type NetworkInsightProperty string
  21. func ConvertNetworkInsightPropertiesToString(nips []NetworkInsightProperty) string {
  22. aggString := make([]string, len(nips))
  23. for i, agg := range nips {
  24. aggString[i] = string(agg)
  25. }
  26. return strings.Join(aggString, "/")
  27. }
  28. // Alias for network traffic direction string
  29. type NetworkTrafficDirection string
  30. const (
  31. NetworkTrafficDirectionNone NetworkTrafficDirection = ""
  32. NetworkTrafficDirectionEgress NetworkTrafficDirection = "Egress"
  33. NetworkTrafficDirectionIngress NetworkTrafficDirection = "Ingress"
  34. )
  35. // Alias for network traffic type string
  36. type NetworkTrafficType string
  37. const (
  38. NetworkTrafficTypeNone NetworkTrafficType = ""
  39. NetworkTrafficTypeCrossZone NetworkTrafficType = "CrossZone"
  40. NetworkTrafficTypeCrossRegion NetworkTrafficType = "CrossRegion"
  41. NetworkTrafficTypeInternet NetworkTrafficType = "Internet"
  42. )
  43. // Struct to store the filter options applied on network
  44. // interaction in networkDetails of network insight
  45. type NetworkDetailsOptions struct {
  46. ShowZeroCost bool
  47. FilterNetworkDetails filter.Filter
  48. }
  49. // struct to lowest level Ingress and Egress details, interaction
  50. // with the endPoint, which is a source in case of Ingress and
  51. // destination in case of Egress and also stores Traffic type property,
  52. // which describes the traffic as either Internet, Cross Region or Cross Zone
  53. type NetworkDetail struct {
  54. Cost float64 `json:"cost"`
  55. Bytes float64 `json:"bytes"`
  56. EndPoint string `json:"endPoint"`
  57. TrafficDirection NetworkTrafficDirection `json:"trafficDirection"`
  58. TrafficType NetworkTrafficType `json:"trafficType"`
  59. }
  60. func NewNetworkDetail(cost float64,
  61. bytes float64, endPoint string,
  62. trafficDirection NetworkTrafficDirection, trafficType NetworkTrafficType) *NetworkDetail {
  63. return &NetworkDetail{
  64. Cost: cost,
  65. Bytes: bytes,
  66. EndPoint: endPoint,
  67. TrafficDirection: trafficDirection,
  68. TrafficType: trafficType,
  69. }
  70. }
  71. func (nd *NetworkDetail) Clone() *NetworkDetail {
  72. return &NetworkDetail{
  73. Cost: nd.Cost,
  74. Bytes: nd.Bytes,
  75. EndPoint: nd.EndPoint,
  76. TrafficDirection: nd.TrafficDirection,
  77. TrafficType: nd.TrafficType,
  78. }
  79. }
  80. func (nd *NetworkDetail) Key() string {
  81. if nd == nil {
  82. return ""
  83. }
  84. return fmt.Sprintf("%s/%s/%s", nd.EndPoint, nd.TrafficDirection, nd.TrafficType)
  85. }
  86. func (nd *NetworkDetail) Add(that *NetworkDetail) {
  87. if nd == nil {
  88. return
  89. }
  90. if nd.Key() != that.Key() {
  91. log.Warnf("adding two NetworkDetail that dont match with key %s: %s", nd.Key(), that.Key())
  92. }
  93. nd.Cost += that.Cost
  94. nd.Bytes += that.Bytes
  95. }
  96. func (nd *NetworkDetail) SanitizeNaN() {
  97. if math.IsNaN(nd.Cost) {
  98. log.DedupedWarningf(5, "NetworkDetail: Unexpected NaN found for Cost: name: %s", nd.Key())
  99. nd.Cost = 0
  100. }
  101. if math.IsNaN(nd.Bytes) {
  102. log.DedupedWarningf(5, "NetworkDetail: Unexpected NaN found for Bytes: name: %s", nd.Key())
  103. nd.Bytes = 0
  104. }
  105. }
  106. func (nd *NetworkDetail) IsZeroCost() bool {
  107. if nd == nil {
  108. log.DedupedWarningf(5, "nd.IsZeroCost called on a nil network detail")
  109. return false
  110. }
  111. return nd.Cost == 0.0
  112. }
  113. type NetworkDetailsSet map[string]*NetworkDetail
  114. func (nds NetworkDetailsSet) Clone() NetworkDetailsSet {
  115. retnids := make(NetworkDetailsSet, 0)
  116. for name, nid := range nds {
  117. retnids[name] = nid.Clone()
  118. }
  119. return retnids
  120. }
  121. func (nds NetworkDetailsSet) Add(nd *NetworkDetail) {
  122. key := nd.Key()
  123. if _, ok := nds[key]; ok {
  124. nds[key].Add(nd)
  125. } else {
  126. nds[key] = nd.Clone()
  127. }
  128. }
  129. // GetTotalInternetCost Gets the total internet cost in egress details i.e has internet flag true
  130. func (nds NetworkDetailsSet) GetTotalInternetCost() float64 {
  131. totalCost := 0.0
  132. for _, nd := range nds {
  133. if nd.TrafficType == NetworkTrafficTypeInternet {
  134. totalCost += nd.Cost
  135. }
  136. }
  137. return totalCost
  138. }
  139. // Gets the total cross zone cost in egress details i.e has sameZone flag false
  140. func (nds NetworkDetailsSet) GetCrossZoneCost() float64 {
  141. totalCost := 0.0
  142. for _, nd := range nds {
  143. if nd.TrafficType == NetworkTrafficTypeCrossZone {
  144. totalCost += nd.Cost
  145. }
  146. }
  147. return totalCost
  148. }
  149. // Gets the total cross region cost in egress details i.e has sameRegion flag false
  150. func (nds NetworkDetailsSet) GetCrossRegionCost() float64 {
  151. totalCost := 0.0
  152. for _, nd := range nds {
  153. if nd.TrafficType == NetworkTrafficTypeCrossRegion {
  154. totalCost += nd.Cost
  155. }
  156. }
  157. return totalCost
  158. }
  159. func (nds NetworkDetailsSet) Combine(that NetworkDetailsSet) {
  160. for _, nd := range that {
  161. nds.Add(nd)
  162. }
  163. }
  164. func (nds NetworkDetailsSet) SanitizeNaN() {
  165. for _, nd := range nds {
  166. nd.SanitizeNaN()
  167. }
  168. }
  169. // filterZeroCost returns a new NetworkDetailsSet with all zero-cost details removed
  170. func (nds NetworkDetailsSet) filterZeroCost() NetworkDetailsSet {
  171. newNds := make(map[string]*NetworkDetail, 0)
  172. for key, nd := range nds {
  173. if nd.IsZeroCost() {
  174. continue
  175. }
  176. newNds[key] = nd.Clone()
  177. }
  178. return newNds
  179. }
  180. // NetworkInsight struct that stores pod interactions both egress and ingress
  181. // Currently only Cluster, namespace and pod will be populated from promsource.go
  182. // Rest are placeholders if we need to support single cluster via cost-model
  183. // using the same prometheus source.In aggregator we will join allocation of
  184. // same time window to get the controller, node, labels, region and zone data.
  185. type NetworkInsight struct {
  186. Cluster string `json:"cluster"`
  187. Namespace string `json:"namespace"`
  188. Controller string `json:"controller"`
  189. Pod string `json:"pod"`
  190. Node string `json:"node"`
  191. Labels map[string]string `json:"labels"`
  192. Region string `json:"region"`
  193. Zone string `json:"zone"`
  194. NetworkTotalCost float64 `json:"networkCost"`
  195. NetworkCrossZoneCost float64 `json:"networkCrossZoneCost"`
  196. NetworkCrossRegionCost float64 `json:"networkCrossRegionCost"`
  197. NetworkInternetCost float64 `json:"networkInternetCost"`
  198. NetworkDetails NetworkDetailsSet `json:"networkDetails"`
  199. }
  200. func NewNetworkInsight(cluster string,
  201. namespace string, controller string, pod string, node string,
  202. labels map[string]string, region string, zone string,
  203. networkTotalCost, networkCrossZoneCost, networkCrossRegionCost, networkInternetCost float64,
  204. networkDetails map[string]*NetworkDetail) *NetworkInsight {
  205. if networkDetails == nil {
  206. networkDetails = make(map[string]*NetworkDetail, 0)
  207. }
  208. return &NetworkInsight{
  209. Cluster: cluster,
  210. Namespace: namespace,
  211. Controller: controller,
  212. Pod: pod,
  213. Node: node,
  214. Labels: labels,
  215. Region: region,
  216. Zone: zone,
  217. NetworkTotalCost: networkTotalCost,
  218. NetworkCrossZoneCost: networkCrossZoneCost,
  219. NetworkCrossRegionCost: networkCrossRegionCost,
  220. NetworkInternetCost: networkInternetCost,
  221. NetworkDetails: networkDetails,
  222. }
  223. }
  224. func (ni *NetworkInsight) Clone() *NetworkInsight {
  225. if ni == nil {
  226. return nil
  227. }
  228. return &NetworkInsight{
  229. Cluster: ni.Cluster,
  230. Namespace: ni.Namespace,
  231. Pod: ni.Pod,
  232. Node: ni.Node,
  233. Labels: ni.Labels,
  234. Region: ni.Region,
  235. Zone: ni.Zone,
  236. NetworkTotalCost: ni.NetworkTotalCost,
  237. NetworkCrossZoneCost: ni.NetworkCrossZoneCost,
  238. NetworkCrossRegionCost: ni.NetworkCrossRegionCost,
  239. NetworkInternetCost: ni.NetworkInternetCost,
  240. NetworkDetails: ni.NetworkDetails.Clone(),
  241. }
  242. }
  243. func (ni *NetworkInsight) add(that *NetworkInsight) {
  244. if ni == nil {
  245. log.Warnf("NetworkInsight.Add: trying to add a nil receiver")
  246. return
  247. }
  248. if ni.Cluster != that.Cluster {
  249. ni.Cluster = ""
  250. }
  251. if ni.Namespace != that.Namespace {
  252. ni.Namespace = ""
  253. }
  254. if ni.Pod != that.Pod {
  255. ni.Pod = ""
  256. }
  257. if ni.Controller != that.Controller {
  258. ni.Controller = ""
  259. }
  260. if ni.Node != that.Node {
  261. ni.Node = ""
  262. }
  263. if ni.Region != that.Region {
  264. ni.Region = ""
  265. }
  266. if ni.Zone != that.Zone {
  267. ni.Zone = ""
  268. }
  269. // TO-DO: Check for labels match if we support label in single cluster!
  270. ni.NetworkTotalCost += that.NetworkTotalCost
  271. ni.NetworkCrossZoneCost += that.NetworkCrossZoneCost
  272. ni.NetworkCrossRegionCost += that.NetworkCrossRegionCost
  273. ni.NetworkInternetCost += that.NetworkInternetCost
  274. ni.NetworkDetails.Combine(that.NetworkDetails)
  275. }
  276. // Key takes a list of NetworkInsightProperty and creates a "/"
  277. // seperated key based on the values of the requested properties.
  278. // Invalid values and empty slice are set to default key.
  279. func (ni *NetworkInsight) Key(props []NetworkInsightProperty) (string, error) {
  280. defaultString := fmt.Sprintf("%s/%s/%s", ni.Cluster, ni.Namespace, ni.Pod)
  281. if len(props) == 0 {
  282. return defaultString, nil
  283. }
  284. values := make([]string, len(props))
  285. for i, prop := range props {
  286. switch prop {
  287. case NetworkInsightsNamespace:
  288. values[i] = ni.Namespace
  289. case NetworkInsightsPod:
  290. values[i] = ni.Pod
  291. case NetworkInsightsCluster:
  292. values[i] = ni.Cluster
  293. default:
  294. return defaultString, nil
  295. }
  296. }
  297. return strings.Join(values, "/"), nil
  298. }
  299. func (ni *NetworkInsight) GetTotalEgressByte() float64 {
  300. totalByte := 0.0
  301. for _, nd := range ni.NetworkDetails {
  302. if nd == nil || nd.TrafficDirection != NetworkTrafficDirectionEgress {
  303. continue
  304. }
  305. totalByte += nd.Bytes
  306. }
  307. return totalByte
  308. }
  309. func (ni *NetworkInsight) GetTotalIngressByte() float64 {
  310. totalByte := 0.0
  311. for _, nd := range ni.NetworkDetails {
  312. if nd == nil || nd.TrafficDirection != NetworkTrafficDirectionIngress {
  313. continue
  314. }
  315. totalByte += nd.Bytes
  316. }
  317. return totalByte
  318. }
  319. func (ni *NetworkInsight) SanitizeNaN() {
  320. if ni == nil {
  321. return
  322. }
  323. key, err := ni.Key([]NetworkInsightProperty{})
  324. if err != nil {
  325. log.DedupedWarningf(5, "NetworkInsight: unable to perform santization of network insight for cluster: %s, namespace: %s, pod: %s", ni.Cluster, ni.Namespace, ni.Pod)
  326. }
  327. if math.IsNaN(ni.NetworkTotalCost) {
  328. log.DedupedWarningf(5, "NetworkInsight: Unexpected NaN found for NetworkTotalCost: name: %s", key)
  329. ni.NetworkTotalCost = 0
  330. }
  331. if math.IsNaN(ni.NetworkCrossZoneCost) {
  332. log.DedupedWarningf(5, "NetworkInsight: Unexpected NaN found for NetworkCrossZoneCost: name: %s", key)
  333. ni.NetworkCrossZoneCost = 0
  334. }
  335. if math.IsNaN(ni.NetworkCrossRegionCost) {
  336. log.DedupedWarningf(5, "NetworkInsight: Unexpected NaN found for NetworkCrossRegionCost: name: %s", key)
  337. ni.NetworkCrossRegionCost = 0
  338. }
  339. if math.IsNaN(ni.NetworkInternetCost) {
  340. log.DedupedWarningf(5, "NetworkInsight: Unexpected NaN found for NetworkInternetCost: name: %s", key)
  341. ni.NetworkInternetCost = 0
  342. }
  343. ni.NetworkDetails.SanitizeNaN()
  344. }
  345. func (ni *NetworkInsight) filterZeroCost() {
  346. if ni == nil {
  347. return
  348. }
  349. ni.NetworkDetails = ni.NetworkDetails.filterZeroCost()
  350. }
  351. func (ni *NetworkInsight) filterNetworkDetails(networkDetailFilter NetworkInsightDetailMatcher) {
  352. if ni == nil {
  353. log.DedupedWarningf(5, "NetworkInsight:filterNetworkDetails called on nil network insight")
  354. return
  355. }
  356. newNds := make(NetworkDetailsSet, 0)
  357. for key, nd := range ni.NetworkDetails {
  358. if networkDetailFilter.Matches(nd) {
  359. newNds[key] = nd
  360. }
  361. }
  362. ni.NetworkDetails = newNds
  363. }
  364. // SetWithNetworkInsightProperty sets the corresponding property
  365. // variable in the struct with the value passed to the function.
  366. func (ni *NetworkInsight) SetWithNetworkInsightProperty(property NetworkInsightProperty, value interface{}) error {
  367. switch property {
  368. case NetworkInsightsCluster:
  369. ni.Cluster = value.(string)
  370. case NetworkInsightsNamespace:
  371. ni.Namespace = value.(string)
  372. case NetworkInsightsPod:
  373. ni.Pod = value.(string)
  374. }
  375. return fmt.Errorf("unsupported property: %s", string(property))
  376. }
  377. type NetworkInsightSet struct {
  378. NetworkInsights map[string]*NetworkInsight `json:"networkInsights"`
  379. Window Window `json:"window"`
  380. }
  381. // NewNetworkInsightSet instantiates a new NetworkInsights set and, optionally, inserts
  382. // the given list of NetworkInsight
  383. func NewNetworkInsightSet(start, end time.Time, networkInsight ...*NetworkInsight) *NetworkInsightSet {
  384. nis := &NetworkInsightSet{
  385. NetworkInsights: make(map[string]*NetworkInsight, 0),
  386. Window: NewWindow(&start, &end),
  387. }
  388. for _, ni := range networkInsight {
  389. nis.Insert(ni, []NetworkInsightProperty{})
  390. }
  391. return nis
  392. }
  393. func (nis *NetworkInsightSet) Add(that *NetworkInsightSet, keyProperties []NetworkInsightProperty) (*NetworkInsightSet, error) {
  394. if (nis == nil || len(nis.NetworkInsights) == 0) && (that == nil || len(that.NetworkInsights) == 0) {
  395. return nis, nil
  396. }
  397. if nis == nil || len(nis.NetworkInsights) == 0 {
  398. return that, nil
  399. }
  400. if that == nil || len(that.NetworkInsights) == 0 {
  401. return that, nil
  402. }
  403. start := *nis.Window.Start()
  404. end := *nis.Window.End()
  405. if that.Window.Start().Before(start) {
  406. start = *that.Window.Start()
  407. }
  408. if that.Window.End().After(end) {
  409. end = *that.Window.End()
  410. }
  411. acc := &NetworkInsightSet{
  412. NetworkInsights: make(map[string]*NetworkInsight, len(nis.NetworkInsights)),
  413. Window: NewClosedWindow(start, end),
  414. }
  415. for _, ni := range nis.NetworkInsights {
  416. err := acc.Insert(ni, keyProperties)
  417. if err != nil {
  418. return nil, err
  419. }
  420. }
  421. for _, ni := range that.NetworkInsights {
  422. err := acc.Insert(ni, keyProperties)
  423. if err != nil {
  424. return nil, err
  425. }
  426. }
  427. return acc, nil
  428. }
  429. func (nis *NetworkInsightSet) Insert(that *NetworkInsight, aggregateBy []NetworkInsightProperty) error {
  430. if nis == nil {
  431. return fmt.Errorf("cannot insert into nil networkInsightSet")
  432. }
  433. if nis.NetworkInsights == nil {
  434. nis.NetworkInsights = map[string]*NetworkInsight{}
  435. }
  436. key, err := that.Key(aggregateBy)
  437. if err != nil {
  438. return fmt.Errorf("unable to generate key for aggregation: %v", err)
  439. }
  440. if _, ok := nis.NetworkInsights[key]; !ok {
  441. nis.NetworkInsights[key] = that
  442. } else {
  443. nis.NetworkInsights[key].add(that)
  444. }
  445. return nil
  446. }
  447. func (nis *NetworkInsightSet) Clone() *NetworkInsightSet {
  448. if nis == nil {
  449. return nil
  450. }
  451. networkInsights := make(map[string]*NetworkInsight, len(nis.NetworkInsights))
  452. for k, v := range nis.NetworkInsights {
  453. networkInsights[k] = v.Clone()
  454. }
  455. return &NetworkInsightSet{
  456. NetworkInsights: networkInsights,
  457. Window: nis.Window.Clone(),
  458. }
  459. }
  460. func (nis *NetworkInsightSet) GetWindow() Window {
  461. return nis.Window
  462. }
  463. func (nis *NetworkInsightSet) IsValid() bool {
  464. if !nis.IsEmpty() {
  465. return false
  466. }
  467. if nis.Window.IsOpen() {
  468. return false
  469. }
  470. return true
  471. }
  472. func (nis *NetworkInsightSet) IsEmpty() bool {
  473. if nis == nil || len(nis.NetworkInsights) == 0 {
  474. return true
  475. }
  476. return false
  477. }
  478. func (nis *NetworkInsightSet) AggregateBy(aggregateBy []NetworkInsightProperty) error {
  479. if nis.IsEmpty() {
  480. return nil
  481. }
  482. aggSet := &NetworkInsightSet{}
  483. for _, ni := range nis.NetworkInsights {
  484. err := aggSet.Insert(ni, aggregateBy)
  485. if err != nil {
  486. return fmt.Errorf("NetworkInsightSet:AggregateBy failed with err: %v", err)
  487. }
  488. }
  489. nis.NetworkInsights = aggSet.NetworkInsights
  490. return nil
  491. }
  492. func (nis *NetworkInsightSet) Accumulate(that *NetworkInsightSet, keyProperties []NetworkInsightProperty) (*NetworkInsightSet, error) {
  493. if nis.IsEmpty() {
  494. return that.Clone(), nil
  495. }
  496. if that.IsEmpty() {
  497. return nis.Clone(), nil
  498. }
  499. start := nis.Window.Start()
  500. end := nis.Window.End()
  501. if start.After(*that.Window.Start()) {
  502. start = that.Window.Start()
  503. }
  504. if end.Before(*that.Window.End()) {
  505. end = that.Window.End()
  506. }
  507. newNis := nis.Clone()
  508. newNis.Window = NewClosedWindow(*start, *end)
  509. for _, ni := range that.NetworkInsights {
  510. err := newNis.Insert(ni, keyProperties)
  511. if err != nil {
  512. return nil, err
  513. }
  514. }
  515. return newNis, nil
  516. }
  517. func (nis *NetworkInsightSet) Length() int {
  518. if nis == nil {
  519. return 0
  520. }
  521. return len(nis.NetworkInsights)
  522. }
  523. func (nis *NetworkInsightSet) FilterOn(filter filter.Filter) error {
  524. if nis.IsEmpty() {
  525. return fmt.Errorf("NetworkInsightSet:FilterOn called on empty network insight set")
  526. }
  527. var networkInsightFilter NetworkInsightMatcher
  528. if filter == nil {
  529. networkInsightFilter = &matcher.AllPass[*NetworkInsight]{}
  530. } else {
  531. compiler := NewNetworkInsightMatchCompiler()
  532. var err error
  533. networkInsightFilter, err = compiler.Compile(filter)
  534. if err != nil {
  535. return fmt.Errorf("compiling filter '%s': %w", ast.ToPreOrderShortString(filter), err)
  536. }
  537. }
  538. if networkInsightFilter == nil {
  539. return fmt.Errorf("unexpected nil filter")
  540. }
  541. for key, ni := range nis.NetworkInsights {
  542. if ni == nil {
  543. continue
  544. }
  545. if !networkInsightFilter.Matches(ni) {
  546. delete(nis.NetworkInsights, key)
  547. }
  548. }
  549. return nil
  550. }
  551. // Resolution returns the NetworkInsightSet's window duration
  552. func (nis *NetworkInsightSet) Resolution() time.Duration {
  553. if nis == nil {
  554. return time.Duration(0)
  555. }
  556. return nis.Window.Duration()
  557. }
  558. func (nis *NetworkInsightSet) FilterNetworkDetails(opts *NetworkDetailsOptions) error {
  559. if nis == nil {
  560. return fmt.Errorf("filterNetworkDetails called on nil network insight set")
  561. }
  562. if opts == nil {
  563. return nil
  564. }
  565. var networkDetailFilter NetworkInsightDetailMatcher
  566. if opts.FilterNetworkDetails == nil {
  567. networkDetailFilter = &matcher.AllPass[*NetworkDetail]{}
  568. } else {
  569. compiler := NewNetworkInsightDetailMatchCompiler()
  570. var err error
  571. networkDetailFilter, err = compiler.Compile(opts.FilterNetworkDetails)
  572. if err != nil {
  573. return fmt.Errorf("compiling filter '%s': %w", ast.ToPreOrderShortString(opts.FilterNetworkDetails), err)
  574. }
  575. }
  576. if networkDetailFilter == nil {
  577. return fmt.Errorf("unexpected nil filter")
  578. }
  579. for _, ni := range nis.NetworkInsights {
  580. // filter network details that satisfy the
  581. // network detail filter
  582. ni.filterNetworkDetails(networkDetailFilter)
  583. // filter zero cost network details
  584. if !opts.ShowZeroCost {
  585. ni.filterZeroCost()
  586. }
  587. }
  588. return nil
  589. }
  590. func (nis *NetworkInsightSet) SanitizeNaN() {
  591. if nis == nil {
  592. return
  593. }
  594. for _, ni := range nis.NetworkInsights {
  595. ni.SanitizeNaN()
  596. }
  597. }
  598. type NetworkInsightSetRange struct {
  599. sync.RWMutex
  600. NetworkInsightsSet []*NetworkInsightSet `json:"networkInsightSet"`
  601. Window Window `json:"window"`
  602. }
  603. func NewNetworkInsightSetRange(window Window, nis ...*NetworkInsightSet) *NetworkInsightSetRange {
  604. return &NetworkInsightSetRange{
  605. NetworkInsightsSet: nis,
  606. Window: window,
  607. }
  608. }
  609. func (nisr *NetworkInsightSetRange) AggregateBy(aggregateBy []NetworkInsightProperty) error {
  610. if nisr == nil || len(nisr.NetworkInsightsSet) == 0 {
  611. return nil
  612. }
  613. if nisr.Window.IsOpen() {
  614. return fmt.Errorf("cannot aggregate a NetworkInsightSetRange with an open window")
  615. }
  616. tempNis := &NetworkInsightSetRange{NetworkInsightsSet: []*NetworkInsightSet{}}
  617. for _, ni := range nisr.NetworkInsightsSet {
  618. err := ni.AggregateBy(aggregateBy)
  619. if err != nil {
  620. return err
  621. }
  622. tempNis.NetworkInsightsSet = append(tempNis.NetworkInsightsSet, ni)
  623. }
  624. nisr.NetworkInsightsSet = tempNis.NetworkInsightsSet
  625. return nil
  626. }
  627. func (nisr *NetworkInsightSetRange) Append(that *NetworkInsightSet) {
  628. if nisr == nil {
  629. log.DedupedWarningf(5, "NetworkInsightSetRange:Append called on nil Network Insight Set Range")
  630. return
  631. }
  632. nisr.Lock()
  633. defer nisr.Unlock()
  634. nisr.NetworkInsightsSet = append(nisr.NetworkInsightsSet, that)
  635. // Adjust window
  636. start := nisr.Window.Start()
  637. end := nisr.Window.End()
  638. if nisr.Window.Start() == nil || (that.Window.Start() != nil && that.Window.Start().Before(*nisr.Window.Start())) {
  639. start = that.Window.Start()
  640. }
  641. if nisr.Window.End() == nil || (that.Window.End() != nil && that.Window.End().After(*nisr.Window.End())) {
  642. end = that.Window.End()
  643. }
  644. nisr.Window = NewClosedWindow(*start, *end)
  645. }
  646. func (nisr *NetworkInsightSetRange) Clone() *NetworkInsightSetRange {
  647. if nisr == nil {
  648. return nil
  649. }
  650. nisrClone := NewNetworkInsightSetRange(nisr.Window)
  651. for _, nis := range nisr.NetworkInsightsSet {
  652. nisClone := nis.Clone()
  653. nisrClone.Append(nisClone)
  654. }
  655. return nisrClone
  656. }
  657. func (nisr *NetworkInsightSetRange) accumulateByNone(keyProperties []NetworkInsightProperty) (*NetworkInsightSetRange, error) {
  658. return nisr.Clone(), nil
  659. }
  660. func (nisr *NetworkInsightSetRange) accumulateByAll(keyProperties []NetworkInsightProperty) (*NetworkInsightSetRange, error) {
  661. nis, err := nisr.newAccumulation(keyProperties)
  662. if err != nil {
  663. return nil, fmt.Errorf("error accumulating NetworkInsightSetRange:%w", err)
  664. }
  665. accumulated := NewNetworkInsightSetRange(nisr.Window, nis)
  666. return accumulated, nil
  667. }
  668. func (nisr *NetworkInsightSetRange) accumulateByHour(keyProperties []NetworkInsightProperty) (*NetworkInsightSetRange, error) {
  669. if nisr == nil {
  670. return nil, fmt.Errorf("NetworkInsightSetRange:accumulateByHour called on nil set range")
  671. }
  672. // ensure that the network insight set have a 1-hour window and if a set exists
  673. duration := nisr.Window.Duration()
  674. if len(nisr.NetworkInsightsSet) > 0 && duration != time.Hour {
  675. return nil, fmt.Errorf("window duration must equal 1 hour; got:%s", duration.String())
  676. }
  677. return nisr.Clone(), nil
  678. }
  679. func (nisr *NetworkInsightSetRange) accumulate(keyProperties []NetworkInsightProperty) (*NetworkInsightSet, error) {
  680. if nisr == nil {
  681. return nil, fmt.Errorf("NetworkInsightSetRange:accumulate called on nil set range")
  682. }
  683. var result *NetworkInsightSet
  684. var err error
  685. nisr.RLock()
  686. defer nisr.RUnlock()
  687. for _, ni := range nisr.NetworkInsightsSet {
  688. result, err = result.Add(ni, keyProperties)
  689. if err != nil {
  690. return nil, err
  691. }
  692. }
  693. return result, nil
  694. }
  695. func (nisr *NetworkInsightSetRange) accumulateByDay(keyProperties []NetworkInsightProperty) (*NetworkInsightSetRange, error) {
  696. if nisr == nil {
  697. return nil, fmt.Errorf("NetworkInsightSetRange:accumulateByDay called on nil set range")
  698. }
  699. // if the network insight set window is 1-day, just return the existing allocation set range
  700. duration := nisr.Window.Duration()
  701. if len(nisr.NetworkInsightsSet) > 0 && duration == timeutil.Day {
  702. return nisr, nil
  703. }
  704. var toAccumulate *NetworkInsightSetRange
  705. result := NewNetworkInsightSetRange(NewWindow(nil, nil))
  706. for i, nis := range nisr.NetworkInsightsSet {
  707. if nis.Window.Duration() != time.Hour {
  708. return nil, fmt.Errorf("window duration must equal 1 hour; got:%s", nis.Window.Duration())
  709. }
  710. hour := nis.Window.Start().Hour()
  711. if toAccumulate == nil {
  712. toAccumulate = NewNetworkInsightSetRange(NewWindow(nil, nil))
  713. nis = nis.Clone()
  714. }
  715. toAccumulate.Append(nis)
  716. nis, err := toAccumulate.accumulate(keyProperties)
  717. if err != nil {
  718. return nil, fmt.Errorf("error accumulating result: %s", err)
  719. }
  720. if nis == nil {
  721. continue
  722. }
  723. toAccumulate = NewNetworkInsightSetRange(nis.Window, nis)
  724. if hour == 23 || i == len(nisr.NetworkInsightsSet)-1 {
  725. if length := len(toAccumulate.NetworkInsightsSet); length != 1 {
  726. return nil, fmt.Errorf("failed accumulation, detected %d sets instead of 1", length)
  727. }
  728. result.Append(toAccumulate.NetworkInsightsSet[0])
  729. toAccumulate = nil
  730. }
  731. }
  732. return result, nil
  733. }
  734. func (nisr *NetworkInsightSetRange) accumulateByWeek(keyProperties []NetworkInsightProperty) (*NetworkInsightSetRange, error) {
  735. if nisr == nil {
  736. return nil, fmt.Errorf("NetworkInsightSetRange:accumulateByWeek called on nil set range")
  737. }
  738. var toAccumulate *NetworkInsightSetRange
  739. result := NewNetworkInsightSetRange(NewWindow(nil, nil))
  740. for i, nis := range nisr.NetworkInsightsSet {
  741. if nis.Window.Duration() != timeutil.Day {
  742. return nil, fmt.Errorf("window duration must equal 24 hours; got:%s", nis.Window.Duration())
  743. }
  744. dayOfWeek := nis.Window.Start().Weekday()
  745. if toAccumulate == nil {
  746. toAccumulate = NewNetworkInsightSetRange(NewWindow(nil, nil))
  747. nis = nis.Clone()
  748. }
  749. toAccumulate.Append(nis)
  750. nis, err := toAccumulate.accumulate(keyProperties)
  751. if err != nil {
  752. return nil, fmt.Errorf("error accumulating result: %s", err)
  753. }
  754. if nis == nil {
  755. continue
  756. }
  757. toAccumulate = NewNetworkInsightSetRange(nis.Window, nis)
  758. if dayOfWeek == time.Saturday || i == len(nisr.NetworkInsightsSet)-1 {
  759. if length := len(toAccumulate.NetworkInsightsSet); length != 1 {
  760. return nil, fmt.Errorf("failed accumulation, detected %d sets instead of 1", length)
  761. }
  762. result.Append(toAccumulate.NetworkInsightsSet[0])
  763. toAccumulate = nil
  764. }
  765. }
  766. return result, nil
  767. }
  768. func (nisr *NetworkInsightSetRange) accumulateByMonth(keyProperties []NetworkInsightProperty) (*NetworkInsightSetRange, error) {
  769. if nisr == nil {
  770. return nil, fmt.Errorf("NetworkInsightSetRange:accumulateByMonth called on nil set range")
  771. }
  772. var toAccumulate *NetworkInsightSetRange
  773. result := NewNetworkInsightSetRange(NewWindow(nil, nil))
  774. for i, nis := range nisr.NetworkInsightsSet {
  775. if nis.Window.Duration() != timeutil.Day {
  776. return nil, fmt.Errorf("window duration must equal 24 hours; got:%s", nis.Window.Duration())
  777. }
  778. _, month, _ := nis.Window.Start().Date()
  779. _, nextDayMonth, _ := nis.Window.Start().Add(time.Hour * 24).Date()
  780. if toAccumulate == nil {
  781. toAccumulate = NewNetworkInsightSetRange(NewWindow(nil, nil))
  782. nis = nis.Clone()
  783. }
  784. toAccumulate.Append(nis)
  785. nis, err := toAccumulate.accumulate(keyProperties)
  786. if err != nil {
  787. return nil, fmt.Errorf("error accumulating result: %s", err)
  788. }
  789. if nis == nil {
  790. continue
  791. }
  792. toAccumulate = NewNetworkInsightSetRange(nis.Window, nis)
  793. if month != nextDayMonth || i == len(nisr.NetworkInsightsSet)-1 {
  794. if length := len(toAccumulate.NetworkInsightsSet); length != 1 {
  795. return nil, fmt.Errorf("failed accumulation, detected %d sets instead of 1", length)
  796. }
  797. result.Append(toAccumulate.NetworkInsightsSet[0])
  798. toAccumulate = nil
  799. }
  800. }
  801. return result, nil
  802. }
  803. func (nisr *NetworkInsightSetRange) Accumulate(accumulateBy AccumulateOption, keyProperties []NetworkInsightProperty) (*NetworkInsightSetRange, error) {
  804. if nisr == nil {
  805. return nil, fmt.Errorf("NetworkInsightSetRange:Accumulate called on nil set range")
  806. }
  807. switch accumulateBy {
  808. case AccumulateOptionNone:
  809. return nisr.accumulateByNone(keyProperties)
  810. case AccumulateOptionAll:
  811. return nisr.accumulateByAll(keyProperties)
  812. case AccumulateOptionHour:
  813. return nisr.accumulateByHour(keyProperties)
  814. case AccumulateOptionDay:
  815. return nisr.accumulateByDay(keyProperties)
  816. case AccumulateOptionWeek:
  817. return nisr.accumulateByWeek(keyProperties)
  818. case AccumulateOptionMonth:
  819. return nisr.accumulateByMonth(keyProperties)
  820. default:
  821. // ideally, this should never happen
  822. return nil, fmt.Errorf("unexpected error, invalid accumulateByType: %s", accumulateBy)
  823. }
  824. }
  825. func (nisr *NetworkInsightSetRange) newAccumulation(keyProperties []NetworkInsightProperty) (*NetworkInsightSet, error) {
  826. if nisr == nil {
  827. return nil, fmt.Errorf("nil NetworkInsightSetRange in accumulation")
  828. }
  829. var networkInsigthSet *NetworkInsightSet
  830. var err error
  831. if len(nisr.NetworkInsightsSet) == 0 {
  832. return nil, fmt.Errorf("NetworkInsightSetRange has empty NetworkInsightSet in accumulation")
  833. }
  834. for _, nis := range nisr.NetworkInsightsSet {
  835. if networkInsigthSet == nil {
  836. networkInsigthSet = nis.Clone()
  837. continue
  838. }
  839. networkInsigthSet, err = networkInsigthSet.Accumulate(nis, keyProperties)
  840. if err != nil {
  841. return nil, err
  842. }
  843. }
  844. return networkInsigthSet, nil
  845. }
  846. func (nisr *NetworkInsightSetRange) FilterOn(filter filter.Filter) error {
  847. if nisr == nil {
  848. return fmt.Errorf("filter called on nil networkInsightSetRange")
  849. }
  850. for _, nis := range nisr.NetworkInsightsSet {
  851. err := nis.FilterOn(filter)
  852. if err != nil {
  853. return fmt.Errorf("unable to filter nis for window: %s with err: %v", nis.Window.String(), err)
  854. }
  855. }
  856. return nil
  857. }
  858. // FilterNetworkDetails for a given network insight set with the options applied.
  859. // When ShowZeroCost is set to false, all the network detail interactions with
  860. // zero cost are dropped and based on the applied filter only.
  861. func (nisr *NetworkInsightSetRange) FilterNetworkDetails(opts *NetworkDetailsOptions) error {
  862. if opts == nil {
  863. return nil
  864. }
  865. if nisr == nil {
  866. return fmt.Errorf("filter called on nil networkInsightSetRange")
  867. }
  868. for _, nis := range nisr.NetworkInsightsSet {
  869. err := nis.FilterNetworkDetails(opts)
  870. if err != nil {
  871. return fmt.Errorf("unable to filter network details in nis for window: %s with err: %v", nis.Window.String(), err)
  872. }
  873. }
  874. return nil
  875. }