refactor(gridproxy): use native options

- use native option types in filters intead of own option types
- reflect changes across dependant modules
This commit is contained in:
2025-01-15 16:19:13 +02:00
parent dac062eee9
commit 98ce764992
4 changed files with 139 additions and 545 deletions

View File

@@ -1,498 +1,110 @@
module model
import json
type OptionU64 = EmptyOption | u64
type OptionBool = EmptyOption | bool
@[params]
pub struct FarmFilter {
pub mut:
page OptionU64 = EmptyOption{}
size OptionU64 = EmptyOption{}
ret_count OptionBool = EmptyOption{}
randomize OptionBool = EmptyOption{}
free_ips OptionU64 = EmptyOption{}
total_ips OptionU64 = EmptyOption{}
stellar_address string
pricing_policy_id OptionU64 = EmptyOption{}
farm_id OptionU64 = EmptyOption{}
twin_id OptionU64 = EmptyOption{}
name string
name_contains string
certification_type string
dedicated OptionBool = EmptyOption{}
country string
node_free_mru OptionU64 = EmptyOption{}
node_free_hru OptionU64 = EmptyOption{}
node_free_sru OptionU64 = EmptyOption{}
node_status string
node_rented_by OptionU64 = EmptyOption{}
node_available_for OptionU64 = EmptyOption{}
node_has_gpu OptionBool = EmptyOption{}
node_certified OptionBool = EmptyOption{}
page ?u64
size ?u64
ret_count ?bool
randomize ?bool
free_ips ?u64
total_ips ?u64
stellar_address ?string
pricing_policy_id ?u64
farm_id ?u64
twin_id ?u64
name ?string
name_contains ?string
certification_type ?string
dedicated ?bool
country ?string
node_free_mru ?u64
node_free_hru ?u64
node_free_sru ?u64
node_status ?string
node_rented_by ?u64
node_available_for ?u64
node_has_gpu ?bool
node_certified ?bool
}
// serialize FarmFilter to map
pub fn (f &FarmFilter) to_map() map[string]string {
mut m := map[string]string{}
match f.page {
EmptyOption {}
u64 {
m['page'] = f.page.str()
}
}
match f.size {
EmptyOption {}
u64 {
m['size'] = f.size.str()
}
}
match f.ret_count {
EmptyOption {}
bool {
m['ret_count'] = f.ret_count.str()
}
}
match f.randomize {
EmptyOption {}
bool {
m['randomize'] = f.randomize.str()
}
}
match f.free_ips {
EmptyOption {}
u64 {
m['free_ips'] = f.free_ips.str()
}
}
match f.total_ips {
EmptyOption {}
u64 {
m['total_ips'] = f.total_ips.str()
}
}
if f.stellar_address != '' {
m['stellar_address'] = f.stellar_address
}
match f.pricing_policy_id {
EmptyOption {}
u64 {
m['pricing_policy_id'] = f.pricing_policy_id.str()
}
}
match f.farm_id {
EmptyOption {}
u64 {
m['farm_id'] = f.farm_id.str()
}
}
match f.twin_id {
EmptyOption {}
u64 {
m['twin_id'] = f.twin_id.str()
}
}
if f.name != '' {
m['name'] = f.name
}
if f.name_contains != '' {
m['name_contains'] = f.name_contains
}
if f.certification_type != '' {
m['certification_type'] = f.certification_type
}
if f.country != '' {
m['country'] = f.country
}
match f.dedicated {
EmptyOption {}
bool {
m['dedicated'] = f.dedicated.str()
}
}
match f.node_available_for {
EmptyOption {}
u64 {
m['node_available_for'] = f.node_available_for.str()
}
}
match f.node_free_hru {
EmptyOption {}
u64 {
m['node_free_hru'] = f.node_free_hru.str()
}
}
match f.node_free_mru {
EmptyOption {}
u64 {
m['node_free_mru'] = f.node_free_mru.str()
}
}
match f.node_free_sru {
EmptyOption {}
u64 {
m['node_free_sru'] = f.node_free_sru.str()
}
}
match f.node_rented_by {
EmptyOption {}
u64 {
m['node_rented_by'] = f.node_rented_by.str()
}
}
match f.node_has_gpu {
EmptyOption {}
bool {
m['node_has_gpu'] = f.node_has_gpu.str()
}
}
match f.node_certified {
EmptyOption {}
bool {
m['node_certified'] = f.node_certified.str()
}
}
if f.node_status != '' {
m['node_status'] = f.node_status
}
return m
pub fn (f FarmFilter) to_map() map[string]string {
return to_map(f)
}
@[params]
pub struct ContractFilter {
pub mut:
page OptionU64 = EmptyOption{}
size OptionU64 = EmptyOption{}
ret_count OptionBool = EmptyOption{}
randomize OptionBool = EmptyOption{}
contract_id OptionU64 = EmptyOption{}
twin_id OptionU64 = EmptyOption{}
node_id OptionU64 = EmptyOption{}
contract_type string
state string
name string
number_of_public_ips OptionU64 = EmptyOption{}
deployment_data string
deployment_hash string
page ?u64
size ?u64
ret_count ?bool
randomize ?bool
contract_id ?u64
twin_id ?u64
node_id ?u64
contract_type ?string
state ?string
name ?string
number_of_public_ips ?u64
deployment_data ?string
deployment_hash ?string
}
// serialize ContractFilter to map
pub fn (f &ContractFilter) to_map() map[string]string {
mut m := map[string]string{}
match f.page {
EmptyOption {}
u64 {
m['page'] = f.page.str()
}
}
match f.size {
EmptyOption {}
u64 {
m['size'] = f.size.str()
}
}
match f.ret_count {
EmptyOption {}
bool {
m['ret_count'] = f.ret_count.str()
}
}
match f.randomize {
EmptyOption {}
bool {
m['randomize'] = f.randomize.str()
}
}
match f.contract_id {
EmptyOption {}
u64 {
m['contract_id'] = f.contract_id.str()
}
}
match f.twin_id {
EmptyOption {}
u64 {
m['twin_id'] = f.twin_id.str()
}
}
match f.node_id {
EmptyOption {}
u64 {
m['node_id'] = f.node_id.str()
}
}
if f.contract_type != '' {
m['type'] = f.contract_type
}
if f.state != '' {
m['state'] = f.state
}
if f.name != '' {
m['name'] = f.name
}
match f.number_of_public_ips {
EmptyOption {}
u64 {
m['number_of_public_ips'] = f.number_of_public_ips.str()
}
}
if f.deployment_data != '' {
m['deployment_data'] = f.deployment_data
}
if f.deployment_hash != '' {
m['deployment_hash'] = f.deployment_hash
}
return m
pub fn (f ContractFilter) to_map() map[string]string {
return to_map(f)
}
@[params]
pub struct NodeFilter {
pub mut:
page OptionU64 = EmptyOption{}
size OptionU64 = EmptyOption{}
ret_count OptionBool = EmptyOption{}
randomize OptionBool = EmptyOption{}
free_mru OptionU64 = EmptyOption{}
free_sru OptionU64 = EmptyOption{}
free_hru OptionU64 = EmptyOption{}
page ?u64
size ?u64
ret_count ?bool
randomize ?bool
free_mru ?u64
free_sru ?u64
free_hru ?u64
free_ips ?u64
total_mru OptionU64 = EmptyOption{}
total_sru OptionU64 = EmptyOption{}
total_hru OptionU64 = EmptyOption{}
total_cru OptionU64 = EmptyOption{}
city string
city_contains string
country string
country_contains string
farm_name string
farm_name_contains string
ipv4 OptionBool = EmptyOption{}
ipv6 OptionBool = EmptyOption{}
domain OptionBool = EmptyOption{}
status string
dedicated OptionBool = EmptyOption{}
healthy OptionBool = EmptyOption{}
rentable OptionBool = EmptyOption{}
rented_by OptionU64 = EmptyOption{}
rented OptionBool = EmptyOption{}
available_for OptionU64 = EmptyOption{}
total_mru ?u64
total_sru ?u64
total_hru ?u64
total_cru ?u64
city ?string
city_contains ?string
country ?string
country_contains ?string
farm_name ?string
farm_name_contains ?string
ipv4 ?bool
ipv6 ?bool
domain ?bool
status ?string
dedicated ?bool
healthy ?bool
rentable ?bool
rented_by ?u64
rented ?bool
available_for ?u64
farm_ids []u64
node_ids []u64
node_id ?u32
twin_id OptionU64 = EmptyOption{}
certification_type string
has_gpu OptionBool = EmptyOption{}
twin_id ?u64
certification_type ?string
has_gpu ?bool
has_ipv6 ?bool
gpu_device_id string
gpu_device_name string
gpu_vendor_id string
gpu_vendor_name string
gpu_available OptionBool = EmptyOption{}
gpu_device_id ?string
gpu_device_name ?string
gpu_vendor_id ?string
gpu_vendor_name ?string
gpu_available ?bool
features []string
}
// serialize NodeFilter to map
pub fn (p &NodeFilter) to_map() map[string]string {
mut m := map[string]string{}
match p.page {
EmptyOption {}
u64 {
m['page'] = p.page.str()
}
}
match p.size {
EmptyOption {}
u64 {
m['size'] = p.size.str()
}
}
match p.ret_count {
EmptyOption {}
bool {
m['ret_count'] = p.ret_count.str()
}
}
match p.randomize {
EmptyOption {}
bool {
m['randomize'] = p.randomize.str()
}
}
match p.free_mru {
EmptyOption {}
u64 {
m['free_mru'] = p.free_mru.str()
}
}
match p.free_sru {
EmptyOption {}
u64 {
m['free_sru'] = p.free_sru.str()
}
}
match p.free_hru {
EmptyOption {}
u64 {
m['free_hru'] = p.free_hru.str()
}
}
if v := p.free_ips {
m['free_ips'] = v.str()
}
if v := p.has_ipv6 {
m['has_ipv6'] = v.str()
}
match p.total_cru {
EmptyOption {}
u64 {
m['total_cru'] = p.total_cru.str()
}
}
match p.total_hru {
EmptyOption {}
u64 {
m['total_hru'] = p.total_hru.str()
}
}
match p.total_mru {
EmptyOption {}
u64 {
m['total_mru'] = p.total_mru.str()
}
}
match p.total_sru {
EmptyOption {}
u64 {
m['total_sru'] = p.total_sru.str()
}
}
if p.status != '' {
m['status'] = p.status
}
if p.city != '' {
m['city'] = p.city
}
if p.city_contains != '' {
m['city_contains'] = p.city_contains
}
if p.country != '' {
m['country'] = p.country
}
if p.country_contains != '' {
m['country_contains'] = p.country_contains
}
if p.farm_name != '' {
m['farm_name'] = p.farm_name
}
if p.farm_name_contains != '' {
m['farm_name_contains'] = p.farm_name_contains
}
match p.ipv4 {
EmptyOption {}
bool {
m['ipv4'] = p.ipv4.str()
}
}
match p.ipv6 {
EmptyOption {}
bool {
m['ipv6'] = p.ipv6.str()
}
}
match p.healthy {
EmptyOption {}
bool {
m['healthy'] = p.healthy.str()
}
}
match p.domain {
EmptyOption {}
bool {
m['domain'] = p.domain.str()
}
}
match p.dedicated {
EmptyOption {}
bool {
m['dedicated'] = p.dedicated.str()
}
}
match p.rentable {
EmptyOption {}
bool {
m['rentable'] = p.rentable.str()
}
}
match p.rented_by {
EmptyOption {}
u64 {
m['rented_by'] = p.rented_by.str()
}
}
match p.rented {
EmptyOption {}
bool {
m['rented'] = p.rented.str()
}
}
match p.available_for {
EmptyOption {}
u64 {
m['available_for'] = p.available_for.str()
}
}
if p.features.len > 0 {
m['features'] = json.encode(p.features).all_after('[').all_before(']')
}
if p.farm_ids.len > 0 {
m['farm_ids'] = json.encode(p.farm_ids).all_after('[').all_before(']')
}
if p.node_ids.len > 0 {
m['node_ids'] = json.encode(p.node_ids).all_after('[').all_before(']')
}
if n := p.node_id {
m['node_id'] = n.str()
}
match p.twin_id {
EmptyOption {}
u64 {
m['twin_id'] = p.twin_id.str()
}
}
if p.certification_type != '' {
m['certification_type'] = p.certification_type
}
match p.has_gpu {
EmptyOption {}
bool {
m['has_gpu'] = p.has_gpu.str()
}
}
if p.gpu_device_id != '' {
m['gpu_device_id'] = p.gpu_device_id
}
if p.gpu_device_name != '' {
m['gpu_device_name'] = p.gpu_device_name
}
if p.gpu_vendor_id != '' {
m['gpu_vendor_id'] = p.gpu_vendor_id
}
if p.gpu_vendor_name != '' {
m['gpu_vendor_name'] = p.gpu_vendor_name
}
match p.gpu_available {
EmptyOption {}
bool {
m['gpu_available'] = p.gpu_available.str()
}
}
return m
pub fn (f NodeFilter) to_map() map[string]string {
return to_map(f)
}
pub enum NodeStatus {
@@ -519,57 +131,51 @@ pub mut:
@[params]
pub struct TwinFilter {
pub mut:
page OptionU64 = EmptyOption{}
size OptionU64 = EmptyOption{}
ret_count OptionBool = EmptyOption{}
randomize OptionBool = EmptyOption{}
twin_id OptionU64 = EmptyOption{}
account_id string
relay string
public_key string
page ?u64
size ?u64
ret_count ?bool
randomize ?bool
twin_id ?u64
account_id ?string
relay ?string
public_key ?string
}
// serialize TwinFilter to map
pub fn (p &TwinFilter) to_map() map[string]string {
pub fn (f TwinFilter) to_map() map[string]string {
return to_map(f)
}
pub fn to_map[T](t T) map[string]string {
mut m := map[string]string{}
match p.page {
EmptyOption {}
u64 {
m['page'] = p.page.str()
$for field in T.fields {
value := t.$(field.name)
$if value is $option {
opt := t.$(field.name)
if opt != none {
// NOTE: for some reason when passing the value to another function
// it is not recognized as an Option and is dereferenced
encode_val(field.name, value, mut m)
}
}
$if value !is $option {
encode_val(field.name, value, mut m)
}
}
match p.size {
EmptyOption {}
u64 {
m['size'] = p.size.str()
}
}
match p.ret_count {
EmptyOption {}
bool {
m['ret_count'] = p.ret_count.str()
}
}
match p.randomize {
EmptyOption {}
bool {
m['randomize'] = p.randomize.str()
}
}
match p.twin_id {
EmptyOption {}
u64 {
m['twin_id'] = p.twin_id.str()
}
}
if p.account_id != '' {
m['account_id'] = p.account_id
}
if p.relay != '' {
m['relay'] = p.relay
}
if p.public_key != '' {
m['public_key'] = p.public_key
}
println('encoded map: ${m}')
return m
}
fn encode_val[T](field_name string, val T, mut m map[string]string) {
$if T is $array {
mut arr := []string{}
for a in val {
arr << a.str()
}
m[field_name] = arr.join(',')
} $else {
m[field_name] = val.str()
}
}

View File

@@ -10,14 +10,12 @@ pub:
}
pub fn (mut i NodeIterator) next() ?[]Node {
match i.filter.page {
EmptyOption {
i.filter.page = u64(1)
}
u64 {
i.filter.page = i.filter.page as u64 + 1
}
if v := i.filter.page {
i.filter.page = v + 1
} else {
i.filter.page = u64(1)
}
nodes := i.get_func(i.filter) or { return none }
if nodes.len == 0 {
return none
@@ -35,13 +33,10 @@ pub:
}
pub fn (mut i FarmIterator) next() ?[]Farm {
match i.filter.page {
EmptyOption {
i.filter.page = u64(1)
}
u64 {
i.filter.page = i.filter.page as u64 + 1
}
if v := i.filter.page {
i.filter.page = v + 1
} else {
i.filter.page = u64(1)
}
farms := i.get_func(i.filter) or { return none }
if farms.len == 0 {
@@ -60,13 +55,10 @@ pub:
}
pub fn (mut i ContractIterator) next() ?[]Contract {
match i.filter.page {
EmptyOption {
i.filter.page = u64(1)
}
u64 {
i.filter.page = i.filter.page as u64 + 1
}
if v := i.filter.page {
i.filter.page = v + 1
} else {
i.filter.page = u64(1)
}
contracts := i.get_func(i.filter) or { return none }
if contracts.len == 0 {
@@ -85,13 +77,10 @@ pub:
}
pub fn (mut i TwinIterator) next() ?[]Twin {
match i.filter.page {
EmptyOption {
i.filter.page = u64(1)
}
u64 {
i.filter.page = i.filter.page as u64 + 1
}
if v := i.filter.page {
i.filter.page = v + 1
} else {
i.filter.page = u64(1)
}
twins := i.get_func(i.filter) or { return none }
if twins.len == 0 {

View File

@@ -102,5 +102,3 @@ pub fn (u DropTFTUnit) str() string {
}
return '${u64(u)} dTFT' // Short for dropTFT (1 TFT = 10_000_000 drops). dylan suggests the name and i'm using this till we have an officail name!
}
struct EmptyOption {}

View File

@@ -131,11 +131,11 @@ fn (mut self TFDeployment) set_nodes() ! {
free_mru: convert_to_gigabytes(u64(vm.requirements.memory))
total_cru: u64(vm.requirements.cpu)
free_sru: convert_to_gigabytes(u64(vm.requirements.size))
available_for: gridproxy_models.OptionU64(u64(self.deployer.twin_id))
available_for: u64(self.deployer.twin_id)
free_ips: if vm.requirements.public_ip4 { u64(1) } else { none }
has_ipv6: if vm.requirements.public_ip6 { vm.requirements.public_ip6 } else { none }
status: 'up'
features: if vm.requirements.public_ip4 { [] } else { ['zmachine'] }
features: if vm.requirements.public_ip4 { ['zmachine'] } else { [] }
)!
if nodes.len == 0 {
@@ -158,7 +158,7 @@ fn (mut self TFDeployment) set_nodes() ! {
status: 'up'
healthy: true
node_id: zdb.requirements.node_id
available_for: gridproxy_models.OptionU64(u64(self.deployer.twin_id))
available_for: u64(self.deployer.twin_id)
)!
if nodes.len == 0 {
@@ -178,7 +178,8 @@ fn (mut self TFDeployment) set_nodes() ! {
status: 'up'
healthy: true
node_id: webname.requirements.node_id
available_for: gridproxy_models.OptionU64(u64(self.deployer.twin_id))
available_for: u64(self.deployer.twin_id)
features: ['zmachine']
)!
if nodes.len == 0 {