Update task model

parent b9d4d19f
......@@ -13,7 +13,7 @@ const TaskSchema = new mongoose.Schema({
},
created: {
type: Date,
default: new Date()
default: Date.now
},
type: {
type: String,
......@@ -55,9 +55,9 @@ const TaskSchema = new mongoose.Schema({
type: Number,
default: 1
},
hard: {
type: Boolean,
default: false
difficulty: {
type: String,
default: defines.TASK_DIFFICULTY.USUAL
},
testerConfig: {
resultVisibility: {
......@@ -72,9 +72,6 @@ const TaskSchema = new mongoose.Schema({
patternFile: {
type: mongoose.Schema.Types.ObjectId
},
sampleFile: {
type: mongoose.Schema.Types.ObjectId
},
compileFile: {
type: mongoose.Schema.Types.ObjectId
},
......@@ -88,7 +85,7 @@ const TaskSchema = new mongoose.Schema({
},
arguments: {
type: String,
required: true
default: ""
},
compileProjectFileName: {
type: String,
......@@ -115,6 +112,11 @@ const TaskSchema = new mongoose.Schema({
type: mongoose.Schema.Types.ObjectId,
ref: 'Program'
},
sampleFiles: {
type: Map,
of: mongoose.Schema.Types.ObjectId,
default: {}
},
inputFileNames: {
type: [String],
default: []
......@@ -126,6 +128,14 @@ const TaskSchema = new mongoose.Schema({
checkPlagiarism: {
type: Boolean,
default: false
},
useStdin: {
type: Boolean,
default: false
},
checkStdout: {
type: Boolean,
default: false
}
},
testingContent: {
......@@ -141,13 +151,13 @@ const TaskSchema = new mongoose.Schema({
type: Number,
default: 2
},
question: {
type: String,
default: ''
errorLimit: {
type: Number,
default: 0
},
answer: {
type: Object,
default: 0
default: ''
},
variants: {
type: Array,
......@@ -160,21 +170,20 @@ const TaskSchema = new mongoose.Schema({
}
});
TaskSchema.index({ contest: 1, status: 1, difficulty: 1 });
module.exports.schema = TaskSchema;
const Task = mongoose.connection.model('Task', TaskSchema);
module.exports.model = Task;
module.exports.add = (contest, course, owner, name, brief, description, index, type) => {
let task = new Task({contest, course, owner, name, brief, description, index, type});
return task.save();
module.exports.add = task => {
let t = new Task(task);
return t.save();
};
module.exports.getActive = () => {
return Task.find({
$or: [
{ status: {$exists: false} },
{ status: defines.OBJECT_STATUS.ACTIVE }
]
status: defines.OBJECT_STATUS.ACTIVE
});
};
......@@ -182,37 +191,50 @@ module.exports.get = id => {
return Task.findOne({_id: id});
};
module.exports.getMany = (ids) => {
return Task.find({
_id: {$in: ids},
status: defines.OBJECT_STATUS.ACTIVE
});
};
module.exports.getManyShort = ids => {
return Task.find({
_id: {$in: ids},
status: defines.OBJECT_STATUS.ACTIVE
}).select({name: 1, brief: 1, logo: 1});
};
module.exports.getForContest = contest => {
return Task.find({
contest,
$or: [
{ status: {$exists: false} },
{ status: defines.OBJECT_STATUS.ACTIVE }
]
status: defines.OBJECT_STATUS.ACTIVE
}).sort({index: 1});
};
module.exports.getShortForContest = contest => {
return module.exports.getForContest(contest).select({_id: 1, name: 1});
return module.exports.getForContest(contest).select({_id: 1, type: 1, name: 1, brief: 1, logo: 1, weight: 1, difficulty: 1});
};
module.exports.getCountForContest = contest => {
return Task.countDocuments({
contest,
$or: [
{ status: {$exists: false} },
{ status: defines.OBJECT_STATUS.ACTIVE }
]
status: defines.OBJECT_STATUS.ACTIVE
});
};
module.exports.getCountForContestAndVariant = (contest, variant) => {
return Task.countDocuments({
contest,
variant,
status: defines.OBJECT_STATUS.ACTIVE
});
};
module.exports.getForContests = contests => {
return Task.find({
contest: {$in: contests},
$or: [
{status: {$exists: false}},
{status: defines.OBJECT_STATUS.ACTIVE}
]
status: defines.OBJECT_STATUS.ACTIVE
});
};
......@@ -220,35 +242,83 @@ module.exports.aggregateForContests = contests => {
return Task.aggregate()
.match({
contest: {$in: contests},
$or: [
{status: {$exists: false}},
{status: defines.OBJECT_STATUS.ACTIVE}
]
status: defines.OBJECT_STATUS.ACTIVE
})
.group({
_id: {contest: "$contest", variant: {$ifNull: ["$variant", 1]}},
tasks: {$push: {_id: "$_id", name: "$name", index: "$index", difficulty: "$difficulty", checkPlagiarism: "$testerConfig.checkPlagiarism", weight: {$ifNull: ["$weight", 1]}}},
weightSum: {
$sum: {
$cond: [{$eq: ["$difficulty", defines.TASK_DIFFICULTY.USUAL]}, {$ifNull: ["$weight", 1]}, 0]
}
}
})
};
module.exports.aggregateWeightForContests = contests => {
return Task.aggregate()
.match({
contest: {$in: contests},
difficulty: defines.TASK_DIFFICULTY.USUAL,
status: defines.OBJECT_STATUS.ACTIVE
})
.group({
_id: "$contest",
weightSum: {$sum: {$ifNull: ["$weight", 1]}}
})
};
module.exports.aggregateWeightForContest = contest => {
return Task.aggregate()
.match({
contest,
difficulty: defines.TASK_DIFFICULTY.USUAL,
status: defines.OBJECT_STATUS.ACTIVE
})
.group({
_id: "$variant",
weightSum: {$sum: {$ifNull: ["$weight", 1]}}
})
};
module.exports.aggregateWeightForContestAndVariant = (contest, variant) => {
return Task.aggregate()
.match({
contest,
variant,
difficulty: defines.TASK_DIFFICULTY.USUAL,
status: defines.OBJECT_STATUS.ACTIVE
})
.group({
_id: {contest: "$contest", variant: {$ifNull: ["$variant", 0]}},
tasks: {$push: {_id: "$_id", name: "$name", index: "$index", weight: {$ifNull: ["$weight", 1]}}},
_id: "$variant",
weightSum: {$sum: {$ifNull: ["$weight", 1]}}
})
};
module.exports.getForVariantAndContest = (contest, variant) => {
module.exports.getForVariantAndContest = (contest, variant, hard) => {
return Task.find({
contest,
variant, $or: [
{status: {$exists: false}},
{status: defines.OBJECT_STATUS.ACTIVE}
]
variant,
status: defines.OBJECT_STATUS.ACTIVE
}).sort({index: 1});
};
module.exports.getNearForVariantAndContestAndIndex = (contest, variant, index) => {
return Task.find({
contest,
variant,
status: defines.OBJECT_STATUS.ACTIVE,
index: {$in: [index - 1, index + 1]}
}).select({_id: 1, index: 1, contest: 1, variant: 1, difficulty: 1}).sort({index: 1});
};
module.exports.getShortForContestAndVariant = (contest, variant) => {
return module.exports.getForVariantAndContest(contest, variant).select({_id: 1, name: 1});
return module.exports.getForVariantAndContest(contest, variant).select({_id: 1, type: 1, name: 1, difficulty: 1});
};
module.exports.getShortForVariantAndContest = (contest, variant) => {
return module.exports.getForVariantAndContest(contest, variant)
.select({name: 1, brief: 1, logo: 1, weight: 1, hard: 1})
.select({name: 1, brief: 1, logo: 1, weight: 1, type: 1, difficulty: 1})
};
module.exports.setVariant = (id, variant) => {
......@@ -265,45 +335,37 @@ module.exports.setType = (id, taskType) => {
module.exports.setTestingContent = (id, content) => {
return Task.findOneAndUpdate({_id: id}, {
'testingContent.question': content.question || '',
'testingContent.answer': content.answer || [],
'testingContent.answer': content.answer,
'testingContent.variants': content.variants || ['', ''],
'testingContent.type': content.type || 0,
});
};
module.exports.setInfo = (id, name, brief, description, variant) => {
module.exports.setInfo = (id, name, brief, description) => {
let update = {};
if(name)
update.name = name;
if(brief)
if(brief !== undefined)
update.brief = brief;
if(description)
if(description !== undefined)
update.description = description;
if (variant)
update.variant = variant;
return Task.findOneAndUpdate({_id: id}, update);
};
module.exports.removeTest = (id, test) => {
return Task.findOneAndUpdate({_id: id}, { $pull: {'testerConfig.tests': test} });
};
module.exports.addTest = (id, test) => {
return Task.findOneAndUpdate({_id: id}, { $addToSet: {'testerConfig.tests': test} });
};
module.exports.setWeight = (id, weight) => {
return Task.findOneAndUpdate({_id: id}, {weight});
};
module.exports.setHard = (id, hard) => {
return Task.findOneAndUpdate({_id: id}, {hard});
module.exports.setDifficulty = (id, difficulty) => {
return Task.findOneAndUpdate({_id: id}, {difficulty});
};
module.exports.setDifficultiesForContest = (contest, difficulty) => {
return Task.updateMany({contest}, {difficulty});
};
module.exports.setTesterResultVisibility = (id, resultVisibility) => {
......@@ -318,22 +380,37 @@ module.exports.setTestingResultLimits = (id, resultLimits) => {
});
};
module.exports.setTesterResultVisibilityMany = (ids, resultVisibility) => {
return Task.updateMany({_id: {$in: ids}}, {'testerConfig.resultVisibility': resultVisibility})
module.exports.setTestingErrorLimit = (id, errorLimit) => {
return Task.findOneAndUpdate({ _id: id }, {
'testingContent.errorLimit': errorLimit
});
};
module.exports.setTesterResultVisibilityForContest = (contest, resultVisibility) => {
return Task.updateMany({
contest,
type: defines.TASK_TYPES.TESTER
}, {'testerConfig.resultVisibility': resultVisibility})
};
module.exports.setTesterConfig = (id, config) => {
return Task.findOneAndUpdate({_id: id},
{
'testerConfig.compilers' : config.compilers,
'testerConfig.cpuTimeLimit' : config.cpuTimeLimit || -1,
'testerConfig.memoryLimit' : config.memoryLimit || -1,
'testerConfig.testingType' : config.testingType || null,
'testerConfig.compileProjectFileName': config.compileProjectFileName || null,
'testerConfig.inputFileNames': config.inputFileNames || null,
'testerConfig.outputFileNames': config.outputFileNames || null,
'testerConfig.customTester': config.customTester,
'testerConfig.externalTester': config.externalTester
'testerConfig.externalTester': config.externalTester,
'testerConfig.inputTestProgram': config.inputTestProgram,
'testerConfig.outputTestProgram': config.outputTestProgram
});
};
module.exports.setStdConfig = (id, config) => {
return Task.findOneAndUpdate({_id: id},
{
'testerConfig.useStdin': config.useStdin,
'testerConfig.checkStdout': config.checkStdout
});
};
......@@ -341,6 +418,10 @@ module.exports.setPlagiarism = (id, checkPlagiarism) => {
return Task.findOneAndUpdate({_id: id}, {'testerConfig.checkPlagiarism': checkPlagiarism});
};
module.exports.setPlagiarismForContest = (contest, checkPlagiarism) => {
return Task.updateMany({contest}, {'testerConfig.checkPlagiarism': checkPlagiarism});
};
module.exports.addInputFile = (id, fileName) => {
return Task.findOneAndUpdate({_id: id}, {$push: {'testerConfig.inputFileNames': fileName}});
};
......@@ -357,16 +438,33 @@ module.exports.popOutputFile = id => {
return Task.findOneAndUpdate({_id: id}, {$pop: {'testerConfig.outputFileNames': 1}});
};
module.exports.setCompileFile = (id, compileFile) => {
return Task.findOneAndUpdate({_id: id}, {'testerConfig.compileFile' : compileFile});
module.exports.setCompilers = (id, addCompilers, removeCompilers) => {
return Task.findOneAndUpdate({_id: id}, {
$unset: removeCompilers,
$set: addCompilers
});
};
module.exports.setCompilerConfig = (id, compiler, config) => {
return Task.findOneAndUpdate({_id: id},
{
[`testerConfig.compilers.${compiler}.cpuTimeLimit`] : config.cpuTimeLimit || -1,
[`testerConfig.compilers.${compiler}.memoryLimit`]: config.memoryLimit || -1,
[`testerConfig.compilers.${compiler}.arguments`] : config.arguments || '',
[`testerConfig.compilers.${compiler}.compileProjectFileName`] : config.compileProjectFileName || ''
});
};
module.exports.setCompileFile = (id, compiler, compileFile) => {
return Task.findOneAndUpdate({_id: id}, {[`testerConfig.compilers.${compiler}.compileFile`]: compileFile});
};
module.exports.setPatternFile = (id, patternFile) => {
return Task.findOneAndUpdate({_id: id}, {'testerConfig.patternFile': patternFile});
module.exports.setPatternFile = (id, compiler, patternFile) => {
return Task.findOneAndUpdate({_id: id}, {[`testerConfig.compilers.${compiler}.patternFile`]: patternFile});
};
module.exports.setSampleFile = (id, sampleFile) => {
return Task.findOneAndUpdate({_id: id}, {'testerConfig.sampleFile': sampleFile});
module.exports.setSampleFile = (id, compiler, sampleFile) => {
return Task.findOneAndUpdate({_id: id}, {[`testerConfig.sampleFiles.${compiler}`]: sampleFile});
};
module.exports.setTester = (id, tester) => {
......@@ -377,12 +475,12 @@ module.exports.setExternalTester = (id, externalTester) => {
return Task.findOneAndUpdate({_id: id}, {'testerConfig.externalTester': externalTester});
};
module.exports.getArchived = owner => {
return Task.find({owner, status: defines.OBJECT_STATUS.ARCHIVED});
module.exports.getRemoved = contest => {
return Task.find({contest, status: defines.OBJECT_STATUS.REMOVED});
};
module.exports.archive = (id) => {
return Task.findOneAndUpdate({_id: id}, {status: defines.OBJECT_STATUS.ARCHIVED});
module.exports.remove = (id) => {
return Task.findOneAndUpdate({_id: id}, {status: defines.OBJECT_STATUS.REMOVED});
};
//Set deleted status
......@@ -390,15 +488,35 @@ module.exports.delete = id => {
return Task.findOneAndUpdate({_id: id}, {status: defines.OBJECT_STATUS.DELETED});
};
module.exports.deleteMany = ids => {
return Task.updateMany({_id: {$in: ids}}, { status: defines.OBJECT_STATUS.DELETED });
module.exports.deleteForContest = contest => {
return Task.updateMany({contest}, {status: defines.OBJECT_STATUS.DELETED});
};
module.exports.removeMany = ids => {
return Task.updateMany({_id: {$in: ids}}, {status: defines.OBJECT_STATUS.REMOVED});
};
module.exports.removeForContest = contest => {
return Task.updateMany({contest}, {status: defines.OBJECT_STATUS.REMOVED});
};
module.exports.removeManyForContest = (contest, ids) => {
return Task.updateMany({_id: {$in: ids}, contest}, { status: defines.OBJECT_STATUS.REMOVED });
};
module.exports.activate = id => {
return Task.findOneAndUpdate({_id: id}, {status: defines.OBJECT_STATUS.ACTIVE});
};
//Totally remove from db
module.exports.remove = id => {
module.exports.activateForContest = contest => {
return Task.updateMany({contest}, {status: defines.OBJECT_STATUS.ACTIVE});
};
module.exports.setStatusForContests = (contests, status) => {
return Task.updateMany({contest: {$in: contests}}, {status});
};
//Totally erase from db
module.exports.erase = id => {
return Task.deleteOne({_id: id});
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment