Update db to new state

parent 8b091016
'use strict';
const mongoose = require('mongoose');
const TYPES = module.exports.TYPES = {
GROUP: "group",
COURSE: "course",
CONTEST: "contest",
TASK: "task",
};
const AttachmentSchema = new mongoose.Schema({
created: {
type: Date,
default: new Date()
},
file: {
type: mongoose.Schema.Types.ObjectId,
required: true
},
filename: {
type: String,
required: true,
default: 'No_Filename'
},
type: {
type: String,
required: true
},
group: {
type: mongoose.Schema.Types.ObjectId
},
course: {
type: mongoose.Schema.Types.ObjectId
},
contest: {
type: mongoose.Schema.Types.ObjectId
},
task: {
type: mongoose.Schema.Types.ObjectId
},
owner: {
type: mongoose.Schema.Types.ObjectId
}
});
module.exports.schema = AttachmentSchema;
const Attachment = mongoose.connection.model('Attachment', AttachmentSchema);
module.exports.model = Attachment;
module.exports.add = attachment => {
return new Attachment(attachment).save();
};
module.exports.remove = id => {
return Attachment.deleteOne({_id: id})
};
module.exports.get = id => {
return Attachment.findOne({_id: id});
};
module.exports.getForCourse = course => {
return Attachment.find({course, type: TYPES.COURSE});
};
module.exports.getForContest = contest => {
return Attachment.find({contest, type: TYPES.CONTEST});
};
module.exports.getForTask = task => {
return Attachment.find({task, type: TYPES.TASK});
};
module.exports.getForGroup = group => {
return Attachment.find({group, type: TYPES.GROUP});
};
/**
* Created by danilaeremin on 17.07.17.
*/
"use strict";
const mongoose = require('mongoose');
const logger = require('./logger');
const logger = require('../libs/log');
const CompilerSchema = new mongoose.Schema({
name: {
......@@ -35,21 +31,40 @@ const CompilerSchema = new mongoose.Schema({
},
forcedCompileOutputExtension: {
type: String
},
extensions: {
type: [String],
default: []
}
});
module.exports.MANUAL_CONPILER = {
name: 'manual',
displayedName: 'Интеллектульаный',
isProject: false
}
module.exports.schema = CompilerSchema;
const Compiler = mongoose.connection.model('Compiler', CompilerSchema);
module.exports.model = Compiler;
module.exports.getCompilers = () => {
return Compiler.find({})
.catch(mongoose.catch_function);
module.exports.getAll = () => {
return Compiler.find({});
};
module.exports.getMany = (ids) => {
return Compiler.findOne({_id: {$in: ids}});
};
module.exports.getShotForNames = (names) => {
return Compiler.find({name: {$in: names}}).select({_id: 0, name: 1, displayedName: 1, isProject: 1, extensions: 1});
};
module.exports.getCompiler = (compilerId) => {
return Compiler.findOne({_id: compilerId})
.catch(mongoose.catch_function);
.catch(err => {
logger.error(err);
});
};
module.exports.addCompiler = (name, displayedName, isProject, compileCommands, detectCommand, detectOutput, runCommand, forcedCompileOutputExtension) => {
......@@ -64,7 +79,9 @@ module.exports.addCompiler = (name, displayedName, isProject, compileCommands, d
forcedCompileOutputExtension: forcedCompileOutputExtension
}))
.save()
.catch(mongoose.catch_function);
.catch(err => {
logger.error(err);
});
};
module.exports.updateCompiler = (compilerId, name, displayedName, isProject, compileCommands, detectCommand, detectOutput, runCommand, forcedCompileOutputExtension) => {
......@@ -78,5 +95,7 @@ module.exports.updateCompiler = (compilerId, name, displayedName, isProject, com
runCommand: runCommand,
forcedCompileOutputExtension: forcedCompileOutputExtension
})
.catch(mongoose.catch_function)
.catch(err => {
logger.error(err);
})
};
/**
* Created by danilaeremin on 17.07.17.
*/
"use strict";
const mongoose = require('mongoose');
const logger = require('./logger');
const _ = require('lodash');
const Random = require('random-js');
const random = new Random(Random.engines.mt19937().autoSeed());
const DISTRIBUTION = {
RANDOM : 'random',
LAST: 'last',
UNIFORM_RANDOM: 'uniform_random',
MANUAL: 'manual'
const defines = require('./defines');
const CONTEST_TYPES = {
REGULAR: 'regular',
OPTIONAL: 'optional',
EXAM: 'exam'
};
module.exports.DISTRIBUTION = DISTRIBUTION;
module.exports.CONTEST_TYPES = CONTEST_TYPES;
const ContestSchema = new mongoose.Schema({
owner: {
type: mongoose.Schema.Types.ObjectId,
required: true
},
created: {
type: Date,
default: new Date()
},
course: {
type: mongoose.Schema.Types.ObjectId,
required: true
},
status: {
type: Number,
default: defines.OBJECT_STATUS.ACTIVE
},
type: {
type: String,
default: CONTEST_TYPES.REGULAR
},
name: {
type: String,
required: true
......@@ -33,41 +46,60 @@ const ContestSchema = new mongoose.Schema({
default: ''
},
logo: {
type: String,
default: ''
type: mongoose.Schema.Types.ObjectId
},
visible: {
type: Boolean,
default: false
default: true
},
course: {
type: mongoose.Schema.Types.ObjectId,
required: true
},
files: {
type: [
mongoose.Schema.Types.ObjectId
],
default: []
},
exam: {
type: Boolean,
default: false
threshold: {
score: {
type: Number,
default: null
},
time: {
type: Number,
default: null
}
},
variantsCount: {
type: Number,
default: 1
},
distribution: {
type: String,
default: DISTRIBUTION.RANDOM
default: defines.DISTRIBUTIONS.RANDOM
},
userVariants: {
type: {},
default: {}
},
testerConfig: {
resultVisibility: {
input: {type: Boolean, default: true},
output: {type: Boolean, default: true},
correct: {type: Boolean, default: true},
error: {type: Boolean, default: true}
},
inputFileNames: {
type: [String],
default: []
},
outputFileNames: {
type: [String],
default: []
},
tests: {
type: [mongoose.Schema.Types.ObjectId],
default: []
}
},
mailing: {
type: {},
default: 86400 * 1000 * 7
},
jobId: {
type: mongoose.Schema.Types.ObjectId
}
});
......@@ -75,135 +107,152 @@ module.exports.schema = ContestSchema;
const Contest = mongoose.connection.model('Contest', ContestSchema);
module.exports.model = Contest;
module.exports.getAllContests = () => {
return Contest.find({})
.catch(mongoose.catch_function);
module.exports.add = (course, owner, name, brief, description, visible, type) => {
return new Contest({course, owner, name, brief, description, visible, type}).save()
};
module.exports.setVisible = (id, visible) => {
return Contest.findOneAndUpdate({_id: id}, {visible});
};
module.exports.setType = (id, type) => {
return Contest.findOneAndUpdate({ _id: id }, { type });
};
module.exports.setScoreThreshold = (id, score) => {
return Contest.findOneAndUpdate({ _id: id }, { 'threshold.score': score });
};
module.exports.setTimeThreshold = (id, time) => {
return Contest.findOneAndUpdate({ _id: id }, { 'threshold.time': time });
};
module.exports.getContest = (contestId) => {
return Contest.findOne({_id: contestId})
.catch(mongoose.catch_function);
module.exports.setLogo = (id, logo) => {
return Contest.findOneAndUpdate({_id: id}, {logo});
};
module.exports.getContestsForCourse = (courseId) => {
return Contest.find({course: courseId})
.catch(mongoose.catch_function);
module.exports.get = async id => {
return Contest.findOne({_id: id});
};
module.exports.addContest = (courseId, name, brief, description) => {
return new Contest({course: courseId, name: name, brief: brief, description: description})
.save()
.catch(mongoose.catch_function);
module.exports.getForCourse = async course => {
return Contest.find({
course,
$or: [
{ status: {$exists: false} },
{ status: defines.OBJECT_STATUS.ACTIVE }
]
});
};
module.exports.setInfo = (taskId, name, brief, description) => {
return Contest.findOneAndUpdate({_id: taskId}, {name: name, brief: brief, description: description})
.catch(mongoose.catch_function)
module.exports.getActive = () => {
return Contest.find({
$or: [
{ status: {$exists: false} },
{ status: defines.OBJECT_STATUS.ACTIVE }
]
});
};
module.exports.setExamMode = (contestId, exam, variants, distribution) => {
return Contest.findOneAndUpdate({_id: contestId}, {exam: exam, variantsCount:variants, distribution: distribution})
module.exports.getMany = (ids) => {
return Contest.find({
_id: {$in: ids},
$or: [
{ status: {$exists: false} },
{ status: defines.OBJECT_STATUS.ACTIVE }
]
});
};
module.exports.nextVariant = (contestId) => {
return module.exports.getContest(contestId)
.then (contest => {
let total = contest.variantsCount;
let variants = [];
for (let v = 0; v < total; v++) {
variants[v] = 0;
}
module.exports.getVisibleForCourse = courseId => {
return Contest.find({
course: courseId,
visible: true,
$or: [
{ status: {$exists: false} },
{ status: defines.OBJECT_STATUS.ACTIVE }
]
});
};
for (let user in contest.userVariants) {
if (contest.userVariants.hasOwnProperty(user)) {
variants[contest.userVariants[user]] += 1;
}
}
module.exports.getVisibleForCourses = (courseIds) => {
return Contest.find({
course: {$in: courseIds},
visible: true,
$or: [
{ status: {$exists: false} },
{ status: defines.OBJECT_STATUS.ACTIVE }
]
});
};
let variant = 0;
let max = _.max(variants);
let min = _.min(variants);
module.exports.setInfo = (id, name, brief, description, variantsCount) => {
let update = {};
switch (contest.distribution) {
case DISTRIBUTION.LAST:
variant = _.indexOf(variants, min);
break;
case DISTRIBUTION.RANDOM:
variant = random.integer(0, total-1);
break;
case DISTRIBUTION.UNIFORM_RANDOM:
let vars = [];
if (name)
update.name = name;
for (let cur_var = 0; cur_var < total; cur_var ++) {
if (variants[cur_var] < max) {
vars.push(cur_var)
}
}
if (brief)
update.brief = brief;
variant = vars[random.integer(0, vars.length-1)] || 0;
if (description)
update.description = description;
break;
case DISTRIBUTION.MANUAL:
throw 'No next variant, distribution is manual';
break;
}
if (variantsCount)
update.variantsCount = variantsCount;
return variant;
})
return Contest.findOneAndUpdate({_id: id}, update);
};
module.exports.setUserVariant = (contestId, userId, variant) => {
return (new Promise( s => {
if (variant === undefined) {
module.exports.nextVariant(contestId)
.then(nextVariant => {
s(nextVariant)
})
.catch(() => {
s(0)
})
} else {
s(variant);
module.exports.setTestingFileNames = (id, inputFileNames, outputFileNames) => {
return Contest.findOneAndUpdate({_id: id}, {
testing : {
inputFileNames,
outputFileNames
}
}))
.then(user_varinat => {
let path = 'userVariants.' + userId;
let set = {};
set[path] = user_varinat;
return Contest.findOneAndUpdate({_id: contestId}, {$set: set})
.then(() => {
return user_varinat;
})
})
.catch(err => {
console.log(err);
logger.error(err);
});
};
module.exports.getUserVariant = (contestId, userId) => {
return module.exports.getContest(contestId)
.then(contest => {
if (contest.userVariants[userId] !== undefined) {
return contest.userVariants[userId];
} else {
return module.exports.setUserVariant(contestId, userId)
}
})
.catch(mongoose.catch_function);
};
module.exports.addFile = (contestId, fileId) => {
return Contest.findOneAndUpdate({_id: contestId}, { $addToSet: {files: fileId} })
.catch(mongoose.catch_function);
};
module.exports.removeFile = (contestId, fileId) => {
return Contest.findOneAndUpdate({_id: contestId}, { $pull: {files: fileId} })
.catch(mongoose.catch_function);
};
module.exports.removeContest = (contestId) => {
return Contest.remove({_id: contestId})
.catch(mongoose.catch_function);
});
};
module.exports.setResultVisibility = (id, resultVisibility) => {
return Contest.findOneAndUpdate({ _id: id }, {'testerConfig.resultVisibility': resultVisibility});
};
module.exports.setUserVariant = (id, userId, variant) => {
let path = 'userVariants.' + userId;
let set = {};
set[path] = variant;
return Contest.findOneAndUpdate({_id: id}, {$set: set});
};
module.exports.removeTest = (id, test) => {
return Contest.findOneAndUpdate({_id: id}, { $pull: {'testerConfig.tests': test} });
};
module.exports.addTest = (id, test) => {
return Contest.findOneAndUpdate({_id: id}, { $addToSet: {'testerConfig.tests': test} });
};
module.exports.getArchived = owner => {
return Contest.find({owner, status: defines.OBJECT_STATUS.ARCHIVED})
};
module.exports.archive = (id) => {
return Contest.findOneAndUpdate({_id: id}, {status: defines.OBJECT_STATUS.ARCHIVED});
};
//Set deleted status
module.exports.delete = id => {
return Contest.findOneAndUpdate({_id: id}, {status: defines.OBJECT_STATUS.DELETED});
};
module.exports.activate = id => {
return Contest.findOneAndUpdate({_id: id}, {status: defines.OBJECT_STATUS.ACTIVE});
};
//Totally remove from db
module.exports.remove = id => {
return Contest.deleteOne({_id: id});
};
/**
* Created by danilaeremin on 17.07.17.
*/
"use strict";
const mongoose = require('mongoose');
const logger = require('./logger');
const defines = require('./defines');
const CourseSchema = new mongoose.Schema({
status: {
type: Number,
default: defines.OBJECT_STATUS.ACTIVE
},
created: {
type: Date,
default: new Date()
},
owner: {
type: mongoose.Schema.Types.ObjectId,
required: true
},
name: {
type: String,
required: true
......@@ -21,31 +30,14 @@ const CourseSchema = new mongoose.Schema({
default: ''
},
logo: {
type: String,
default: ''
type: mongoose.Schema.Types.ObjectId
},
visible: {
type: Boolean,
default: false
},
groups: {
type: [
mongoose.Schema.Types.ObjectId
],
default: []
},
owner: {
type: mongoose.Schema.Types.ObjectId,
required: true
default: true
},
teachers: {
type: mongoose.Schema.Types.ObjectId,
default: []
},
files: {
type: [
mongoose.Schema.Types.ObjectId
],
type: [mongoose.Schema.Types.ObjectId],
default: []
}
});
......@@ -54,94 +46,149 @@ module.exports.schema = CourseSchema;
const Course = mongoose.connection.model('Course', CourseSchema);
module.exports.model = Course;
module.exports.getAllCourses = () => {
return Course.find({})
.catch(mongo