<commit_before>async = require 'async'
zlib = require 'zlib'
request = require "request"
rclient = require("redis").createClient() # Only works locally for now
{db, ObjectId} = require "../../../../app/js/mongojs"
Settings = require "settings-sharelatex"

module.exports = TrackChangesClient =
	flushAndGetCompressedUpdates: (project_id, doc_id, callback = (error, updates) ->) ->
		TrackChangesClient.flushDoc project_id, doc_id, (error) ->
			return callback(error) if error?
			TrackChangesClient.getCompressedUpdates doc_id, callback

	flushDoc: (project_id, doc_id, callback = (error) ->) ->
		request.post {
			url: "http://localhost:3015/project/#{project_id}/doc/#{doc_id}/flush"
		}, (error, response, body) =>
			response.statusCode.should.equal 204
			callback(error)

	flushProject: (project_id, callback = (error) ->) ->
		request.post {
			url: "http://localhost:3015/project/#{project_id}/flush"
		}, (error, response, body) =>
			response.statusCode.should.equal 204
			callback(error)

	getCompressedUpdates: (doc_id, callback = (error, updates) ->) ->
		db.docHistory
			.find(doc_id: ObjectId(doc_id))
			.sort("meta.end_ts": 1)
			.toArray callback

	getProjectMetaData: (project_id, callback = (error, updates) ->) ->
		db.projectHistoryMetaData
			.find {
				project_id: ObjectId(project_id)
			},
			(error, projects) ->
				callback error, projects[0]

	setPreserveHistoryForProject: (project_id, callback = (error) ->) ->
		db.projectHistoryMetaData.update {
			project_id: ObjectId(project_id)
			}, {
				$set: { preserveHistory: true }
			}, {
				upsert: true
			}, callback

	pushRawUpdates: (project_id, doc_id, updates, callback = (error) ->) ->
		rclient.sadd "DocsWithHistoryOps:#{project_id}", doc_id, (error) ->
			return callback(error) if error?
			rclient.rpush "UncompressedHistoryOps:#{doc_id}", (JSON.stringify(u) for u in updates)..., callback

	getDiff: (project_id, doc_id, from, to, callback = (error, diff) ->) ->
		request.get {
			url: "http://localhost:3015/project/#{project_id}/doc/#{doc_id}/diff?from=#{from}&to=#{to}"
		}, (error, response, body) =>
			response.statusCode.should.equal 200
			callback null, JSON.parse(body)

	getUpdates: (project_id, options, callback = (error, body) ->) ->
		request.get {
			url: "http://localhost:3015/project/#{project_id}/updates?before=#{options.before}&min_count=#{options.min_count}"
		}, (error, response, body) =>
			response.statusCode.should.equal 200
			callback null, JSON.parse(body)

	restoreDoc: (project_id, doc_id, version, user_id, callback = (error) ->) ->
		request.post {
			url: "http://localhost:3015/project/#{project_id}/doc/#{doc_id}/version/#{version}/restore"
			headers:
				"X-User-Id": user_id
		}, (error, response, body) =>
			response.statusCode.should.equal 204<commit_msg>fix acceptance tests to work with redis cluster<commit_after>async = require 'async'
zlib = require 'zlib'
request = require "request"
Settings = require "settings-sharelatex"
rclient = require("redis-sharelatex").createClient(Settings.redis.history) # Only works locally for now
Keys = Settings.redis.history.key_schema
{db, ObjectId} = require "../../../../app/js/mongojs"

module.exports = TrackChangesClient =
	flushAndGetCompressedUpdates: (project_id, doc_id, callback = (error, updates) ->) ->
		TrackChangesClient.flushDoc project_id, doc_id, (error) ->
			return callback(error) if error?
			TrackChangesClient.getCompressedUpdates doc_id, callback

	flushDoc: (project_id, doc_id, callback = (error) ->) ->
		request.post {
			url: "http://localhost:3015/project/#{project_id}/doc/#{doc_id}/flush"
		}, (error, response, body) =>
			response.statusCode.should.equal 204
			callback(error)

	flushProject: (project_id, callback = (error) ->) ->
		request.post {
			url: "http://localhost:3015/project/#{project_id}/flush"
		}, (error, response, body) =>
			response.statusCode.should.equal 204
			callback(error)

	getCompressedUpdates: (doc_id, callback = (error, updates) ->) ->
		db.docHistory
			.find(doc_id: ObjectId(doc_id))
			.sort("meta.end_ts": 1)
			.toArray callback

	getProjectMetaData: (project_id, callback = (error, updates) ->) ->
		db.projectHistoryMetaData
			.find {
				project_id: ObjectId(project_id)
			},
			(error, projects) ->
				callback error, projects[0]

	setPreserveHistoryForProject: (project_id, callback = (error) ->) ->
		db.projectHistoryMetaData.update {
			project_id: ObjectId(project_id)
			}, {
				$set: { preserveHistory: true }
			}, {
				upsert: true
			}, callback

	pushRawUpdates: (project_id, doc_id, updates, callback = (error) ->) ->
		rclient.sadd Keys.docsWithHistoryOps({project_id}), doc_id, (error) ->
			return callback(error) if error?
			rclient.rpush Keys.uncompressedHistoryOps({doc_id}), (JSON.stringify(u) for u in updates)..., callback

	getDiff: (project_id, doc_id, from, to, callback = (error, diff) ->) ->
		request.get {
			url: "http://localhost:3015/project/#{project_id}/doc/#{doc_id}/diff?from=#{from}&to=#{to}"
		}, (error, response, body) =>
			response.statusCode.should.equal 200
			callback null, JSON.parse(body)

	getUpdates: (project_id, options, callback = (error, body) ->) ->
		request.get {
			url: "http://localhost:3015/project/#{project_id}/updates?before=#{options.before}&min_count=#{options.min_count}"
		}, (error, response, body) =>
			response.statusCode.should.equal 200
			callback null, JSON.parse(body)

	restoreDoc: (project_id, doc_id, version, user_id, callback = (error) ->) ->
		request.post {
			url: "http://localhost:3015/project/#{project_id}/doc/#{doc_id}/version/#{version}/restore"
			headers:
				"X-User-Id": user_id
		}, (error, response, body) =>
			response.statusCode.should.equal 204
{'fraction_non_alphanumeric': 0.16188309415470772, 'fraction_numerical': 0.01295064753237662, 'mean_word_length': 4.366197183098592, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 59, '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': '1214429', 'n_tokens_mistral': 2143, 'n_tokens_neox': 1903, 'n_words': 376}