# How-to-fix-broken-SLT-data-via-Python-script_686074161
## Introduction

This script will fix SLT issues for **Request** & **Incident** in the following 4 scenarios:

- Tickets have empty SLT information.
- Tickets have super large or negative SLT values.
- Tickets have been suspended but the SLT is still being calculated.
- Tickets have wrong/empty values for their SLA status.

**Note: This script applies to SMAX versions from 2022.05 to 25.2. Back up your data before running this script.**

## Resolution

1. Install python v3.8 ([https://www.python.org/downloads/source/](https://www.python.org/downloads/source/)) or above on the control plane node.
2. Run the following DB query in the xservices\_ems database to collect any customized Enum Items in the customer’s environment.
	SELECT \* FROM enum\_value\_<tenantId> WHERE enumeration\_descriptor\_name in ('ImpactScopeEnum','SawPriority','SLTStatus','TargetType','AgreementUsage', 'DefinitionCalculationPolicy', 'RequestStatusEnum', 'IncidentStatusEnum');
3. Based on the result of step 2, update the script from Line 108 to 148. Pick up the customized content, then add them into the last line of each class. Here is the template script.[fixslt2\_20231129.zip](attachments/686074161/686074162.zip)  
	For example, in the following case, you can insert “P1\_c=20129” into the last line of the SawPriority class.  
	![](attachments/686074161/686074160.png)
4. Based on the result of step 2, check the result of 'RequestStatusEnum' & 'IncidentStatusEnum', go to web > Studio > Request/Incident > Process and Rules, use the response as a keyword and search them in the rules. If they are used in the rule related to suspend status, then they need to be added to Line 181 & 182 for each record type.
5. Prepare the following parameters for the environment, they will be used for input when you execute the script.
	SMAX web information.  
	Hostname;  
	Port;  
	Username;  
	Password;  
	DB Credential information.  
	DB username;  
	DB password;  
	DB host;  
	DB port;  
	Entity type(currently we only support Request & Incident);  
	Start time;  
	Tenant ID;
6. Prepare the ticket IDs to be fixed by executing the following DB query in xservices\_ems.  
	Use the “copy to” command to save the result into the target folder.  
	For example: copy (**#1 SELECT COMMAND**) to ‘/tmp/request\_ids.txt  
	(**Note: Fill in the record type and update the creation time value before execution.**)  
	1. The following query is to filter out all tickets without SLT target data.
		SELECT entity\_id FROM entities\_<tenant\_id> where entity\_type\_id = (select distinct id from entity\_descriptor where localization\_key = '<entity\_type>') and creation\_time >= 1675180800000  
		  
		EXCEPT  
		  
		SELECT parent\_id FROM slt\_targets\_<tenant\_id> WHERE parent\_id IN (SELECT entity\_id FROM entities\_<tenant\_id> where entity\_type\_id = (SELECT distinct id FROM entity\_descriptor WHERE localization\_key = '<entity\_type>') and creation\_time >= 1675180800000) GROUP BY parent\_id;
		2. The following query is to filter out all tickets whose SLT calculations with super large values.
		SELECT parent\_id from  
		  
		(SELECT \* from slt\_targets\_<tenantId> where parent\_id in (SELECT entity\_id FROM entities\_<tenantId> where entity\_type\_id = (select distinct id from entity\_descriptor where localization\_key = '<entity\_type>') and creation\_time >= 1680278400000)) s  
		  
		WHERE (elapsed\_duration > 31536000 or elapsed\_duration\_min > 525600) group by parent\_id;
		**Note: The value of elapsed\_duration is in seconds and elapsed\_duration\_min is in minutes. The example above filters out a duration that is longer than 365 days. You may set different values according to your business needs.**  
		  
		The following query is to filter out all tickets whose SLT calculations with negative values.
		SELECT parent\_id from  
		  
		(SELECT \* from slt\_targets\_<tenant\_id> WHERE parent\_id in (SELECT entity\_id FROM entities\_<tenant\_id> WHERE entity\_type\_id = (select distinct id from entity\_descriptor WHERE localization\_key = '<entity\_type>') and creation\_time >= 1675180800000)) s  
		  
		WHERE (elapsed\_duration < 0 or elapsed\_duration\_min < 0) group by parent\_id;
		3. The following query is to filter out all closed tickets but their SLT is still being calculated.
		SELECT distinct physical\_type\_name as close\_time\_field FROM entitydescriptor\_mapping WHERE entity\_type='<entity\_type>' and logical\_type\_name='CloseTime';  
		  
		note: query field name of 'CloseTime', and the query result will be applied to the <CloseTime\_field>  
		  
		SELECT parent\_id FROM slt\_record\_status\_<tenant\_id> WHERE parent\_id IN (SELECT entity\_id FROM entities\_<tenantId> where entity\_type\_id = (select distinct id from entity\_descriptor where localization\_key = '<entityType>') and creation\_time >= 1675180800000 and <CloseTime\_field> is not NUll) and status not in (100, 400);
		4. The following query is to filter out all tickets whose SLA Overall status value is NULL.
		SELECT parent\_id FROM slt\_record\_status\_<tenant\_id> WHERE parent\_id IN (SELECT entity\_id FROM entities\_<tenant\_id> where entity\_type\_id = (select distinct id from entity\_descriptor where localization\_key = '<entityType>') and creation\_time >= 1675180800000) and sla\_status is null;
7. Copy the updated fixSlt script and the attached [triageslt.py](attachments/686074161/704987186.py) into the same folder with request\_ids.txt and give the folder full permission via the following command:
	chmod 777 -R <Path to the fixSltData directory>
8. Run the following command to execute the triageslt script to collect the SLT related tables data before the fixSlt script is run for the problematic request IDs given in the request\_ids.txt and the results of the script execution is written into the **triageslt.log** file.  
	python3 triageslt.py
	  
	**NOTE**: On script run with the above command, it prompt the user to enter the required details. If we want to avoid entering the values each time, then we can hardcode the required parameter values in the script and then run which will then read the hardcoded values.
9. Run the following command to start the fix script and input the parameter in step 5.
	python3 fixslt2.py
10. After the fixing work is finished, save the log file “fix\_slt.log” in the same level directory, it will record the operation with all tickets fixed by the script.
11. Run the following command to execute the triageslt script to collect the SLT related tables data after the fixSlt script is run for the problematic request IDs given in the request\_ids.txt and the results of the script execution is appended into the **triageslt.log** file created in the step 8 above.  
	python3 triageslt.py
12. Share the triageslt.log and fix\_slt.log files to RnD via CPE engineer for the further troubleshooting of the SLT problem reported.
