@@ -504,31 +504,11 @@ func (p *OnPrem) GetInstances(ctx *lepton.Context) (instances []lepton.CloudInst
504
504
return
505
505
}
506
506
507
- // InstanceStats shows metrics for instance onprem .
508
- func (p * OnPrem ) InstanceStats (ctx * lepton.Context , iname string ) error {
509
- instances , err := p .GetInstances (ctx )
510
- if err != nil {
511
- return err
512
- }
513
-
514
- rinstances := []lepton.CloudInstance {}
515
-
516
- for i := 0 ; i < len (instances ); i ++ {
517
- if iname != "" {
518
- if iname != instances [i ].Name {
519
- continue
520
- } else {
521
- rinstances = append (rinstances , instances [i ])
522
- }
523
- } else {
524
- rinstances = append (rinstances , instances [i ])
525
- }
526
- }
527
-
507
+ func (p * OnPrem ) getInstancesStats (ctx * lepton.Context , rinstances []lepton.CloudInstance ) ([]lepton.CloudInstance , error ) {
528
508
for i := 0 ; i < len (rinstances ); i ++ {
529
509
instance , err := p .GetMetaInstanceByName (ctx , rinstances [i ].Name )
530
510
if err != nil {
531
- return err
511
+ fmt . Println ( err )
532
512
}
533
513
534
514
last := instance .Mgmt
@@ -557,38 +537,79 @@ func (p *OnPrem) InstanceStats(ctx *lepton.Context, iname string) error {
557
537
rinstances [i ].TotalMemory = (lr .qmpReturn .Stats .TotalMemory / int64 (1000000 ))
558
538
}
559
539
560
- // perhaps this could be a new type
561
- if ctx .Config ().RunConfig .JSON {
562
- if len (rinstances ) == 0 {
563
- fmt .Println ("[]" )
564
- return nil
540
+ return rinstances , nil
541
+ }
542
+
543
+ // InstanceStats shows metrics for instance onprem .
544
+ func (p * OnPrem ) InstanceStats (ctx * lepton.Context , iname string , watch bool ) error {
545
+ instances , err := p .GetInstances (ctx )
546
+ if err != nil {
547
+ return err
548
+ }
549
+
550
+ rinstances := []lepton.CloudInstance {}
551
+
552
+ for i := 0 ; i < len (instances ); i ++ {
553
+ if iname != "" {
554
+ if iname != instances [i ].Name {
555
+ continue
556
+ } else {
557
+ rinstances = append (rinstances , instances [i ])
558
+ }
559
+ } else {
560
+ rinstances = append (rinstances , instances [i ])
565
561
}
566
- return json .NewEncoder (os .Stdout ).Encode (rinstances )
567
562
}
568
563
569
- table := tablewriter .NewWriter (os .Stdout )
570
- table .SetHeader ([]string {"PID" , "Name" , "Memory" })
571
- table .SetHeaderColor (
572
- tablewriter.Colors {tablewriter .Bold , tablewriter .FgCyanColor },
573
- tablewriter.Colors {tablewriter .Bold , tablewriter .FgCyanColor },
574
- tablewriter.Colors {tablewriter .Bold , tablewriter .FgCyanColor })
564
+ // FIXME: if we're watching no need to establish a connection
565
+ // everytime
566
+ if watch {
567
+ for {
568
+ rinstances , err = p .getInstancesStats (ctx , rinstances )
569
+ if err != nil {
570
+ fmt .Println (err )
571
+ }
575
572
576
- table .SetRowLine (true )
573
+ json .NewEncoder (os .Stdout ).Encode (rinstances )
574
+ time .Sleep (500 * time .Millisecond )
575
+ }
576
+ } else {
577
+ rinstances , err = p .getInstancesStats (ctx , rinstances )
578
+ if err != nil {
579
+ fmt .Println (err )
580
+ }
577
581
578
- for _ , i := range rinstances {
579
- var rows []string
582
+ if ctx .Config ().RunConfig .JSON {
583
+ if len (rinstances ) == 0 {
584
+ fmt .Println ("[]" )
585
+ return nil
586
+ }
587
+ return json .NewEncoder (os .Stdout ).Encode (rinstances )
588
+ }
580
589
581
- rows = append (rows , i .ID )
582
- rows = append (rows , i .Name )
583
- rows = append (rows , i .HumanMem ())
590
+ table := tablewriter .NewWriter (os .Stdout )
591
+ table .SetHeader ([]string {"PID" , "Name" , "Memory" })
592
+ table .SetHeaderColor (
593
+ tablewriter.Colors {tablewriter .Bold , tablewriter .FgCyanColor },
594
+ tablewriter.Colors {tablewriter .Bold , tablewriter .FgCyanColor },
595
+ tablewriter.Colors {tablewriter .Bold , tablewriter .FgCyanColor })
584
596
585
- table .Append (rows )
586
- }
597
+ table .SetRowLine (true )
587
598
588
- table .Render ()
599
+ for _ , i := range rinstances {
600
+ var rows []string
589
601
590
- return nil
602
+ rows = append (rows , i .ID )
603
+ rows = append (rows , i .Name )
604
+ rows = append (rows , i .HumanMem ())
591
605
606
+ table .Append (rows )
607
+ }
608
+
609
+ table .Render ()
610
+
611
+ return nil
612
+ }
592
613
}
593
614
594
615
// ListInstances on premise
0 commit comments