use fastly_shared::FastlyStatus;
use fastly_sys::fastly_compute_runtime::{get_heap_mib, get_vcpu_ms};
#[cfg(not(target_env = "p1"))]
use fastly_sys::service0_1_0::fastly::compute as wit;
use lazy_static::lazy_static;
pub fn elapsed_vcpu_ms() -> Result<u64, FastlyStatus> {
let mut vcpu_time = 0u64;
let vcpu_time_result = unsafe { get_vcpu_ms(&mut vcpu_time) };
if vcpu_time_result != FastlyStatus::OK {
return Err(vcpu_time_result);
}
Ok(vcpu_time)
}
pub fn heap_memory_snapshot_mib() -> Result<u32, FastlyStatus> {
let mut memory_mib = 0u32;
let result = unsafe { get_heap_mib(&mut memory_mib) };
if result != FastlyStatus::OK {
return Err(result);
}
Ok(memory_mib)
}
pub fn hostname() -> &'static str {
#[cfg(target_env = "p1")]
lazy_static! {
static ref HOSTNAME: String = std::env::var("FASTLY_HOSTNAME").unwrap();
}
#[cfg(not(target_env = "p1"))]
lazy_static! {
static ref HOSTNAME: String = wit::compute_runtime::get_hostname();
}
HOSTNAME.as_str()
}
pub fn pop() -> &'static str {
#[cfg(target_env = "p1")]
lazy_static! {
static ref POP: String = std::env::var("FASTLY_POP").unwrap();
}
#[cfg(not(target_env = "p1"))]
lazy_static! {
static ref POP: String = wit::compute_runtime::get_pop();
}
POP.as_str()
}
pub fn region() -> &'static str {
#[cfg(target_env = "p1")]
lazy_static! {
static ref REGION: String = std::env::var("FASTLY_REGION").unwrap();
}
#[cfg(not(target_env = "p1"))]
lazy_static! {
static ref REGION: String = wit::compute_runtime::get_region();
}
REGION.as_str()
}
pub fn cache_generation() -> u64 {
#[cfg(target_env = "p1")]
lazy_static! {
static ref CACHE_GENERATION: u64 = std::env::var("FASTLY_CACHE_GENERATION")
.unwrap()
.parse()
.unwrap();
}
#[cfg(not(target_env = "p1"))]
lazy_static! {
static ref CACHE_GENERATION: u64 = wit::compute_runtime::get_cache_generation();
}
*CACHE_GENERATION
}
pub fn customer_id() -> &'static str {
#[cfg(target_env = "p1")]
lazy_static! {
static ref CUSTOMER_ID: String = std::env::var("FASTLY_CUSTOMER_ID").unwrap();
}
#[cfg(not(target_env = "p1"))]
lazy_static! {
static ref CUSTOMER_ID: String = wit::compute_runtime::get_customer_id();
}
CUSTOMER_ID.as_str()
}
pub fn is_staging() -> bool {
#[cfg(target_env = "p1")]
lazy_static! {
static ref IS_STAGING: bool = match std::env::var("FASTLY_IS_STAGING").unwrap().as_str() {
"0" => false,
"1" => true,
_ => unreachable!(),
};
}
#[cfg(not(target_env = "p1"))]
lazy_static! {
static ref IS_STAGING: bool = wit::compute_runtime::get_is_staging();
}
*IS_STAGING
}
pub fn service_id() -> &'static str {
#[cfg(target_env = "p1")]
lazy_static! {
static ref SERVICE_ID: String = std::env::var("FASTLY_SERVICE_ID").unwrap();
}
#[cfg(not(target_env = "p1"))]
lazy_static! {
static ref SERVICE_ID: String = wit::compute_runtime::get_service_id();
}
SERVICE_ID.as_str()
}
pub fn service_version() -> u64 {
#[cfg(target_env = "p1")]
lazy_static! {
static ref SERVICE_VERSION: u64 = std::env::var("FASTLY_SERVICE_VERSION")
.unwrap()
.parse()
.unwrap();
}
#[cfg(not(target_env = "p1"))]
lazy_static! {
static ref SERVICE_VERSION: u64 = wit::compute_runtime::get_service_version();
}
*SERVICE_VERSION
}
#[doc(hidden)]
pub fn namespace_id() -> &'static str {
#[cfg(target_env = "p1")]
lazy_static! {
static ref NAMESPACE_ID: String = std::env::var("FASTLY_NAMESPACE_ID").unwrap();
}
#[cfg(not(target_env = "p1"))]
lazy_static! {
static ref NAMESPACE_ID: String = wit::compute_runtime::get_hostname();
}
NAMESPACE_ID.as_str()
}
pub fn sandbox_id() -> &'static str {
#[cfg(target_env = "p1")]
lazy_static! {
static ref SANDBOX_ID: String = std::env::var("FASTLY_TRACE_ID").unwrap();
}
#[cfg(not(target_env = "p1"))]
lazy_static! {
static ref SANDBOX_ID: String = wit::compute_runtime::get_sandbox_id();
}
SANDBOX_ID.as_str()
}