@allurereport/web-commons 3.0.0-beta.13 → 3.0.0-beta.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -72,19 +72,40 @@ export const attachmentType = (type) => {
72
72
  icon: "file",
73
73
  };
74
74
  case "text/xml":
75
- case "application/xml":
76
- case "application/json":
77
75
  case "text/json":
78
76
  case "text/yaml":
79
- case "application/yaml":
80
- case "application/x-yaml":
77
+ case "text/javascript":
78
+ case "text/typescript":
79
+ case "text/ruby":
80
+ case "text/python":
81
+ case "text/php":
82
+ case "text/java":
83
+ case "text/csharp":
84
+ case "text/cpp":
85
+ case "text/c":
86
+ case "text/go":
87
+ case "text/rust":
88
+ case "text/swift":
89
+ case "text/kotlin":
90
+ case "text/scala":
91
+ case "text/perl":
92
+ case "text/r":
93
+ case "text/dart":
94
+ case "text/lua":
95
+ case "text/haskell":
96
+ case "text/sql":
81
97
  case "text/x-yaml":
82
98
  case "text/css":
99
+ case "application/yaml":
100
+ case "application/x-yaml":
101
+ case "application/xml":
102
+ case "application/json":
83
103
  return {
84
104
  type: "code",
85
105
  icon: "file",
86
106
  };
87
107
  case "text/plain":
108
+ case "text/markdown":
88
109
  case "text/*":
89
110
  return {
90
111
  type: "text",
@@ -0,0 +1,10 @@
1
+ import type { Statistic, TestStatus } from "@allurereport/core-api";
2
+ export type TestResultSlice = {
3
+ status: TestStatus;
4
+ count: number;
5
+ };
6
+ export type TestResultChartData = {
7
+ percentage: number;
8
+ slices: TestResultSlice[];
9
+ };
10
+ export declare const getPieChartData: (stats: Statistic) => TestResultChartData;
@@ -0,0 +1,20 @@
1
+ import { statusesList } from "@allurereport/core-api";
2
+ import { d3Arc, d3Pie, getPercentage } from "../charts.js";
3
+ export const getPieChartData = (stats) => {
4
+ const convertedStatuses = statusesList
5
+ .filter((status) => !!stats?.[status])
6
+ .map((status) => ({
7
+ status,
8
+ count: stats[status],
9
+ }));
10
+ const arcsData = d3Pie(convertedStatuses);
11
+ const slices = arcsData.map((arcData) => ({
12
+ d: d3Arc(arcData),
13
+ ...arcData.data,
14
+ }));
15
+ const percentage = getPercentage(stats.passed ?? 0, stats.total);
16
+ return {
17
+ slices,
18
+ percentage,
19
+ };
20
+ };
@@ -0,0 +1,3 @@
1
+ import type { Statistic } from "@allurereport/core-api";
2
+ import type { PieChartData, PieChartOptions } from "../charts.js";
3
+ export declare const getPieChartDataDashboard: (stats: Statistic, chartOptions: PieChartOptions) => PieChartData;
@@ -0,0 +1,22 @@
1
+ import { statusesList } from "@allurereport/core-api";
2
+ import { d3Arc, d3Pie, getPercentage } from "../charts.js";
3
+ export const getPieChartDataDashboard = (stats, chartOptions) => {
4
+ const convertedStatuses = statusesList
5
+ .filter((status) => !!stats?.[status])
6
+ .map((status) => ({
7
+ status,
8
+ count: stats[status],
9
+ }));
10
+ const arcsData = d3Pie(convertedStatuses);
11
+ const slices = arcsData.map((arcData) => ({
12
+ d: d3Arc(arcData),
13
+ ...arcData.data,
14
+ }));
15
+ const percentage = getPercentage(stats.passed ?? 0, stats.total);
16
+ return {
17
+ type: chartOptions.type,
18
+ title: chartOptions?.title,
19
+ slices,
20
+ percentage,
21
+ };
22
+ };
@@ -0,0 +1,3 @@
1
+ import type { HistoryDataPoint, TestResult } from "@allurereport/core-api";
2
+ import type { SeverityTrendChartData, TrendChartOptions } from "../charts.js";
3
+ export declare const getSeverityTrendData: (testResults: TestResult[], reportName: string, historyPoints: HistoryDataPoint[], chartOptions: TrendChartOptions) => SeverityTrendChartData;
@@ -0,0 +1,46 @@
1
+ import { severityLabelName, severityLevels } from "@allurereport/core-api";
2
+ import { createEmptySeries, createEmptyStats, getTrendDataGeneric, mergeTrendDataGeneric, normalizeStatistic, } from "../charts.js";
3
+ export const getSeverityTrendData = (testResults, reportName, historyPoints, chartOptions) => {
4
+ const { limit } = chartOptions;
5
+ const historyLimit = limit && limit > 0 ? Math.max(0, limit - 1) : undefined;
6
+ const limitedHistoryPoints = historyLimit !== undefined ? historyPoints.slice(-historyLimit) : historyPoints;
7
+ const firstOriginalIndex = historyLimit !== undefined ? Math.max(0, historyPoints.length - historyLimit) : 0;
8
+ const convertedHistoryPoints = limitedHistoryPoints.map((point, index) => {
9
+ const originalIndex = firstOriginalIndex + index;
10
+ return {
11
+ name: point.name,
12
+ originalIndex,
13
+ statistic: Object.values(point.testResults).reduce((stat, test) => {
14
+ const severityLabel = test.labels?.find((label) => label.name === severityLabelName);
15
+ const severity = severityLabel?.value?.toLowerCase();
16
+ if (severity) {
17
+ stat[severity] = (stat[severity] ?? 0) + 1;
18
+ }
19
+ return stat;
20
+ }, createEmptyStats(severityLevels)),
21
+ };
22
+ });
23
+ const currentSeverityStats = testResults.reduce((acc, test) => {
24
+ const severityLabel = test.labels.find((label) => label.name === severityLabelName);
25
+ const severity = severityLabel?.value?.toLowerCase();
26
+ if (severity) {
27
+ acc[severity] = (acc[severity] ?? 0) + 1;
28
+ }
29
+ return acc;
30
+ }, createEmptyStats(severityLevels));
31
+ const currentTrendData = getTrendDataGeneric(normalizeStatistic(currentSeverityStats, severityLevels), reportName, historyPoints.length + 1, severityLevels, chartOptions);
32
+ const historicalTrendData = convertedHistoryPoints.reduce((acc, historyPoint) => {
33
+ const trendDataPart = getTrendDataGeneric(normalizeStatistic(historyPoint.statistic, severityLevels), historyPoint.name, historyPoint.originalIndex + 1, severityLevels, chartOptions);
34
+ return mergeTrendDataGeneric(acc, trendDataPart, severityLevels);
35
+ }, {
36
+ type: chartOptions.type,
37
+ dataType: chartOptions.dataType,
38
+ title: chartOptions.title,
39
+ points: {},
40
+ slices: {},
41
+ series: createEmptySeries(severityLevels),
42
+ min: Infinity,
43
+ max: -Infinity,
44
+ });
45
+ return mergeTrendDataGeneric(historicalTrendData, currentTrendData, severityLevels);
46
+ };
@@ -0,0 +1,3 @@
1
+ import type { HistoryDataPoint, Statistic } from "@allurereport/core-api";
2
+ import type { StatusTrendChartData, TrendChartOptions } from "../charts.js";
3
+ export declare const getStatusTrendData: (currentStatistic: Statistic, reportName: string, historyPoints: HistoryDataPoint[], chartOptions: TrendChartOptions) => StatusTrendChartData;
@@ -0,0 +1,37 @@
1
+ import { statusesList } from "@allurereport/core-api";
2
+ import { createEmptySeries, createEmptyStats, getTrendDataGeneric, mergeTrendDataGeneric, normalizeStatistic } from "../charts.js";
3
+ export const getStatusTrendData = (currentStatistic, reportName, historyPoints, chartOptions) => {
4
+ const { limit } = chartOptions;
5
+ const historyLimit = limit && limit > 0 ? Math.max(0, limit - 1) : undefined;
6
+ const limitedHistoryPoints = historyLimit !== undefined ? historyPoints.slice(-historyLimit) : historyPoints;
7
+ const firstOriginalIndex = historyLimit !== undefined ? Math.max(0, historyPoints.length - historyLimit) : 0;
8
+ const convertedHistoryPoints = limitedHistoryPoints.map((point, index) => {
9
+ const originalIndex = firstOriginalIndex + index;
10
+ return {
11
+ name: point.name,
12
+ originalIndex,
13
+ statistic: Object.values(point.testResults).reduce((stat, test) => {
14
+ if (test.status) {
15
+ stat[test.status] = (stat[test.status] ?? 0) + 1;
16
+ stat.total = (stat.total ?? 0) + 1;
17
+ }
18
+ return stat;
19
+ }, { total: 0, ...createEmptyStats(statusesList) }),
20
+ };
21
+ });
22
+ const currentTrendData = getTrendDataGeneric(normalizeStatistic(currentStatistic, statusesList), reportName, historyPoints.length + 1, statusesList, chartOptions);
23
+ const historicalTrendData = convertedHistoryPoints.reduce((acc, historyPoint) => {
24
+ const trendDataPart = getTrendDataGeneric(normalizeStatistic(historyPoint.statistic, statusesList), historyPoint.name, historyPoint.originalIndex + 1, statusesList, chartOptions);
25
+ return mergeTrendDataGeneric(acc, trendDataPart, statusesList);
26
+ }, {
27
+ type: chartOptions.type,
28
+ dataType: chartOptions.dataType,
29
+ title: chartOptions.title,
30
+ points: {},
31
+ slices: {},
32
+ series: createEmptySeries(statusesList),
33
+ min: Infinity,
34
+ max: -Infinity,
35
+ });
36
+ return mergeTrendDataGeneric(historicalTrendData, currentTrendData, statusesList);
37
+ };
@@ -0,0 +1,105 @@
1
+ import type { SeverityLevel, TestResult, TestStatus } from "@allurereport/core-api";
2
+ import type { PieArcDatum } from "d3-shape";
3
+ export type BasePieSlice = Pick<PieSlice, "status" | "count">;
4
+ export declare const d3Arc: import("d3-shape").Arc<any, PieArcDatum<BasePieSlice>>;
5
+ export declare const d3Pie: import("d3-shape").Pie<any, BasePieSlice>;
6
+ export declare const getPercentage: (value: number, total: number) => number;
7
+ export declare enum ChartType {
8
+ Trend = "trend",
9
+ Pie = "pie"
10
+ }
11
+ export declare enum ChartData {
12
+ Status = "status",
13
+ Severity = "severity"
14
+ }
15
+ export type ChartMode = "raw" | "percent";
16
+ export type ChartId = string;
17
+ export type ExecutionIdFn = (executionOrder: number) => string;
18
+ export type ExecutionNameFn = (executionOrder: number) => string;
19
+ export type TrendMetadataFnOverrides = {
20
+ executionIdAccessor?: ExecutionIdFn;
21
+ executionNameAccessor?: ExecutionNameFn;
22
+ };
23
+ export type TrendChartOptions = {
24
+ type: ChartType.Trend;
25
+ dataType: ChartData;
26
+ mode?: ChartMode;
27
+ title?: string;
28
+ limit?: number;
29
+ metadata?: TrendMetadataFnOverrides;
30
+ };
31
+ export type TrendPointId = string;
32
+ export type TrendSliceId = string;
33
+ export type BaseMetadata = Record<string, unknown>;
34
+ export interface BaseTrendSliceMetadata extends Record<string, unknown> {
35
+ executionId: string;
36
+ executionName: string;
37
+ }
38
+ export type TrendSliceMetadata<Metadata extends BaseMetadata> = BaseTrendSliceMetadata & Metadata;
39
+ export type TrendPoint = {
40
+ x: string;
41
+ y: number;
42
+ };
43
+ export type TrendSlice<Metadata extends BaseMetadata> = {
44
+ min: number;
45
+ max: number;
46
+ metadata: TrendSliceMetadata<Metadata>;
47
+ };
48
+ export type GenericTrendChartData<Metadata extends BaseMetadata, SeriesType extends string> = {
49
+ type: ChartType.Trend;
50
+ dataType: ChartData;
51
+ title?: string;
52
+ points: Record<TrendPointId, TrendPoint>;
53
+ slices: Record<TrendSliceId, TrendSlice<Metadata>>;
54
+ series: Record<SeriesType, TrendPointId[]>;
55
+ min: number;
56
+ max: number;
57
+ };
58
+ export interface StatusMetadata extends BaseTrendSliceMetadata {
59
+ }
60
+ export type StatusTrendSliceMetadata = TrendSliceMetadata<StatusMetadata>;
61
+ export type StatusTrendSlice = TrendSlice<StatusTrendSliceMetadata>;
62
+ export type StatusTrendChartData = GenericTrendChartData<StatusTrendSliceMetadata, TestStatus>;
63
+ export interface SeverityMetadata extends BaseTrendSliceMetadata {
64
+ }
65
+ export type SeverityTrendSliceMetadata = TrendSliceMetadata<SeverityMetadata>;
66
+ export type SeverityTrendSlice = TrendSlice<SeverityTrendSliceMetadata>;
67
+ export type SeverityTrendChartData = GenericTrendChartData<SeverityTrendSliceMetadata, SeverityLevel>;
68
+ export type TrendChartData = StatusTrendChartData | SeverityTrendChartData;
69
+ export type PieChartOptions = {
70
+ type: ChartType.Pie;
71
+ title?: string;
72
+ };
73
+ export type PieSlice = {
74
+ status: TestStatus;
75
+ count: number;
76
+ d: string | null;
77
+ };
78
+ export type PieChartData = {
79
+ type: ChartType.Pie;
80
+ title?: string;
81
+ slices: PieSlice[];
82
+ percentage: number;
83
+ };
84
+ export type GeneratedChartData = TrendChartData | PieChartData;
85
+ export type GeneratedChartsData = Record<ChartId, GeneratedChartData>;
86
+ export type ChartOptions = TrendChartOptions | PieChartOptions;
87
+ export type DashboardOptions = {
88
+ reportName?: string;
89
+ singleFile?: boolean;
90
+ logo?: string;
91
+ theme?: "light" | "dark";
92
+ reportLanguage?: "en" | "ru";
93
+ layout?: ChartOptions[];
94
+ filter?: (testResult: TestResult) => boolean;
95
+ };
96
+ export type TrendDataType = TestStatus | SeverityLevel;
97
+ export type TrendCalculationResult<T extends TrendDataType> = {
98
+ points: Record<TrendPointId, TrendPoint>;
99
+ series: Record<T, TrendPointId[]>;
100
+ };
101
+ export declare const createEmptyStats: <T extends TrendDataType>(items: readonly T[]) => Record<T, number>;
102
+ export declare const createEmptySeries: <T extends TrendDataType>(items: readonly T[]) => Record<T, string[]>;
103
+ export declare const normalizeStatistic: <T extends TrendDataType>(statistic: Partial<Record<T, number>>, itemType: readonly T[]) => Record<T, number>;
104
+ export declare const mergeTrendDataGeneric: <M extends BaseTrendSliceMetadata, T extends TrendDataType>(trendData: GenericTrendChartData<M, T>, trendDataPart: GenericTrendChartData<M, T>, itemType: readonly T[]) => GenericTrendChartData<M, T>;
105
+ export declare const getTrendDataGeneric: <M extends BaseTrendSliceMetadata, T extends TrendDataType>(stats: Record<T, number>, reportName: string, executionOrder: number, itemType: readonly T[], chartOptions: TrendChartOptions) => GenericTrendChartData<M, T>;
package/dist/charts.js ADDED
@@ -0,0 +1,117 @@
1
+ import { arc, pie } from "d3-shape";
2
+ export const d3Arc = arc().innerRadius(40).outerRadius(50).cornerRadius(2).padAngle(0.03);
3
+ export const d3Pie = pie()
4
+ .value((d) => d.count)
5
+ .padAngle(0.03)
6
+ .sortValues((a, b) => a - b);
7
+ export const getPercentage = (value, total) => Math.floor((value / total) * 10000) / 100;
8
+ export var ChartType;
9
+ (function (ChartType) {
10
+ ChartType["Trend"] = "trend";
11
+ ChartType["Pie"] = "pie";
12
+ })(ChartType || (ChartType = {}));
13
+ export var ChartData;
14
+ (function (ChartData) {
15
+ ChartData["Status"] = "status";
16
+ ChartData["Severity"] = "severity";
17
+ })(ChartData || (ChartData = {}));
18
+ export const createEmptyStats = (items) => items.reduce((acc, item) => ({ ...acc, [item]: 0 }), {});
19
+ export const createEmptySeries = (items) => items.reduce((acc, item) => ({ ...acc, [item]: [] }), {});
20
+ export const normalizeStatistic = (statistic, itemType) => {
21
+ return itemType.reduce((acc, item) => {
22
+ acc[item] = statistic[item] ?? 0;
23
+ return acc;
24
+ }, {});
25
+ };
26
+ const calculateRawValues = (stats, executionId, itemType) => {
27
+ const points = {};
28
+ const series = createEmptySeries(itemType);
29
+ itemType.forEach((item) => {
30
+ const pointId = `${executionId}-${item}`;
31
+ const value = stats[item] ?? 0;
32
+ points[pointId] = {
33
+ x: executionId,
34
+ y: value,
35
+ };
36
+ series[item].push(pointId);
37
+ });
38
+ return { points, series };
39
+ };
40
+ const calculatePercentValues = (stats, executionId, itemType) => {
41
+ const points = {};
42
+ const series = createEmptySeries(itemType);
43
+ const values = Object.values(stats);
44
+ const total = values.reduce((sum, value) => sum + value, 0);
45
+ if (total === 0) {
46
+ return { points, series };
47
+ }
48
+ itemType.forEach((item) => {
49
+ const pointId = `${executionId}-${item}`;
50
+ const value = stats[item] ?? 0;
51
+ points[pointId] = {
52
+ x: executionId,
53
+ y: (value / total) * 100,
54
+ };
55
+ series[item].push(pointId);
56
+ });
57
+ return { points, series };
58
+ };
59
+ export const mergeTrendDataGeneric = (trendData, trendDataPart, itemType) => {
60
+ return {
61
+ ...trendData,
62
+ points: {
63
+ ...trendData.points,
64
+ ...trendDataPart.points,
65
+ },
66
+ slices: {
67
+ ...trendData.slices,
68
+ ...trendDataPart.slices,
69
+ },
70
+ series: Object.entries(trendDataPart.series).reduce((series, [group, pointIds]) => {
71
+ if (Array.isArray(pointIds)) {
72
+ return {
73
+ ...series,
74
+ [group]: [...(trendData.series?.[group] || []), ...pointIds],
75
+ };
76
+ }
77
+ return series;
78
+ }, trendData.series || createEmptySeries(itemType)),
79
+ min: Math.min(trendData.min ?? Infinity, trendDataPart.min),
80
+ max: Math.max(trendData.max ?? -Infinity, trendDataPart.max),
81
+ };
82
+ };
83
+ export const getTrendDataGeneric = (stats, reportName, executionOrder, itemType, chartOptions) => {
84
+ const { type, dataType, title, mode = "raw", metadata = {} } = chartOptions;
85
+ const { executionIdAccessor, executionNameAccessor } = metadata;
86
+ const executionId = executionIdAccessor ? executionIdAccessor(executionOrder) : `execution-${executionOrder}`;
87
+ const { points, series } = mode === "percent"
88
+ ? calculatePercentValues(stats, executionId, itemType)
89
+ : calculateRawValues(stats, executionId, itemType);
90
+ const slices = {};
91
+ const pointsAsArray = Object.values(points);
92
+ const pointsCount = pointsAsArray.length;
93
+ const values = pointsAsArray.map((point) => point.y);
94
+ const min = pointsCount ? Math.min(...values) : 0;
95
+ const max = pointsCount ? Math.max(...values) : 0;
96
+ if (pointsCount > 0) {
97
+ const executionName = executionNameAccessor ? executionNameAccessor(executionOrder) : reportName;
98
+ slices[executionId] = {
99
+ min,
100
+ max,
101
+ metadata: {
102
+ executionId,
103
+ executionName,
104
+ },
105
+ };
106
+ }
107
+ return {
108
+ type,
109
+ dataType,
110
+ title,
111
+ points,
112
+ slices,
113
+ series,
114
+ min,
115
+ max,
116
+ };
117
+ };
package/dist/index.d.ts CHANGED
@@ -3,3 +3,8 @@ export * from "./static.js";
3
3
  export * from "./attachments.js";
4
4
  export * from "./i18n.js";
5
5
  export * from "./strings.js";
6
+ export * from "./charts.js";
7
+ export * from "./charts/getPieChart.js";
8
+ export * from "./charts/getPieChartDashboard.js";
9
+ export * from "./charts/getSeverityTrendData.js";
10
+ export * from "./charts/getStatusTrendData.js";
package/dist/index.js CHANGED
@@ -3,3 +3,8 @@ export * from "./static.js";
3
3
  export * from "./attachments.js";
4
4
  export * from "./i18n.js";
5
5
  export * from "./strings.js";
6
+ export * from "./charts.js";
7
+ export * from "./charts/getPieChart.js";
8
+ export * from "./charts/getPieChartDashboard.js";
9
+ export * from "./charts/getSeverityTrendData.js";
10
+ export * from "./charts/getStatusTrendData.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@allurereport/web-commons",
3
- "version": "3.0.0-beta.13",
3
+ "version": "3.0.0-beta.14",
4
4
  "description": "Collection of utilities used across the web Allure reports",
5
5
  "keywords": [
6
6
  "allure",
@@ -23,16 +23,18 @@
23
23
  "clean": "rimraf ./dist"
24
24
  },
25
25
  "dependencies": {
26
- "@allurereport/core-api": "3.0.0-beta.13"
26
+ "@allurereport/core-api": "3.0.0-beta.14"
27
27
  },
28
28
  "devDependencies": {
29
29
  "@stylistic/eslint-plugin": "^2.6.1",
30
+ "@types/d3-shape": "^3.1.6",
30
31
  "@types/eslint": "^8.56.11",
31
32
  "@typescript-eslint/eslint-plugin": "^8.0.0",
32
33
  "@typescript-eslint/parser": "^8.0.0",
33
34
  "@vitest/runner": "^2.1.8",
34
35
  "allure-js-commons": "^3.0.9",
35
36
  "allure-vitest": "^3.0.9",
37
+ "d3-shape": "^3.2.0",
36
38
  "eslint": "^8.57.0",
37
39
  "eslint-config-prettier": "^9.1.0",
38
40
  "eslint-plugin-import": "^2.29.1",