From 87f2b09281712c1de1ab86932e8a5e02f77929be Mon Sep 17 00:00:00 2001 From: Xiaozhe Yao Date: Thu, 8 Nov 2018 18:27:44 +0800 Subject: [PATCH] add package put/get methods --- discovery/.gitignore | 3 +- discovery/src/controller/index.ts | 1 + discovery/src/controller/pretrained.ts | 19 ++++++++++ discovery/src/dynamo/action.ts | 49 ++++++++++++++++++++++++-- discovery/src/dynamo/config.ts | 9 +++++ discovery/src/dynamo/dynamo.ts | 4 +++ discovery/src/dynamo/entity.ts | 17 ++++++++- discovery/src/routes.ts | 4 ++- discovery/tsconfig.json | 3 +- 9 files changed, 103 insertions(+), 6 deletions(-) create mode 100644 discovery/src/controller/pretrained.ts create mode 100644 discovery/src/dynamo/config.ts diff --git a/discovery/.gitignore b/discovery/.gitignore index 04c01ba7b..5a27aae62 100644 --- a/discovery/.gitignore +++ b/discovery/.gitignore @@ -1,2 +1,3 @@ node_modules/ -dist/ \ No newline at end of file +dist/ +*.log \ No newline at end of file diff --git a/discovery/src/controller/index.ts b/discovery/src/controller/index.ts index c29dfa74a..0c1532be7 100644 --- a/discovery/src/controller/index.ts +++ b/discovery/src/controller/index.ts @@ -1,2 +1,3 @@ export { default as system } from './system'; export { default as package } from './package'; +export { default as pretrained } from './pretrained'; \ No newline at end of file diff --git a/discovery/src/controller/pretrained.ts b/discovery/src/controller/pretrained.ts new file mode 100644 index 000000000..b8fdafda2 --- /dev/null +++ b/discovery/src/controller/pretrained.ts @@ -0,0 +1,19 @@ +import { Context } from 'koa'; +import { putPretrained, getPretrained } from '../dynamo/action'; +export default class PackageController { + public static async importPretrained (ctx: Context) { + putPretrained('yolo_tiny','https://premium.file.cvtron.xyz/data/yolo_tiny.ckpt'); + ctx.status = 200; + ctx.body = { + 'code': '200', + 'desc': 'success' + }; + } + public static async getAllPretrained (ctx: Context) { + ctx.status = 200; + ctx.body = { + 'code': '200', + 'results': await getPretrained() + } + } +} diff --git a/discovery/src/dynamo/action.ts b/discovery/src/dynamo/action.ts index 2af0af9f5..62d3e967d 100644 --- a/discovery/src/dynamo/action.ts +++ b/discovery/src/dynamo/action.ts @@ -1,9 +1,17 @@ -import mapper from './dynamo' -import { Package } from './entity' +import mapper from './dynamo'; +import { Package, Pretrained } from './entity'; import { Guid } from 'guid-typescript'; +import { isSymbol } from 'util'; + /** * Following will be package manipulations */ + +/** + * + * @param isSymbol + * @param linkedTo + */ function putPackage (isSymbol: boolean, linkedTo: string) { const toPutPackage = Object.assign(new Package, { id: Guid.create(), @@ -14,3 +22,40 @@ function putPackage (isSymbol: boolean, linkedTo: string) { console.log(objectSaved) }) } +/** + * + * Following will be pretrained manipulations + */ + +/** + * + * @param linkedTo + * @param name + */ +function putPretrained (name: string, linkedTo: string) { + const toPutPretrained = Object.assign(new Pretrained, { + id: Guid.create(), + name: name, + linkedTo: linkedTo + }) + mapper.put(toPutPretrained).then(objectSaved => { + console.log(objectSaved) + }) +} + +/** + * + */ +async function getPretrained () { + let results: Pretrained[] = [] + for await (const item of mapper.scan(Pretrained)) { + results.push(item) + } + return results +} + +export { + putPretrained, + putPackage, + getPretrained +} \ No newline at end of file diff --git a/discovery/src/dynamo/config.ts b/discovery/src/dynamo/config.ts new file mode 100644 index 000000000..7b2aca61e --- /dev/null +++ b/discovery/src/dynamo/config.ts @@ -0,0 +1,9 @@ +const AWSConfig = { + region: 'us-east-1', + accessKeyId: process.env.AWS_ACCESS_KEY, + secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, +} + +export { + AWSConfig +} \ No newline at end of file diff --git a/discovery/src/dynamo/dynamo.ts b/discovery/src/dynamo/dynamo.ts index cf2c6bab9..257d30a06 100644 --- a/discovery/src/dynamo/dynamo.ts +++ b/discovery/src/dynamo/dynamo.ts @@ -5,6 +5,10 @@ import { DataMapper } from '@aws/dynamodb-data-mapper'; import DynamoDB = require('aws-sdk/clients/dynamodb'); +import AWS = require('aws-sdk') +import { AWSConfig } from './config' + +AWS.config.update(AWSConfig) const mapper = new DataMapper({ client: new DynamoDB({region: 'us-east-1'}) diff --git a/discovery/src/dynamo/entity.ts b/discovery/src/dynamo/entity.ts index a61c14b9d..486e020f0 100644 --- a/discovery/src/dynamo/entity.ts +++ b/discovery/src/dynamo/entity.ts @@ -21,9 +21,24 @@ class Package { @attribute() linkedTo: string; +} + +@table('cvpm-pretrained') +class Pretrained { + @hashKey() + id: string; + @rangeKey({defaultProvider: () => new Date()}) + createdAt: Date; + + @attribute() + linkedTo: string; + + @attribute() + name: string; } export { - Package + Package, + Pretrained } \ No newline at end of file diff --git a/discovery/src/routes.ts b/discovery/src/routes.ts index c509ddb9f..a083e8c27 100644 --- a/discovery/src/routes.ts +++ b/discovery/src/routes.ts @@ -5,6 +5,8 @@ import controller = require('./controller'); const router = new Router(); router.get('/system/status', controller.system.getSystemStatus); -router.post('/packages', controller.package.importPackage) +router.post('/packages', controller.package.importPackage); +router.put('/pretrained', controller.pretrained.importPretrained); +router.get('/pretrained', controller.pretrained.getAllPretrained); export { router }; diff --git a/discovery/tsconfig.json b/discovery/tsconfig.json index 50b9050ca..159d8bf22 100644 --- a/discovery/tsconfig.json +++ b/discovery/tsconfig.json @@ -16,7 +16,8 @@ ], "lib": [ "es2017", - "es6" + "es6", + "esnext.asynciterable" ], "types": [ "node"