github.com/ferranbt/nomad@v0.9.3-0.20190607002617-85c449b7667c/plugins/shared/structs/units.go (about)

     1  package structs
     2  
     3  import "sort"
     4  
     5  const (
     6  	// Binary SI Byte Units
     7  	UnitKiB = "KiB"
     8  	UnitMiB = "MiB"
     9  	UnitGiB = "GiB"
    10  	UnitTiB = "TiB"
    11  	UnitPiB = "PiB"
    12  	UnitEiB = "EiB"
    13  
    14  	// Decimal SI Byte Units
    15  	UnitkB = "kB"
    16  	UnitKB = "KB"
    17  	UnitMB = "MB"
    18  	UnitGB = "GB"
    19  	UnitTB = "TB"
    20  	UnitPB = "PB"
    21  	UnitEB = "EB"
    22  
    23  	// Binary SI Byte Rates
    24  	UnitKiBPerS = "KiB/s"
    25  	UnitMiBPerS = "MiB/s"
    26  	UnitGiBPerS = "GiB/s"
    27  	UnitTiBPerS = "TiB/s"
    28  	UnitPiBPerS = "PiB/s"
    29  	UnitEiBPerS = "EiB/s"
    30  
    31  	// Decimal SI Byte Rates
    32  	UnitkBPerS = "kB/s"
    33  	UnitKBPerS = "KB/s"
    34  	UnitMBPerS = "MB/s"
    35  	UnitGBPerS = "GB/s"
    36  	UnitTBPerS = "TB/s"
    37  	UnitPBPerS = "PB/s"
    38  	UnitEBPerS = "EB/s"
    39  
    40  	// Hertz units
    41  	UnitMHz = "MHz"
    42  	UnitGHz = "GHz"
    43  
    44  	// Watts units
    45  	UnitmW = "mW"
    46  	UnitW  = "W"
    47  	UnitkW = "kW"
    48  	UnitMW = "MW"
    49  	UnitGW = "GW"
    50  )
    51  
    52  var (
    53  	// numUnits is the number of known units
    54  	numUnits = len(binarySIBytes) + len(decimalSIBytes) + len(binarySIByteRates) + len(decimalSIByteRates) + len(watts) + len(hertz)
    55  
    56  	// UnitIndex is a map of unit name to unit
    57  	UnitIndex = make(map[string]*Unit, numUnits)
    58  
    59  	// lengthSortedUnits is a list of unit names sorted by length with longest
    60  	// first
    61  	lengthSortedUnits = make([]string, 0, numUnits)
    62  
    63  	binarySIBytes = []*Unit{
    64  		{
    65  			Name:       UnitKiB,
    66  			Base:       UnitByte,
    67  			Multiplier: 1 << 10,
    68  		},
    69  		{
    70  			Name:       UnitMiB,
    71  			Base:       UnitByte,
    72  			Multiplier: 1 << 20,
    73  		},
    74  		{
    75  			Name:       UnitGiB,
    76  			Base:       UnitByte,
    77  			Multiplier: 1 << 30,
    78  		},
    79  		{
    80  			Name:       UnitTiB,
    81  			Base:       UnitByte,
    82  			Multiplier: 1 << 40,
    83  		},
    84  		{
    85  			Name:       UnitPiB,
    86  			Base:       UnitByte,
    87  			Multiplier: 1 << 50,
    88  		},
    89  		{
    90  			Name:       UnitEiB,
    91  			Base:       UnitByte,
    92  			Multiplier: 1 << 60,
    93  		},
    94  	}
    95  
    96  	decimalSIBytes = []*Unit{
    97  		{
    98  			Name:       UnitkB,
    99  			Base:       UnitByte,
   100  			Multiplier: Pow(1000, 1),
   101  		},
   102  		{
   103  			Name:       UnitKB, // Alternative name for kB
   104  			Base:       UnitByte,
   105  			Multiplier: Pow(1000, 1),
   106  		},
   107  		{
   108  			Name:       UnitMB,
   109  			Base:       UnitByte,
   110  			Multiplier: Pow(1000, 2),
   111  		},
   112  		{
   113  			Name:       UnitGB,
   114  			Base:       UnitByte,
   115  			Multiplier: Pow(1000, 3),
   116  		},
   117  		{
   118  			Name:       UnitTB,
   119  			Base:       UnitByte,
   120  			Multiplier: Pow(1000, 4),
   121  		},
   122  		{
   123  			Name:       UnitPB,
   124  			Base:       UnitByte,
   125  			Multiplier: Pow(1000, 5),
   126  		},
   127  		{
   128  			Name:       UnitEB,
   129  			Base:       UnitByte,
   130  			Multiplier: Pow(1000, 6),
   131  		},
   132  	}
   133  
   134  	binarySIByteRates = []*Unit{
   135  		{
   136  			Name:       UnitKiBPerS,
   137  			Base:       UnitByteRate,
   138  			Multiplier: 1 << 10,
   139  		},
   140  		{
   141  			Name:       UnitMiBPerS,
   142  			Base:       UnitByteRate,
   143  			Multiplier: 1 << 20,
   144  		},
   145  		{
   146  			Name:       UnitGiBPerS,
   147  			Base:       UnitByteRate,
   148  			Multiplier: 1 << 30,
   149  		},
   150  		{
   151  			Name:       UnitTiBPerS,
   152  			Base:       UnitByteRate,
   153  			Multiplier: 1 << 40,
   154  		},
   155  		{
   156  			Name:       UnitPiBPerS,
   157  			Base:       UnitByteRate,
   158  			Multiplier: 1 << 50,
   159  		},
   160  		{
   161  			Name:       UnitEiBPerS,
   162  			Base:       UnitByteRate,
   163  			Multiplier: 1 << 60,
   164  		},
   165  	}
   166  
   167  	decimalSIByteRates = []*Unit{
   168  		{
   169  			Name:       UnitkBPerS,
   170  			Base:       UnitByteRate,
   171  			Multiplier: Pow(1000, 1),
   172  		},
   173  		{
   174  			Name:       UnitKBPerS,
   175  			Base:       UnitByteRate,
   176  			Multiplier: Pow(1000, 1),
   177  		},
   178  		{
   179  			Name:       UnitMBPerS,
   180  			Base:       UnitByteRate,
   181  			Multiplier: Pow(1000, 2),
   182  		},
   183  		{
   184  			Name:       UnitGBPerS,
   185  			Base:       UnitByteRate,
   186  			Multiplier: Pow(1000, 3),
   187  		},
   188  		{
   189  			Name:       UnitTBPerS,
   190  			Base:       UnitByteRate,
   191  			Multiplier: Pow(1000, 4),
   192  		},
   193  		{
   194  			Name:       UnitPBPerS,
   195  			Base:       UnitByteRate,
   196  			Multiplier: Pow(1000, 5),
   197  		},
   198  		{
   199  			Name:       UnitEBPerS,
   200  			Base:       UnitByteRate,
   201  			Multiplier: Pow(1000, 6),
   202  		},
   203  	}
   204  
   205  	hertz = []*Unit{
   206  		{
   207  			Name:       UnitMHz,
   208  			Base:       UnitHertz,
   209  			Multiplier: Pow(1000, 2),
   210  		},
   211  		{
   212  			Name:       UnitGHz,
   213  			Base:       UnitHertz,
   214  			Multiplier: Pow(1000, 3),
   215  		},
   216  	}
   217  
   218  	watts = []*Unit{
   219  		{
   220  			Name:              UnitmW,
   221  			Base:              UnitWatt,
   222  			Multiplier:        Pow(10, 3),
   223  			InverseMultiplier: true,
   224  		},
   225  		{
   226  			Name:       UnitW,
   227  			Base:       UnitWatt,
   228  			Multiplier: 1,
   229  		},
   230  		{
   231  			Name:       UnitkW,
   232  			Base:       UnitWatt,
   233  			Multiplier: Pow(10, 3),
   234  		},
   235  		{
   236  			Name:       UnitMW,
   237  			Base:       UnitWatt,
   238  			Multiplier: Pow(10, 6),
   239  		},
   240  		{
   241  			Name:       UnitGW,
   242  			Base:       UnitWatt,
   243  			Multiplier: Pow(10, 9),
   244  		},
   245  	}
   246  )
   247  
   248  func init() {
   249  	// Build the index
   250  	for _, units := range [][]*Unit{binarySIBytes, decimalSIBytes, binarySIByteRates, decimalSIByteRates, watts, hertz} {
   251  		for _, unit := range units {
   252  			UnitIndex[unit.Name] = unit
   253  			lengthSortedUnits = append(lengthSortedUnits, unit.Name)
   254  		}
   255  	}
   256  
   257  	sort.Slice(lengthSortedUnits, func(i, j int) bool {
   258  		return len(lengthSortedUnits[i]) >= len(lengthSortedUnits[j])
   259  	})
   260  }