|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/csv" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "sort" |
| 9 | + "strconv" |
| 10 | + |
| 11 | + "github.com/utmstack/UTMStack/installer/types" |
| 12 | + "github.com/utmstack/UTMStack/installer/utils" |
| 13 | +) |
| 14 | + |
| 15 | +func main() { |
| 16 | + // Total memory values in MB. |
| 17 | + totals := []int{16000, 20000, 32000, 48000, 64000, 120000, 200000} |
| 18 | + |
| 19 | + // Create a sorted copy of the services (sorted by Priority then Name). |
| 20 | + sortedServices := make([]utils.ServiceConfig, len(types.Services)) |
| 21 | + copy(sortedServices, types.Services) |
| 22 | + sort.Slice(sortedServices, func(i, j int) bool { |
| 23 | + if sortedServices[i].Priority == sortedServices[j].Priority { |
| 24 | + return sortedServices[i].Name < sortedServices[j].Name |
| 25 | + } |
| 26 | + return sortedServices[i].Priority < sortedServices[j].Priority |
| 27 | + }) |
| 28 | + |
| 29 | + // Create the output CSV file. |
| 30 | + file, err := os.Create("output.csv") |
| 31 | + if err != nil { |
| 32 | + log.Fatalf("Error creating CSV file: %v", err) |
| 33 | + } |
| 34 | + defer file.Close() |
| 35 | + |
| 36 | + // Create a CSV writer. |
| 37 | + writer := csv.NewWriter(file) |
| 38 | + defer writer.Flush() |
| 39 | + |
| 40 | + // Iterate over each total memory value. |
| 41 | + for _, total := range totals { |
| 42 | + // Write a comment line indicating the total memory. |
| 43 | + comment := fmt.Sprintf("# Total Memory: %d MB", total) |
| 44 | + // Since csv.Writer does not support comment lines, |
| 45 | + // write the comment directly to the file. |
| 46 | + if _, err := file.WriteString(comment + "\n"); err != nil { |
| 47 | + log.Fatalf("Error writing comment: %v", err) |
| 48 | + } |
| 49 | + |
| 50 | + // Write the header row. |
| 51 | + if err := writer.Write([]string{"Service", "AssignedMemory"}); err != nil { |
| 52 | + log.Fatalf("Error writing header: %v", err) |
| 53 | + } |
| 54 | + |
| 55 | + // Balance memory using the utils package. |
| 56 | + rsrcs, err := utils.BalanceMemory(types.Services, total) |
| 57 | + if err != nil { |
| 58 | + log.Fatalf("Error balancing memory for total %d MB: %v", total, err) |
| 59 | + } |
| 60 | + |
| 61 | + // Write each service's allocated memory. |
| 62 | + for _, svc := range sortedServices { |
| 63 | + // Assume that rsrcs[svc.Name] returns a pointer to a ServiceConfig, |
| 64 | + // and that AssignedMemory is an integer field. |
| 65 | + assigned := rsrcs[svc.Name].AssignedMemory |
| 66 | + row := []string{svc.Name, strconv.Itoa(assigned)} |
| 67 | + if err := writer.Write(row); err != nil { |
| 68 | + log.Fatalf("Error writing row: %v", err) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Flush the CSV writer so far. |
| 73 | + writer.Flush() |
| 74 | + if err := writer.Error(); err != nil { |
| 75 | + log.Fatalf("Error flushing CSV writer: %v", err) |
| 76 | + } |
| 77 | + |
| 78 | + // Write an empty line to separate tables. |
| 79 | + if _, err := file.WriteString("\n"); err != nil { |
| 80 | + log.Fatalf("Error writing empty line: %v", err) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + log.Println("CSV file 'output.csv' created successfully") |
| 85 | +} |
0 commit comments