|
| 1 | +package common |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sort" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/oasisprotocol/oasis-sdk/client-sdk/go/helpers" |
| 9 | + "github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/roflmarket" |
| 10 | + "github.com/oasisprotocol/oasis-sdk/client-sdk/go/types" |
| 11 | + |
| 12 | + "github.com/oasisprotocol/cli/build/rofl/provider" |
| 13 | + "github.com/oasisprotocol/cli/cmd/common" |
| 14 | +) |
| 15 | + |
| 16 | +// FilterOffers removes the offers marked as private from the given listing, unless the user |
| 17 | +// explicitly asked for them via --all. |
| 18 | +func FilterOffers(offers []*roflmarket.Offer) []*roflmarket.Offer { |
| 19 | + if ShowPrivateOffers { |
| 20 | + return offers |
| 21 | + } |
| 22 | + |
| 23 | + public := make([]*roflmarket.Offer, 0, len(offers)) |
| 24 | + for _, offer := range offers { |
| 25 | + if provider.IsOfferPrivate(offer) { |
| 26 | + continue |
| 27 | + } |
| 28 | + public = append(public, offer) |
| 29 | + } |
| 30 | + return public |
| 31 | +} |
| 32 | + |
| 33 | +// ShowOfferSummary outputs a summary of a single offer. |
| 34 | +func ShowOfferSummary(npa *common.NPASelection, offer *roflmarket.Offer) { |
| 35 | + // Extract offer name from metadata if available. |
| 36 | + name, ok := offer.Metadata[provider.SchedulerMetadataOfferKey] |
| 37 | + if !ok { |
| 38 | + name = "<unnamed>" |
| 39 | + } |
| 40 | + |
| 41 | + // Determine TEE type. |
| 42 | + tee := FormatTeeType(offer.Resources.TEE) |
| 43 | + |
| 44 | + // Format GPU info if present. |
| 45 | + var gpu string |
| 46 | + if offer.Resources.GPU != nil { |
| 47 | + gpu = fmt.Sprintf(" | GPU: %d", offer.Resources.GPU.Count) |
| 48 | + if offer.Resources.GPU.Model != "" { |
| 49 | + gpu += fmt.Sprintf(" (%s)", offer.Resources.GPU.Model) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + var private string |
| 54 | + if provider.IsOfferPrivate(offer) { |
| 55 | + private = " (private)" |
| 56 | + } |
| 57 | + |
| 58 | + fmt.Printf(" - %s [%s]%s\n", name, offer.ID, private) |
| 59 | + fmt.Printf(" TEE: %s | Memory: %d MiB | vCPUs: %d | Storage: %.2f GiB%s\n", |
| 60 | + tee, |
| 61 | + offer.Resources.Memory, |
| 62 | + offer.Resources.CPUCount, |
| 63 | + float64(offer.Resources.Storage)/1024., |
| 64 | + gpu, |
| 65 | + ) |
| 66 | + fmt.Printf(" Capacity: %d\n", offer.Capacity) |
| 67 | + |
| 68 | + // Note and Description from metadata. |
| 69 | + if note, ok := offer.Metadata[provider.NoteMetadataKey]; ok { |
| 70 | + fmt.Printf(" Note: %s\n", note) |
| 71 | + } |
| 72 | + if desc, ok := offer.Metadata[provider.DescriptionMetadataKey]; ok { |
| 73 | + fmt.Printf(" Description:\n %s\n", strings.ReplaceAll(desc, "\n", "\n ")) |
| 74 | + } |
| 75 | + |
| 76 | + // Accounts allowed to rent machines, when the offer is restricted. |
| 77 | + if creators := provider.OfferAllowedCreators(offer); len(creators) > 0 { |
| 78 | + prettyCreators := make([]string, 0, len(creators)) |
| 79 | + for _, creator := range creators { |
| 80 | + prettyCreators = append(prettyCreators, common.PrettyAddress(creator)) |
| 81 | + } |
| 82 | + fmt.Printf(" Allowed creators: %s\n", strings.Join(prettyCreators, ", ")) |
| 83 | + } |
| 84 | + |
| 85 | + // Payment info. |
| 86 | + switch { |
| 87 | + case offer.Payment.Native != nil: |
| 88 | + if len(offer.Payment.Native.Terms) > 0 { |
| 89 | + var terms []string //nolint: prealloc |
| 90 | + for term, amount := range offer.Payment.Native.Terms { |
| 91 | + bu := types.NewBaseUnits(amount, offer.Payment.Native.Denomination) |
| 92 | + formattedAmount := helpers.FormatParaTimeDenomination(npa.ParaTime, bu) |
| 93 | + terms = append(terms, fmt.Sprintf("%s: %s", FormatTermAdjectival(term), formattedAmount)) |
| 94 | + } |
| 95 | + sort.Strings(terms) |
| 96 | + fmt.Printf(" Payment: %s\n", strings.Join(terms, ", ")) |
| 97 | + } |
| 98 | + case offer.Payment.EvmContract != nil: |
| 99 | + fmt.Printf(" Payment: EVM Contract (0x%x)\n", offer.Payment.EvmContract.Address[:]) |
| 100 | + } |
| 101 | +} |
0 commit comments