debian-mirror-gitlab/app/assets/javascripts/analytics/shared/utils.js

156 lines
5.4 KiB
JavaScript
Raw Normal View History

2023-01-13 00:05:48 +05:30
import { flatten } from 'lodash';
2022-10-11 01:57:18 +05:30
import dateFormat from '~/lib/dateformat';
2022-04-04 11:22:00 +05:30
import { slugify } from '~/lib/utils/text_utility';
2023-05-27 22:25:52 +05:30
import { joinPaths } from '~/lib/utils/url_utility';
2021-11-18 22:05:49 +05:30
import { urlQueryToFilter } from '~/vue_shared/components/filtered_search_bar/filtered_search_utils';
2023-01-13 00:05:48 +05:30
import { dateFormats, METRICS_POPOVER_CONTENT } from './constants';
2021-11-11 11:23:49 +05:30
2021-09-30 23:02:18 +05:30
export const filterBySearchTerm = (data = [], searchTerm = '', filterByKey = 'name') => {
if (!searchTerm?.length) return data;
return data.filter((item) => item[filterByKey].toLowerCase().includes(searchTerm.toLowerCase()));
};
2021-11-11 11:23:49 +05:30
export const toYmd = (date) => dateFormat(date, dateFormats.isoDate);
2021-11-18 22:05:49 +05:30
/**
* Takes a url and extracts query parameters used for the shared
* filter bar
*
* @param {string} url The URL to extract query parameters from
* @returns {Object}
*/
export const extractFilterQueryParameters = (url = '') => {
const {
2022-08-27 11:52:29 +05:30
source_branch_name: selectedSourceBranch = null,
target_branch_name: selectedTargetBranch = null,
author_username: selectedAuthor = null,
milestone_title: selectedMilestone = null,
assignee_username: selectedAssigneeList = [],
label_name: selectedLabelList = [],
2021-11-18 22:05:49 +05:30
} = urlQueryToFilter(url);
return {
2022-08-27 11:52:29 +05:30
selectedSourceBranch,
selectedTargetBranch,
selectedAuthor,
selectedMilestone,
selectedAssigneeList,
selectedLabelList,
2021-11-18 22:05:49 +05:30
};
};
/**
* Takes a url and extracts sorting and pagination query parameters into an object
*
* @param {string} url The URL to extract query parameters from
* @returns {Object}
*/
export const extractPaginationQueryParameters = (url = '') => {
const { sort, direction, page } = urlQueryToFilter(url);
return {
sort: sort?.value || null,
direction: direction?.value || null,
page: page?.value || null,
};
};
export const getDataZoomOption = ({
totalItems = 0,
maxItemsPerPage = 40,
dataZoom = [{ type: 'slider', bottom: 10, start: 0 }],
}) => {
if (totalItems <= maxItemsPerPage) {
return {};
}
const intervalEnd = Math.ceil((maxItemsPerPage / totalItems) * 100);
return dataZoom.map((item) => {
return {
...item,
end: intervalEnd,
};
});
};
2022-04-04 11:22:00 +05:30
export const removeFlash = (type = 'alert') => {
2023-04-23 21:23:45 +05:30
// flash-warning don't have dismiss button.
document.querySelector(`.flash-${type} .js-close`)?.click();
2022-04-04 11:22:00 +05:30
};
/**
* Prepares metric data to be rendered in the metric_card component
*
* @param {MetricData[]} data - The metric data to be rendered
* @param {Object} popoverContent - Key value pair of data to display in the popover
* @returns {TransformedMetricData[]} An array of metrics ready to render in the metric_card
*/
export const prepareTimeMetricsData = (data = [], popoverContent = {}) =>
data.map(({ title: label, identifier, ...rest }) => {
const metricIdentifier = identifier || slugify(label);
return {
...rest,
label,
identifier: metricIdentifier,
description: popoverContent[metricIdentifier]?.description || '',
};
});
2023-01-13 00:05:48 +05:30
const requestData = ({ request, endpoint, requestPath, params, name }) => {
return request({ endpoint, params, requestPath })
.then(({ data }) => data)
.catch(() => {
throw new Error(name);
});
};
/**
* Takes a configuration array of metrics requests (key metrics and DORA) and returns
* a flat array of all the responses. Different metrics are retrieved from different endpoints
* additionally we only support certain metrics for FOSS users.
*
* @param {Array} requests - array of metric api requests to be made
* @param {String} requestPath - path for the group / project we are requesting
* @param {Object} params - optional parameters to filter, including `created_after` and `created_before` dates
* @returns a flat array of metrics
*/
export const fetchMetricsData = (requests = [], requestPath, params) => {
const promises = requests.map((r) => requestData({ ...r, requestPath, params }));
return Promise.all(promises).then((responses) =>
prepareTimeMetricsData(flatten(responses), METRICS_POPOVER_CONTENT),
);
};
2023-05-27 22:25:52 +05:30
/**
* Generates a URL link to the VSD dashboard based on the group
* and project paths passed into the method.
*
* @param {String} groupPath - Path of the specified group
* @param {Array} projectPaths - Array of project paths to include in the `query` parameter
* @returns a URL or blank string if there is no groupPath set
*/
2023-06-20 00:43:36 +05:30
export const generateValueStreamsDashboardLink = (namespacePath, projectPaths = []) => {
if (namespacePath.length) {
2023-05-27 22:25:52 +05:30
const query = projectPaths.length ? `?query=${projectPaths.join(',')}` : '';
const dashboardsSlug = '/-/analytics/dashboards/value_streams_dashboard';
2023-06-20 00:43:36 +05:30
const segments = [gon.relative_url_root || '', '/', namespacePath, dashboardsSlug];
2023-05-27 22:25:52 +05:30
return joinPaths(...segments).concat(query);
}
return '';
};
2023-06-20 00:43:36 +05:30
/**
* Extracts the relevant feature and license flags needed for VSA
*
* @param {Object} gon the global `window.gon` object populated when the page loads
* @returns an object containing the extracted feature flags and their boolean status
*/
export const extractVSAFeaturesFromGON = () => ({
// licensed feature toggles
cycleAnalyticsForGroups: Boolean(gon?.licensed_features?.cycleAnalyticsForGroups),
cycleAnalyticsForProjects: Boolean(gon?.licensed_features?.cycleAnalyticsForProjects),
groupLevelAnalyticsDashboard: Boolean(gon?.licensed_features?.groupLevelAnalyticsDashboard),
// feature flags
vsaGroupAndProjectParity: Boolean(gon?.features?.vsaGroupAndProjectParity),
});