<commit_before>"""This file contains route definitions and handles requests for these routes."""
import json
from flask import render_template, request, jsonify
from . import app
from time import sleep

from .src.HadoopPersonsParser import HadoopPersonsParser
from .src.HadoopFoundationsParser import HadoopFoundationsParser
from .src.HadoopSeriesParser import HadoopSeriesParser
from .src.HDFSFileReader import HDFSFileReader

__author__ = "<NAME>"

DAYS = [x+1 for x in range(31)]
MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
LANGUAGES = ['English', 'German']
TITLE = 'Hadoop!!!'
AUTHORS = [
	'<NAME>',
	'<NAME>',
	'<NAME>',
	'<NAME>',
	'<NAME>'
]
CATEGORIES = [
	'Persons',
	'Foundations',
	'Series'
]

@app.route('/')
@app.route('/index')
def index():
	"""This function handles requests for the landing page. It renders a Jinja2 template and returns the resulting HTML code."""

	return render_template(
		'content.j2',
		title=TITLE,
		authors=AUTHORS,
		categories=CATEGORIES
	)



@app.route('/persons/<year>/<month>/<day>/<language>', methods=["GET"])
def what_happened_persons(year, month, day, language):
	"""This function handles GET requests for JSON data from DBPedia. The URL contains data, which will be used to query the server."""

	print('Received request!')
	print('year:', year)
	print('month:', month)
	print('day:', day)
	print('language:', language)

	result_data = get_data_from_hadoop('persons', year, month, day, language)
	print('got result data')

	return jsonify(result_data)

@app.route('/foundations/<year>/<language>', methods=["GET"])
def what_happened_foundations(year, language):
	print('Foundations called.')

	result_data = get_data_from_hadoop('foundations', year, None, None, language)
	return result_data

@app.route('/series/<year>/<language>', methods=["GET"])
def what_happened_series(year, language):
	print('Series called.')
	return 'not yet implemented'


def get_data_from_hadoop(category, year, month, day, language):
	"""This function takes care of building a query from its parameters."""

	print('getting data from Hadoop ...')
	sleep(1.5)  # TODO: Remove this in production code!

	if category == 'persons':
		print('Getting persons.')
		if language == 'english':
			lines = HDFSFileReader.read('Persons_en')
		elif language == 'german':
			lines = HDFSFileReader.read('Persons_de')
		else:
			print('Language unknown.')

		parser = HadoopPersonsParser()
		return parser.parse(lines)


	elif category == 'foundations':
		print('Getting Foundations')
		if language == 'english':
			lines = HDFSFileReader.read('Foundations_en')
		elif language == 'german':
			lines = HDFSFileReader.read('Foundations_de')
		else:
			print('Language unknown.')

		parser = HadoopFoundationsParser()
		result = parser.parse(lines)  # returns a dictionary

		return jsonify(result)

	elif category == 'series':
		print('Getting Series')
		if language == 'english':
			lines = HDFSFileReader.read('Series_en')
		elif language == 'german':
			lines = HDFSFileReader.read('Series_de')
		else:
			print('Language unknown.')

		parser = HadoopSeriesParser()
		json_result = parser.parse(lines)

		return jsonify(json_result)<commit_msg>only returns the series of the year specified by the user<commit_after>"""This file contains route definitions and handles requests for these routes."""
import json
from flask import render_template, request, jsonify
from . import app
from time import sleep

from .src.HadoopPersonsParser import HadoopPersonsParser
from .src.HadoopFoundationsParser import HadoopFoundationsParser
from .src.HadoopSeriesParser import HadoopSeriesParser
from .src.HDFSFileReader import HDFSFileReader

__author__ = "<NAME>"

DAYS = [x+1 for x in range(31)]
MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
LANGUAGES = ['English', 'German']
TITLE = 'Hadoop!!!'
AUTHORS = [
	'<NAME>',
	'<NAME>',
	'<NAME>',
	'<NAME>',
	'<NAME>'
]
CATEGORIES = [
	'Persons',
	'Foundations',
	'Series'
]

@app.route('/')
@app.route('/index')
def index():
	"""This function handles requests for the landing page. It renders a Jinja2 template and returns the resulting HTML code."""

	return render_template(
		'content.j2',
		title=TITLE,
		authors=AUTHORS,
		categories=CATEGORIES
	)



@app.route('/persons/<year>/<month>/<day>/<language>', methods=["GET"])
def what_happened_persons(year, month, day, language):
	"""This function handles GET requests for JSON data from DBPedia. The URL contains data, which will be used to query the server."""

	print('Received request!')
	print('year:', year)
	print('month:', month)
	print('day:', day)
	print('language:', language)

	result_data = get_data_from_hadoop('persons', year, month, day, language)
	print('got result data')

	return jsonify(result_data)

@app.route('/foundations/<year>/<language>', methods=["GET"])
def what_happened_foundations(year, language):
	print('Foundations called.')

	result_data = get_data_from_hadoop('foundations', year, None, None, language)
	return result_data

@app.route('/series/<year>/<language>', methods=["GET"])
def what_happened_series(year, language):
	print('Series called.')
	return 'not yet implemented'


def get_data_from_hadoop(category, year, month, day, language):
	"""This function takes care of building a query from its parameters."""

	print('getting data from Hadoop ...')
	sleep(1.5)  # TODO: Remove this in production code!

	if category == 'persons':
		print('Getting persons.')
		if language == 'english':
			lines = HDFSFileReader.read('Persons_en')
		elif language == 'german':
			lines = HDFSFileReader.read('Persons_de')
		else:
			print('Language unknown.')

		parser = HadoopPersonsParser()
		return parser.parse(lines)


	elif category == 'foundations':
		print('Getting Foundations')
		if language == 'english':
			lines = HDFSFileReader.read('Foundations_en')
		elif language == 'german':
			lines = HDFSFileReader.read('Foundations_de')
		else:
			print('Language unknown.')

		parser = HadoopFoundationsParser()
		result = parser.parse(lines)  # returns a dictionary

		return jsonify(result)

	elif category == 'series':
		print('Getting Series')
		if language == 'english':
			lines = HDFSFileReader.read('Series_en')
		elif language == 'german':
			lines = HDFSFileReader.read('Series_de')
		else:
			print('Language unknown.')

		parser = HadoopSeriesParser()
		json_result = parser.parse(lines)
		json_result = [elem for elem in json_result if elem['foundation_date_year'] == year]  # only return the ones of the year specified by the user

		return jsonify(json_result)
{'fraction_non_alphanumeric': 0.1445637380916377, 'fraction_numerical': 0.0021170421896264933, 'mean_word_length': 5.281101614434948, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, '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': '2070542', 'n_tokens_mistral': 2270, 'n_tokens_neox': 2085, 'n_words': 597}