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
6 changes: 6 additions & 0 deletions workspaces/scorecard/.changeset/cold-experts-stop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@red-hat-developer-hub/backstage-plugin-scorecard-backend': minor
'@red-hat-developer-hub/backstage-plugin-scorecard-common': minor
---

Implemented endpoint to aggregate metrics for scorecard
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): MockEntityBuilder {
this.kind = kind;
return this;
}

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

withSpec(spec: Entity['spec']): MockEntityBuilder {
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 @@ -32,13 +32,15 @@ const metricValues: Omit<DbMetricValue, 'id'>[] = [
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',
Expand Down Expand Up @@ -191,4 +193,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: undefined,
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 @@ -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 @@ -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