<commit_before>async = require "async"
logger = require "logger-sharelatex"
request  = require "request"
Settings = require "settings-sharelatex"
HistoryRedisManager = require "./HistoryRedisManager"
ProjectHistoryRedisManager = require "./ProjectHistoryRedisManager"
RedisManager = require "./RedisManager"

module.exports = HistoryManager =
	flushDocChangesAsync: (project_id, doc_id) ->
		if !Settings.apis?.trackchanges?
			logger.warn { doc_id }, "track changes API is not configured, so not flushing"
			return
		RedisManager.getHistoryType doc_id, (err, projectHistoryType) ->
			if err?
				logger.error {err, doc_id}, "error getting history type"
			else if projectHistoryType is "project-history"
				logger.debug {doc_id, projectHistoryType}, "skipping track-changes flush"
			else
				url = "#{Settings.apis.trackchanges.url}/project/#{project_id}/doc/#{doc_id}/flush"
				logger.log { project_id, doc_id, url, projectHistoryType }, "flushing doc in track changes api"
				request.post url, (error, res, body)->
					if error?
						logger.error { error, doc_id, project_id}, "track changes doc to track changes api"
					else if res.statusCode < 200 and res.statusCode >= 300
						logger.error { doc_id, project_id }, "track changes api returned a failure status code: #{res.statusCode}"

	# flush changes in the background<commit_msg>make flush to track-changes failsafe<commit_after>async = require "async"
logger = require "logger-sharelatex"
request  = require "request"
Settings = require "settings-sharelatex"
HistoryRedisManager = require "./HistoryRedisManager"
ProjectHistoryRedisManager = require "./ProjectHistoryRedisManager"
RedisManager = require "./RedisManager"

module.exports = HistoryManager =
	flushDocChangesAsync: (project_id, doc_id) ->
		if !Settings.apis?.trackchanges?
			logger.warn { doc_id }, "track changes API is not configured, so not flushing"
			return
		RedisManager.getHistoryType doc_id, (err, projectHistoryType) ->
			if err?
				logger.warn {err, doc_id}, "error getting history type"
				# if there's an error continue and flush to track-changes for safety
			if projectHistoryType is "project-history"
				logger.debug {doc_id, projectHistoryType}, "skipping track-changes flush"
			else
				url = "#{Settings.apis.trackchanges.url}/project/#{project_id}/doc/#{doc_id}/flush"
				logger.log { project_id, doc_id, url, projectHistoryType }, "flushing doc in track changes api"
				request.post url, (error, res, body)->
					if error?
						logger.error { error, doc_id, project_id}, "track changes doc to track changes api"
					else if res.statusCode < 200 and res.statusCode >= 300
						logger.error { doc_id, project_id }, "track changes api returned a failure status code: #{res.statusCode}"

	# flush changes in the background
{'fraction_non_alphanumeric': 0.10401721664275466, 'fraction_numerical': 0.00430416068866571, 'mean_word_length': 4.511857707509882, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '6696681', 'n_tokens_mistral': 907, 'n_tokens_neox': 797, 'n_words': 253}