Skip to content
Open
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
67 changes: 67 additions & 0 deletions scripts/complete_cases_metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#
# Copyright (c) 2025, 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 argparse
import constant
import sys
from tqdm import tqdm

from functions.plateform.plateform import check_server_status
from functions.plateform.plateform import get_plateform_info
from scripts.functions.cases.cases import get_cases_with_empty_metadata
from scripts.functions.cases.cases import complete_cases_metadata

parser = argparse.ArgumentParser(description='Send requests to the gridsuite services to update cases metadata by filling missing information')
parser.add_argument("-n", "--dry-run", help="test mode (default) will not execute any update request", action='store_true')

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

print("---------------------------------------------------------")
print("Cases metadata migration script")
if dry_run:
print("dry-run=" + str(dry_run) + " -> will run without modifying anything (test mode)")

# Check powsybl-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
plateformName = get_plateform_info()['redirect_uri']
print("\n")

print("---------------------------------------------------------")
print("This script will apply on plateform = " + plateformName )
print("\n")
print("===> powsybl-case-server seems OK ! The script can proceed")
print("\n")
casesUuids = get_cases_with_empty_metadata()
print("table content : ")
print(str(casesUuids))
print("For a total of " + str(len(casesUuids)) + " casesUuids ")
print("---------------------------------------------------------")

print("cases metadata update migration (dry-run=" + str(dry_run) + ") in processing...")
failCount = 0
successCount = 0

for caseUuid in tqdm(casesUuids):
if not dry_run:
try:
complete_cases_metadata(caseUuid)
successCount += 1
except Exception as e:
failCount += 1
# print only str(e) instead of the full traceback because we call this method from a simple for loop script
tqdm.write("case " + caseUuid + " => migration failed: "+ str(e))
if isinstance(e, requests.exceptions.RequestException) and e.response is not None:
tqdm.write("Response body: " + repr(e.response.text)) # repr for cheap escaping
tqdm.write("") # emtpy newline between errors for legibility
print("End of case metadata update migration")
print("cases migration sucesses : " + str(successCount))
print("cases migration failures : " + str(failCount))
2 changes: 2 additions & 0 deletions scripts/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@
GET_NETWORK = NETWORK_STORE_SERVER_URL + "/networks/{networkId}"
MIGRATE_V211_LIMITS = NETWORK_STORE_SERVER_URL + "/migration/v211limits/{networkId}/{variantNum}"
MIGRATE_V214_TAP_CHANGER_STEPS = NETWORK_STORE_SERVER_URL + "/migration/v214tapChangeSteps/{networkId}/{variantNum}"
GET_CASES_WITH_EMPTY_METADATA = CASE_SERVER_URL + "/migration/cases/emptyMetadata"
MIGRATE_CASE_UPDATE_METADATA = CASE_SERVER_URL + "/migration/cases/{uuid}/metadata"

DELETE_NETWORKS = NETWORK_STORE_SERVER_URL + "/networks"

Expand Down
10 changes: 10 additions & 0 deletions scripts/functions/cases/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,13 @@ def copy_to_s3_storage(case_uuid, case_name, case):

def exists_case_on_s3(case_uuid):
return requests.get(constant.S3_EXISTS_CASE.format(caseUuid = case_uuid)).text == 'true'

def get_cases_with_empty_metadata():
response = requests.get(constant.GET_CASES_WITH_EMPTY_METADATA)
response.raise_for_status()
print(response.json())
return response.json()

def complete_cases_metadata(case_uuid):
response = requests.put(constant.MIGRATE_CASE_UPDATE_METADATA.format(uuid=case_uuid))
response.raise_for_status()