All files / app/assets/javascripts/performance_bar/components detailed_metric.vue

96.87% Statements 31/32
95% Branches 19/20
100% Functions 19/19
96.87% Lines 31/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231                      3x                                                                         104x             24x     121x     24x   24x 22x     24x 20x     24x     24x 16x   8x 3x     5x     24x           24x     26x 1x     25x     24x     24x 24x           24x         4x   4x 3x   1x           87x     21x     4x                                                                                                                                                                                                    
<script>
import {
  GlButton,
  GlTooltipDirective,
  GlModal,
  GlModalDirective,
  GlCollapsibleListbox,
} from '@gitlab/ui';
 
import { __, s__ } from '~/locale';
import { sortOrders, sortOrderOptions } from '../constants';
import RequestWarning from './request_warning.vue';
 
export default {
  components: {
    RequestWarning,
    GlButton,
    GlModal,
    GlCollapsibleListbox,
  },
  directives: {
    GlTooltip: GlTooltipDirective,
    'gl-modal': GlModalDirective,
  },
  props: {
    currentRequest: {
      type: Object,
      required: true,
    },
    metric: {
      type: String,
      required: true,
    },
    title: {
      type: String,
      required: false,
      default: null,
    },
    header: {
      type: String,
      required: true,
    },
    keys: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      openedBacktraces: [],
      sortOrder: sortOrders.DURATION,
    };
  },
  computed: {
    modalId() {
      return `modal-peek-${this.metric}-details`;
    },
    metricDetails() {
      return this.currentRequest.details[this.metric];
    },
    metricDetailsSummary() {
      const summary = {};
 
      if (!this.metricDetails.summaryOptions?.hideTotal) {
        summary[__('Total')] = this.metricDetails.calls;
      }
 
      if (!this.metricDetails.summaryOptions?.hideDuration) {
        summary[s__('PerformanceBar|Total duration')] = this.metricDetails.duration;
      }
 
      return { ...summary, ...(this.metricDetails.summary || {}) };
    },
    metricDetailsLabel() {
      if (this.metricDetails.duration && this.metricDetails.calls) {
        return `${this.metricDetails.duration} / ${this.metricDetails.calls}`;
      }
      if (this.metricDetails.calls) {
        return this.metricDetails.calls;
      }
 
      return '0';
    },
    displaySortOrder() {
      return (
        this.metricDetails.details.length !== 0 &&
        this.metricDetails.details.every((item) => item.start)
      );
    },
    detailsList() {
      return this.metricDetails.details.map((item, index) => ({ ...item, id: index }));
    },
    sortedList() {
      if (this.sortOrder === sortOrders.CHRONOLOGICAL) {
        return this.detailsList.slice().sort(this.sortDetailChronologically);
      }
 
      return this.detailsList.slice().sort(this.sortDetailByDuration);
    },
    warnings() {
      return this.metricDetails.warnings || [];
    },
    htmlId() {
      if (this.currentRequest) {
        return `performance-bar-warning-${this.currentRequest.id}-${this.metric}`;
      }
 
      return '';
    },
    actualTitle() {
      return this.title ?? this.metric;
    },
  },
  methods: {
    toggleBacktrace(toggledIndex) {
      const toggledOpenedIndex = this.openedBacktraces.indexOf(toggledIndex);
 
      if (toggledOpenedIndex === -1) {
        this.openedBacktraces = [...this.openedBacktraces, toggledIndex];
      } else {
        this.openedBacktraces = this.openedBacktraces.filter(
          (openedIndex) => openedIndex !== toggledIndex,
        );
      }
    },
    itemHasOpenedBacktrace(toggledIndex) {
      return this.openedBacktraces.find((openedIndex) => openedIndex === toggledIndex) >= 0;
    },
    sortDetailByDuration(a, b) {
      return a.duration < b.duration ? 1 : -1;
    },
    sortDetailChronologically(a, b) {
      return a.start < b.start ? -1 : 1;
    },
  },
  sortOrderOptions,
};
</script>
<template>
  <div
    v-if="currentRequest.details && metricDetails"
    :id="`peek-view-${metric}`"
    class="gl-display-flex gl-align-items-baseline view"
    data-testid="detailed-metric-content"
  >
    <gl-button
      v-gl-tooltip.viewport
      v-gl-modal="modalId"
      class="gl-mr-2"
      :title="header"
      variant="link"
    >
      <span class="gl-font-sm gl-font-weight-semibold" data-testid="performance-bar-details-label">
        {{ metricDetailsLabel }}
      </span>
    </gl-button>
    <gl-modal :modal-id="modalId" :title="header" size="lg" footer-class="d-none" scrollable>
      <div class="gl-display-flex gl-align-items-center gl-justify-content-space-between">
        <div class="gl-display-flex gl-align-items-center" data-testid="performance-bar-summary">
          <div v-for="(value, name) in metricDetailsSummary" :key="name" class="gl-pr-8">
            <div v-if="value" data-testid="performance-bar-summary-item">
              <div>{{ name }}</div>
              <div class="gl-font-size-h1 gl-font-weight-semibold">{{ value }}</div>
            </div>
          </div>
        </div>
        <gl-collapsible-listbox
          v-if="displaySortOrder"
          v-model="sortOrder"
          :toggle-text="$options.sortOrderOptions[sortOrder].text"
          :items="Object.values($options.sortOrderOptions)"
          placement="right"
          data-testid="performance-bar-sort-order"
        />
      </div>
      <hr />
      <table class="table gl-table">
        <template v-if="sortedList.length">
          <tr v-for="item in sortedList" :key="item.id">
            <td data-testid="performance-item-duration">
              <span v-if="item.duration">{{
                sprintf(__('%{duration}ms'), { duration: item.duration })
              }}</span>
            </td>
            <td>
              <div>
                <div
                  v-for="(key, keyIndex) in keys"
                  :key="key"
                  class="text-break-word"
                  :class="{ 'mb-3 gl-font-weight-semibold': keyIndex == 0 }"
                >
                  {{ item[key] }}
                  <gl-button
                    v-if="keyIndex == 0 && item.backtrace"
                    class="gl-ml-3 button-ellipsis-horizontal"
                    data-testid="backtrace-expand-btn"
                    category="primary"
                    variant="default"
                    icon="ellipsis_h"
                    size="small"
                    :selected="itemHasOpenedBacktrace(item.id)"
                    :aria-label="__('Toggle backtrace')"
                    @click="toggleBacktrace(item.id)"
                  />
                </div>
                <pre v-if="itemHasOpenedBacktrace(item.id)" class="backtrace-row gl-mt-3">{{
                  item.backtrace
                }}</pre>
              </div>
            </td>
          </tr>
        </template>
        <template v-else>
          <tr>
            <td data-testid="performance-bar-empty-detail-notice">
              {{ sprintf(__('No %{header} for this request.'), { header: header.toLowerCase() }) }}
            </td>
          </tr>
        </template>
      </table>
 
      <template #modal-footer>
        <div></div>
      </template>
    </gl-modal>
    <span class="gl-opacity-7">{{ actualTitle }}</span>
    <request-warning :html-id="htmlId" :warnings="warnings" />
  </div>
</template>