Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions workspaces/scorecard/.changeset/seven-guests-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
'@red-hat-developer-hub/backstage-plugin-scorecard-backend': minor
'@red-hat-developer-hub/backstage-plugin-scorecard-common': minor
'@red-hat-developer-hub/backstage-plugin-scorecard': minor
---

Implemented endpoint to aggregate metrics for scorecard metrics

**BREAKING** Update attribute `value` in the `MetricResult` type and update validation to support `null` instead `undefined` for the updated attribute

```diff
export type MetricResult = {
id: string;
status: 'success' | 'error';
metadata: {
title: string;
description: string;
type: MetricType;
history?: boolean;
};
result: {
- value?: MetricValue;
+ value: MetricValue | null;
timestamp: string;
thresholdResult: ThresholdResult;
};
error?: string;
};
```

**BREAKING** Update attribute `evaluation` in the `ThresholdResult` type and update validation to support `null` instead `undefined` for the updated attribute

```diff
export type ThresholdResult = {
status: 'success' | 'error';
- definition: ThresholdConfig | undefined;
+ definition: ThresholdConfig | null;
evaluation: string | undefined; // threshold key the expression evaluated to
error?: string;
};
```
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,42 @@ type BuildMockDatabaseMetricValuesParams = {
metricValues?: DbMetricValue[];
latestEntityMetric?: DbMetricValue[];
countOfExpiredMetrics?: number;
latestAggregatedEntityMetric?: DbMetricValue[];
};

export const mockDatabaseMetricValues = {
createMetricValues: jest.fn(),
readLatestEntityMetricValues: jest.fn(),
cleanupExpiredMetrics: jest.fn(),
readLatestEntityMetricValuesByEntityRefs: jest.fn(),
} as unknown as jest.Mocked<DatabaseMetricValues>;

export const buildMockDatabaseMetricValues = ({
metricValues,
latestEntityMetric,
countOfExpiredMetrics,
latestAggregatedEntityMetric,
}: BuildMockDatabaseMetricValuesParams) => {
const createMetricValues = metricValues
? jest.fn().mockResolvedValue(metricValues)
: mockDatabaseMetricValues.createMetricValues;

const readLatestEntityMetricValues = latestEntityMetric
? jest.fn().mockResolvedValue(latestEntityMetric)
: mockDatabaseMetricValues.readLatestEntityMetricValues;

const cleanupExpiredMetrics = countOfExpiredMetrics
? jest.fn().mockResolvedValue(countOfExpiredMetrics)
: mockDatabaseMetricValues.cleanupExpiredMetrics;

const readLatestEntityMetricValuesByEntityRefs = latestAggregatedEntityMetric
? jest.fn().mockResolvedValue(latestAggregatedEntityMetric)
: mockDatabaseMetricValues.readLatestEntityMetricValuesByEntityRefs;

return {
createMetricValues,
readLatestEntityMetricValues,
cleanupExpiredMetrics,
readLatestEntityMetricValuesByEntityRefs,
} as unknown as jest.Mocked<DatabaseMetricValues>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright Red Hat, Inc.
*
* Licensed 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.
*/

import type { Entity } from '@backstage/catalog-model';

export class MockEntityBuilder {
private kind: string = 'Component';
private metadata: Entity['metadata'] = {
name: 'default-component',
namespace: 'default',
};
private spec: Entity['spec'] = {
owner: 'guests',
};

withKind(kind: string): this {
this.kind = kind;
return this;
}

withMetadata(metadata: Entity['metadata']): this {
this.metadata = metadata;
return this;
}

withSpec(spec: Entity['spec']): this {
this.spec = spec;
return this;
}

build(): Entity {
return {
apiVersion: 'backstage.io/v1alpha1',
kind: this.kind,
metadata: this.metadata,
spec: this.spec,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ export const buildMockMetricProvidersRegistry = ({
? jest.fn().mockReturnValue(provider)
: jest.fn();
const listMetrics = metricsList
? jest.fn().mockReturnValue(metricsList)
? jest.fn().mockImplementation((metricIds?: string[]) => {
if (metricIds && metricIds.length !== 0) {
return metricsList.filter(metric => metricIds.includes(metric.id));
}
return metricsList;
})
: jest.fn();

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,29 @@ import {
TestDatabases,
} from '@backstage/backend-test-utils';
import { DatabaseMetricValues } from './DatabaseMetricValues';
import { DbMetricValue } from './types';
import { DbMetricValueCreate } from './types';
import { migrate } from './migration';

jest.setTimeout(60000);

const metricValues: Omit<DbMetricValue, 'id'>[] = [
const metricValues: Omit<DbMetricValueCreate, 'id'>[] = [
{
catalog_entity_ref: 'component:default/test-service',
metric_id: 'github.metric1',
value: 41,
timestamp: new Date('2023-01-01T00:00:00Z'),
error_message: undefined,
status: 'success',
},
{
catalog_entity_ref: 'component:default/another-service',
metric_id: 'github.metric1',
value: 25,
timestamp: new Date('2023-01-01T00:00:00Z'),
error_message: undefined,
status: 'success',
},
{
catalog_entity_ref: 'component:default/another-service',
metric_id: 'github.metric2',
value: undefined,
timestamp: new Date('2023-01-01T00:00:00Z'),
error_message: 'Failed to fetch metric',
},
Expand Down Expand Up @@ -191,4 +190,69 @@ describe('DatabaseMetricValues', () => {
},
);
});

describe('readLatestEntityMetricValuesByEntityRefs', () => {
it.each(databases.eachSupportedId())(
'should return latest metric values for multiple entities and metrics - %p',
async databaseId => {
const { client, db } = await createDatabase(databaseId);

const baseTime = new Date('2023-01-01T00:00:00Z');
const laterTime = new Date('2023-01-01T01:00:00Z');

await client('metric_values').insert([
{
...metricValues[0],
timestamp: baseTime,
},
{
...metricValues[1],
timestamp: baseTime,
},
{
...metricValues[2],
timestamp: laterTime,
},
{
...metricValues[2],
timestamp: laterTime,
value: 10,
error_message: null,
status: 'success',
},
]);

const result = await db.readLatestEntityMetricValuesByEntityRefs(
[
'component:default/test-service',
'component:default/another-service',
],
['github.metric1', 'github.metric2'],
);

expect(result).toHaveLength(3);

const testServiceMetric1 = result.find(
r =>
r.metric_id === 'github.metric1' &&
r.catalog_entity_ref === 'component:default/test-service',
);
const anotherServiceMetric1 = result.find(
r =>
r.metric_id === 'github.metric1' &&
r.catalog_entity_ref === 'component:default/another-service',
);

const anotherServiceMetric2 = result.find(
r =>
r.metric_id === 'github.metric2' &&
r.catalog_entity_ref === 'component:default/another-service',
);

expect(testServiceMetric1?.value).toBe(41);
expect(anotherServiceMetric1?.value).toBe(25);
expect(anotherServiceMetric2?.value).toBe(10);
},
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import { Knex } from 'knex';
import { DbMetricValue } from './types';
import { DbMetricValueCreate, DbMetricValue } from './types';

export class DatabaseMetricValues {
private readonly tableName = 'metric_values';
Expand All @@ -26,7 +26,7 @@ export class DatabaseMetricValues {
* Insert multiple metric values
*/
async createMetricValues(
metricValues: Omit<DbMetricValue, 'id'>[],
metricValues: Omit<DbMetricValueCreate, 'id'>[],
): Promise<void> {
await this.dbClient(this.tableName).insert(metricValues);
}
Expand Down Expand Up @@ -58,4 +58,23 @@ export class DatabaseMetricValues {
.where('timestamp', '<', olderThan)
.del();
}

/**
* Get the latest metric values for multiple entities and metrics
*/
async readLatestEntityMetricValuesByEntityRefs(
catalog_entity_refs: string[],
metric_ids: string[],
): Promise<DbMetricValue[]> {
return await this.dbClient(this.tableName)
.select('*')
.whereIn(
'id',
this.dbClient(this.tableName)
.max('id')
.whereIn('metric_id', metric_ids)
.whereIn('catalog_entity_ref', catalog_entity_refs)
.groupBy('metric_id', 'catalog_entity_ref'),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,24 @@

import { MetricValue } from '@red-hat-developer-hub/backstage-plugin-scorecard-common';

export type DbMetricValue = {
export type DbMetricValueStatus = 'success' | 'warning' | 'error';

export type DbMetricValueCreate = {
id: number;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need id if it is used everywhere with omit?

catalog_entity_ref: string;
metric_id: string;
value?: MetricValue;
timestamp: Date;
error_message?: string;
status?: 'success' | 'warning' | 'error';
status?: DbMetricValueStatus;
};

export type DbMetricValue = {
id: number;
catalog_entity_ref: string;
metric_id: string;
value: MetricValue | null;
timestamp: Date;
error_message: string | null;
status: DbMetricValueStatus | null;
};
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,11 @@ describe('permissionUtils', () => {
mockPermissionsService,
mockHttpAuthService,
),
).rejects.toThrow(new NotAllowedError('Access to entity metrics denied'));
).rejects.toThrow(
new NotAllowedError(
'Access to "component:default/my-service" entity metrics denied',
),
);

expect(mockPermissionsService.authorize).toHaveBeenCalledWith(
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const checkEntityAccess = async (
);

if (entityAccessDecision[0].result !== AuthorizeResult.ALLOW) {
throw new NotAllowedError('Access to entity metrics denied');
throw new NotAllowedError(`Access to "${entityRef}" entity metrics denied`);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ import {
MockNumberProvider,
MockBooleanProvider,
} from '../../__fixtures__/mockProviders';
import { mockEntity } from '../../__fixtures__/mockEntities';
import { MockEntityBuilder } from '../../__fixtures__/mockEntityBuilder';

describe('MetricProvidersRegistry', () => {
let registry: MetricProvidersRegistry;

const mockEntity = new MockEntityBuilder().build();

beforeEach(() => {
registry = new MetricProvidersRegistry();
});
Expand Down
Loading