Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add case indexation scripts #21

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions scripts/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
ACTIONS_SERVER_HOSTNAME = "172.17.0.1:5022" if DEV else "actions-server"
FILTER_SERVER_HOSTNAME = "172.17.0.1:5027" if DEV else "filter-server"
EXPLORE_SERVER_HOSTNAME = "172.17.0.1:5029" if DEV else "explore-server"
CASE_SERVER_HOSTNAME = "172.17.0.1:5000" if DEV else "case-server"

LOADFLOW_SERVER_HOSTNAME = "172.17.0.1:5008" if DEV else "loadflow-server"
DYNAMIC_SIMULATION_SERVER_HOSTNAME = "172.17.0.1:5032" if DEV else "dynamic-simulation-server"
Expand All @@ -62,6 +63,7 @@
ACTIONS_SERVER_URL = HTTP_PROTOCOL + ACTIONS_SERVER_HOSTNAME + API_VERSION
FILTER_SERVER_URL = HTTP_PROTOCOL + FILTER_SERVER_HOSTNAME + API_VERSION
EXPLORE_SERVER_URL = HTTP_PROTOCOL + EXPLORE_SERVER_HOSTNAME + API_VERSION
CASE_SERVER_URL = HTTP_PROTOCOL + CASE_SERVER_HOSTNAME + API_VERSION

LOADFLOW_SERVER_URL = HTTP_PROTOCOL + LOADFLOW_SERVER_HOSTNAME + API_VERSION
DYNAMIC_SIMULATION_SERVER_URL = HTTP_PROTOCOL + DYNAMIC_SIMULATION_SERVER_HOSTNAME + API_VERSION
Expand Down Expand Up @@ -122,3 +124,10 @@
GET_DIRECTORY_ELEMENTS_INDEX_NAME = DIRECTORY_SERVER_URL + "/supervision/elements/index-name"
DELETE_INDEXED_ELEMENTS = DIRECTORY_SERVER_URL + "/supervision/elements/indexation"
REINDEX_ELEMENTS = DIRECTORY_SERVER_URL + "/supervision/elements/reindex"


# INDEXED ELEMENTS
GET_CASES_ELEMENTS_COUNT = CASE_SERVER_URL + "/supervision/cases/indexation-count"
GET_CASES_ELEMENTS_INDEX_NAME = CASE_SERVER_URL + "/supervision/cases/index-name"
DELETE_INDEXED_CASES_ELEMENTS = CASE_SERVER_URL + "/supervision/cases/indexation"
REINDEX_CASES_ELEMENTS = CASE_SERVER_URL + "/supervision/cases/reindex"
33 changes: 33 additions & 0 deletions scripts/functions/indexes/cases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#
# Copyright (c) 2024, RTE (http://www.rte-france.com)
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

import requests
import constant
#
# @author Jamal KHEYYAD <jamal.kheyyad at rte-international.com>
#

def get_nb_indexed_elements():
return requests.get(constant.GET_CASES_ELEMENTS_COUNT).text

def get_elements_index_name():
try:
return requests.get(constant.GET_CASES_ELEMENTS_INDEX_NAME).text
except requests.exceptions.RequestException as e:
print(e)
return ""

def delete_indexed_elements():
result = requests.delete(url = constant.DELETE_INDEXED_CASES_ELEMENTS)
if not result.ok :
print("An error occured : " + str(result.json()))

def reindex_elements():
result = requests.post(url = constant.REINDEX_CASES_ELEMENTS)
if not result.ok :
print("An error occured : " + str(result.json()))

79 changes: 79 additions & 0 deletions scripts/reset_indexed_cases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#
# Copyright (c) 2024, RTE (http://www.rte-france.com)
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

import argparse
import constant
import time
import sys
from tqdm import tqdm

from functions.indexes.cases import delete_indexed_elements
from functions.indexes.cases import reindex_elements
from functions.indexes.cases import get_nb_indexed_elements
from functions.indexes.cases import get_elements_index_name
from functions.indexes.elasticsearch import get_elasticsearch_host
from functions.indexes.elasticsearch import check_status_elasticsearch
from functions.indexes.elasticsearch import expunge_deletes
from functions.plateform.plateform import check_server_status
from functions.plateform.plateform import get_plateform_info

#
# @author Jamal KHEYYAD <jamal.kheyyad at rte-international.com>
#

parser = argparse.ArgumentParser(description='Send requests to the gridsuite services to reset indexed cases', )

parser.add_argument("-n", "--dry-run", help="test mode (default) will not execute any deletion or saving request",
action="store_true")


args = parser.parse_args()
dry_run = args.dry_run

print("---------------------------------------------------------")
print("indexed cases reset script")
if dry_run:
print("dry-run=" + str(dry_run) + " -> will run without deleting or saving anything (test mode)")
if constant.DEV:
print("DEV=" + str(constant.DEV) + " -> hostnames configured for a local execution (172.17.0.1:xxxx)")
print("\n")

# Check case-server
if not check_server_status(constant.CASE_SERVER_HOSTNAME): sys.exit()
print("\n")
# Just getting an enlightening url opportunistically from here because it exists
# TODO better ?
plateformName = get_plateform_info()['redirect_uri']
elasticsearch_ip, elasticsearch_url = get_elasticsearch_host(constant.CASE_SERVER_HOSTNAME)
elements_index_name = get_elements_index_name()

if not check_status_elasticsearch(elasticsearch_url) : sys.exit()
print("\n")

print("---------------------------------------------------------")
print("This script will apply on plateform = " + plateformName )
# TODO It would be nice to show the environment prefix used for elasticsearch indexes names
print("This plateform will execute queries on elasticsearch = " + elasticsearch_url + " (" + elasticsearch_ip + ")")
print("\n")
print("===> Both elasticsearch and case-server seem OK ! The script can proceed")
print("\n")
print("Number of indexed cases elements (name: " + elements_index_name + ") = " + get_nb_indexed_elements())
print("And will execute on elasticsearch force merge expunge deletes to reclaim space.")
print("---------------------------------------------------------")
if not dry_run:
print("CASES indexed elements deletion processing...")
delete_indexed_elements()
print("Waiting 30 secondes before force merge expunge_deletes...")
time.sleep(30)
expunge_deletes(elasticsearch_url, elements_index_name)
print("End of deletion")
print("CASES indexed elements reindexation processing...")
reindex_elements()
print("End of reindexation")
else:
print("Nothing has been impacted (dry-run)")

Loading