(send_data) Improve script output
This commit is contained in:
31
send_data.py
31
send_data.py
@@ -10,6 +10,17 @@ import json
|
|||||||
import time
|
import time
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
class bcolors:
|
||||||
|
HEADER = '\033[95m'
|
||||||
|
OKBLUE = '\033[94m'
|
||||||
|
OKCYAN = '\033[96m'
|
||||||
|
OKGREEN = '\033[92m'
|
||||||
|
WARNING = '\033[93m'
|
||||||
|
FAIL = '\033[91m'
|
||||||
|
ENDC = '\033[0m'
|
||||||
|
BOLD = '\033[1m'
|
||||||
|
UNDERLINE = '\033[4m'
|
||||||
|
|
||||||
# Default file names
|
# Default file names
|
||||||
DEFAULT_SONG_FILE = 'es-songs.json'
|
DEFAULT_SONG_FILE = 'es-songs.json'
|
||||||
DEFAULT_ALBUM_FILE = 'es-albums.json'
|
DEFAULT_ALBUM_FILE = 'es-albums.json'
|
||||||
@@ -231,7 +242,7 @@ def send_data(file, quiet=False):
|
|||||||
print(res.text)
|
print(res.text)
|
||||||
else:
|
else:
|
||||||
if not quiet:
|
if not quiet:
|
||||||
print("File '{}' sent to Elasticsearch!".format(file.name))
|
print(bcolors.OKGREEN + "File '{}' sent to Elasticsearch!".format(file.name) + bcolors.ENDC)
|
||||||
|
|
||||||
def delete_index(index_name, quiet=False):
|
def delete_index(index_name, quiet=False):
|
||||||
"""
|
"""
|
||||||
@@ -242,9 +253,9 @@ def delete_index(index_name, quiet=False):
|
|||||||
res = requests.delete(url=ELASTICSEARCH_URL + index_name)
|
res = requests.delete(url=ELASTICSEARCH_URL + index_name)
|
||||||
if res.status_code == 200:
|
if res.status_code == 200:
|
||||||
if not quiet:
|
if not quiet:
|
||||||
print("Deleted!")
|
print(bcolors.OKGREEN + "Index deleted!" + bcolors.ENDC)
|
||||||
else:
|
else:
|
||||||
print("An error occured")
|
print(bcolors.FAIL + "An error occured" + bcolors.ENDC)
|
||||||
if res.json()['error']['type'] == 'index_not_found_exception':
|
if res.json()['error']['type'] == 'index_not_found_exception':
|
||||||
print("Index '{}' doesn't exist and can't be deleted".format(index_name))
|
print("Index '{}' doesn't exist and can't be deleted".format(index_name))
|
||||||
else:
|
else:
|
||||||
@@ -261,11 +272,11 @@ def put_mapping(index_name, mapping_file, quiet=False):
|
|||||||
data=mapping_file,
|
data=mapping_file,
|
||||||
headers={'Content-Type': 'application/json'})
|
headers={'Content-Type': 'application/json'})
|
||||||
if res.status_code != 200:
|
if res.status_code != 200:
|
||||||
print("An error occured")
|
print(bcolors.FAIL + "An error occured")
|
||||||
print(res.text)
|
print(res.text + bcolors.ENDC)
|
||||||
else:
|
else:
|
||||||
if not quiet:
|
if not quiet:
|
||||||
print("File '{}' sent to Elasticsearch!".format(mapping_file.name))
|
print(bcolors.OKGREEN + "Mapping sent" + bcolors.ENDC)
|
||||||
|
|
||||||
put_setting(index_name, 0, quiet)
|
put_setting(index_name, 0, quiet)
|
||||||
|
|
||||||
@@ -292,8 +303,8 @@ def check_all_data_is_saved(data_file, index_name, quiet=False):
|
|||||||
data=json.dumps(payload),
|
data=json.dumps(payload),
|
||||||
headers={'Content-Type': 'application/x-ndjson'})
|
headers={'Content-Type': 'application/x-ndjson'})
|
||||||
if res.status_code != 200:
|
if res.status_code != 200:
|
||||||
print("An error occured")
|
print(bcolors.FAIL + "An error occured")
|
||||||
print(res.text)
|
print(res.text + bcolors.ENDC)
|
||||||
|
|
||||||
els_nb_doc = res.json()['hits']['total']['value']
|
els_nb_doc = res.json()['hits']['total']['value']
|
||||||
|
|
||||||
@@ -301,9 +312,9 @@ def check_all_data_is_saved(data_file, index_name, quiet=False):
|
|||||||
print("\tFound: {} documents in index '{}' in ELS".format(els_nb_doc, index_name))
|
print("\tFound: {} documents in index '{}' in ELS".format(els_nb_doc, index_name))
|
||||||
|
|
||||||
if file_nb_line != els_nb_doc:
|
if file_nb_line != els_nb_doc:
|
||||||
print('Look out! Not all the data has been found in ELS')
|
print(bcolors.WARNING + 'Look out! Not all the data has been found in ELS' + bcolors.ENDC)
|
||||||
elif not quiet:
|
elif not quiet:
|
||||||
print('All data is in ELS, it\'s ok')
|
print(bcolors.OKGREEN + 'All data is in ELS, it\'s ok' + bcolors.ENDC)
|
||||||
|
|
||||||
return file_nb_line == els_nb_doc
|
return file_nb_line == els_nb_doc
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user