From e04eefb8a292d98f056b6f742ca801952bb5987f Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sun, 14 May 2023 15:26:22 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B2=D1=8B=D0=B9=20=D1=8D?= =?UTF-8?q?=D1=82=D0=B0=D0=BF=20=D0=B3=D0=BE=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- abitur_list_client/.env | 2 + abitur_list_client/index.js | 1 + .../mongo/controllers/abitur.controller.js | 12 ++- .../controllers/examresult.controller.js | 17 ++++ .../mongo/controllers/request.controller.js | 16 ++++ abitur_list_client/package-lock.json | 34 +++++-- abitur_list_client/package.json | 1 + abitur_list_client/routes/abitur.routes.js | 4 +- .../routes/educationform.routes.js | 4 +- .../routes/examresult.routes.js | 4 +- abitur_list_client/routes/request.routes.js | 4 +- .../routes/specialization.routes.js | 4 +- abitur_list_client/src/components/Request.vue | 8 +- .../src/components/TestPage.vue | 95 ++++++++++++++++++- 14 files changed, 177 insertions(+), 29 deletions(-) create mode 100644 abitur_list_client/.env diff --git a/abitur_list_client/.env b/abitur_list_client/.env new file mode 100644 index 0000000..7a34bcf --- /dev/null +++ b/abitur_list_client/.env @@ -0,0 +1,2 @@ +dbPath='../controllers/' +#delete '/mongo' to switch to pgsql \ No newline at end of file diff --git a/abitur_list_client/index.js b/abitur_list_client/index.js index f91782c..97d3e1f 100644 --- a/abitur_list_client/index.js +++ b/abitur_list_client/index.js @@ -1,4 +1,5 @@ const express = require('express') +require('dotenv').config() const abiturRouter = require('./routes/abitur.routes') const examResultRouter = require('./routes/examresult.routes') diff --git a/abitur_list_client/mongo/controllers/abitur.controller.js b/abitur_list_client/mongo/controllers/abitur.controller.js index e2b61fa..a426e62 100644 --- a/abitur_list_client/mongo/controllers/abitur.controller.js +++ b/abitur_list_client/mongo/controllers/abitur.controller.js @@ -5,6 +5,7 @@ const crypto = require('crypto'); const AbiturControllerInterface = require('../../controllers/interfaces/abitur.controller.interface'); const { ObjectId } = require('mongodb'); +const e = require('express'); class AbiturController extends AbiturControllerInterface{ @@ -39,7 +40,7 @@ class AbiturController extends AbiturControllerInterface{ // const newAbitur = await db.query('INSERT INTO abitur (id, first_name, last_name, middle_name, email, password) VALUES ($1, $2, $3, $4, $5, $6) RETURNING *', [id, first_name, last_name, middle_name, email, hash]) // res.json(newAbitur.rows[0]) - const {id, first_name, last_name, middle_name, email, password} = req.body + const {id, first_name, last_name, middle_name, email, password, exam_results, requests} = req.body var hash = crypto.createHash('md5').update(password).digest('hex') @@ -51,10 +52,12 @@ class AbiturController extends AbiturControllerInterface{ last_name: last_name, middle_name: middle_name, email: email, - password: hash + password: hash, + exam_results: exam_results, + requests: requests }) - if (newAbitur!=null) res.json({id: 1}) + res.json({id: 1}) } async getAbiturs(req, res) { // const abiturs = await db.query('SELECT * FROM abitur') @@ -116,7 +119,8 @@ class AbiturController extends AbiturControllerInterface{ // res.json(abiturs.rows) const collection = mymongodb.collection('abiturs') - await collection.drop() + await collection.deleteMany({}) + res.json({id: 1}) } } diff --git a/abitur_list_client/mongo/controllers/examresult.controller.js b/abitur_list_client/mongo/controllers/examresult.controller.js index 2c724ae..aec4f84 100644 --- a/abitur_list_client/mongo/controllers/examresult.controller.js +++ b/abitur_list_client/mongo/controllers/examresult.controller.js @@ -80,6 +80,23 @@ class ExamResultController extends ExamResultControllerInterface { // const id = req.params.id // const result = await db.query('select avg(result) from exam_result where id in (select exam_result_id from request_exam_result where request_id in (select id from request where specialization_id = $1))', [id]) // res.json(result.rows[0]) + + const title = req.params.id + const collection = mymongodb.collection('abiturs') + const answer = await collection.aggregate([ + { $match: { "requests.specialization": title } }, + { $unwind: "$requests" }, + { $unwind: "$requests.exam_results" }, + { + $group: { + _id: { + specialization: "$requests.specialization" + }, + avg_result: { $avg: "$requests.exam_results.result" } + } + } + ]).toArray() + res.json(answer[0]) } } diff --git a/abitur_list_client/mongo/controllers/request.controller.js b/abitur_list_client/mongo/controllers/request.controller.js index ccdb09c..0f61f7e 100644 --- a/abitur_list_client/mongo/controllers/request.controller.js +++ b/abitur_list_client/mongo/controllers/request.controller.js @@ -113,6 +113,8 @@ class RequestController extends RequestControllerInterface { } } } ) + + res.json({id: 1}) } async deleteRequest(req, res) { // const id = req.params.id @@ -137,6 +139,20 @@ class RequestController extends RequestControllerInterface { // const id = req.params.id // const requests = await db.query('SELECT COUNT(*) FROM request WHERE specialization_id=$1', [id]) // res.json(requests.rows[0]) + + const title = req.params.id + const collection = mymongodb.collection('abiturs') + const answer = await collection.aggregate([ + { $unwind: "$requests" }, + { $match: { "requests.specialization": title } }, + { + $group: { + _id: { firstname: "$first_name", lastname: "$last_name"}, + count: { $sum: 1 } + } + } + ]).toArray() + res.json(answer.length) } } diff --git a/abitur_list_client/package-lock.json b/abitur_list_client/package-lock.json index a301d24..9777b4d 100644 --- a/abitur_list_client/package-lock.json +++ b/abitur_list_client/package-lock.json @@ -10,6 +10,7 @@ "dependencies": { "axios": "^1.3.4", "core-js": "^3.8.3", + "dotenv": "^16.0.3", "express": "^4.18.2", "mongodb": "^5.5.0", "mongoose": "^7.1.1", @@ -2588,6 +2589,15 @@ } } }, + "node_modules/@vue/cli-service/node_modules/dotenv": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-10.0.0.tgz", + "integrity": "sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==", + "dev": true, + "engines": { + "node": ">=10" + } + }, "node_modules/@vue/cli-shared-utils": { "version": "5.0.8", "resolved": "https://registry.npmmirror.com/@vue/cli-shared-utils/-/cli-shared-utils-5.0.8.tgz", @@ -4727,12 +4737,11 @@ } }, "node_modules/dotenv": { - "version": "10.0.0", - "resolved": "https://registry.npmmirror.com/dotenv/-/dotenv-10.0.0.tgz", - "integrity": "sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==", - "dev": true, + "version": "16.0.3", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.3.tgz", + "integrity": "sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ==", "engines": { - "node": ">=10" + "node": ">=12" } }, "node_modules/dotenv-expand": { @@ -12186,6 +12195,14 @@ "webpack-merge": "^5.7.3", "webpack-virtual-modules": "^0.4.2", "whatwg-fetch": "^3.6.2" + }, + "dependencies": { + "dotenv": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-10.0.0.tgz", + "integrity": "sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==", + "dev": true + } } }, "@vue/cli-shared-utils": { @@ -13944,10 +13961,9 @@ } }, "dotenv": { - "version": "10.0.0", - "resolved": "https://registry.npmmirror.com/dotenv/-/dotenv-10.0.0.tgz", - "integrity": "sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==", - "dev": true + "version": "16.0.3", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.3.tgz", + "integrity": "sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ==" }, "dotenv-expand": { "version": "5.1.0", diff --git a/abitur_list_client/package.json b/abitur_list_client/package.json index 8682d88..c83dcb6 100644 --- a/abitur_list_client/package.json +++ b/abitur_list_client/package.json @@ -11,6 +11,7 @@ "dependencies": { "axios": "^1.3.4", "core-js": "^3.8.3", + "dotenv": "^16.0.3", "express": "^4.18.2", "mongodb": "^5.5.0", "mongoose": "^7.1.1", diff --git a/abitur_list_client/routes/abitur.routes.js b/abitur_list_client/routes/abitur.routes.js index b205796..4c3a51a 100644 --- a/abitur_list_client/routes/abitur.routes.js +++ b/abitur_list_client/routes/abitur.routes.js @@ -1,8 +1,8 @@ const Router = require('express') const router = new Router() -const abiturController = require('../controllers/abitur.controller') -//const abiturController = require('../mongo/controllers/abitur.controller') +//const abiturController = require('../controllers/abitur.controller') +const abiturController = require(process.env.dbPath + 'abitur.controller') router.post('/abitur', abiturController.createAbitur) router.post('/abitur/manual', abiturController.createAbiturWithId) diff --git a/abitur_list_client/routes/educationform.routes.js b/abitur_list_client/routes/educationform.routes.js index e40b9d8..8c6e61d 100644 --- a/abitur_list_client/routes/educationform.routes.js +++ b/abitur_list_client/routes/educationform.routes.js @@ -1,8 +1,8 @@ const Router = require('express') const router = new Router() -const educationformController = require('../controllers/educationform.controller') -//const educationformController = require('../mongo/controllers/educationform.controller') +//const educationformController = require('../controllers/educationform.controller') +const educationformController = require(process.env.dbPath + 'educationform.controller') router.post('/educationform', educationformController.createEducationForm) router.get('/educationform', educationformController.getEducationForms) diff --git a/abitur_list_client/routes/examresult.routes.js b/abitur_list_client/routes/examresult.routes.js index 6a983ff..6a5c5fe 100644 --- a/abitur_list_client/routes/examresult.routes.js +++ b/abitur_list_client/routes/examresult.routes.js @@ -1,8 +1,8 @@ const Router = require('express') const router = new Router() -const examResultController = require('../controllers/examresult.controller') -//const examResultController = require('../mongo/controllers/examresult.controller') +//const examResultController = require('../controllers/examresult.controller') +const examResultController = require(process.env.dbPath + 'examresult.controller') router.post('/examresult', examResultController.createExamResult) router.post('/examresult/manual', examResultController.createExamResultWithId) diff --git a/abitur_list_client/routes/request.routes.js b/abitur_list_client/routes/request.routes.js index 29d32c8..8c14bff 100644 --- a/abitur_list_client/routes/request.routes.js +++ b/abitur_list_client/routes/request.routes.js @@ -1,8 +1,8 @@ const Router = require('express') const router = new Router() -const requestController = require('..//controllers/request.controller') -//const requestController = require('../mongo/controllers/request.controller') +//const requestController = require('..//controllers/request.controller') +const requestController = require(process.env.dbPath + 'request.controller') router.post('/request', requestController.createRequest) router.post('/request/manual', requestController.createRequestWithId) diff --git a/abitur_list_client/routes/specialization.routes.js b/abitur_list_client/routes/specialization.routes.js index 75ef85e..1d467b9 100644 --- a/abitur_list_client/routes/specialization.routes.js +++ b/abitur_list_client/routes/specialization.routes.js @@ -1,8 +1,8 @@ const Router = require('express') const router = new Router() -const specializationController = require('../controllers/specialization.controller') -//const specializationController = require('../mongo/controllers/specialization.controller') +//const specializationController = require('../controllers/specialization.controller') +const specializationController = require(process.env.dbPath + 'specialization.controller') router.post('/specialization', specializationController.createSpecialization) router.get('/specialization', specializationController.getSpecializations) diff --git a/abitur_list_client/src/components/Request.vue b/abitur_list_client/src/components/Request.vue index b89cf60..6ac4c69 100644 --- a/abitur_list_client/src/components/Request.vue +++ b/abitur_list_client/src/components/Request.vue @@ -183,8 +183,8 @@ export default { let data = JSON.stringify({ "id": this.requestId, - "education_form": this.educationForm, - "specialization": this.specialization, + "education_form": this.educationForm['title'] ?? this.educationForm, + "specialization": this.specialization['title'] ?? this.specialization, "exam_results": this.results, "date": this.request['date'], "abitur_id": localStorage.getItem('abiturId') @@ -200,7 +200,7 @@ export default { data : data }; - axios.request(config) + await axios.request(config) .then((response) => { console.log(JSON.stringify(response.data)); }) @@ -239,7 +239,7 @@ export default { data : data }; - axios.request(config) + await axios.request(config) .then((response) => { console.log(JSON.stringify(response.data)); }) diff --git a/abitur_list_client/src/components/TestPage.vue b/abitur_list_client/src/components/TestPage.vue index 74705da..f89cf45 100644 --- a/abitur_list_client/src/components/TestPage.vue +++ b/abitur_list_client/src/components/TestPage.vue @@ -39,7 +39,10 @@ export default { methods: { async getDestiny() { if (this.specialization == null) return - + if (localStorage.getItem('db') == 'mongo') { + await this.getMongoDestiny() + return + } var time = performance.now() var avg = (await axios.get('http://127.0.0.1:8080/api/examresult/avgBySpec/'+this.specialization['id'])).data @@ -57,6 +60,22 @@ export default { this.destiny = 'ЗАПРОСОВ: ' + count['count'] + ', СРЕДНИЙ BALL: ' + avg['avg'] + '. ПОЗДРАВЛЯЮ! Время: ' + time + 'c.' }, + async getMongoDestiny() { + var time = performance.now() + var avg = (await axios.get('http://127.0.0.1:8080/api/examresult/avgBySpec/'+this.specialization['title'] ?? this.specialization)).data + console.log(avg['avg_result']) + var count = (await axios.get('http://127.0.0.1:8080/api/request/spec/'+this.specialization['title'])).data + console.log(count) + var time = (performance.now() - time) / 1000 + + if (count == null || avg == null) { + this.destiny = 'ТВОЯ ОТЧЕТНОСТЬ ПРОКЛЯТА. УБИРАЙСЯ!' + return + } + this.destiny = 'ЗАПРОСОВ: ' + count + ', СРЕДНИЙ BALL: ' + avg['avg_result'] + '. ПОЗДРАВЛЯЮ! Время: ' + time + 'c.' + + }, + async deleteAllAbiturs() { try { this.deleteAllMessage='Жнец идет за их душами...' @@ -77,6 +96,12 @@ export default { if (this.specializations == null || this.specialization.length == 0) { return } + + if (localStorage.getItem('db') == 'mongo') { + await this.generateMongoSlaves() + return + } + console.log(this.specializations) // удалим старых, дорогу молодым емае await this.deleteAllAbiturs() @@ -200,11 +225,77 @@ export default { var matan = Math.floor(Math.random() * max); console.log('matan end') return matan - } + }, + async generateMongoSlaves() { + // удалим старых, дорогу молодым емае + await this.deleteAllAbiturs() + this.magicMessage='Плодим, ждите...' + var averageTime = 0 + + for(const spec of this.specializations) { + // создаем бедных студентиков сто штучек оптовая цена + var oneSpecTime = performance.now() + + for (let abiturNum = 0 + parseInt(spec['id'], 10) * 100; abiturNum < 100 + parseInt(spec['id'], 10) * 100; abiturNum++) { + + let exam_results = [ + { + "title": "Exam#" + abiturNum + "-1", + "result": this.getRandomInt(100) + }, + { + "title": "Exam#" + abiturNum + "-2", + "result": this.getRandomInt(100) + }, + { + "title": "Exam#" + abiturNum + "-3", + "result": this.getRandomInt(100) + } + ] + + let data = JSON.stringify({ + "id": abiturNum, + "first_name": "Vasya " + abiturNum, + "last_name": "Pupkin " + abiturNum, + "middle_name": "Vasylyevich " + abiturNum, + "email": "mylo" + abiturNum, + "password": "parol" + abiturNum, + "exam_results": exam_results, + "requests": [ + { + "exam_results": exam_results, + "date": Date.now(), + "specialization": spec['title'], + "education_form": "Заочная" + } + ] + }); + + let config = { + method: 'post', + maxBodyLength: Infinity, + url: 'http://127.0.0.1:8080/api/abitur/manual', + headers: { + 'Content-Type': 'application/json' + }, + data : data + }; + + await axios.request(config) + + + } + oneSpecTime = performance.now() - oneSpecTime + averageTime += oneSpecTime + } + averageTime = averageTime / this.specializations.length / 1000 + this.magicMessage='Наплодили! Среднее время на каждую специализацию: '+ averageTime + 'c.' + }, }, async created() { + this.specializations = (await axios.get('http://127.0.0.1:8080/api/specialization')).data this.specialization = this.specializations[0] }