(parameterRows []database.GetTemplateParameterInsightsRow)
| 377 | } |
| 378 | |
| 379 | func TemplateInsightsParameters(parameterRows []database.GetTemplateParameterInsightsRow) ([]codersdk.TemplateParameterUsage, error) { |
| 380 | // Use a stable sort, similarly to how we would sort in the query, note that |
| 381 | // we don't sort in the query because order varies depending on the table |
| 382 | // collation. |
| 383 | // |
| 384 | // ORDER BY utp.name, utp.type, utp.display_name, utp.description, utp.options, wbp.value |
| 385 | slices.SortFunc(parameterRows, func(a, b database.GetTemplateParameterInsightsRow) int { |
| 386 | if a.Name != b.Name { |
| 387 | return strings.Compare(a.Name, b.Name) |
| 388 | } |
| 389 | if a.Type != b.Type { |
| 390 | return strings.Compare(a.Type, b.Type) |
| 391 | } |
| 392 | if a.DisplayName != b.DisplayName { |
| 393 | return strings.Compare(a.DisplayName, b.DisplayName) |
| 394 | } |
| 395 | if a.Description != b.Description { |
| 396 | return strings.Compare(a.Description, b.Description) |
| 397 | } |
| 398 | if string(a.Options) != string(b.Options) { |
| 399 | return strings.Compare(string(a.Options), string(b.Options)) |
| 400 | } |
| 401 | return strings.Compare(a.Value, b.Value) |
| 402 | }) |
| 403 | |
| 404 | parametersUsage := []codersdk.TemplateParameterUsage{} |
| 405 | indexByNum := make(map[int64]int) |
| 406 | for _, param := range parameterRows { |
| 407 | if _, ok := indexByNum[param.Num]; !ok { |
| 408 | var opts []codersdk.TemplateVersionParameterOption |
| 409 | err := json.Unmarshal(param.Options, &opts) |
| 410 | if err != nil { |
| 411 | return nil, err |
| 412 | } |
| 413 | |
| 414 | plaintextDescription, err := render.PlaintextFromMarkdown(param.Description) |
| 415 | if err != nil { |
| 416 | return nil, err |
| 417 | } |
| 418 | |
| 419 | parametersUsage = append(parametersUsage, codersdk.TemplateParameterUsage{ |
| 420 | TemplateIDs: param.TemplateIDs, |
| 421 | Name: param.Name, |
| 422 | Type: param.Type, |
| 423 | DisplayName: param.DisplayName, |
| 424 | Description: plaintextDescription, |
| 425 | Options: opts, |
| 426 | }) |
| 427 | indexByNum[param.Num] = len(parametersUsage) - 1 |
| 428 | } |
| 429 | |
| 430 | i := indexByNum[param.Num] |
| 431 | parametersUsage[i].Values = append(parametersUsage[i].Values, codersdk.TemplateParameterValue{ |
| 432 | Value: param.Value, |
| 433 | Count: param.Count, |
| 434 | }) |
| 435 | } |
| 436 |
no test coverage detected