<commit_before>db = require("./DB")
mongoCache = require("./MongoCache")
logger = require 'logger-sharelatex'
metrics = require('metrics-sharelatex')

module.exports = LearnedWordsManager =
	learnWord: (user_token, word, callback = (error)->) ->
		mongoCache.del(user_token)
		db.spellingPreferences.update {
			token: user_token
		}, {
			$push: learnedWords: word
		}, {
			upsert: true
		}, callback

	getLearnedWords: (user_token, callback = (error, words)->) ->
		mongoCachedWords = mongoCache.get(user_token)
		if mongoCachedWords?
			metrics.inc "mongoCache-hit", 0.1
			return callback(null, mongoCachedWords)

		metrics.inc "mongoCache-miss", 0.1
		logger.info user_token:user_token, "mongoCache miss"
		
		db.spellingPreferences.findOne token: user_token, (error, preferences) ->
			return callback error if error?
			words = preferences?.learnedWords || []
			mongoCache.set(user_token, words)
			callback null, words


metrics.timeAsyncMethod(
	LearnedWordsManager, 'learnWord',
	'LearnedWordsManager.learnWord',
	logger
)
metrics.timeAsyncMethod(
	LearnedWordsManager, 'getLearnedWords',
	'LearnedWordsManager.getLearnedWords',
	logger
)<commit_msg>update to new api<commit_after>db = require("./DB")
mongoCache = require("./MongoCache")
logger = require 'logger-sharelatex'
metrics = require('metrics-sharelatex')

module.exports = LearnedWordsManager =
	learnWord: (user_token, word, callback = (error)->) ->
		mongoCache.del(user_token)
		db.spellingPreferences.update {
			token: user_token
		}, {
			$push: learnedWords: word
		}, {
			upsert: true
		}, callback

	getLearnedWords: (user_token, callback = (error, words)->) ->
		mongoCachedWords = mongoCache.get(user_token)
		if mongoCachedWords?
			metrics.inc "mongoCache-hit", 0.1
			return callback(null, mongoCachedWords)

		metrics.inc "mongoCache-miss", 0.1
		logger.info user_token:user_token, "mongoCache miss"
		
		db.spellingPreferences.findOne token: user_token, (error, preferences) ->
			return callback error if error?
			words = preferences?.learnedWords || []
			mongoCache.set(user_token, words)
			callback null, words


[
	'learnWord',
	'getLearnedWords'
].map (method) ->
	metrics.timeAsyncMethod(LearnedWordsManager, method, 'mongo.LearnedWordsManager', logger)
{'fraction_non_alphanumeric': 0.12566607460035523, 'fraction_numerical': 0.003552397868561279, 'mean_word_length': 4.913385826771654, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, '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': '3295506', 'n_tokens_mistral': 845, 'n_tokens_neox': 737, 'n_words': 151}