|
| 1 | +import { Color } from '@hypertrace/common'; |
| 2 | +import { MetricAggregationType } from '@hypertrace/distributed-tracing'; |
| 3 | +import { MetricAggregationSpecificationModel } from '@hypertrace/observability'; |
| 4 | +import { createServiceFactory } from '@ngneat/spectator/jest'; |
| 5 | +import { TopologyMetricCategoryModel } from '../../data/graphql/topology/metrics/topology-metric-category.model'; |
| 6 | +import { TopologyMetricWithCategoryModel } from '../../data/graphql/topology/metrics/topology-metric-with-category.model'; |
| 7 | +import { TopologyMetricsData } from '../../data/graphql/topology/metrics/topology-metrics.model'; |
| 8 | +import { TopologyDataSourceModelPropertiesService } from './topology-data-source-model-properties.service'; |
| 9 | + |
| 10 | +describe('TopologyDataSourceModelPropertiesService', () => { |
| 11 | + const createService = createServiceFactory({ |
| 12 | + service: TopologyDataSourceModelPropertiesService |
| 13 | + }); |
| 14 | + |
| 15 | + const createCategoryModel = ( |
| 16 | + name: string, |
| 17 | + minValue: number, |
| 18 | + fillColor: Color, |
| 19 | + strokeColor: Color, |
| 20 | + focusColor: Color, |
| 21 | + maxValue?: number |
| 22 | + ): TopologyMetricCategoryModel => { |
| 23 | + const categoryModel = new TopologyMetricCategoryModel(); |
| 24 | + categoryModel.name = name; |
| 25 | + categoryModel.minValue = minValue; |
| 26 | + categoryModel.maxValue = maxValue; |
| 27 | + categoryModel.fillColor = fillColor; |
| 28 | + categoryModel.strokeColor = strokeColor; |
| 29 | + categoryModel.focusColor = focusColor; |
| 30 | + |
| 31 | + return categoryModel; |
| 32 | + }; |
| 33 | + |
| 34 | + const createSpecificationModel = (metric: string, aggregation: MetricAggregationType) => { |
| 35 | + const specification = new MetricAggregationSpecificationModel(); |
| 36 | + specification.metric = metric; |
| 37 | + specification.aggregation = aggregation; |
| 38 | + |
| 39 | + specification.modelOnInit(); |
| 40 | + |
| 41 | + return specification; |
| 42 | + }; |
| 43 | + |
| 44 | + const createMetricWithCategory = ( |
| 45 | + spec: MetricAggregationSpecificationModel, |
| 46 | + categories: TopologyMetricCategoryModel[] |
| 47 | + ) => { |
| 48 | + const model = new TopologyMetricWithCategoryModel(); |
| 49 | + model.specification = spec; |
| 50 | + model.categories = categories; |
| 51 | + |
| 52 | + return model; |
| 53 | + }; |
| 54 | + |
| 55 | + test('should return correct results', () => { |
| 56 | + const spectator = createService(); |
| 57 | + |
| 58 | + const nodePrimary = createMetricWithCategory( |
| 59 | + createSpecificationModel('node-metric-1', MetricAggregationType.Average), |
| 60 | + [ |
| 61 | + createCategoryModel('node-first-1', 0, Color.Blue2, Color.Blue3, Color.Blue4, 10), |
| 62 | + createCategoryModel('node-second-1', 10, Color.Red1, Color.Red3, Color.Red4, 50), |
| 63 | + createCategoryModel('node-third-1', 50, Color.Blue2, Color.Blue3, Color.Blue4) |
| 64 | + ] |
| 65 | + ); |
| 66 | + |
| 67 | + const nodeSecondary = createMetricWithCategory( |
| 68 | + createSpecificationModel('node-metric-2', MetricAggregationType.Average), |
| 69 | + [ |
| 70 | + createCategoryModel('node-first-2', 0, Color.Blue2, Color.Blue3, Color.Blue4, 10), |
| 71 | + createCategoryModel('node-second-2', 10, Color.Red1, Color.Red3, Color.Red4, 50), |
| 72 | + createCategoryModel('node-third-2', 50, Color.Blue2, Color.Blue3, Color.Blue4) |
| 73 | + ] |
| 74 | + ); |
| 75 | + |
| 76 | + const nodeOthers = [ |
| 77 | + createMetricWithCategory(createSpecificationModel('node-metric-5', MetricAggregationType.Average), [ |
| 78 | + createCategoryModel('node-others-2', 0, Color.Blue2, Color.Blue3, Color.Blue4, 10) |
| 79 | + ]) |
| 80 | + ]; |
| 81 | + |
| 82 | + const nodeMetrics: TopologyMetricsData = { |
| 83 | + primary: nodePrimary, |
| 84 | + secondary: nodeSecondary, |
| 85 | + others: nodeOthers |
| 86 | + }; |
| 87 | + |
| 88 | + const edgePrimary = createMetricWithCategory( |
| 89 | + createSpecificationModel('edge-metric-3', MetricAggregationType.Average), |
| 90 | + [ |
| 91 | + createCategoryModel('edge-first-1', 0, Color.Blue2, Color.Blue3, Color.Blue4, 10), |
| 92 | + createCategoryModel('edge-second-1', 10, Color.Red1, Color.Red3, Color.Red4, 50), |
| 93 | + createCategoryModel('edge-third-1', 50, Color.Blue2, Color.Blue3, Color.Blue4) |
| 94 | + ] |
| 95 | + ); |
| 96 | + |
| 97 | + const edgeSecondary = createMetricWithCategory( |
| 98 | + createSpecificationModel('metric-4', MetricAggregationType.Average), |
| 99 | + [ |
| 100 | + createCategoryModel('edge-first-2', 0, Color.Blue2, Color.Blue3, Color.Blue4, 10), |
| 101 | + createCategoryModel('edge-second-2', 10, Color.Red1, Color.Red3, Color.Red4, 50), |
| 102 | + createCategoryModel('edge-third-2', 50, Color.Blue2, Color.Blue3, Color.Blue4) |
| 103 | + ] |
| 104 | + ); |
| 105 | + |
| 106 | + const edgeOthers = [ |
| 107 | + createMetricWithCategory(createSpecificationModel('metric-4', MetricAggregationType.Average), [ |
| 108 | + createCategoryModel('edge-others-2', 0, Color.Blue2, Color.Blue3, Color.Blue4, 10) |
| 109 | + ]) |
| 110 | + ]; |
| 111 | + |
| 112 | + const edgeMetrics: TopologyMetricsData = { |
| 113 | + primary: edgePrimary, |
| 114 | + secondary: edgeSecondary, |
| 115 | + others: edgeOthers |
| 116 | + }; |
| 117 | + spectator.service.setModelProperties(nodeMetrics, edgeMetrics); |
| 118 | + |
| 119 | + expect(spectator.service.getPrimaryNodeMetric()).toEqual(nodePrimary); |
| 120 | + expect(spectator.service.getSecondaryNodeMetric()).toEqual(nodeSecondary); |
| 121 | + |
| 122 | + expect(spectator.service.getPrimaryEdgeMetric()).toEqual(edgePrimary); |
| 123 | + expect(spectator.service.getSecondaryEdgeMetric()).toEqual(edgeSecondary); |
| 124 | + }); |
| 125 | +}); |
0 commit comments