<commit_before>const Ajv = require('ajv');
const { Writable } = require('stream');
const {	Forbidden } = require('@feathersjs/errors');
const { authenticate } = require('@feathersjs/authentication');
const {
	iff, isProvider, validateSchema, disallow,
} = require('feathers-hooks-common');
const { hasPermission } = require('../../../hooks');
const { flatten, paginate, convertToSortOrderObject } = require('../../../utils/array');

const createSchema = require('../schemas/datasourceRuns.create.schema');
const { datasourceRunModel } = require('../model');

class DatasourceRuns {
	constructor(options) {
		this.options = options || {};
	}

	registerEventListeners() {

	}

	setup(app) {
		this.app = app;
		this.registerEventListeners();
	}

	async find(params) {
		let { schoolId } = params;
		if (params.account) {
			({ schoolId } = await this.app.service('users').get(params.account.userId));
		}
		const query = params.query || {};
		const filter = {};
		if (schoolId) filter.schoolId = schoolId;
		if (params.datasourceId) filter.datasourceId = params.datasourceId;

		let sortObject = {};
		if (query.sort && /^-?\w{1,50}$/.test(query.sort)) {
			sortObject = convertToSortOrderObject(query.sort);
		}

		const result = await datasourceRunModel.find(
			filter,
			'datasourceId _id status dryrun duration',
		).sort(sortObject);
		return result;
	}

	/**
	 * returns detailed informaton about a specific run of a datasource, including complete logs.
	 * @param {ObjectId} id id of a datasourceRun
	 * @param {Object} params feathers params object.
	 */
	async get(id, params) {
		const datasourceRun = await datasourceRunModel.findById(id);

		if (params.account) {
			const [user, datasource] = await Promise.all([
				this.app.service('users').get(params.account.userId),
				this.app.service('datasources').get(datasourceRun.datasourceId),
			]);
			if (user.schoolId.toString() !== datasource.schoolId.toString()) {
				throw new Forbidden('You do not have valid permission to access this.');
			}
		}

		return datasourceRun;
	}

	/**<commit_msg>mongoose sort supports strings :)<commit_after>const Ajv = require('ajv');
const { Writable } = require('stream');
const {	Forbidden } = require('@feathersjs/errors');
const { authenticate } = require('@feathersjs/authentication');
const {
	iff, isProvider, validateSchema, disallow,
} = require('feathers-hooks-common');
const { hasPermission } = require('../../../hooks');
const { flatten, paginate, convertToSortOrderObject } = require('../../../utils/array');

const createSchema = require('../schemas/datasourceRuns.create.schema');
const { datasourceRunModel } = require('../model');

class DatasourceRuns {
	constructor(options) {
		this.options = options || {};
	}

	registerEventListeners() {

	}

	setup(app) {
		this.app = app;
		this.registerEventListeners();
	}

	async find(params) {
		let { schoolId } = params;
		if (params.account) {
			({ schoolId } = await this.app.service('users').get(params.account.userId));
		}
		const query = params.query || {};
		const filter = {};
		if (schoolId) filter.schoolId = schoolId;
		if (params.datasourceId) filter.datasourceId = params.datasourceId;

		const result = await datasourceRunModel.find(
			filter,
			'datasourceId _id status dryrun duration',
		).sort(query.sort);
		return result;
	}

	/**
	 * returns detailed informaton about a specific run of a datasource, including complete logs.
	 * @param {ObjectId} id id of a datasourceRun
	 * @param {Object} params feathers params object.
	 */
	async get(id, params) {
		const datasourceRun = await datasourceRunModel.findById(id);

		if (params.account) {
			const [user, datasource] = await Promise.all([
				this.app.service('users').get(params.account.userId),
				this.app.service('datasources').get(datasourceRun.datasourceId),
			]);
			if (user.schoolId.toString() !== datasource.schoolId.toString()) {
				throw new Forbidden('You do not have valid permission to access this.');
			}
		}

		return datasourceRun;
	}

	/**
{'fraction_non_alphanumeric': 0.1550561797752809, 'fraction_numerical': 0.000749063670411985, 'mean_word_length': 4.12932138284251, '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': '1883445', 'n_tokens_mistral': 1375, 'n_tokens_neox': 1242, 'n_words': 283}