No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pricing.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.monthlyMinInstanceCost = exports.canCalculateMinInstanceCost = exports.V2_FREE_TIER = exports.V1_FREE_TIER = exports.V2_RATES = exports.V1_RATES = void 0;
  4. const backend = require("./backend");
  5. const V1_REGION_TO_TIER = {
  6. "us-central1": 1,
  7. "us-east1": 1,
  8. "us-east4": 1,
  9. "europe-west1": 1,
  10. "europe-west2": 1,
  11. "asia-east2": 1,
  12. "asia-northeast1": 1,
  13. "asia-northeast2": 1,
  14. "us-west2": 2,
  15. "us-west3": 2,
  16. "us-west4": 2,
  17. "northamerica-northeast1": 2,
  18. "southamerica-east1": 2,
  19. "europe-west3": 2,
  20. "europe-west6": 2,
  21. "europe-central2": 2,
  22. "australia-southeast1": 2,
  23. "asia-south1": 2,
  24. "asia-southeast2": 2,
  25. "asia-northeast3": 2,
  26. };
  27. const V2_REGION_TO_TIER = {
  28. "asia-east1": 1,
  29. "asia-northeast1": 1,
  30. "asia-northeast2": 1,
  31. "europe-north1": 1,
  32. "europe-west1": 1,
  33. "europe-west4": 1,
  34. "us-central1": 1,
  35. "us-east1": 1,
  36. "us-east4": 1,
  37. "us-west1": 1,
  38. "asia-east2": 2,
  39. "asia-northeast3": 2,
  40. "asia-southeast1": 2,
  41. "asia-southeast2": 2,
  42. "asia-south1": 2,
  43. "australia-southeast1": 2,
  44. "europe-central2": 2,
  45. "europe-west2": 2,
  46. "europe-west3": 2,
  47. "europe-west6": 2,
  48. "northamerica-northeast1": 2,
  49. "southamerica-east1": 2,
  50. "us-west2": 2,
  51. "us-west3": 2,
  52. "us-west4": 2,
  53. };
  54. exports.V1_RATES = {
  55. invocations: 4e-7,
  56. memoryGb: {
  57. 1: 0.0000025,
  58. 2: 0.0000035,
  59. },
  60. cpuGhz: {
  61. 1: 0.00001,
  62. 2: 0.000014,
  63. },
  64. idleCpuGhz: {
  65. 1: 0.000001,
  66. 2: 0.00000145,
  67. },
  68. egress: 0.12,
  69. };
  70. exports.V2_RATES = {
  71. invocations: 4e-7,
  72. memoryGb: {
  73. 1: 0.0000025,
  74. 2: 0.0000035,
  75. },
  76. vCpu: {
  77. 1: 0.000024,
  78. 2: 0.0000336,
  79. },
  80. idleVCpu: {
  81. 1: 0.0000025,
  82. 2: 0.0000035,
  83. },
  84. };
  85. exports.V1_FREE_TIER = {
  86. invocations: 2000000,
  87. memoryGb: 400000,
  88. cpuGhz: 200000,
  89. egress: 5,
  90. };
  91. exports.V2_FREE_TIER = {
  92. invocations: 2000000,
  93. memoryGb: 360000,
  94. vCpu: 180000,
  95. egress: 1,
  96. };
  97. const VCPU_TO_GHZ = 2.4;
  98. const MB_TO_GHZ = {
  99. 128: 0.2,
  100. 256: 0.4,
  101. 512: 0.8,
  102. 1024: 1.4,
  103. 2048: 1 * VCPU_TO_GHZ,
  104. 4096: 2 * VCPU_TO_GHZ,
  105. 8192: 2 * VCPU_TO_GHZ,
  106. 16384: 4 * VCPU_TO_GHZ,
  107. 32768: 8 * VCPU_TO_GHZ,
  108. };
  109. function canCalculateMinInstanceCost(endpoint) {
  110. if (!endpoint.minInstances) {
  111. return true;
  112. }
  113. if (endpoint.platform === "gcfv1") {
  114. if (!MB_TO_GHZ[endpoint.availableMemoryMb || backend.DEFAULT_MEMORY]) {
  115. return false;
  116. }
  117. if (!V1_REGION_TO_TIER[endpoint.region]) {
  118. return false;
  119. }
  120. return true;
  121. }
  122. if (!V2_REGION_TO_TIER[endpoint.region]) {
  123. return false;
  124. }
  125. return true;
  126. }
  127. exports.canCalculateMinInstanceCost = canCalculateMinInstanceCost;
  128. const SECONDS_PER_MONTH = 30 * 24 * 60 * 60;
  129. function monthlyMinInstanceCost(endpoints) {
  130. const usage = {
  131. gcfv1: { 1: { ram: 0, cpu: 0 }, 2: { ram: 0, cpu: 0 } },
  132. gcfv2: { 1: { ram: 0, cpu: 0 }, 2: { ram: 0, cpu: 0 } },
  133. };
  134. for (const endpoint of endpoints) {
  135. if (!endpoint.minInstances) {
  136. continue;
  137. }
  138. const ramMb = endpoint.availableMemoryMb || backend.DEFAULT_MEMORY;
  139. const ramGb = ramMb / 1024;
  140. if (endpoint.platform === "gcfv1") {
  141. const cpu = MB_TO_GHZ[ramMb];
  142. const tier = V1_REGION_TO_TIER[endpoint.region];
  143. usage["gcfv1"][tier].ram =
  144. usage["gcfv1"][tier].ram + ramGb * SECONDS_PER_MONTH * endpoint.minInstances;
  145. usage["gcfv1"][tier].cpu =
  146. usage["gcfv1"][tier].cpu + cpu * SECONDS_PER_MONTH * endpoint.minInstances;
  147. }
  148. else {
  149. const tier = V2_REGION_TO_TIER[endpoint.region];
  150. usage["gcfv2"][tier].ram =
  151. usage["gcfv2"][tier].ram + ramGb * SECONDS_PER_MONTH * endpoint.minInstances;
  152. usage["gcfv2"][tier].cpu =
  153. usage["gcfv2"][tier].cpu +
  154. endpoint.cpu * SECONDS_PER_MONTH * endpoint.minInstances;
  155. }
  156. }
  157. let v1MemoryBill = usage["gcfv1"][1].ram * exports.V1_RATES.memoryGb[1] + usage["gcfv1"][2].ram * exports.V1_RATES.memoryGb[2];
  158. v1MemoryBill -= exports.V1_FREE_TIER.memoryGb * exports.V1_RATES.memoryGb[1];
  159. v1MemoryBill = Math.max(v1MemoryBill, 0);
  160. let v1CpuBill = usage["gcfv1"][1].cpu * exports.V1_RATES.idleCpuGhz[1] + usage["gcfv1"][2].cpu * exports.V1_RATES.idleCpuGhz[2];
  161. v1CpuBill -= exports.V1_FREE_TIER.cpuGhz * exports.V1_RATES.cpuGhz[1];
  162. v1CpuBill = Math.max(v1CpuBill, 0);
  163. let v2MemoryBill = usage["gcfv2"][1].ram * exports.V2_RATES.memoryGb[1] + usage["gcfv2"][2].ram * exports.V2_RATES.memoryGb[2];
  164. v2MemoryBill -= exports.V2_FREE_TIER.memoryGb * exports.V2_RATES.memoryGb[1];
  165. v2MemoryBill = Math.max(v2MemoryBill, 0);
  166. let v2CpuBill = usage["gcfv2"][1].cpu * exports.V2_RATES.idleVCpu[1] + usage["gcfv2"][2].cpu * exports.V2_RATES.idleVCpu[2];
  167. v2CpuBill -= exports.V2_FREE_TIER.vCpu * exports.V2_RATES.vCpu[1];
  168. v2CpuBill = Math.max(v2CpuBill, 0);
  169. return v1MemoryBill + v1CpuBill + v2MemoryBill + v2CpuBill;
  170. }
  171. exports.monthlyMinInstanceCost = monthlyMinInstanceCost;