Copilot commented on code in PR #499:
URL: 
https://github.com/apache/skywalking-booster-ui/pull/499#discussion_r2380922047


##########
src/views/dashboard/related/trace/components/TraceQuery/TraceContent.vue:
##########
@@ -0,0 +1,194 @@
+<!-- Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License. -->
+<template>
+  <div class="trace-query-content">
+    <div class="trace-info">
+      <div class="flex-h" style="justify-content: space-between">
+        <h3>{{ trace.label }}</h3>
+        <div>
+          <el-button class="mr-10" size="small" @click="showSpansTable">{{ 
t("spansTable") }}</el-button>
+          <el-dropdown @command="handleDownload" trigger="click">
+            <el-button size="small">
+              {{ t("download") }}
+              <el-icon class="el-icon--right"><arrow-down /></el-icon>
+            </el-button>
+            <template #dropdown>
+              <el-dropdown-menu>
+                <el-dropdown-item command="json">Download 
JSON</el-dropdown-item>
+              </el-dropdown-menu>
+            </template>
+          </el-dropdown>
+        </div>
+      </div>
+      <div class="trace-meta flex-h">
+        <div>
+          <span class="grey mr-5">{{ t("duration") }}</span>
+          <span class="value">{{ trace.duration }}ms</span>
+        </div>
+        <div>
+          <span class="grey mr-5">{{ t("services") }}</span>
+          <span class="value">{{ trace.serviceCode || "Unknown" }}</span>
+        </div>
+        <div>
+          <span class="grey mr-5">{{ t("totalSpans") }}</span>
+          <span class="value">{{ trace.spans?.length || 0 }}</span>
+        </div>
+        <div>
+          <span class="grey mr-5">{{ t("traceID") }}</span>
+          <span class="value">{{ trace.traceId }}</span>
+        </div>
+      </div>
+    </div>
+    <div class="flex-h">
+      <div class="detail-section-timeline flex-v" :style="{ width: 
spanPanelVisible ? '65%' : '100%' }">
+        <MinTimeline
+          v-show="minTimelineVisible"
+          :trace="trace"
+          :minTimestamp="minTimestamp"
+          :maxTimestamp="maxTimestamp"
+          @updateSelectedMaxTimestamp="handleSelectedMaxTimestamp"
+          @updateSelectedMinTimestamp="handleSelectedMinTimestamp"
+        />
+        <TimelineTool @toggleSpanPanel="toggleSpanPanel" 
@toggleMinTimeline="toggleMinTimeline" />
+        <SpansTree
+          :trace="trace"
+          :minTimestamp="minTimestamp"
+          :maxTimestamp="maxTimestamp"
+          :selectedMaxTimestamp="selectedMaxTimestamp"
+          :selectedMinTimestamp="selectedMinTimestamp"
+        />
+      </div>
+      <SpanDetails v-show="spanPanelVisible" />
+    </div>
+    <!-- Spans Table Drawer -->
+    <SpansTableDrawer
+      v-model:visible="spansTableVisible"
+      :spans="trace.spans || []"
+      :trace-id="trace.traceId"
+      @viewSpan="handleViewSpan"
+    />
+  </div>
+</template>
+
+<script lang="ts" setup>
+  import { ref, computed } from "vue";
+  import { useI18n } from "vue-i18n";
+  import { ElMessage } from "element-plus";
+  import { ArrowDown } from "@element-plus/icons-vue";
+  import type { Trace, Span } from "@/types/trace";
+  import SpansTableDrawer from "./SpansTableDrawer.vue";
+  import SpansTree from "./SpansTree.vue";
+  import MinTimeline from "./MinTimeline.vue";
+  import SpanDetails from "./SpanDetails.vue";
+  import { saveFileAsJSON } from "@/utils/file";
+  import { useTraceStore } from "@/store/modules/trace";
+  import TimelineTool from "./TimelineTool.vue";
+
+  interface Props {
+    trace: Trace;
+  }
+
+  const { t } = useI18n();
+  const props = defineProps<Props>();
+  const spansTableVisible = ref<boolean>(false);
+  const traceStore = useTraceStore();
+  // Time range like xScale domain [0, max]
+  const minTimestamp = computed(() => {
+    if (!props.trace.spans.length) return 0;
+    return Math.min(...props.trace.spans.map((s) => s.startTime || 0));
+  });
+
+  const maxTimestamp = computed(() => {
+    const timestamps = props.trace.spans.map((span) => span.endTime || 0);
+    if (timestamps.length === 0) return 0;
+
+    return Math.max(...timestamps);
+  });
+  const selectedMaxTimestamp = ref<number>(maxTimestamp.value);
+  const selectedMinTimestamp = ref<number>(minTimestamp.value);
+  const spanPanelVisible = ref<boolean>(true);
+  const minTimelineVisible = ref<boolean>(true);
+
+  function showSpansTable() {
+    spansTableVisible.value = true;
+  }
+
+  function handleSelectedMaxTimestamp(value: number) {
+    selectedMaxTimestamp.value = value;
+  }
+
+  function handleSelectedMinTimestamp(value: number) {
+    selectedMinTimestamp.value = value;
+  }
+
+  function toggleSpanPanel() {
+    spanPanelVisible.value = !spanPanelVisible.value;
+  }
+
+  function toggleMinTimeline() {
+    minTimelineVisible.value = !minTimelineVisible.value;
+  }
+
+  function handleDownload() {
+    const trace = props.trace;
+    const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
+    const baseFilename = `trace-${trace.traceId}-${timestamp}`;
+    const spans = trace.spans.map((span) => {
+      const newSpan = {
+        ...span,
+        duration: span.duration,
+      };
+      delete newSpan.duration;
+      delete newSpan.label;
+      return newSpan;

Review Comment:
   Deleting properties from a copied object after spreading may cause runtime 
errors if these properties are accessed elsewhere. Consider creating the object 
without these properties instead of deleting them.
   ```suggestion
       const spans = trace.spans.map(({ duration, label, ...rest }) => {
         return { ...rest };
   ```



##########
src/views/dashboard/related/trace/components/TraceQuery/SpansTree.vue:
##########
@@ -0,0 +1,148 @@
+<!-- Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License. -->
+<template>
+  <div
+    class="trace-timeline"
+    ref="spansGraph"
+    :style="{ width: `100%`, height: `${(fixSpansSize + 1) * rowHeight}px` }"
+  ></div>
+</template>
+<script lang="ts" setup>
+  import { ref, onMounted, onUnmounted, nextTick, watch } from "vue";
+  import type { Trace, Span, Ref } from "@/types/trace";
+  import { useTraceStore } from "@/store/modules/trace";
+  import TreeGraph from "../D3Graph/utils/d3-trace-list";
+  import { buildSegmentForest, collapseTree, getRefsAllNodes } from 
"../D3Graph/utils/helper";
+  /* global Nullable */
+  interface Props {
+    trace: Trace;
+    selectedMaxTimestamp: number;
+    selectedMinTimestamp: number;
+    minTimestamp: number;
+    maxTimestamp: number;
+  }
+  const props = defineProps<Props>();
+  const traceStore = useTraceStore();
+  const spansGraph = ref<HTMLDivElement | null>(null);
+  const tree = ref<Nullable<any>>(null);
+  const segmentId = ref<Span[]>([]);
+  const refSpans = ref<Array<Ref>>([]);
+  const fixSpansSize = ref<number>(0);
+  const currentSpan = () => traceStore.currentSpan;
+  const rowHeight = 20; // must match child component vertical spacing
+
+  onMounted(async () => {
+    changeTree();
+    if (!spansGraph.value) {
+      return;
+    }
+    // Wait for DOM to be fully updated before initializing and drawing
+    await nextTick();
+    tree.value = new TreeGraph({ el: spansGraph.value, handleSelectSpan: 
selectSpan });
+    tree.value.init({
+      data: { label: "TRACE_ROOT", children: segmentId.value },
+      row: getRefsAllNodes({ label: "TRACE_ROOT", children: segmentId.value }),
+      fixSpansSize: fixSpansSize.value,
+      selectedMaxTimestamp: props.selectedMaxTimestamp,
+      selectedMinTimestamp: props.selectedMinTimestamp,
+    });
+    // Ensure the element has proper dimensions before drawing
+    await nextTick();
+    tree.value.draw();
+    selectInitialSpan();
+    // Listen for layout changes triggered by span panel toggle and re-draw
+    window.addEventListener("spanPanelToggled", onSpanPanelToggled);
+  });
+
+  onUnmounted(() => {
+    window.removeEventListener("spanPanelToggled", onSpanPanelToggled);
+  });
+
+  async function onSpanPanelToggled() {
+    // Recreate graph so it recalculates width/height based on new layout
+    await nextTick();
+    changeTree();
+    if (!spansGraph.value) {
+      return;
+    }
+    tree.value = new TreeGraph({ el: spansGraph.value, handleSelectSpan: 
selectSpan });
+    tree.value.init({
+      data: { label: "TRACE_ROOT", children: segmentId.value },
+      row: getRefsAllNodes({ label: "TRACE_ROOT", children: segmentId.value }),
+      fixSpansSize: fixSpansSize.value,
+      selectedMaxTimestamp: props.selectedMaxTimestamp,
+      selectedMinTimestamp: props.selectedMinTimestamp,
+    });
+    await nextTick();
+    tree.value.draw();
+    selectInitialSpan();
+  }
+  function changeTree() {
+    if (props.trace.spans.length === 0) {
+      return [];
+    }
+    const { roots, fixSpansSize: fixSize, refSpans: refs } = 
buildSegmentForest(props.trace.spans, props.trace.traceId);
+    segmentId.value = roots as Span[];
+    fixSpansSize.value = fixSize;
+    refSpans.value = refs;
+    for (const root of segmentId.value) {
+      collapseTree(root as unknown as Span, refSpans.value);
+    }
+  }
+
+  function selectSpan(span: any) {
+    traceStore.setCurrentSpan(span.data);
+    // Highlight selected node
+    if (tree.value && typeof tree.value.highlightSpan === "function") {
+      tree.value.highlightSpan(span.data);
+    }
+  }
+
+  function selectInitialSpan() {
+    if (segmentId.value && segmentId.value.length > 0) {
+      const root = segmentId.value[0];
+      traceStore.setCurrentSpan(root);
+      if (tree.value && typeof tree.value.highlightSpan === "function") {
+        tree.value.highlightSpan(root as any);
+      }
+    }
+  }
+  // React to external span selections (e.g., from SpansTableDrawer)
+  watch(
+    currentSpan,
+    (span) => {
+      if (!span || !tree.value || typeof tree.value.highlightSpan !== 
"function") return;
+      tree.value.highlightSpan(span as any);
+    },
+    { deep: false },
+  );
+  watch(
+    () => [props.selectedMaxTimestamp, props.selectedMinTimestamp],
+    (value) => {
+      onSpanPanelToggled();

Review Comment:
   Calling onSpanPanelToggled() on every timestamp change may cause unnecessary 
re-renders. Consider debouncing this call or checking if the change is 
significant enough to warrant a redraw.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to