Skip to content

Commit

Permalink
add few things
Browse files Browse the repository at this point in the history
  • Loading branch information
xzyaoi committed Nov 20, 2018
1 parent 7a69310 commit 2a8ce9b
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 43 deletions.
99 changes: 63 additions & 36 deletions discovery/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions discovery/package.json
Expand Up @@ -19,21 +19,23 @@
"@types/koa": "^2.0.46",
"@types/koa-helmet": "^3.1.2",
"@types/koa-passport": "^4.0.2",
"@types/koa-router": "^7.0.32",
"@types/koa-router": "^7.0.35",
"@types/koa-session": "^5.7.4",
"@types/koa__cors": "^2.2.3",
"@types/parse": "^2.1.0",
"@types/passport": "^0.4.7",
"@types/winston": "^2.4.4",
"aws-sdk": "^2.348.0",
"aws-sdk": "^2.359.0",
"dotenv": "^6.1.0",
"guid-typescript": "^1.0.9",
"koa": "^2.6.1",
"koa-bodyparser": "^4.2.1",
"http-status-codes": "^1.3.0",
"koa": "^2.6.2",
"koa-bodyparser-ts": "^0.3.1",
"koa-helmet": "^4.0.0",
"koa-passport": "^4.1.1",
"koa-router": "^7.4.0",
"koa-session": "^5.10.0",
"parse": "^2.1.0",
"passport": "^0.4.0",
"passport-cognito": "^0.1.13",
"winston": "^3.1.0",
Expand Down
7 changes: 7 additions & 0 deletions discovery/scripts/start-docker.sh
Expand Up @@ -3,4 +3,11 @@ docker run \
--env AWS_ACCESS_KEY=$AWS_ACCESS_KEY \
--env AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY \
--env LOGSENE_TOKEN=$LOGSENE_TOKEN \
--env COGNITO_POOL_ID=$COGNITO_POOL_ID \
--env COGNITO_CLIENT_ID=$COGNITO_CLIENT_ID \
--env COGNITO_REGION=$COGNITO_REGION \
--env PARSE_ID=$PARSE_ID \
--env PARSE_TOKEN=$PARSE_TOKEN \
--env PARSE_URL=$PARSE_URL \
--env PARSE_MASTER_KEY=$PARSE_MASTER_KEY
-d -p 3000:3000 docker.io/autoai/cvpm-discovery
2 changes: 1 addition & 1 deletion discovery/src/controller/registry.ts
Expand Up @@ -9,7 +9,7 @@ export default class RegistryController {
ctx.body = {
'code': 200,
'desc': 'success',
'req': body
'request': body
};
}
public static async getRegistries (ctx: Context) {
Expand Down
20 changes: 19 additions & 1 deletion discovery/src/controller/system.ts
@@ -1,6 +1,8 @@
import { BaseContext } from 'koa';
import { BaseContext, Context } from 'koa';
import { config } from '../config';

import { getCount, updateCount } from '../parse'

export default class SystemController {
public static async getSystemStatus(ctx: BaseContext) {
ctx.status = 200;
Expand All @@ -17,4 +19,20 @@ export default class SystemController {
'port': config.port
};
}
public static async getStatInfo (ctx: Context) {
const body = ctx.request.query
ctx.status = 200;
ctx.body = {
'code': 200,
'results': await getCount(+body.limit)
};
}
public static async putStatInfo (ctx: Context) {
const body = ctx.request.body;
ctx.status = 200;
ctx.body = {
'code': 200,
'request': await updateCount(+body.users, +body.registries, +body.models)
};
}
}
9 changes: 9 additions & 0 deletions discovery/src/parse/entity.ts
@@ -0,0 +1,9 @@
interface CountResult {
users: number,
registries: number,
models: number
}

export {
CountResult
}
39 changes: 39 additions & 0 deletions discovery/src/parse/index.ts
@@ -0,0 +1,39 @@
var Parse = require('parse/node').Parse;

import { CountResult } from './entity'

function initParse() {
Parse.initialize(process.env.PARSE_ID, process.env.PARSE_TOKEN, process.env.PARSE_MASTER_KEY);
(Parse as any).serverURL = process.env.PARSE_URL
}

async function updateCount(users: number, registries: number, models: number) {
const Meta = Parse.Object.extend("Meta");
let meta = new Meta();
meta.set("user", users);
meta.set("registry", registries);
meta.set("model", models);
const results: any[] = await meta.save()
return results
}

async function getCount(limit: number) {
const Meta = Parse.Object.extend("Meta");
const query = new Parse.Query(Meta);
query.descending("createdAt");
query.limit(limit);
const results: any[] = await query.find();
let result: CountResult[];
for (let i = 0; i < results.length; i++) {
result[i].models = results[i]['models'];
result[i].registries = results[i]['registries'];
result[i].users = results[i]['users'];
}
return result
}

export {
initParse,
updateCount,
getCount
}
4 changes: 4 additions & 0 deletions discovery/src/routes.ts
Expand Up @@ -5,14 +5,18 @@ import controller = require('./controller');
const router = new Router();

router.get('/', controller.system.getMetaInfo);

router.get('/system/status', controller.system.getSystemStatus);
router.get('/system/stats', controller.system.getStatInfo);
router.put('/system/stats', controller.system.putStatInfo);

router.post('/packages', controller.package.importPackage);

router.put('/pretrained', controller.pretrained.importPretrained);
router.get('/pretrained', controller.pretrained.getAllPretrained);

router.put('/registry', controller.registry.putRegistry);
router.get('/registries', controller.registry.getRegistries);

router.post('/session', controller.user.createSession);

Expand Down
6 changes: 6 additions & 0 deletions discovery/src/server.ts
Expand Up @@ -5,6 +5,8 @@ import * as helmet from 'koa-helmet';
import * as winston from 'winston';
import * as passport from 'koa-passport';
import * as session from 'koa-session';

import { initParse } from './parse'
import { config } from './config';
import { router } from './routes';
import { logger } from './logging';
Expand All @@ -13,6 +15,10 @@ import * as os from 'os';

export const app = new Koa();

/**
* Initialize Parse independently
*/
initParse()
/**
* Passport Settings
*/
Expand Down
2 changes: 1 addition & 1 deletion discovery/tsconfig.json
Expand Up @@ -21,7 +21,7 @@
"esnext.asynciterable"
],
"types": [
"node", "jest"
"node", "jest", "koa-bodyparser-ts"
],

},
Expand Down

0 comments on commit 2a8ce9b

Please sign in to comment.