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
6 changes: 4 additions & 2 deletions __tests__/queries.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const assert = require('chai').assert;

const mongoose = require("mongoose");
const dbName = "atelier-test";
const dbName = process.env.DB_NAME;
const dbPort = process.env.DB_PORT;
const dbAddress = process.env.DB_ADDRESS;

const {
Counter,
Expand All @@ -18,7 +20,7 @@ const mockData = require('./_mockData.js');

describe("Query Tests", () => {
before((done) => {
mongoose.connect(`mongodb://localhost:27017/${dbName}`, {
mongoose.connect(`mongodb://${dbAddress}:${dbPort}/${dbName}`, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
Expand Down
4 changes: 2 additions & 2 deletions k6/getReviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ export const options = {
};

export default function () {
const low = 900005;
const high = 1000005;
const low = 28279;
const high = 31422;
const id = Math.floor(Math.random() * (high - low)) + low;
http.get(`http://localhost:3000/reviews?product_id=${id}`);
sleep(1);
Expand Down
7 changes: 3 additions & 4 deletions k6/postReview.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ const newReview = {
},
};

const headers = { headers: { 'Content-Type': 'application/json' } };

export default function () {
const low = 900000;
const high = 1000011;
const id = Math.floor(Math.random() * (high - low)) + low;
http.post(`http://localhost:3000/reviews/`, newReview);
http.post(`http://localhost:3000/reviews/`, JSON.stringify(newReview), headers);
sleep(1);
};
8 changes: 4 additions & 4 deletions k6/putHelpful.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ export const options = {
duration: '30s',
};

const choices = [2, 6, 18, 19, 20];

export default function () {
const id = Math.floor(Math.random() * choices.length);
http.put(`http://localhost:3000/reviews/${choices[id]}/helpful`);
const low = 28279;
const high = 31422;
const id = Math.floor(Math.random() * (high - low) + low);
http.put(`http://localhost:3000/reviews/${id}/helpful`);
sleep(1);
};
8 changes: 4 additions & 4 deletions k6/putReport.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ export const options = {
duration: '30s',
};

const choices = [2, 6, 18, 19, 20];

export default function () {
const id = Math.floor(Math.random() * choices.length);
http.put(`http://localhost:3000/reviews/${choices[id]}/report`);
const low = 28279;
const high = 31422;
const id = Math.floor(Math.random() * (high - low) + low);
http.put(`http://localhost:3000/reviews/${id}/report`);
sleep(1);
};
14 changes: 9 additions & 5 deletions server/db/db.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
// Initilize database

const mongoose = require("mongoose");
const dbName = "atelier";
mongoose.connect(`mongodb://localhost:27017/${dbName}`, {
const dbName = process.env.DB_NAME;
const dbPort = process.env.DB_PORT;
const dbAddress = process.env.DB_ADDRESS;

const mongoAddress = `mongodb://${dbAddress}:${dbPort}/${dbName}`;
console.log(`mongo connection address: ${mongoAddress}`);

mongoose.connect(mongoAddress, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
Expand All @@ -25,9 +31,7 @@ db.on("error", console.error.bind(console, "connection error:"));
db.once("open", () => {
console.log(`Mongoose DB "${dbName}" initialized\n`)

Product.findOne({})
.then(result => console.log('products: ' + !!result))
.then(() => Review.findOne({}))
Review.findOne({})
.then(result => console.log('reviews: ' + !!result))
.then(() => ReviewPhoto.findOne({}))
.then(result => console.log('reviews_photos: ' + !!result))
Expand Down
64 changes: 34 additions & 30 deletions server/db/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const getNextValue = async (collectionName) => {
}
};

const getCurrentUnixTimestamp = () => Math.floor(new Date().getTime() / 1000);
const getCurrentUnixTimestamp = () => Math.floor(Date.now() / 1000);

const newestCompare = (a, b) => {
// Assuming UNIX timestamp
Expand Down Expand Up @@ -109,7 +109,7 @@ const getReviews = async (page, count, sort, product_id) => {
recommend: 1,
reviewer_name: 1,
reviewer_email: 1,
response: 1,
response: { $ifNull: [ '$summary', null ]},
helpfulness: 1,
photos: {
id: 1,
Expand Down Expand Up @@ -267,28 +267,31 @@ const postReview = async (reqBody) => {
}

const newReviewId = await getNextValue('reviews');
const newReview = {
id: newReviewId,
product_id,
rating,
date: getCurrentUnixTimestamp(),
summary,
body,
recommend,
reported: false,
reviewer_name: name,
reviewer_email: email,
response: '',
helpfulness: 0,
};
const newReview = [{
insertOne: {
document: {
id: newReviewId,
product_id,
rating,
date: getCurrentUnixTimestamp(),
summary,
body,
recommend,
reported: false,
reviewer_name: name,
reviewer_email: email,
response: null,
helpfulness: 0,
}
}
}];

const newReviewPhotos = [];
photos.forEach(async photoUrl => {
const newReviewPhotoId = await getNextValue('reviews_photos');
newReviewPhotos.push({
insertOne: {
document: {
id: newReviewPhotoId,
id: await getNextValue('reviews_photos'),
review_id: newReviewId,
url: photoUrl,
},
Expand All @@ -297,28 +300,29 @@ const postReview = async (reqBody) => {
});

const newCharacteristicReviews = [];
for (const characteristic_id in characteristics) {
const newCharacteristicReviewId = await getNextValue('characteristic_reviews');
Object.keys(characteristics).forEach(async characteristic_id => {
newCharacteristicReviews.push({
insertOne: {
document: {
id: newCharacteristicReviewId,
id: await getNextValue('characteristic_reviews'),
characteristic_id,
review_id: newReviewId,
value: characteristics[characteristic_id],
},
},
});
}

await Review.create(newReview);
await ReviewPhoto.bulkWrite(newReviewPhotos);
await CharacteristicReview.bulkWrite(newCharacteristicReviews);
});

return {
status: STATUS.CREATED,
message: MESSAGE.CREATED,
};
return await Promise.all([
Review.bulkWrite(newReview),
ReviewPhoto.bulkWrite(newReviewPhotos),
CharacteristicReview.bulkWrite(newCharacteristicReviews)
]).then(() => {
return {
status: STATUS.CREATED,
message: MESSAGE.CREATED,
};
});
} catch (error) {
console.error(error);
return {
Expand Down
17 changes: 12 additions & 5 deletions server/db/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ const productSchema = new mongoose.Schema({

// Create indexes

reviewSchema.index({ product_id: 1 });
reviewSchema.index({ id: 1 });
reviewPhotoSchema.index({ review_id: 1 });
characteristicReviewSchema.index({ review_id: 1 });
characteristicSchema.index({ id: 1 });
// reviewSchema.index({ product_id: 1 });
// reviewSchema.index({ id: 1 });
// reviewPhotoSchema.index({ review_id: 1 });
// characteristicReviewSchema.index({ review_id: 1 });
// characteristicSchema.index({ id: 1 });

// Create models

Expand All @@ -67,6 +67,13 @@ const ReviewPhoto = mongoose.model("reviews_photos", reviewPhotoSchema);
const CharacteristicReview = mongoose.model("characteristic_reviews", characteristicReviewSchema);
const Characteristic = mongoose.model("characteristics", characteristicSchema);

Counter.ensureIndexes()
.then(() => Product.ensureIndexes())
.then(() => Review.ensureIndexes())
.then(() => ReviewPhoto.ensureIndexes())
.then(() => CharacteristicReview.ensureIndexes())
.then(() => Characteristic.ensureIndexes());

module.exports = {
Counter,
Product,
Expand Down
2 changes: 1 addition & 1 deletion server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require('dotenv').config()
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const PORT = process.env.DB_PORT || 9999;
const PORT = process.env.SERVER_PORT || 9999;

app.use(bodyParser.json());

Expand Down