sync
This commit is contained in:
@@ -0,0 +1,338 @@
|
||||
|
||||
USE [HCITOOLS]
|
||||
GO
|
||||
|
||||
IF EXISTS (SELECT * FROM sys.objects o JOIN sys.schemas s ON o.schema_id = s.schema_id WHERE o.name = 'sp_Monitor_AMR_Central' AND OBJECTPROPERTY(object_id,N'IsProcedure') = 1 AND s.name = 'dba')
|
||||
DROP PROCEDURE [dba].[sp_Monitor_AMR_Central]
|
||||
GO
|
||||
|
||||
SET ANSI_NULLS ON
|
||||
SET QUOTED_IDENTIFIER ON
|
||||
GO
|
||||
|
||||
|
||||
|
||||
CREATE PROCEDURE [dba].[sp_Monitor_AMR_Central]
|
||||
|
||||
AS
|
||||
/*=============================================================================
|
||||
|
||||
Explication du traitement realise par la SP
|
||||
-------------------------------------------
|
||||
Monitoring du nombre d'AMR sur une pharmacie et d'envoi d'alertes mails et/ou sms selon le total.
|
||||
Cette SP est exécutée depuis le job _D90290 - SYNC - Monitor AMR - Central
|
||||
|
||||
Parametres
|
||||
----------
|
||||
|
||||
Creation : 01.05.2019 / SPE
|
||||
|
||||
Modifications : 05.02.2020 / SPE - Modification of bmcarizona password
|
||||
16.08.2021 / SPE : Replace hardcoded password in sp and scripts
|
||||
17.03.2022 / FLA : Change DBA mail
|
||||
30.11.2022 / FLA : keep SMS alert only for production environment
|
||||
|
||||
=============================================================================*/
|
||||
|
||||
SET ANSI_NULLS ON;
|
||||
SET QUOTED_IDENTIFIER ON;
|
||||
SET CONCAT_NULL_YIELDS_NULL ON;
|
||||
SET ANSI_WARNINGS ON;
|
||||
SET ANSI_PADDING ON;
|
||||
SET NOCOUNT ON;
|
||||
|
||||
/*------------------- Declaration des variables --------------------*/
|
||||
DECLARE @errno int,
|
||||
@niveau_alerte_AMR int,
|
||||
@niveau_alerte_AMR_Med int,
|
||||
@niveau_alerte_AMR_Sup int,
|
||||
@amr_count_total int,
|
||||
@amr_count_total_limit int,
|
||||
@current_max int,
|
||||
@AMT_table_name_LEN int,
|
||||
@Today varchar(10),
|
||||
@pharmacy varchar(21),
|
||||
@errmsg varchar(255),
|
||||
@email varchar(255),
|
||||
@SMSPiket varchar(255),
|
||||
@password varchar(255),
|
||||
@Result_as_string varchar(max),
|
||||
@message varchar(max),
|
||||
@sqlstmt varchar(max),
|
||||
@Type VARCHAR(5)
|
||||
|
||||
|
||||
|
||||
|
||||
/*------------ Affectation des parametres aux variables ------------*/
|
||||
|
||||
/* A partir de cette valeur pour une table une alerte mail prioritaire est envoyée*/
|
||||
SET @niveau_alerte_AMR = 5000
|
||||
SET @niveau_alerte_AMR_Med = 10000
|
||||
|
||||
/* A partir de cette valeur pour une table une alerte sms est envoyée*/
|
||||
SET @niveau_alerte_AMR_Sup = 50000
|
||||
|
||||
/* Si le total des amr atteint cette valeur, une alerte sms est envoyée */
|
||||
SET @amr_count_total_limit = 100000;
|
||||
|
||||
/* La date d'aujourd'hui en format YYYY-MM-DD */
|
||||
SET @Today = convert(varchar(10), getdate(), 121);
|
||||
|
||||
/* Set current pharmacy from customer */
|
||||
SELECT @pharmacy = CASE
|
||||
WHEN customer = 'AMA' THEN 'ama004aps.amavita.ch'
|
||||
WHEN customer = 'SUN' THEN 'sun111aps.sunstore.ch'
|
||||
WHEN customer = 'AAI' THEN 'aai002aps.aai.local'
|
||||
WHEN customer = 'RUEG' THEN 'aai003aps.aai.local' END
|
||||
from master.cfg.[Identity]
|
||||
|
||||
/* Set email list from customer */
|
||||
SELECT @email = CASE
|
||||
WHEN customer = 'AMA' THEN 'DBA_operator;HCI_Dev_DataMonitoring_Operators;[HCI_Connectic_Managers];HCI_Consultants_Centrale_GaleniCare'
|
||||
WHEN customer = 'SUN' THEN 'DBA_operator;HCI_Dev_DataMonitoring_Operators;[HCI_Connectic_Managers];HCI_Consultants_Centrale_GaleniCare'
|
||||
WHEN customer = 'AAI' THEN 'DBA_operator;HCI_Dev_DataMonitoring_Operators'
|
||||
WHEN customer = 'RUEG' THEN 'DBA_operator;HCI_Dev_DataMonitoring_Operators' END
|
||||
from master.cfg.[Identity]
|
||||
|
||||
SELECT @SMSPiket = DML_Recipients FROM [Arizona].[dbo].[DBA_Mailing_list] WHERE DML_Code = 'HCI_SMS'
|
||||
|
||||
SELECT @Type = type FROM [master].[cfg].[InstanceContext]
|
||||
|
||||
/*-------------------------- Traitement ---------------------------*/
|
||||
|
||||
BEGIN TRY
|
||||
|
||||
/******************************/
|
||||
/* EN PHARMACIE : Recupere les tables monitorees en pharmacie pour APOS */
|
||||
/******************************/
|
||||
|
||||
/* Create temp table */
|
||||
IF NOT EXISTS (select 1 from tempdb.dbo.sysobjects o where o.xtype in ('U') and o.id = object_id(N'tempdb..#aps_monitor_table_MAG'))
|
||||
BEGIN
|
||||
CREATE TABLE #aps_monitor_table_MAG (
|
||||
AMT_table_name varchar(255) NULL,
|
||||
AMT_monitoring_type smallint NULL,
|
||||
AMT_vertical_synchronization bit NOT NULL,
|
||||
AMT_horizontal_synchronization bit NOT NULL);
|
||||
END
|
||||
|
||||
/* Set pharmacy from Identity table */
|
||||
SELECT @password = dbo.fn_Decrypt(0x01000000CB57766E65B2870616508D6B1E3996D8AA04932A5637822356196C4D3D7523A36EB5277FE69D2C21661533909D6AEDBA4E287822DEAD22C59FF2B5F5CE313BE0)
|
||||
|
||||
SET @sqlstmt = 'INSERT INTO #aps_monitor_table_MAG
|
||||
SELECT AMT_table_name,
|
||||
AMT_monitoring_type,
|
||||
AMT_vertical_synchronization,
|
||||
AMT_horizontal_synchronization
|
||||
FROM OPENROWSET(''SQLOLEDB'',
|
||||
''' + @pharmacy + ''';
|
||||
''bmcarizona'';
|
||||
''' + @password + ''',
|
||||
''SELECT AMT_table_name, AMT_monitoring_type, AMT_vertical_synchronization, AMT_horizontal_synchronization FROM Arizona.dbo.APS_monitor_table WHERE AMT_monitoring_type > 1'');'
|
||||
|
||||
EXEC (@sqlstmt)
|
||||
|
||||
|
||||
/******************************/
|
||||
/* A LA CENTRALE : COMPTAGE DES AMR EN ATTENTE DE SYNCHRONISATION */
|
||||
/******************************/
|
||||
DECLARE @aps_monitor_row_CENTRAL table (
|
||||
AMT_table_name varchar(255) NULL,
|
||||
AMR_count int,
|
||||
AMR_APS_TS_min datetime,
|
||||
AMR_APS_TS_max datetime,
|
||||
AMR_count_00_08 int,
|
||||
AMR_count_08_10 int,
|
||||
AMR_count_10_12 int,
|
||||
AMR_count_12_14 int,
|
||||
AMR_count_14_16 int,
|
||||
AMR_count_16_18 int,
|
||||
AMR_count_18_20 int,
|
||||
AMR_count_20_00 int);
|
||||
|
||||
INSERT INTO @aps_monitor_row_CENTRAL
|
||||
SELECT PivotTable.amt_table_name,
|
||||
[<8] + [8..10] + [10..12] + [12..14] + [14..16] + [16..18] + [18..20] + [>20] 'Total AMR en attente',
|
||||
min(AMR_APS_TS),
|
||||
max(AMR_APS_TS),
|
||||
[<8],
|
||||
[8..10],
|
||||
[10..12],
|
||||
[12..14],
|
||||
[14..16],
|
||||
[16..18],
|
||||
[18..20],
|
||||
[>20]
|
||||
FROM ( SELECT DISTINCT amr.amr_row_guid,
|
||||
amr.amr_aps_monitor_table,
|
||||
amt.amt_table_name,
|
||||
CASE
|
||||
WHEN amr.amr_aps_ts < @Today + ' 08:00' THEN '<8'
|
||||
WHEN amr.amr_aps_ts < @Today + ' 10:00' THEN '8..10'
|
||||
WHEN amr.amr_aps_ts < @Today + ' 12:00' THEN '10..12'
|
||||
WHEN amr.amr_aps_ts < @Today + ' 14:00' THEN '12..14'
|
||||
WHEN amr.amr_aps_ts < @Today + ' 16:00' THEN '14..16'
|
||||
WHEN amr.amr_aps_ts < @Today + ' 18:00' THEN '16..18'
|
||||
WHEN amr.amr_aps_ts < @Today + ' 20:00' THEN '18..20'
|
||||
ELSE '>20' END 'AMR_Period'
|
||||
FROM [Arizona].[dbo].aps_monitor_row amr (nolock)
|
||||
JOIN [Arizona].[dbo].aps_monitor_table amt (nolock)
|
||||
ON aps_monitor_table_id = amr_aps_monitor_table
|
||||
AND AMT_monitoring_type > 1
|
||||
AND AMT_vertical_synchronization = 1
|
||||
JOIN #aps_monitor_table_MAG amtM
|
||||
ON amtM.amt_table_name = amt.amt_table_name
|
||||
WHERE amr.amr_extraction_timestamp is null) AS SourceTable
|
||||
PIVOT ( count(amr_row_guid)
|
||||
FOR AMR_Period in ([<8], [8..10], [10..12], [12..14], [14..16], [16..18], [18..20], [>20])) AS PivotTable
|
||||
JOIN [Arizona].[dbo].aps_monitor_row amr (nolock)
|
||||
ON amr.amr_aps_monitor_table = PivotTable.amr_aps_monitor_table
|
||||
AND amr_extraction_timestamp is null
|
||||
GROUP BY PivotTable.amt_table_name,
|
||||
[<8],
|
||||
[8..10],
|
||||
[10..12],
|
||||
[12..14],
|
||||
[14..16],
|
||||
[16..18],
|
||||
[18..20],
|
||||
[>20]
|
||||
ORDER BY [<8] + [8..10] + [10..12] + [12..14] + [14..16] + [16..18] + [18..20] + [>20] desc,
|
||||
PivotTable.amt_table_name;
|
||||
|
||||
SELECT @current_max = MAX(AMR_count) FROM @aps_monitor_row_CENTRAL
|
||||
SELECT @amr_count_total = SUM(AMR_count) FROM @aps_monitor_row_CENTRAL
|
||||
|
||||
/* DROP Temp table */
|
||||
IF EXISTS (select 1 from tempdb.dbo.sysobjects o where o.xtype in ('U') and o.id = object_id(N'tempdb..#aps_monitor_table_MAG'))
|
||||
BEGIN
|
||||
DROP TABLE #aps_monitor_table_MAG
|
||||
END
|
||||
|
||||
/* bloc alertes mail normal */
|
||||
IF @current_max >= @niveau_alerte_AMR AND @current_max < @niveau_alerte_AMR_Med
|
||||
BEGIN
|
||||
|
||||
SELECT @AMT_table_name_LEN = 2 + max(len(AMT_table_name))
|
||||
FROM @aps_monitor_row_CENTRAL;
|
||||
SELECT @Result_as_string
|
||||
= '' + convert(char(10), 'Nb_AMR') + left(convert(char(100), 'Table'), @AMT_table_name_LEN)
|
||||
+ convert(char(10), '<8h') + convert(char(10), '8h-10h') + convert(char(10), '10h-12h')
|
||||
+ convert(char(10), '12h-14h') + convert(char(10), '14h-16h') + convert(char(10), '16h-18h')
|
||||
+ convert(char(10), '18h-20h') + convert(char(10), '>20h') + convert(char(18), 'MIN amr_aps_ts')
|
||||
+ convert(char(18), 'MAX amr_aps_ts')
|
||||
+ '
|
||||
-------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
' ;
|
||||
SELECT @Result_as_string
|
||||
= @Result_as_string + convert(char(10), AMR_count)
|
||||
+ left(convert(char(100), AMT_table_name), @AMT_table_name_LEN) + convert(char(10), AMR_count_00_08)
|
||||
+ convert(char(10), AMR_count_08_10) + convert(char(10), AMR_count_10_12)
|
||||
+ convert(char(10), AMR_count_12_14) + convert(char(10), AMR_count_14_16)
|
||||
+ convert(char(10), AMR_count_16_18) + convert(char(10), AMR_count_18_20)
|
||||
+ convert(char(10), AMR_count_20_00) + convert(char(18), convert(varchar(16), AMR_APS_TS_min, 121))
|
||||
+ convert(char(18), convert(varchar(16), AMR_APS_TS_max, 121)) + '
|
||||
'
|
||||
FROM @aps_monitor_row_CENTRAL
|
||||
ORDER BY AMR_count desc,
|
||||
AMT_table_name;
|
||||
SELECT @Result_as_string
|
||||
= @Result_as_string
|
||||
+ '-------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
' ;
|
||||
SET @message
|
||||
= 'AMR en attente de synchronisation verticale, et synchronisation ensuite par la connectique !
|
||||
|
||||
' + @Result_as_string;
|
||||
EXEC [Arizona].[dbo].aps_Send_Mail_with_template @in_param_varchar_2 = @email,
|
||||
@in_param_varchar_3 = @message,
|
||||
@in_job_type = 0;
|
||||
END
|
||||
|
||||
/* bloc alertes mail prioritaire */
|
||||
IF @current_max >= @niveau_alerte_AMR_Med
|
||||
or @amr_count_total >= @amr_count_total_limit
|
||||
BEGIN
|
||||
|
||||
SELECT @AMT_table_name_LEN = 2 + max(len(AMT_table_name))
|
||||
FROM @aps_monitor_row_CENTRAL;
|
||||
SELECT @Result_as_string
|
||||
= '' + convert(char(10), 'Nb_AMR') + left(convert(char(100), 'Table'), @AMT_table_name_LEN)
|
||||
+ convert(char(10), '<8h') + convert(char(10), '8h-10h') + convert(char(10), '10h-12h')
|
||||
+ convert(char(10), '12h-14h') + convert(char(10), '14h-16h') + convert(char(10), '16h-18h')
|
||||
+ convert(char(10), '18h-20h') + convert(char(10), '>20h') + convert(char(18), 'MIN amr_aps_ts')
|
||||
+ convert(char(18), 'MAX amr_aps_ts')
|
||||
+ '
|
||||
-------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
' ;
|
||||
SELECT @Result_as_string
|
||||
= @Result_as_string + convert(char(10), AMR_count)
|
||||
+ left(convert(char(100), AMT_table_name), @AMT_table_name_LEN) + convert(char(10), AMR_count_00_08)
|
||||
+ convert(char(10), AMR_count_08_10) + convert(char(10), AMR_count_10_12)
|
||||
+ convert(char(10), AMR_count_12_14) + convert(char(10), AMR_count_14_16)
|
||||
+ convert(char(10), AMR_count_16_18) + convert(char(10), AMR_count_18_20)
|
||||
+ convert(char(10), AMR_count_20_00) + convert(char(18), convert(varchar(16), AMR_APS_TS_min, 121))
|
||||
+ convert(char(18), convert(varchar(16), AMR_APS_TS_max, 121)) + '
|
||||
'
|
||||
FROM @aps_monitor_row_CENTRAL
|
||||
ORDER BY AMR_count desc,
|
||||
AMT_table_name;
|
||||
SELECT @Result_as_string
|
||||
= @Result_as_string
|
||||
+ '-------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
' ;
|
||||
SELECT @message
|
||||
= 'AMR en attente de synchronisation verticale, et synchronisation ensuite par la connectique !
|
||||
|
||||
' + @Result_as_string;
|
||||
EXEC [Arizona].[dbo].aps_Send_Mail_with_template @in_param_varchar_2 = @email,
|
||||
@in_param_varchar_3 = @message,
|
||||
@in_job_type = 3;
|
||||
END
|
||||
|
||||
IF @Type = 'PROD'
|
||||
BEGIN
|
||||
|
||||
/* bloc rajouté pour les alertes sms */
|
||||
IF @current_max >= @niveau_alerte_AMR_Sup
|
||||
OR @amr_count_total >= @amr_count_total_limit
|
||||
BEGIN
|
||||
|
||||
DECLARE @subject varchar(20);
|
||||
SELECT @subject = 'AMR alert:' + cast(SERVERPROPERTY('machinename') as varchar(20))
|
||||
SELECT @AMT_table_name_LEN = 2 + max(len(AMT_table_name))
|
||||
FROM @aps_monitor_row_CENTRAL;
|
||||
SELECT @Result_as_string = 'Total ' + convert(varchar(10), @amr_count_total);
|
||||
SELECT TOP 5 @Result_as_string
|
||||
= @Result_as_string + +CHAR(13) + CHAR(10)
|
||||
+ left(convert(varchar(30), AMT_table_name), @AMT_table_name_LEN) + ' '
|
||||
+ convert(varchar(10), AMR_count) + ''
|
||||
FROM @aps_monitor_row_CENTRAL
|
||||
ORDER BY AMR_count desc,
|
||||
AMT_table_name;
|
||||
SELECT @Result_as_string = @Result_as_string;
|
||||
SELECT @message = left(@Result_as_string, 100);
|
||||
EXEC [Arizona].[dbo].aps_Send_Mail_with_template @in_param_recipient = @SMSPiket,
|
||||
@in_param_subject = @subject,
|
||||
@in_param_message = @message,
|
||||
@in_job_type = 40;
|
||||
END
|
||||
END
|
||||
END TRY
|
||||
BEGIN CATCH
|
||||
SELECT @errno = 70003,
|
||||
@errmsg = 'Monitoring AMR Central error!'
|
||||
goto error_99
|
||||
END CATCH;
|
||||
|
||||
/*------------------ Retour au programme appelant -----------------*/
|
||||
|
||||
RETURN(@@error);
|
||||
|
||||
/*---------------------- Traitement des erreurs ----------------------*/
|
||||
error_99:
|
||||
RAISERROR (@errmsg, 16, 1);
|
||||
RETURN(@errno);
|
||||
|
||||
GO
|
||||
Reference in New Issue
Block a user