github.com/piotrnar/gocoin@v0.0.0-20240512203912-faa0448c5e96/client/usif/webui/counts.go (about)

     1  package webui
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  	"sort"
     7  
     8  	"github.com/piotrnar/gocoin/client/common"
     9  )
    10  
    11  type many_counters []one_counter
    12  
    13  type one_counter struct {
    14  	key string
    15  	cnt uint64
    16  }
    17  
    18  func (c many_counters) Len() int {
    19  	return len(c)
    20  }
    21  
    22  func (c many_counters) Less(i, j int) bool {
    23  	return c[i].key < c[j].key
    24  }
    25  
    26  func (c many_counters) Swap(i, j int) {
    27  	c[i], c[j] = c[j], c[i]
    28  }
    29  
    30  func p_counts(w http.ResponseWriter, r *http.Request) {
    31  	if !ipchecker(r) {
    32  		return
    33  	}
    34  	s := load_template("counts.html")
    35  	write_html_head(w, r)
    36  	w.Write([]byte(s))
    37  	write_html_tail(w)
    38  }
    39  
    40  func json_counts(w http.ResponseWriter, r *http.Request) {
    41  	if !ipchecker(r) {
    42  		return
    43  	}
    44  	type one_var_cnt struct {
    45  		Var string `json:"var"`
    46  		Cnt uint64 `json:"cnt"`
    47  	}
    48  	type one_net_rec struct {
    49  		Var  string `json:"var"`
    50  		Rcvd uint64 `json:"rcvd"`
    51  		Rbts uint64 `json:"rbts"`
    52  		Sent uint64 `json:"sent"`
    53  		Sbts uint64 `json:"sbts"`
    54  	}
    55  
    56  	var all_var_cnt struct {
    57  		Gen []*one_var_cnt `json:"gen"`
    58  		Txs []*one_var_cnt `json:"txs"`
    59  		Net []*one_net_rec `json:"net"`
    60  	}
    61  
    62  	common.CounterMutex.Lock()
    63  	for k, v := range common.Counter {
    64  		if k[4] == '_' {
    65  			var i int
    66  			for i = 0; i < len(all_var_cnt.Net); i++ {
    67  				if all_var_cnt.Net[i].Var == k[5:] {
    68  					break
    69  				}
    70  			}
    71  			if i == len(all_var_cnt.Net) {
    72  				fin := k[5:]
    73  				var nrec one_net_rec
    74  				nrec.Var = fin
    75  				nrec.Rcvd = common.Counter["rcvd_"+fin]
    76  				nrec.Rbts = common.Counter["rbts_"+fin]
    77  				nrec.Sent = common.Counter["sent_"+fin]
    78  				nrec.Sbts = common.Counter["sbts_"+fin]
    79  				all_var_cnt.Net = append(all_var_cnt.Net, &nrec)
    80  			}
    81  		} else if k[:2] == "Tx" {
    82  			all_var_cnt.Txs = append(all_var_cnt.Txs, &one_var_cnt{Var: k[2:], Cnt: v})
    83  		} else {
    84  			all_var_cnt.Gen = append(all_var_cnt.Gen, &one_var_cnt{Var: k, Cnt: v})
    85  		}
    86  	}
    87  	common.CounterMutex.Unlock()
    88  	sort.Slice(all_var_cnt.Gen, func(i, j int) bool {
    89  		return all_var_cnt.Gen[i].Var < all_var_cnt.Gen[j].Var
    90  	})
    91  	sort.Slice(all_var_cnt.Txs, func(i, j int) bool {
    92  		return all_var_cnt.Txs[i].Var < all_var_cnt.Txs[j].Var
    93  	})
    94  	sort.Slice(all_var_cnt.Net, func(i, j int) bool {
    95  		return all_var_cnt.Net[i].Var < all_var_cnt.Net[j].Var
    96  	})
    97  
    98  	bx, er := json.Marshal(all_var_cnt)
    99  	if er == nil {
   100  		w.Header()["Content-Type"] = []string{"application/json"}
   101  		w.Write(bx)
   102  	} else {
   103  		println(er.Error())
   104  	}
   105  }