|
1 | 1 | package service |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "sort" |
| 6 | + "strings" |
5 | 7 |
|
6 | 8 | "github.com/metrico/qryn/v4/reader/prof" |
7 | 9 | ) |
@@ -457,3 +459,127 @@ func findNode(nodeID uint64, children []*TreeNodeV2) int { |
457 | 459 | } |
458 | 460 | return -1 |
459 | 461 | } |
| 462 | + |
| 463 | +// ToDot converts the tree to Graphviz DOT format suitable for flamegraph visualization and AI analysis. |
| 464 | +// sampleType should be in "type:unit" form (e.g. "cpu:nanoseconds"). |
| 465 | +// profileName is used as the graph title (e.g. "process_cpu:cpu:nanoseconds:cpu:nanoseconds"). |
| 466 | +func (t *Tree) ToDot(sampleType string, profileName string) string { |
| 467 | + sampleTypeIndex := -1 |
| 468 | + for i, st := range t.SampleTypes { |
| 469 | + if st == sampleType { |
| 470 | + sampleTypeIndex = i |
| 471 | + break |
| 472 | + } |
| 473 | + } |
| 474 | + if sampleTypeIndex == -1 { |
| 475 | + return "digraph flamegraph {}\n" |
| 476 | + } |
| 477 | + |
| 478 | + total := t.Total() |
| 479 | + var totalVal int64 |
| 480 | + if len(total) > sampleTypeIndex { |
| 481 | + totalVal = total[sampleTypeIndex] |
| 482 | + } |
| 483 | + |
| 484 | + pct := func(v int64) float64 { |
| 485 | + if totalVal == 0 { |
| 486 | + return 0 |
| 487 | + } |
| 488 | + return float64(v) / float64(totalVal) * 100 |
| 489 | + } |
| 490 | + // weight is an integer 1–100 proportional to the child's share of total samples. |
| 491 | + // Graphviz requires weight >= 1 and works best with small integers. |
| 492 | + edgeWeight := func(v int64) int { |
| 493 | + w := int(pct(v)) |
| 494 | + if w < 1 { |
| 495 | + w = 1 |
| 496 | + } |
| 497 | + return w |
| 498 | + } |
| 499 | + |
| 500 | + // Map raw NodeIDs to compact sequential integers so Graphviz node names stay small. |
| 501 | + seqID := make(map[uint64]int) |
| 502 | + nextID := 0 |
| 503 | + assignID := func(nodeID uint64) int { |
| 504 | + if id, ok := seqID[nodeID]; ok { |
| 505 | + return id |
| 506 | + } |
| 507 | + seqID[nodeID] = nextID |
| 508 | + nextID++ |
| 509 | + return nextID - 1 |
| 510 | + } |
| 511 | + |
| 512 | + var sb strings.Builder |
| 513 | + sb.WriteString(fmt.Sprintf("digraph %q {\n", profileName)) |
| 514 | + sb.WriteString(" node [shape=box fontsize=12];\n") |
| 515 | + sb.WriteString(" edge [fontsize=10];\n") |
| 516 | + rootSeq := assignID(0) |
| 517 | + sb.WriteString(fmt.Sprintf(" N%d [label=%q style=filled fillcolor=\"#eeeeee\"];\n", |
| 518 | + rootSeq, fmt.Sprintf("total\nsamples: %d (100%%)", totalVal))) |
| 519 | + |
| 520 | + visited := make(map[uint64]bool) |
| 521 | + visited[0] = true |
| 522 | + queue := []uint64{0} |
| 523 | + |
| 524 | + for len(queue) > 0 { |
| 525 | + parentID := queue[0] |
| 526 | + queue = queue[1:] |
| 527 | + parentSeq := seqID[parentID] |
| 528 | + |
| 529 | + children, ok := t.Nodes[parentID] |
| 530 | + if !ok { |
| 531 | + continue |
| 532 | + } |
| 533 | + |
| 534 | + for _, child := range children { |
| 535 | + if visited[child.NodeID] { |
| 536 | + continue |
| 537 | + } |
| 538 | + visited[child.NodeID] = true |
| 539 | + childSeq := assignID(child.NodeID) |
| 540 | + |
| 541 | + name := "n/a" |
| 542 | + if idx, exists := t.NamesMap[child.FnID]; exists && idx < len(t.Names) { |
| 543 | + name = t.Names[idx] |
| 544 | + } |
| 545 | + |
| 546 | + selfVal := child.Self[sampleTypeIndex] |
| 547 | + childTotal := child.Total[sampleTypeIndex] |
| 548 | + label := fmt.Sprintf("%s\ntotal: %d (%.1f%%) self: %d (%.1f%%)", |
| 549 | + name, childTotal, pct(childTotal), selfVal, pct(selfVal)) |
| 550 | + |
| 551 | + fillColor := heatColor(selfVal, totalVal) |
| 552 | + |
| 553 | + sb.WriteString(fmt.Sprintf(" N%d [label=%q style=filled fillcolor=%q];\n", |
| 554 | + childSeq, label, fillColor)) |
| 555 | + sb.WriteString(fmt.Sprintf(" N%d -> N%d [label=%q weight=%d];\n", |
| 556 | + parentSeq, childSeq, |
| 557 | + fmt.Sprintf("%.1f%%", pct(childTotal)), |
| 558 | + edgeWeight(childTotal))) |
| 559 | + |
| 560 | + queue = append(queue, child.NodeID) |
| 561 | + } |
| 562 | + } |
| 563 | + |
| 564 | + sb.WriteString("}\n") |
| 565 | + return sb.String() |
| 566 | +} |
| 567 | + |
| 568 | +// heatColor returns a hex fill color for a DOT node based on self-sample fraction. |
| 569 | +// 0% self → light gray, 100% self → red. |
| 570 | +func heatColor(self, total int64) string { |
| 571 | + if total == 0 || self == 0 { |
| 572 | + return "#f8f8f8" |
| 573 | + } |
| 574 | + ratio := float64(self) / float64(total) |
| 575 | + if ratio > 1 { |
| 576 | + ratio = 1 |
| 577 | + } |
| 578 | + r := 248 - int(ratio*float64(248-255)) |
| 579 | + if r > 255 { |
| 580 | + r = 255 |
| 581 | + } |
| 582 | + g := int(248 - ratio*248) |
| 583 | + b := int(248 - ratio*248) |
| 584 | + return fmt.Sprintf("#%02x%02x%02x", r, g, b) |
| 585 | +} |
0 commit comments