Skip to content

Commit

Permalink
fix: Move Scratch training data API to JSON
Browse files Browse the repository at this point in the history
This commit moves the Scratch API for storing training data from
a GET jsonp-based API to a POST json-based API.

I needed to do that because I'd broken the GET API with the new speech /
sounds API.

Signed-off-by: Dale Lane <dale.lane@uk.ibm.com>
  • Loading branch information
dalelane committed Mar 30, 2019
1 parent c657f8a commit 7bf3978
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 291 deletions.
13 changes: 9 additions & 4 deletions resources/scratchx-images-classify.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@
dataType : 'json',
type : 'POST',
contentType : 'application/json',
data : '{"data":"' + imagedata + '","displayedhelp":' + displayedMLforKidsHelp + '}',
data : JSON.stringify({
data : imagedata,
displayedhelp : displayedMLforKidsHelp
}),
headers : {
'If-Modified-Since': lastmodified,
'X-User-Agent': 'mlforkids-scratch2-images'
Expand Down Expand Up @@ -173,14 +176,16 @@
function storeImage(imagedata, label, callback) {
$.ajax({
url : '{{{ storeurl }}}',
dataType : 'jsonp',
dataType : 'json',
method : 'POST',
contentType : 'application/json',
headers : {
'X-User-Agent': 'mlforkids-scratch2-images'
},
data : {
data : JSON.stringify({
data : imagedata,
label : label
},
}),
success : function (data) {
callback();
},
Expand Down
8 changes: 5 additions & 3 deletions resources/scratchx-numbers-classify.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,16 @@
function storeNumbers(numbers, label, callback) {
$.ajax({
url : '{{{ storeurl }}}',
dataType : 'jsonp',
dataType : 'json',
method : 'POST',
contentType : 'application/json',
headers : {
'X-User-Agent': 'mlforkids-scratch2-numbers'
},
data : {
data : JSON.stringify({
data : numbers,
label : label
},
}),
success : function (data) {
callback();
},
Expand Down
8 changes: 5 additions & 3 deletions resources/scratchx-text-classify.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,16 @@
function storeText(text, label, callback) {
$.ajax({
url : '{{{ storeurl }}}',
dataType : 'jsonp',
dataType : 'json',
method : 'POST',
contentType : 'application/json',
headers : {
'X-User-Agent': 'mlforkids-scratch2-text'
},
data : {
data : JSON.stringify({
data : cleanUpText(text, 1024),
label : label
},
}),
success : function (data) {
callback();
},
Expand Down
43 changes: 1 addition & 42 deletions src/lib/restapi/scratch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,46 +196,6 @@ async function getTrainingData(req: Express.Request, res: Express.Response) {
async function storeTrainingData(req: Express.Request, res: Express.Response) {
const apikey = req.params.scratchkey;

try {
if (!req.query.data || !req.query.label) {
log.warn({
agent : req.header('X-User-Agent'),
key : apikey,
func : 'storeTrainingData',
}, 'Missing data');
throw new Error('Missing data');
}

const scratchKey = await store.getScratchKey(apikey);
const stored = await training.storeTrainingData(scratchKey, req.query.label, req.query.data);

return res.set(headers.NO_CACHE).jsonp(stored);
}
catch (err) {
if (err.message === 'Missing data' ||
err.message === 'Invalid data' ||
err.message === 'Invalid label' ||
err.message === 'Number is too small' ||
err.message === 'Number is too big')
{
return res.status(httpstatus.BAD_REQUEST).jsonp({ error : err.message });
}
if (err.message === 'Project already has maximum allowed amount of training data') {
return res.status(httpstatus.CONFLICT).jsonp({ error : err.message });
}
if (err.message === 'Unexpected response when retrieving credentials for Scratch') {
return res.status(httpstatus.NOT_FOUND).jsonp({ error : 'Scratch key not found' });
}

log.error({ err, agent : req.header('X-User-Agent') }, 'Store error');
return res.status(httpstatus.INTERNAL_SERVER_ERROR).jsonp(err);
}
}


async function postStoreTrainingData(req: Express.Request, res: Express.Response) {
const apikey = req.params.scratchkey;

try {
if (!req.body.data || !req.body.label) {
log.warn({
Expand Down Expand Up @@ -367,8 +327,7 @@ export default function registerApis(app: Express.Application) {
app.post(urls.SCRATCHKEY_MODEL, trainNewClassifier);

app.get(urls.SCRATCHKEY_TRAIN, getTrainingData);
app.get(urls.SCRATCHKEY_TRAIN, storeTrainingData);
app.post(urls.SCRATCHKEY_TRAIN, postStoreTrainingData);
app.post(urls.SCRATCHKEY_TRAIN, storeTrainingData);

app.get(urls.SCRATCHKEY_EXTENSION, getScratchxExtension);
app.get(urls.SCRATCH3_EXTENSION, getScratch3Extension);
Expand Down
16 changes: 11 additions & 5 deletions src/lib/utils/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,17 @@ export function file(url: string, targetFilePath: string, callback: IErrCallback
.on('finish', resolve);

try {
request.get({ url, timeout : 10000, rejectUnauthorized : false })
.on('error', () => {
resolve(new Error(ERRORS.DOWNLOAD_FAIL + url));
})
.pipe(writeStream);
request.get({
url,
timeout : 10000,
rejectUnauthorized : false,
strictSSL : false,
})
.on('error', (err) => {
log.error({ err }, 'request get fail');
resolve(new Error(ERRORS.DOWNLOAD_FAIL + url));
})
.pipe(writeStream);
}
catch (err) {
log.error({ err, url }, 'Failed to download file');
Expand Down
2 changes: 1 addition & 1 deletion src/tests/db/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1276,7 +1276,7 @@ describe('DB objects', () => {
id : 'testing',
projecttypes : 'text,images,numbers',
ismanaged : 0,
maxusers : 25,
maxusers : 30,
maxprojectsperuser : 2,
textclassifiersexpiry : 24,
imageclassifiersexpiry : 24,
Expand Down
12 changes: 6 additions & 6 deletions src/tests/db/tenantstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('DB store - tenants', () => {
id,
supportedProjectTypes : ['text', 'images', 'numbers'],
isManaged : false,
maxUsers : 25,
maxUsers : 30,
maxProjectsPerUser : 2,
textClassifierExpiry : 100,
imageClassifierExpiry : 40,
Expand All @@ -80,7 +80,7 @@ describe('DB store - tenants', () => {
id,
supportedProjectTypes : ['text', 'images', 'numbers'],
isManaged : false,
maxUsers : 25,
maxUsers : 30,
maxProjectsPerUser : 2,
textClassifierExpiry : 6,
imageClassifierExpiry : 7,
Expand All @@ -91,7 +91,7 @@ describe('DB store - tenants', () => {
id,
supportedProjectTypes : ['text', 'images', 'numbers'],
isManaged : false,
maxUsers : 25,
maxUsers : 30,
maxProjectsPerUser : 2,
textClassifierExpiry : 6,
imageClassifierExpiry : 7,
Expand All @@ -102,7 +102,7 @@ describe('DB store - tenants', () => {
id,
supportedProjectTypes : ['text', 'images', 'numbers'],
isManaged : false,
maxUsers : 25,
maxUsers : 30,
maxProjectsPerUser : 2,
textClassifierExpiry : 12,
imageClassifierExpiry : 14,
Expand All @@ -113,7 +113,7 @@ describe('DB store - tenants', () => {
id,
supportedProjectTypes : ['text', 'images', 'numbers'],
isManaged : false,
maxUsers : 25,
maxUsers : 30,
maxProjectsPerUser : 2,
textClassifierExpiry : 12,
imageClassifierExpiry : 14,
Expand All @@ -126,7 +126,7 @@ describe('DB store - tenants', () => {
id,
supportedProjectTypes : ['text', 'images', 'numbers'],
isManaged : false,
maxUsers : 25,
maxUsers : 30,
maxProjectsPerUser : 2,
textClassifierExpiry : 24,
imageClassifierExpiry : 24,
Expand Down

0 comments on commit 7bf3978

Please sign in to comment.