This repository has been archived on 2021-09-02. You can view files and clone it, but cannot push or open issues or pull requests.
my-bloody-hetzner-sb-notifier/hetzner/types.go

85 lines
2.2 KiB
Go
Raw Normal View History

package hetzner
import (
"fmt"
"strconv"
"strings"
)
2018-10-26 22:31:39 +02:00
type Deals struct {
ResultStats FilterResultStats
Servers []Server
}
type FilterResultStats struct {
OriginalCount int `json:"count"`
FilteredCount int `json:"filtered"`
}
type Offers struct {
Server []Server `json:"server"`
}
type Server struct {
Key int `json:"key"`
Name string `json:"name"`
Freetext string `json:"freetext"`
Description []string `json:"description"`
Dist []string `json:"dist"`
Datacenter []string `json:"datacenter"`
Specials []string `json:"specials"`
Traffic string `json:"traffic"`
2018-10-25 21:18:45 +02:00
Bandwidth int `json:"bandwith"`
2018-10-25 21:18:45 +02:00
Price string `json:"price"`
PriceV string `json:"price_v"`
SetupPrice string `json:"setup_price"`
2018-10-25 21:18:45 +02:00
Cpu string `json:"cpu"`
2018-10-26 22:31:39 +02:00
CpuBenchmark int64 `json:"cpu_benchmark"`
CpuCount int64 `json:"cpu_count"`
Ram int64 `json:"ram"`
2018-10-25 21:18:45 +02:00
RamHr string `json:"ram_hr"`
2018-10-26 22:31:39 +02:00
HddSize int64 `json:"hdd_size"`
2018-10-25 21:18:45 +02:00
HddHr string `json:"hdd_hr"`
2018-10-26 22:31:39 +02:00
HddCount int64 `json:"hdd_count"`
2018-10-25 21:18:45 +02:00
SpecialHdd string `json:"specialHdd"`
2018-10-25 21:18:45 +02:00
NextReduce int `json:"next_reduce"`
NextReduceHr string `json:"next_reduce_hr"`
2018-10-25 21:18:45 +02:00
FixedPrice bool `json:"fixed_price"`
IsHighio bool `json:"is_highio"`
IsEcc bool `json:"is_ecc"`
}
2018-10-26 22:31:39 +02:00
func (s *Server) TotalHdd() int64 {
2018-10-25 21:18:45 +02:00
return s.HddCount * s.HddSize
}
func (s *Server) Score() float64 {
2018-10-25 21:18:45 +02:00
return (float64(s.TotalHdd())*0.2 + float64(s.Ram)*0.4 + float64(s.CpuBenchmark)*0.4) / s.ParsePrice()
}
func (s *Server) ParsePrice() float64 {
priceParsed, err := strconv.ParseFloat(s.Price, 32)
if err != nil {
2018-10-25 21:18:45 +02:00
fmt.Printf("Could not parse price %s for server %d: %s", s.Price, s.Key, err)
return -1
}
2018-10-25 21:18:45 +02:00
return priceParsed * 1.19
}
func (s *Server) Header() string {
return fmt.Sprint("ID\tRam\tHDD\tCPU\tPrice\tScore\tReduce time\tSpecials")
}
func (s *Server) ToString() string {
fixedPriceSymbol := "*"
2018-10-25 21:18:45 +02:00
if !s.FixedPrice {
fixedPriceSymbol = ""
}
specials := strings.Join(s.Specials, ", ")
2018-10-25 21:18:45 +02:00
return fmt.Sprintf("%s-%d\t%s\t%s (%d)\t%s (%d)\t%.2f €%s\t%.2f\t%s\t%s", s.Name, s.Key, s.RamHr, s.HddHr, s.TotalHdd(), s.Cpu, s.CpuBenchmark, s.ParsePrice(), fixedPriceSymbol, s.Score(), s.NextReduceHr, specials)
}