<commit_before>/**
 * TestsController
 *
 * @description :: Server-side logic for managing tests
 * @help        :: See http://sailsjs.org/#!/documentation/concepts/Controllers
 */

module.exports = {
	/**
	 * Create new test in given environment
	 *
	 * @param req
	 * @param res
	 */
	create: function (req, res) {
		var parameters = {
			usersId: req.token.id,
			environmentsId: req.environmentId,
			name: req.param ('name'),
			description: req.param ('description')
		};

		// create new test
		tests.create (parameters).then (function (test) {
			return res.created (test);
		}, function (error) {
			return res.serverError (error);
		});
	}
	,

	update: function (req, res) {
		tests.find ({where: {id: req.testId}}).then (function (test) {

			var nextRun = req.param ('nextRun', false);

			test.update ({
				name: req.param ('name'),
				description: req.param ('description'),
				runInterval: req.param ('runInterval'),
				nextRun: nextRun ? new Date (nextRun) : null
			}).then (function (edited) {
				return res.ok (edited);
			});

		}, function (error) {
			return res.serverError (error);
		})
	},

	/**
	 * List of all tests in given environment
	 *
	 * Environment is passed as route parameter :environmentId
	 *
	 * @param req
	 * @param res
	 */
	index: function (req, res) {
		tests.findAll ({where: {environmentsId: req.environmentId}}).then (function (data) {
			return res.ok (data);
		});
	},

	/**
	 * Get details about given test. Test is specified as route parameter
	 *
	 * Test may be loaded with requests, results and/or headers.
	 *
	 * @param req
	 * @param res
	 */
	detail: function (req, res) {
		var findCriterium = {where: {id: req.testId}, include: []};

		// should requests be also part of response?
		if (req.param ('withRequests', false)) {
			findCriterium.include.push ({
				model: requests,
				as: 'requests'
			});
		}

		// append also test results?
		if (req.param ('withResults', false)) {
			findCriterium.include.push ({model: runnedTests})
		}

		tests.find (findCriterium).then (function (test) {
			// because headers may be assigned to project, environment or test itself, we need to select all these three possibilities
			if (req.param ('withHeaders', false)) {
				headers.findAll ({
					where: {
						$or: [
							{testsId: req.testId},
							{projectsId: req.projectId},
							{environmentsId: req.environmentId}
						]
					}
				}).then (function (allHeaders) {
<commit_msg>FEATURE: limit number of results in test detail<commit_after>/**
 * TestsController
 *
 * @description :: Server-side logic for managing tests
 * @help        :: See http://sailsjs.org/#!/documentation/concepts/Controllers
 */

module.exports = {
	/**
	 * Create new test in given environment
	 *
	 * @param req
	 * @param res
	 */
	create: function (req, res) {
		var parameters = {
			usersId: req.token.id,
			environmentsId: req.environmentId,
			name: req.param ('name'),
			description: req.param ('description')
		};

		// create new test
		tests.create (parameters).then (function (test) {
			return res.created (test);
		}, function (error) {
			return res.serverError (error);
		});
	}
	,

	update: function (req, res) {
		tests.find ({where: {id: req.testId}}).then (function (test) {

			var nextRun = req.param ('nextRun', false);

			test.update ({
				name: req.param ('name'),
				description: req.param ('description'),
				runInterval: req.param ('runInterval'),
				nextRun: nextRun ? new Date (nextRun) : null
			}).then (function (edited) {
				return res.ok (edited);
			});

		}, function (error) {
			return res.serverError (error);
		})
	},

	/**
	 * List of all tests in given environment
	 *
	 * Environment is passed as route parameter :environmentId
	 *
	 * @param req
	 * @param res
	 */
	index: function (req, res) {
		tests.findAll ({where: {environmentsId: req.environmentId}}).then (function (data) {
			return res.ok (data);
		});
	},

	/**
	 * Get details about given test. Test is specified as route parameter
	 *
	 * Test may be loaded with requests, results and/or headers.
	 *
	 * @param req
	 * @param res
	 */
	detail: function (req, res) {
		var findCriterium = {where: {id: req.testId}, include: []};

		// should requests be also part of response?
		if (req.param ('withRequests', false)) {
			findCriterium.include.push ({
				model: requests,
				as: 'requests'
			});
		}

		// append also test results?
		if (req.param ('withResults', false)) {
			findCriterium.include.push ({
				model: runnedTests,
				// include results only from past 30 days
				where: {updatedAt: {$lt: new Date (Date.now () - 30 * 24 * 3600 * 1000)}}
			})
		}

		tests.find (findCriterium).then (function (test) {
			// because headers may be assigned to project, environment or test itself, we need to select all these three possibilities
			if (req.param ('withHeaders', false)) {
				headers.findAll ({
					where: {
						$or: [
							{testsId: req.testId},
							{projectsId: req.projectId},
							{environmentsId: req.environmentId}
						]
					}
				}).then (function (allHeaders) {

{'fraction_non_alphanumeric': 0.14575936883629192, 'fraction_numerical': 0.0027613412228796844, 'mean_word_length': 2.9803767660910516, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2876792', 'n_tokens_mistral': 1876, 'n_tokens_neox': 1556, 'n_words': 476}