<commit_before>logger = require "logger-sharelatex"
Errors = require "./Errors"
fs = require "fs"
{db, ObjectId} = require "./mongojs"

module.exports = HttpController =

	something: (req, res) ->
		logger.log "Something works"
		res.send 200

	packageIndex: (req, res, next = (error) ->) ->
		HttpController.load_index (err, index_data) ->
			return next(err) if err?
			res.setHeader "Content-Type", "application/json"
			res.status(200).send(index_data)

	search: (req, res, next = (error) ->) ->
		search_params = req.body
		query = {
			name: {$regex: new RegExp("^#{search_params.query}")}
			"language": search_params.language or 'python'
		}
		logger.log params: search_params, "searching package index"
		db.packageIndex.find(query).limit 100, (err, docs=[]) ->
			if err
				return next err
			logger.log params: search_params, count: docs.length, "found packages in index, sending to client"<commit_msg>Explain what we're querying.<commit_after>logger = require "logger-sharelatex"
Errors = require "./Errors"
fs = require "fs"
{db, ObjectId} = require "./mongojs"

module.exports = HttpController =

	something: (req, res) ->
		logger.log "Something works"
		res.send 200

	packageIndex: (req, res, next = (error) ->) ->
		HttpController.load_index (err, index_data) ->
			return next(err) if err?
			res.setHeader "Content-Type", "application/json"
			res.status(200).send(index_data)

	search: (req, res, next = (error) ->) ->
		search_params = req.body
		# simple regex match on the name field for now.
		query =
			name: {$regex: new RegExp("^#{search_params.query}")}
			language: search_params.language or 'python'
		logger.log params: search_params, "searching package index"
		db.packageIndex.find(query).limit 100, (err, docs=[]) ->
			if err
				return next err
			logger.log params: search_params, count: docs.length, "found packages in index, sending to client"
{'fraction_non_alphanumeric': 0.1442970822281167, 'fraction_numerical': 0.009549071618037135, 'mean_word_length': 4.097297297297297, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 17, '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': '4800988', 'n_tokens_mistral': 684, 'n_tokens_neox': 610, 'n_words': 175}