<commit_before>###
db is a database module that handles storage and retrieval of events and groups.
It uses ElasticSearch as a database backend and the elasticsearch JavaScript
client.

@author <NAME>
###
elasticsearch = require 'elasticsearch'
client = new elasticsearch.Client
	host: 'localhost:9200'
	log: 'trace'
	apiVersion: '1.5'


# Add ID mapping to index
# @param [string] name of index
# @private
_putIdMapping = (name) ->
	client.indices.putMapping


# Create Elastic Search index with given name.
# @param [string] name of the index
# @private
_createIndex = (name) ->
	params =
		'index': name
		'mappings': {}


	if name is 'events'
		params.mappings.event = '_id': 'path': 'id'
	else
		params.mappings.group = '_id': 'path': 'id'

	client.indices.exists
		'index': name
	.then(
		(exists) ->
			if not exists
				client.indices.create
					'index': name
					'_id':
						'path': 'id'
				.then(
					(body) ->
						console.log "Created #{name} index successfully"
					(error) ->
						console.error 'Failed to create event index')
			else

				console.log "Index already exists"
	
		(error) ->
			console.error error)



# Make sure elastic search is running
client.ping
	requestTimeout: Infinity,
	hello: 'elasticsearch!'
.then(
	(body) ->
		_createIndex 'events'
		_createIndex 'groups'
	(error) ->
		console.error 'Elasticsearch cluster is down'
		throw new Error 'Elastic search cluster is down')


# Add events to ElasticSearch event index
# @param [object] query
# @option query [array] events
# @option query [function] onSuccess
# @option query [function] onError
set = (query) ->
	console.log JSON.stringify query
	bulkEvents = []
	for event in query.events
		bulkEvents.push
			'index':
				'_index': 'events'
				'_type': 'event'
				'_id': event.id
		bulkEvents.push event

	client.bulk
		'body': bulkEvents
	.then(
		(result) ->
			query.onSuccess result
		(error) ->
			query.onError
				error: error
				module 'db')


# Add events to ElasticSearch event index
# @param [object] query
# @option query [object] group
# @option query [function] onSuccess
# @option query [function] onError
setGroup = (query) ->
	client.index
		'index': 'groups',
		'type': 'group'
		'id': query.group.id
		'body': query.group
	.then(
		(result) ->
			query.onSuccess result
		(error) ->
			query.onError
				error: error
				module 'db')


# Get all upcoming events including recently started ones.
# Returns events based on server time minus three hours for ongoing events.
# @param [object] query
# @option query [function] onSuccess
# @option query [function] onError
get = (query) ->
	client.search
		index: 'events'
		body:
			query: filtered: filter: range: date: gt: 'now-3h'
			sort: date: order: "asc"
	.then(
		(events) ->
			query.onSuccess events.hits.hits
		(error) ->
			query.onError
				error: error
				module 'db')


# Export set and setGroup functions
exports = module.exports =
	'set': set
	'setGroup': setGroup
	'get': get<commit_msg>Add getGroups function to db and fixes<commit_after>###
db is a database module that handles storage and retrieval of events and groups.
It uses ElasticSearch as a database backend and the elasticsearch JavaScript
client.

@author <NAME>
###
elasticsearch = require 'elasticsearch'
client = new elasticsearch.Client
	host: 'localhost:9200'
	log: 'trace'
	apiVersion: '1.5'


# Add ID mapping to index
# @param [string] name of index
# @private
_putIdMapping = (name) ->
	client.indices.putMapping


# Create Elastic Search index with given name.
# @param [string] name of the index
# @private
_createIndex = (name) ->
	params =
		'index': name
		'mappings': {}


	if name is 'events'
		params.mappings.event = '_id': 'path': 'id'
	else
		params.mappings.group = '_id': 'path': 'id'

	client.indices.exists
		'index': name
	.then(
		(exists) ->
			if not exists
				client.indices.create
					'index': name
					'_id':
						'path': 'id'
				.then(
					(body) ->
						console.log "Created #{name} index successfully"
					(error) ->
						console.error 'Failed to create event index')
			else

				console.log "Index already exists"
	
		(error) ->
			console.error error)



# Make sure elastic search is running
client.ping
	requestTimeout: Infinity,
	hello: 'elasticsearch!'
.then(
	(body) ->
		_createIndex 'events'
		_createIndex 'groups'
	(error) ->
		console.error 'Elasticsearch cluster is down'
		throw new Error 'Elastic search cluster is down')


# Add events to ElasticSearch event index
# @param [object] query
# @option query [array] events
# @option query [function] onSuccess
# @option query [function] onError
set = (query) ->
	console.log JSON.stringify query
	bulkEvents = []
	for event in query.events
		bulkEvents.push
			'index':
				'_index': 'events'
				'_type': 'event'
				'_id': event.id
		bulkEvents.push event

	client.bulk
		'body': bulkEvents
	.then(
		(result) ->
			query.onSuccess result
		(error) ->
			query.onError
				error: error
				module 'db')


# Add events to ElasticSearch event index
# @param [object] query
# @option query [object] group
# @option query [function] onSuccess
# @option query [function] onError
setGroup = (query) ->
	client.index
		'index': 'groups',
		'type': 'group'
		'id': query.group.id
		'body': query.group
	.then(
		(result) ->
			query.onSuccess result
		(error) ->
			query.onError
				error: error
				module 'db')


# Get all upcoming events including recently started ones.
# Returns events based on server time minus three hours for ongoing events.
# @param [object] query
# @option query [function] onSuccess
# @option query [function] onError
get = (query) ->
	client.search
		index: 'events'
		body:
			query: filtered: filter: range: date: gt: 'now-3h'
			sort: date: order: "asc"
	.then(
		(esEvents) ->
			events = []
			for event in esEvents.hits.hits
				events.push event._source
			query.onSuccess events
		(error) ->
			query.onError
				error: error
				module 'db')


# Get all groups from group index
# @param [object] query
# @option query [function] onSuccess
# @option query [function] onError
getGroups = (query) ->
	client.search
		index: 'groups'
		body: query: match_all: {}
	.then(
		(esGroups) ->
			groups = []
			for group in esGroups.hits.hits
				groups.push group._source
			query.onSuccess groups
		(error) ->
			query.onError
				error: error
				module: 'db')


# Export set and setGroup functions
exports = module.exports =
	'set': set
	'setGroup': setGroup
	'get': get
	'getGroups': getGroups
{'fraction_non_alphanumeric': 0.12281785879808435, 'fraction_numerical': 0.0021628302178279004, 'mean_word_length': 3.419112627986348, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 42, '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': '172935', 'n_tokens_mistral': 2376, 'n_tokens_neox': 2077, 'n_words': 700}