Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@ nbactions.xml

**/.iml
**/.idea
/storage/
/storage/
/frontend/mock-api/functions/api/index.js
3 changes: 2 additions & 1 deletion frontend/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"singleQuote": false,
"arrowParens": "avoid"
}
]
],
"no-console": "error"
}
}
1 change: 1 addition & 0 deletions frontend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ package-lock.json
*.tgz

app/api/dnd/**/_*.*
storybook-static
2 changes: 1 addition & 1 deletion frontend/mock-api/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const version = require("../../package.json").version;
const config = require("./config.json");
const config = require("./functions/api/config.json");

config.version = version;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[
module.exports = [
"Afghanistan",
"Albania",
"Algeria",
Expand Down Expand Up @@ -209,4 +209,4 @@
"Yemen",
"Zambia",
"Zimbabwe"
]
];
9 changes: 9 additions & 0 deletions frontend/mock-api/functions/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as serverless from "serverless-http";
import * as createApi from "../../index";
import * as express from "express";
const app = createApi();
const wrapper = express();

module.exports.handler = serverless(
wrapper.use("/.netlify/functions/api", app)
);
110 changes: 68 additions & 42 deletions frontend/mock-api/index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
const path = require("path");
const version = require("../package.json").version;
const EXPORT_FORM_CONFIG = require("./forms/export-form.json");
const EXPORT_FORM_CONFIG = require("./functions/api/forms/export-form.json");
const mockAuthMiddleware = require("./mockAuthMiddleware");

const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
// Taken from:
// http://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
const temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}

const ERROR = JSON.stringify({
message: "Could not process the request",
message: "Could not process the request"
});

const LONG_DELAY = 500;
const SHORT_DELAY = 300;
const NO_DELAY = 10;

// Simulate API
module.exports = function (app, port) {
function mountApi(app) {
/*
QUERIES
*/
Expand Down Expand Up @@ -78,7 +80,7 @@ module.exports = function (app, port) {
id: 1,
status: "DONE",
numberOfResults: 5,
resultUrl: `/api/results/results.csv`,
resultUrl: `/api/results/results.csv`
})
);
}, LONG_DELAY);
Expand All @@ -97,8 +99,8 @@ module.exports = function (app, port) {
{ id: "empty-set", label: "Empty Dataset" },
{
id: "another-empty-set",
label: "Another empty dataset with a long name",
},
label: "Another empty dataset with a long name"
}
])
);
});
Expand All @@ -110,7 +112,9 @@ module.exports = function (app, port) {
req,
res
) {
res.sendFile(path.join(__dirname, `./results/${req.params.filename}`));
res.sendFile(
path.join(__dirname, "functions/api", `./results/${req.params.filename}`)
);
});

/*
Expand All @@ -120,14 +124,20 @@ module.exports = function (app, port) {
req,
res
) {
res.sendFile(path.join(__dirname, "./concepts.json"));
res.sendFile(path.join(__dirname, "functions/api", "./concepts.json"));
});

app.get(
"/api/datasets/:datasetId/concepts/:id",
mockAuthMiddleware,
function response(req, res) {
res.sendFile(path.join(__dirname, `./concepts/${req.params.id}.json`));
res.sendFile(
path.join(
__dirname,
"functions/api",
`./concepts/${req.params.id}.json`
)
);
}
);

Expand All @@ -149,10 +159,10 @@ module.exports = function (app, port) {
"group 1",
"important",
"jk",
"interesting",
"interesting"
];

for (var i = 25600; i < 35600; i++) {
for (let i = 25600; i < 35600; i++) {
const notExecuted = Math.random() < 0.1;

ids.push({
Expand All @@ -168,7 +178,7 @@ module.exports = function (app, port) {
own: Math.random() < 0.1,
shared: Math.random() < 0.8,
resultUrl: notExecuted ? null : `/api/results/results.csv`,
ownerName: "System",
ownerName: "System"
});
}

Expand All @@ -182,7 +192,9 @@ module.exports = function (app, port) {
mockAuthMiddleware,
function response(req, res) {
setTimeout(() => {
res.sendFile(path.join(__dirname, "./stored-queries/25.json"));
res.sendFile(
path.join(__dirname, "functions/api", "./stored-queries/25.json")
);
}, LONG_DELAY);
}
);
Expand Down Expand Up @@ -229,7 +241,7 @@ module.exports = function (app, port) {
res.send(
JSON.stringify({
successful: 1 + Math.floor(Math.random() * 200),
unsuccessful: 586,
unsuccessful: 586
})
);
}, LONG_DELAY);
Expand All @@ -247,7 +259,7 @@ module.exports = function (app, port) {
const countriesRequested = req.params.filterId === "production_country";

const storedValues = countriesRequested
? require("./autocomplete/countries")
? require("./functions/api/autocomplete/countries")
: [
"1008508208",
"1015841172",
Expand All @@ -258,16 +270,16 @@ module.exports = function (app, port) {
"1000326535",
"1014150881",
"1017126347",
"1008445564",
"1008445564"
];

const suggestions = storedValues
.map((v, id) => ({
label: v,
value: id,
templateValues: { company: "Columbia Pictures Corporation" },
templateValues: { company: "Columbia Pictures Corporation" }
}))
.filter((v) => v.label.toLowerCase().startsWith(text));
.filter(v => v.label.toLowerCase().startsWith(text));

res.send(JSON.stringify(suggestions));
}, LONG_DELAY);
Expand All @@ -285,7 +297,7 @@ module.exports = function (app, port) {

res.send({
unknownCodes: concepts.slice(5),
resolvedConcepts: concepts.slice(1),
resolvedConcepts: concepts.slice(1)
});
}, LONG_DELAY);
}
Expand All @@ -299,7 +311,7 @@ module.exports = function (app, port) {

res.send({
version: version,
isDevelopment: process.env.NODE_ENV !== "production",
isDevelopment: process.env.NODE_ENV !== "production"
});
});

Expand All @@ -317,17 +329,17 @@ module.exports = function (app, port) {

if (req.params.filterId !== "production_country") return null;

const countries = require("./autocomplete/countries");
const unknownCodes = values.filter((val) => !countries.includes(val));
const resolvedValues = values.filter((val) => countries.includes(val));
const countries = require("./functions/api/autocomplete/countries");
const unknownCodes = values.filter(val => !countries.includes(val));
const resolvedValues = values.filter(val => countries.includes(val));

res.send({
unknownCodes: unknownCodes,
resolvedFilter: {
tableId: req.params.tableId,
filterId: req.params.filterId,
value: resolvedValues.map((val) => ({ label: val, value: val })),
},
value: resolvedValues.map(val => ({ label: val, value: val }))
}
});
}, LONG_DELAY);
}
Expand All @@ -336,7 +348,7 @@ module.exports = function (app, port) {
app.get("/api/config/frontend", mockAuthMiddleware, (req, res) => {
res.setHeader("Content-Type", "application/json");

const config = require("./config.json");
const config = require("./functions/api/config.json");

config.version = version;

Expand All @@ -351,13 +363,13 @@ module.exports = function (app, port) {

if (user === "test" && password === "test") {
res.send({
access_token: "VALID",
access_token: "VALID"
});
} else {
res.status(422);
res.send(
JSON.stringify({
message: "Login failed",
message: "Login failed"
})
);
}
Expand All @@ -374,10 +386,10 @@ module.exports = function (app, port) {
domains: ["datasets"],
abilities: ["read", "download", "preserve_id"],
targets: ["imdb"],
creationTime: "2020-01-23T09:52:31.3318485",
},
creationTime: "2020-01-23T09:52:31.3318485"
}
],
groups: [],
groups: []
});
});

Expand All @@ -390,7 +402,7 @@ module.exports = function (app, port) {
res.status(201);
res.send(
JSON.stringify({
id: 56000 + Math.floor(Math.random() * 200),
id: 56000 + Math.floor(Math.random() * 200)
})
);
}, LONG_DELAY);
Expand All @@ -409,12 +421,12 @@ module.exports = function (app, port) {
if (dice < 0.5) {
return {
formType: "EXPORT_FORM",
values: {},
values: {}
};
} else {
return {
formType: "Other form",
values: {},
values: {}
};
}
}
Expand All @@ -428,10 +440,10 @@ module.exports = function (app, port) {
"group 1",
"important",
"jk",
"interesting",
"interesting"
];

for (var i = 55600; i < 85600; i++) {
for (let i = 55600; i < 58600; i++) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The let instead of var comes from prettier on file save. let is of course correct here as i is going to be heavily mutated. I also changed the number of returned entities here because the lambda functions refuse to return more than 6mb of data and I see no need for this heavy load test anyhow.

configs.push({
id: i,
label: "Saved Config",
Expand All @@ -442,7 +454,7 @@ module.exports = function (app, port) {
own: Math.random() < 0.1,
shared: Math.random() < 0.8,
ownerName: "System",
...getFormConfigAttributes(),
...getFormConfigAttributes()
});
}

Expand All @@ -456,7 +468,9 @@ module.exports = function (app, port) {
mockAuthMiddleware,
function response(req, res) {
setTimeout(() => {
res.sendFile(path.join(__dirname, "./form-configs/testconf.json"));
res.sendFile(
path.join(__dirname, "functions/api", "./form-configs/testconf.json")
);
}, LONG_DELAY);
}
);
Expand All @@ -481,4 +495,16 @@ module.exports = function (app, port) {
}, LONG_DELAY);
}
);
}

const createApi = () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice factory function that returns a ready to start express app for us.

const app = express();
app.use(cors());
// body parser must be set up before routes are attached
app.use(bodyParser.json());

mountApi(app);
return app;
};

module.exports = createApi;
13 changes: 2 additions & 11 deletions frontend/mock-api/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,12 @@
// EXPRESS SETUP
// -----------
const path = require("path");
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const mountApi = require(".");
const createApi = require(".");

const isDeveloping = process.env.NODE_ENV !== "production";
const port = process.env.PORT || 8001;
const app = express();
const lang = process.env.APP_LANG || "en";

app.use(cors());
// body parser must be set up before routes are attached
app.use(bodyParser.json());

mountApi(app, port);
const app = createApi()

if (!isDeveloping) {
app.use("/app/static", express.static(__dirname + "/build"));
Expand Down
Loading