-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created endpoints for viewing NNs and getting them and their metadata. Resolves #23.
- Loading branch information
1 parent
a38e232
commit 5d02e04
Showing
12 changed files
with
167 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
server/src/neural-networks/controllers/neural-networks.controller.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { NeuralNetworksController } from './neural-networks.controller'; | ||
|
||
describe('NeuralNetworksController', () => { | ||
let controller: NeuralNetworksController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [NeuralNetworksController], | ||
}).compile(); | ||
|
||
controller = module.get<NeuralNetworksController>(NeuralNetworksController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
41 changes: 41 additions & 0 deletions
41
server/src/neural-networks/controllers/neural-networks.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { | ||
Controller, | ||
Get, | ||
HttpCode, | ||
HttpStatus, | ||
Param, | ||
ParseBoolPipe, | ||
Query, | ||
} from '@nestjs/common'; | ||
import { ApiNotFoundResponse, ApiOkResponse } from '@nestjs/swagger'; | ||
import { NeuralNetworksService } from '../service/neural-networks.service'; | ||
|
||
@Controller('/neural-networks') | ||
export class NeuralNetworksController { | ||
constructor(private readonly neuralNetworksService: NeuralNetworksService) {} | ||
|
||
@Get('/') | ||
@HttpCode(HttpStatus.OK) | ||
@ApiOkResponse({ | ||
description: 'List of neural network models on the server.', | ||
}) | ||
@ApiNotFoundResponse({ description: 'No such file or directory.' }) | ||
async listNetworks() { | ||
return await this.neuralNetworksService.listModels(); | ||
} | ||
|
||
@Get(':path(*)') | ||
@HttpCode(HttpStatus.OK) | ||
@ApiOkResponse({ | ||
description: 'Neural network model or its metadata in JSON.', | ||
}) | ||
@ApiNotFoundResponse({ description: 'No such file or directory.' }) | ||
async getNetwork( | ||
@Param('path') path: string, | ||
@Query('metadata', new ParseBoolPipe()) metadata: boolean, | ||
) { | ||
return metadata | ||
? await this.neuralNetworksService.getModelMetadata(path) | ||
: await this.neuralNetworksService.getModel(path); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
server/src/neural-networks/errors/neural-networks.error.codes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export enum NeuralNetworksErrorCode { | ||
INVALID_FILE_PATH = 'Invalid file path.', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default class NeuralNetworksError extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
this.name = 'NeuralNetworksError'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { NeuralNetworksService } from './service/neural-networks.service'; | ||
import { NeuralNetworksController } from './controllers/neural-networks.controller'; | ||
import { AwsSdkModule } from 'nest-aws-sdk'; | ||
import { S3 } from 'aws-sdk'; | ||
|
||
@Module({ | ||
imports: [AwsSdkModule.forFeatures([S3])], | ||
providers: [NeuralNetworksService], | ||
controllers: [NeuralNetworksController], | ||
}) | ||
export class NeuralNetworksModule {} |
18 changes: 18 additions & 0 deletions
18
server/src/neural-networks/service/neural-networks.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { NeuralNetworksService } from './neural-networks.service'; | ||
|
||
describe('NeuralNetworksService', () => { | ||
let service: NeuralNetworksService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [NeuralNetworksService], | ||
}).compile(); | ||
|
||
service = module.get<NeuralNetworksService>(NeuralNetworksService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
60 changes: 60 additions & 0 deletions
60
server/src/neural-networks/service/neural-networks.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { S3 } from 'aws-sdk'; | ||
import { InjectAwsService } from 'nest-aws-sdk'; | ||
import { configObject } from 'src/configuration'; | ||
import NeuralNetworksError from '../errors/neural-networks.error'; | ||
import { NeuralNetworksErrorCode } from '../errors/neural-networks.error.codes'; | ||
|
||
@Injectable() | ||
export class NeuralNetworksService { | ||
constructor(@InjectAwsService(S3) private readonly s3: S3) {} | ||
|
||
async listModels() { | ||
const queryResponse = await this.s3 | ||
.listObjectsV2({ | ||
Bucket: configObject.aws.neuralNetworksS3Bucket, | ||
Prefix: `models/`, | ||
}) | ||
.promise(); | ||
|
||
const result = queryResponse.Contents.filter( | ||
(item) => !item.Key.endsWith('/'), | ||
).map((item) => item.Key.replace('models/', '')); | ||
|
||
return result; | ||
} | ||
|
||
async getModel(filepath: string) { | ||
const completeFilepath = `models/${filepath}`; | ||
|
||
if (completeFilepath.endsWith('/')) { | ||
throw new NeuralNetworksError(NeuralNetworksErrorCode.INVALID_FILE_PATH); | ||
} | ||
|
||
const queryResponse = await this.s3 | ||
.getObject({ | ||
Bucket: configObject.aws.neuralNetworksS3Bucket, | ||
Key: completeFilepath, | ||
}) | ||
.promise(); | ||
|
||
return JSON.parse(queryResponse.Body.toString()); | ||
} | ||
|
||
async getModelMetadata(filepath: string) { | ||
const completeFilepath = `metadata/${filepath}`; | ||
|
||
if (completeFilepath.endsWith('/')) { | ||
throw new NeuralNetworksError(NeuralNetworksErrorCode.INVALID_FILE_PATH); | ||
} | ||
|
||
const queryResponse = await this.s3 | ||
.getObject({ | ||
Bucket: configObject.aws.neuralNetworksS3Bucket, | ||
Key: completeFilepath, | ||
}) | ||
.promise(); | ||
|
||
return JSON.parse(queryResponse.Body.toString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters