Merge pull request #1017 from spotify/shmidt-i/location-update-results
Location update results
This commit is contained in:
@@ -45,6 +45,9 @@
|
||||
"typescript": "^3.9.2"
|
||||
},
|
||||
"nodemonConfig": {
|
||||
"watch": "./dist"
|
||||
"watch": [
|
||||
"./dist",
|
||||
"node_modules/@backstage*"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
/*
|
||||
* Copyright 2020 Spotify AB
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
@@ -14,9 +14,14 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { Database } from '../database';
|
||||
import { Database, DatabaseLocationUpdateLogEvent } from '../database';
|
||||
import { IngestionModel } from '../ingestion/types';
|
||||
import { AddLocation, Location, LocationsCatalog } from './types';
|
||||
import {
|
||||
AddLocation,
|
||||
Location,
|
||||
LocationsCatalog,
|
||||
LocationResponse,
|
||||
} from './types';
|
||||
|
||||
export class DatabaseLocationsCatalog implements LocationsCatalog {
|
||||
constructor(
|
||||
@@ -50,13 +55,36 @@ export class DatabaseLocationsCatalog implements LocationsCatalog {
|
||||
await this.database.removeLocation(id);
|
||||
}
|
||||
|
||||
async locations(): Promise<Location[]> {
|
||||
async locations(): Promise<LocationResponse[]> {
|
||||
const items = await this.database.locations();
|
||||
return items;
|
||||
return items.map(({ message, status, timestamp, ...data }) => ({
|
||||
currentStatus: {
|
||||
message,
|
||||
status,
|
||||
timestamp,
|
||||
},
|
||||
data,
|
||||
}));
|
||||
}
|
||||
|
||||
async location(id: string): Promise<Location> {
|
||||
const item = await this.database.location(id);
|
||||
return item;
|
||||
async locationHistory(id: string): Promise<DatabaseLocationUpdateLogEvent[]> {
|
||||
return this.database.locationHistory(id);
|
||||
}
|
||||
|
||||
async location(id: string): Promise<LocationResponse> {
|
||||
const {
|
||||
message,
|
||||
status,
|
||||
timestamp,
|
||||
...data
|
||||
} = await this.database.location(id);
|
||||
return {
|
||||
currentStatus: {
|
||||
message,
|
||||
status,
|
||||
timestamp,
|
||||
},
|
||||
data,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,12 +41,31 @@ export type EntitiesCatalog = {
|
||||
// Locations
|
||||
//
|
||||
|
||||
export type LocationUpdateStatus = {
|
||||
timestamp: string | null;
|
||||
status: string | null;
|
||||
message: string | null;
|
||||
};
|
||||
export type LocationUpdateLogEvent = {
|
||||
id: string;
|
||||
status: 'fail' | 'success';
|
||||
location_id: string;
|
||||
entity_name: string;
|
||||
created_at?: string;
|
||||
message?: string;
|
||||
};
|
||||
|
||||
export type Location = {
|
||||
id: string;
|
||||
type: string;
|
||||
target: string;
|
||||
};
|
||||
|
||||
export type LocationResponse = {
|
||||
data: Location;
|
||||
currentStatus: LocationUpdateStatus;
|
||||
};
|
||||
|
||||
export type AddLocation = {
|
||||
type: string;
|
||||
target: string;
|
||||
@@ -62,6 +81,7 @@ export const addLocationSchema: yup.Schema<AddLocation> = yup
|
||||
export type LocationsCatalog = {
|
||||
addLocation(location: AddLocation): Promise<Location>;
|
||||
removeLocation(id: string): Promise<void>;
|
||||
locations(): Promise<Location[]>;
|
||||
location(id: string): Promise<Location>;
|
||||
locations(): Promise<LocationResponse[]>;
|
||||
location(id: string): Promise<LocationResponse>;
|
||||
locationHistory(id: string): Promise<LocationUpdateLogEvent[]>;
|
||||
};
|
||||
|
||||
@@ -28,6 +28,8 @@ import {
|
||||
DbEntityRequest,
|
||||
DbEntityResponse,
|
||||
DbLocationsRow,
|
||||
DbLocationsRowWithStatus,
|
||||
DatabaseLocationUpdateLogStatus,
|
||||
} from './types';
|
||||
|
||||
describe('Database', () => {
|
||||
@@ -74,7 +76,9 @@ describe('Database', () => {
|
||||
name: 'c',
|
||||
namespace: 'd',
|
||||
labels: { e: 'f' },
|
||||
annotations: { g: 'h' },
|
||||
annotations: {
|
||||
g: 'h',
|
||||
},
|
||||
},
|
||||
spec: { i: 'j' },
|
||||
},
|
||||
@@ -84,10 +88,13 @@ describe('Database', () => {
|
||||
it('manages locations', async () => {
|
||||
const db = new Database(database, getVoidLogger());
|
||||
const input: AddDatabaseLocation = { type: 'a', target: 'b' };
|
||||
const output: DbLocationsRow = {
|
||||
const output: DbLocationsRowWithStatus = {
|
||||
id: expect.anything(),
|
||||
type: 'a',
|
||||
target: 'b',
|
||||
message: null,
|
||||
status: null,
|
||||
timestamp: null,
|
||||
};
|
||||
|
||||
await db.addLocation(input);
|
||||
@@ -118,7 +125,7 @@ describe('Database', () => {
|
||||
// Output is the same
|
||||
expect(output2).toEqual(output1);
|
||||
// Locations contain only one record
|
||||
expect(locations).toEqual([output1]);
|
||||
expect(locations[0]).toMatchObject(output1);
|
||||
});
|
||||
|
||||
describe('addEntity', () => {
|
||||
@@ -150,6 +157,45 @@ describe('Database', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('locationHistory', () => {
|
||||
it('outputs the history correctly', async () => {
|
||||
const catalog = new Database(database, getVoidLogger());
|
||||
const location: AddDatabaseLocation = { type: 'a', target: 'b' };
|
||||
const { id: locationId } = await catalog.addLocation(location);
|
||||
|
||||
await catalog.addLocationUpdateLogEvent(
|
||||
locationId,
|
||||
DatabaseLocationUpdateLogStatus.SUCCESS,
|
||||
);
|
||||
await catalog.addLocationUpdateLogEvent(
|
||||
locationId,
|
||||
DatabaseLocationUpdateLogStatus.FAIL,
|
||||
undefined,
|
||||
'Something went wrong',
|
||||
);
|
||||
|
||||
const result = await catalog.locationHistory(locationId);
|
||||
expect(result).toEqual([
|
||||
{
|
||||
created_at: expect.anything(),
|
||||
entity_name: null,
|
||||
id: expect.anything(),
|
||||
location_id: locationId,
|
||||
message: null,
|
||||
status: DatabaseLocationUpdateLogStatus.SUCCESS,
|
||||
},
|
||||
{
|
||||
created_at: expect.anything(),
|
||||
entity_name: null,
|
||||
id: expect.anything(),
|
||||
location_id: locationId,
|
||||
message: 'Something went wrong',
|
||||
status: DatabaseLocationUpdateLogStatus.FAIL,
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('updateEntity', () => {
|
||||
it('can read and no-op-update an entity', async () => {
|
||||
const catalog = new Database(database, getVoidLogger());
|
||||
|
||||
@@ -35,6 +35,7 @@ import {
|
||||
DbEntityRequest,
|
||||
DbEntityResponse,
|
||||
DbLocationsRow,
|
||||
DbLocationsRowWithStatus,
|
||||
} from './types';
|
||||
|
||||
function getStrippedMetadata(metadata: EntityMeta): EntityMeta {
|
||||
@@ -182,6 +183,12 @@ export class Database {
|
||||
uid: generateUid(),
|
||||
etag: generateEtag(),
|
||||
generation: 1,
|
||||
annotations: {
|
||||
...(newEntity.metadata?.annotations ?? {}),
|
||||
...(request.locationId
|
||||
? { 'backstage.io/managed-by-location': request.locationId }
|
||||
: {}),
|
||||
},
|
||||
};
|
||||
|
||||
const newRow = toEntityRow(request.locationId, newEntity);
|
||||
@@ -378,18 +385,52 @@ export class Database {
|
||||
}
|
||||
}
|
||||
|
||||
async location(id: string): Promise<DbLocationsRow> {
|
||||
const items = await this.database<DbLocationsRow>('locations')
|
||||
.where({ id })
|
||||
.select();
|
||||
async location(id: string): Promise<DbLocationsRowWithStatus> {
|
||||
const items = await this.database<DbLocationsRowWithStatus>('locations')
|
||||
.where('locations.id', id)
|
||||
.leftOuterJoin(
|
||||
'location_update_log_latest',
|
||||
'locations.id',
|
||||
'location_update_log_latest.location_id',
|
||||
)
|
||||
.select('locations.*', {
|
||||
status: 'location_update_log_latest.status',
|
||||
timestamp: 'location_update_log_latest.created_at',
|
||||
message: 'location_update_log_latest.message',
|
||||
});
|
||||
|
||||
if (!items.length) {
|
||||
throw new NotFoundError(`Found no location with ID ${id}`);
|
||||
}
|
||||
return items[0];
|
||||
}
|
||||
|
||||
async locations(): Promise<DbLocationsRow[]> {
|
||||
return this.database<DbLocationsRow>('locations').select();
|
||||
async locations(): Promise<DbLocationsRowWithStatus[]> {
|
||||
const locations = await this.database('locations')
|
||||
.leftOuterJoin(
|
||||
'location_update_log_latest',
|
||||
'locations.id',
|
||||
'location_update_log_latest.location_id',
|
||||
)
|
||||
.select('locations.*', {
|
||||
status: 'location_update_log_latest.status',
|
||||
timestamp: 'location_update_log_latest.created_at',
|
||||
message: 'location_update_log_latest.message',
|
||||
});
|
||||
|
||||
return locations;
|
||||
}
|
||||
|
||||
async locationHistory(id: string): Promise<DatabaseLocationUpdateLogEvent[]> {
|
||||
const result = await this.database<DatabaseLocationUpdateLogEvent>(
|
||||
'location_update_log',
|
||||
)
|
||||
.where('location_id', id)
|
||||
.orderBy('created_at', 'desc')
|
||||
.limit(10)
|
||||
.select();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
async addLocationUpdateLogEvent(
|
||||
@@ -402,7 +443,7 @@ export class Database {
|
||||
'location_update_log',
|
||||
).insert({
|
||||
id: uuidv4(),
|
||||
status: status,
|
||||
status,
|
||||
location_id: locationId,
|
||||
entity_name: entityName,
|
||||
message,
|
||||
|
||||
@@ -20,7 +20,11 @@ import Knex from 'knex';
|
||||
import { IngestionModel } from '../ingestion/types';
|
||||
import { Database } from './Database';
|
||||
import { DatabaseManager } from './DatabaseManager';
|
||||
import { DatabaseLocationUpdateLogStatus, DbLocationsRow } from './types';
|
||||
import {
|
||||
DatabaseLocationUpdateLogStatus,
|
||||
DbLocationsRow,
|
||||
DbLocationsRowWithStatus,
|
||||
} from './types';
|
||||
|
||||
describe('DatabaseManager', () => {
|
||||
describe('refreshLocations', () => {
|
||||
@@ -43,10 +47,13 @@ describe('DatabaseManager', () => {
|
||||
});
|
||||
|
||||
it('can update a single location', async () => {
|
||||
const location: DbLocationsRow = {
|
||||
const location: DbLocationsRowWithStatus = {
|
||||
id: '123',
|
||||
type: 'some',
|
||||
target: 'thing',
|
||||
message: '',
|
||||
status: DatabaseLocationUpdateLogStatus.SUCCESS,
|
||||
timestamp: new Date(314159265).toISOString(),
|
||||
};
|
||||
const desc: Entity = {
|
||||
apiVersion: 'backstage.io/v1beta1',
|
||||
@@ -54,6 +61,7 @@ describe('DatabaseManager', () => {
|
||||
metadata: { name: 'c1' },
|
||||
spec: { type: 'service' },
|
||||
};
|
||||
|
||||
const tx = (undefined as unknown) as Knex.Transaction<any, any>;
|
||||
|
||||
const db = ({
|
||||
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 2020 Spotify AB
|
||||
*
|
||||
* 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 * as Knex from 'knex';
|
||||
|
||||
export async function up(knex: Knex): Promise<any> {
|
||||
// Need to first order by date of creation
|
||||
const query = knex
|
||||
.select()
|
||||
.from('location_update_log')
|
||||
.orderBy('location_update_log.created_at', 'desc');
|
||||
|
||||
// And only then to do the grouping to get the latest per location
|
||||
const groupedQuery = knex(query).groupBy('location_id').select();
|
||||
|
||||
await knex.schema.raw(
|
||||
`CREATE VIEW location_update_log_latest AS ${groupedQuery.toString()};`,
|
||||
);
|
||||
}
|
||||
|
||||
export async function down(knex: Knex): Promise<any> {
|
||||
return knex.schema.raw(`DROP VIEW location_update_log_latest;`);
|
||||
}
|
||||
@@ -52,6 +52,12 @@ export type DbLocationsRow = {
|
||||
target: string;
|
||||
};
|
||||
|
||||
export type DbLocationsRowWithStatus = DbLocationsRow & {
|
||||
status: string | null;
|
||||
timestamp: string | null;
|
||||
message: string | null;
|
||||
};
|
||||
|
||||
export type AddDatabaseLocation = {
|
||||
type: string;
|
||||
target: string;
|
||||
|
||||
@@ -32,6 +32,7 @@ class MockLocationsCatalog implements LocationsCatalog {
|
||||
removeLocation = jest.fn();
|
||||
locations = jest.fn();
|
||||
location = jest.fn();
|
||||
locationHistory = jest.fn();
|
||||
}
|
||||
|
||||
describe('createRouter', () => {
|
||||
|
||||
@@ -84,6 +84,11 @@ export async function createRouter(
|
||||
const output = await locationsCatalog.locations();
|
||||
res.status(200).send(output);
|
||||
})
|
||||
.get('/locations/:id/history', async (req, res) => {
|
||||
const { id } = req.params;
|
||||
const output = await locationsCatalog.locationHistory(id);
|
||||
res.status(200).send(output);
|
||||
})
|
||||
.get('/locations/:id', async (req, res) => {
|
||||
const { id } = req.params;
|
||||
const output = await locationsCatalog.location(id);
|
||||
|
||||
Reference in New Issue
Block a user