MAGGA03Q ;WOIFO/GEK/BNT/NST/JSL - TASK IMAGE STATISTICS ; 07 Oct 2010 9:48 PM
 ;;3.0;IMAGING;**117,122**;Mar 19, 2002;Build 92;Aug 02, 2012
 ;; Per VHA Directive 2004-038, this routine should not be modified.
 ;; +---------------------------------------------------------------+
 ;; | Property of the US Government.                                |
 ;; | No permission to copy or redistribute this software is given. |
 ;; | Use of unreleased versions of this software requires the user |
 ;; | to execute a written test agreement with the VistA Imaging    |
 ;; | Development Office of the Department of Veterans Affairs,     |
 ;; | telephone (301) 734-0100.                                     |
 ;; | The Food and Drug Administration classifies this software as  |
 ;; | a medical device.  As such, it may not be changed in any way. |
 ;; | Modifications to this software may result in an adulterated   |
 ;; | medical device under 21CFR820, the use of which is considered |
 ;; | to be a violation of US Federal Statutes.                     |
 ;; +---------------------------------------------------------------+
 ;;
 ;P122 gek Fix the issue of not being able to Re-Run a report that
 ;  was previously canceled while it was still running.
 Q
 ;
 ;***** RETURNS VARIOUS IMAGE STATISTICS DATA
 ; RPC: MAGG IMAGE STATISTICS QUE
 ;
 ; .MAGRY        Reference to a local variable where the results
 ;               are returned to.
 ;
 ; FLAGS         Flags that control the execution (can be combined):
 ;
 ;                 C  Capture date range. If this flag is provided,
 ;                    then the remote procedure uses values of the
 ;                    FROMDATE and TODATE parameters to select images
 ;                    that were captured in this date range.
 ;
 ;                    Otherwise, values of those parameters are
 ;                    treated as the date range when procedures were
 ;                    performed.
 ;
 ;                 D  Include only deleted images (file #2005.1)
 ;                 E  Include only existing images (file #2005)
 ;
 ;                 S  Return image counts grouped by status
 ;                 U  Return image counts grouped by users and status
 ;
 ;               If neither 'E' nor 'D' flag is provided, then an
 ;               error (-6) is returned.
 ;
 ;               If neither 'S' nor 'U' flag is provided, then an
 ;               error (-6) is returned.
 ;
 ; [FROMDATE]    Date range for image selection. Dates can be in
 ; [TODATE]      internal or external FileMan format. If a date
 ;               parameter is not defined or empty, then the date
 ;               range remains open on the corresponding side.
 ;
 ;               Time parts of parameter values are ignored and both
 ;               ends of the date range are included in the search.
 ;               For example, in order to search images for May 21,
 ;               2008, the inernal value of both parameters should
 ;               be 3080521.
 ;
 ;               If the FROMDATE is after the TODATE, then values of
 ;               the parameters are swapped.
 ; 
 ; [MQUE]        Flags for tasking reports and action on previously
 ;               tasked reports.
 ;               Q  (Default) Queue a new report task or return the status of
 ;                  In Progress for a running report. If previously 
 ;                  ran task is complete, then return report data.
 ;               R  Stop and Requeue a running report with the 
 ;                  same parameters. Existing data collected is removed 
 ;                  from temporary storage.
 ;               D  Stop a running or completed report and delete the data.
 ;
 ; Return Values
 ; =============
 ;     
 ; Zero value of the 1st '^'-piece of the @MAGRESULTS@(0) indicates an
 ; error during execution of the procedure. In this case, the array
 ; is formatted as described in the comments to the RPCERRS^MAGUERR1.
 ;
 ; Otherwise, the array contains the requested data. See description
 ; of the MAGG QUE IMAGE STATISTICS remote procedure for details.
 ;
 ; Notes
 ; =====
 ;
 ; Temporary global nodes ^TMP("MAGGA03Q",$J) and ^XTMP("MAGGA03Q,DUZ")
 ; are used by this procedure.
 ;
STATS(MAGRY,FLAGS,FROMDATE,TODATE,MQUE) ; RPC [MAGG IMAGE STATISTICS QUE]
 N MOTH,MERR,MAGXTN,MAGRES,MTDESC,X,Y,RC
 S MAGRY=$NA(^TMP("MAGGA03Q",$J))
 K @MAGRY
 S (RC,MERR)=0
 D CLEAR^MAGUERR(1)
 ;
 ;--- Validate FLAGS Parameters
 S FLAGS=$G(FLAGS) I $TR(FLAGS,"CDESU")'="" D IPVE^MAGUERR("FLAGS") S MERR=1
 ;--- Missing required flag(s)
 I $TR(FLAGS,"DE")=FLAGS D ERROR^MAGUERR(-6,,"D,E") S MERR=1
 I $TR(FLAGS,"SU")=FLAGS D ERROR^MAGUERR(-6,,"S,U") S MERR=1
 ;
 ;--- Validate Date Range
 S:$$DTRANGE^MAGUTL03(.FROMDATE,.TODATE)<0 MERR=1
 ;
 ;--- Validate TaskMan Queing parameters
 S MQUE=$G(MQUE) I MQUE="" S MQUE="Q"
 I $TR(MQUE,"QRD")'="" D IPVE^MAGUERR("MQUE") S MERR=1
 ;
 ;--- Check if error occurred and quit if so
 I MERR D ERROR^MAGUERR(-30) S RC=$$FIRSTERR^MAGUERR1()
 I RC<0 D RPCERRS^MAGUERR1(.MAGRY,RC) Q
 ;
 ;--- Create unique XTMP node
 S MAGXTN=$$TNODE(FLAGS,FROMDATE,TODATE)
 ;
 ;--- Resolve previously tasked report
 I $D(^XTMP(MAGXTN,0)) D RESOLVE(.RC,MAGXTN,MQUE)
 ;--- If 1 is returned then the report is in progress
 I +RC=1 M @MAGRY@(0)=RC Q
 ;--- If 2 is returned then the report is complete and is returned
 I +RC=2 M @MAGRY@(0)=^XTMP(MAGXTN,"R",0) Q
 ;
 ;--- Create the TaskMan parameters and queue the report
 S MTDESC="Imaging Statistics: "_FLAGS_" "_FROMDATE_" to "_TODATE
 S MOTH("ZTDTH")=$H
 ; p117 T5:  To enable a complete list of reports to be displayed 
 ; in the Client Report list.  If this "I" node isn't set, the report 
 ; will not show up in list until it is run. Could be minutes.  
 ; Added a new status to the Delphi Window : 'Queuing'.  
 ; 'Queuing' status will account for the time between the Task being
 ;  created and the job being run.  The Queuing below isn't used. It
 ;  is replaced with the TaskMan Task number later.
 ;  Delphi uses $p(3) = '' (start time) to determine 'Queuing', and not Running.
 S ^XTMP(MAGXTN,"I",0)="Queuing"_U_$G(DUZ)_U_U
 ;
 S MAGRES=$$NODEV^XUTMDEVQ("TASK^MAGGA03Q",MTDESC,"FLAGS;FROMDATE;TODATE;MAGXTN",.MOTH)
 ;--- Save thru date ^ create date ^ Task Number
 S ^XTMP(MAGXTN,0)=$$FMADD^XLFDT(DT,1)_U_DT_U_MAGRES
 ;--- Save user cross reference node for report lookup by user
 S ^XTMP("MAGGA03Q",DUZ,MAGXTN)=""
 ;--- Return successful queued report notification with Task ID
 S @MAGRY@(0)="1^Report Queued on Task ID: "_MAGRES
 Q
 ;
TASK ;
 N MAGXTN,MAGRES
 S MAGXTN=$$TNODE(FLAGS,FROMDATE,TODATE)
 ;--- Save Internal Data as follows
 ;--- ^XTMP($$TNODE,"I",0)=Task Number^User ID^Start Date/Time^Complete Date/Time
 S ^XTMP(MAGXTN,"I",0)=$G(ZTSK)_U_$G(DUZ)_U_$$NOW^XLFDT_U
 ;--- Collect Report Data from Imaging API
 D IMGQUERY^MAGGA03(.MAGRES,FLAGS,FROMDATE,TODATE) ; GEK BOOKMARK 1
 ;P122 gek 
 ;  If the variable ZSTOP = '1', then the TASK/Report was stopped,
 ;  or if the Report's User Index: ^XTMP("MAGGAO3Q",DUZ,MAGXTN)  
 ;  does not exist, the report was stopped.
 ;  We only save report Data if report was not Stopped.
 ;  This Fixes the issue of not being able to Re-Run a report that
 ;  was previously canceled while it was still running.
 ;  
 I '$G(ZTSTOP),($D(^XTMP("MAGGA03Q",DUZ,MAGXTN))) D
 . ;--- Save Completed date/time of Report
 . S $P(^XTMP(MAGXTN,"I",0),U,4)=$$NOW^XLFDT
 . ;--- Update the Save Through date to midnight
 . S $P(^XTMP(MAGXTN,0),U,1)=$$FMADD^XLFDT($$DT^XLFDT(),1)
 . ;--- Save Report Data in temporary storage
 . M ^XTMP(MAGXTN,"R")=@MAGRES
 . Q
 Q
 ;
 ; Returns status of existing report based on MQUE flag
RESOLVE(RY,MAGXTN,MQUE) ;
 ; if task is finished, then return the data.
 ; Q flag will return a completed report or an In Progress status if still running
 I MQUE="Q" D  Q
 . S X=$P($G(^XTMP(MAGXTN,"I",0)),U,4)
 . ;/p117 T5 gek-  add $G above to stop <undef>. 
 . ; Occurred rarely (pre T5) when report is Re-Run. CodeCR731
 . I X="" S RY="1^In Progress" Q
 . S RY="2^Report Complete"
 . M @MAGRY=^XTMP(MAGXTN,"R")
 . Q
 ; R flag will Stop and Requeue a running report with same parameters
 I MQUE="R" D  Q
 . N MAGSTP,ZTSK
 . S ZTSK=$$GETTASK(MAGXTN)
 . ; Try to stop the task if it's currently running
 . S MAGSTP=$$ASKSTOP^%ZTLOAD(ZTSK)
 . I 'MAGSTP S RY="1^Report cannot be stopped. Try again later" Q
 . ;
 . D STAT^%ZTLOAD
 . I 'ZTSK(0) S RY="0^Task is undefined" D  Q
 . . ;Kill Report Data and Report's User Index
 . . K ^XTMP(MAGXTN),^XTMP("MAGGA03Q",DUZ,MAGXTN)
 . . S RY="0^Okay to retask"
 . . Q
 . I ZTSK(1)<3 S RY="1^Task In Progress : "_ZTSK Q
 . ;I ZTSK(1) is either 4 or 5,  both mean not a running task. Inactive. problem
 . ;Kill Report Data and Report's User Index
 . K ^XTMP(MAGXTN),^XTMP("MAGGA03Q",DUZ,MAGXTN)
 . S RY="0^Okay to retask"
 . Q
 ; D flag will delete a previously ran report and stop a currently running task
 I MQUE="D" D  Q
 . N MAGSTP,ZTSK
 . S ZTSK=$$GETTASK(MAGXTN)
 . S MAGSTP=$$ASKSTOP^%ZTLOAD(ZTSK)
 . I 'MAGSTP S RY="1^Report cannot be stopped. Try again later" Q
 . ;Kill Report Data and Report's User Index
 . K ^XTMP(MAGXTN),^XTMP("MAGGA03Q",DUZ,MAGXTN)
 . S RY="1^Report data deleted"
 Q
 ;
 ;***** RETURNS VARIOUS IMAGE STATISTICS DATA
 ; RPC: MAGG IMAGE STATISTICS BY USER
 ;
 ; Return all statistics reports previously tasked for a user
 ;
 ; .MAGRY        Reference to a local variable where the results
 ;               are returned to.
 ; 
 ; MAGDUZ        Internal ID of a user that has previously queued an Image Statistics 
 ;               Report.
 ;               The default value is the current user DUZ.
 ;
 ; Return Values
 ; =============
 ; 
 ; MAGRY(0) -  ^01: 1 Successful execution of the remote procedure
 ;                  0 An error occurred during the execution of the remote procedure
 ;
 ;             ^02: The number of reports identified for the user
 ;
 ; MAGRY(1..n) ^01: report FLAGS parameter
 ;             ^02: report FROMDATE parameter
 ;             ^03: report TODATE parameter
 ;             ^04: report REPORT START DATE/TIME parameter
 ;             ^05: report REPORT COMPLETE DATE/TIEM parameter
 ; 
 ; e.g.
 ; 0)=1^5 Reports found for user IMAGING,USER
 ; 1)=CDE^2900613^3100503^3100505.09053^3100505.09053
 ;
GETUSRPT(MAGRY,MAGDUZ) ; RPC [MAGG IMAGE STATISTICS BY USER]
 N MAGX,MAGCNT
 N MAGINF ; XTMP node information.
 I MAGDUZ="" S MAGDUZ=DUZ
 ;--- Delete yesterdays temp data in ^XTMP
 D CLEARTMP(MAGDUZ)
 ;
 S MAGRY=$NA(^TMP("MAGGUSRPT",$J)),(MAGX,MAGCNT)=0
 K @MAGRY
 F  S MAGX=$O(^XTMP("MAGGA03Q",MAGDUZ,MAGX)) Q:MAGX=""  D
 . I '$D(^XTMP(MAGX,"I",0)) Q
 . S MAGCNT=MAGCNT+1
 . S MAGINF=$G(^XTMP(MAGX,"I",0))
 . ; Status of 'Queuing' is now set from the Delphi App, if the start time is '' (null).
 . ; Do not change next line. Any change causes list entries to not be displayed.
 . S @MAGRY@(MAGCNT)=$P(MAGX,"-",3)_U_$P(MAGX,"-",4)_U_$P(MAGX,"-",5)_U_$P(MAGINF,U,3)_U_$P(MAGINF,U,4)
 S @MAGRY@(0)="1^"_MAGCNT_$S(MAGCNT>1:" Reports ",1:" Report ")_"found for user "_$$GET1^DIQ(200,MAGDUZ_",",.01)
 Q
 ;
 ; Get unique XTMP node
 ; Namespace + User id + Flags + From Date + To Date
TNODE(FLAG,FROMDT,TODT) ;
 ;/p117 T5 GEK  THIS IS 'maggaO3q' (LETTER 'O') It should be 'magga03q' (Zero) 
 ;Q "MAGGAO3Q"_"-"_DUZ_"-"_FLAG_"-"_FROMDT_"-"_TODT ; this had letter 'o'
 Q "MAGGA03Q"_"-"_DUZ_"-"_FLAG_"-"_FROMDT_"-"_TODT ; this is Zero 
 ;
 ; Returns the Task Number from XTMP global
 ; TNODE = Value created in $$TNODE
GETTASK(TNODE) ;
 Q $S('$D(^XTMP(TNODE,0)):0,1:+$P(^XTMP(TNODE,0),U,3))
 ;
 ; Delete temp data from yesterday
 ; 
CLEARTMP(MAGDUZ) ; Delete temp data from yesterday
 N MAGDAT,MAGXTN
 S MAGXTN=""
 F  S MAGXTN=$O(^XTMP("MAGGA03Q",MAGDUZ,MAGXTN)) Q:MAGXTN=""  D
 . S MAGDAT=$P($G(^XTMP(MAGXTN,0)),U,2)
 . I MAGDAT<DT D  ; delete all data if created date is before today 
 . . N MAGSTP,ZTSK
 . . S ZTSK=$$GETTASK(MAGXTN)
 . . ; Try to stop the task if it's currently running
 . . S MAGSTP=$$ASKSTOP^%ZTLOAD(ZTSK)
 . . I 'MAGSTP Q  ; Report cannot be stopped
 . . K ^XTMP(MAGXTN),^XTMP("MAGGA03Q",$P(MAGXTN,"-",2),MAGXTN)
 . . Q
 . Q
 Q
 ;*****  CLNXTMP  
 ;  Clean XTMP nodes. 
 ;  Due to an error (rare), there may be some orphaned 
 ;  report data in the XTMP global.  This routine will
 ;  clear out any orphaned XTMP nodes for the MAGGA03Q
 ;  and MAGGAO3Q nodes of the QA Statistics Reports.
 ;  It won't hurt valid reports.  Valid reports are pointed 
 ;  to from the XTMP("MAGGA03Q",DUZ,xxxx) cross reference.
CLNXTMP ;
 ; 0 = zero   O = letter O
 ; make a list of the valid XTMP Nodes for Reports. These are referenced by the 
 ; cross ref :    XTMP('MAGGA03Q',duz,xnode)
 N MAGXTN,MAGDUZ,MWIN
 S MAGDUZ=""
 K ^TMP($J,"MAGXTMP")
 ; for all users, save reference to their Report Nodes.
 F  S MAGDUZ=$O(^XTMP("MAGGA03Q",MAGDUZ)) Q:MAGDUZ=""  D
 . S MAGXTN=""
 . F  S MAGXTN=$O(^XTMP("MAGGA03Q",MAGDUZ,MAGXTN)) Q:MAGXTN=""  D
 . . S ^TMP($J,"MAGXTMP",MAGXTN)="" ; save list of valid XTMP Nodes.
 . . Q
 . Q
 ; The orphaned XTMP Node used 'O' (letter),  and '0' (Zero)
 ; We'll delete any nodes that aren't referenced by 
 ; the cross Ref :    XTMP('MAGGA03Q',duz,xnode)
 N XNODE,TNODE,DONE
 S DONE=0
 F XNODE="MAGGAO3Q","MAGGA03Q" D
 . S TNODE=XNODE
 . S DONE=0
 . F  S XNODE=$O(^XTMP(XNODE)) D  Q:DONE
 . . I $E(XNODE,1,8)'=TNODE S DONE=1 Q  ; Quit if node is beyond QA Report Nodes.
 . . I $D(^TMP($J,"MAGXTMP",XNODE)) Q  ; This is Valid, we won't delete.
 . . ; kill the orphaned node.
 . . K ^XTMP(XNODE)
 . . Q
 . Q
 K ^TMP($J,"MAGXTMP") ; clean up 
 Q 
 
--- Routine Detail   --- with STRUCTURED ROUTINE LISTING ---[H[J[2J[HMAGGA03Q   13844     printed  Sep 23, 2025@19:38:33                                                                                                                                                                                                   Page 2
MAGGA03Q  ;WOIFO/GEK/BNT/NST/JSL - TASK IMAGE STATISTICS ; 07 Oct 2010 9:48 PM
 +1       ;;3.0;IMAGING;**117,122**;Mar 19, 2002;Build 92;Aug 02, 2012
 +2       ;; Per VHA Directive 2004-038, this routine should not be modified.
 +3       ;; +---------------------------------------------------------------+
 +4       ;; | Property of the US Government.                                |
 +5       ;; | No permission to copy or redistribute this software is given. |
 +6       ;; | Use of unreleased versions of this software requires the user |
 +7       ;; | to execute a written test agreement with the VistA Imaging    |
 +8       ;; | Development Office of the Department of Veterans Affairs,     |
 +9       ;; | telephone (301) 734-0100.                                     |
 +10      ;; | The Food and Drug Administration classifies this software as  |
 +11      ;; | a medical device.  As such, it may not be changed in any way. |
 +12      ;; | Modifications to this software may result in an adulterated   |
 +13      ;; | medical device under 21CFR820, the use of which is considered |
 +14      ;; | to be a violation of US Federal Statutes.                     |
 +15      ;; +---------------------------------------------------------------+
 +16      ;;
 +17      ;P122 gek Fix the issue of not being able to Re-Run a report that
 +18      ;  was previously canceled while it was still running.
 +19       QUIT 
 +20      ;
 +21      ;***** RETURNS VARIOUS IMAGE STATISTICS DATA
 +22      ; RPC: MAGG IMAGE STATISTICS QUE
 +23      ;
 +24      ; .MAGRY        Reference to a local variable where the results
 +25      ;               are returned to.
 +26      ;
 +27      ; FLAGS         Flags that control the execution (can be combined):
 +28      ;
 +29      ;                 C  Capture date range. If this flag is provided,
 +30      ;                    then the remote procedure uses values of the
 +31      ;                    FROMDATE and TODATE parameters to select images
 +32      ;                    that were captured in this date range.
 +33      ;
 +34      ;                    Otherwise, values of those parameters are
 +35      ;                    treated as the date range when procedures were
 +36      ;                    performed.
 +37      ;
 +38      ;                 D  Include only deleted images (file #2005.1)
 +39      ;                 E  Include only existing images (file #2005)
 +40      ;
 +41      ;                 S  Return image counts grouped by status
 +42      ;                 U  Return image counts grouped by users and status
 +43      ;
 +44      ;               If neither 'E' nor 'D' flag is provided, then an
 +45      ;               error (-6) is returned.
 +46      ;
 +47      ;               If neither 'S' nor 'U' flag is provided, then an
 +48      ;               error (-6) is returned.
 +49      ;
 +50      ; [FROMDATE]    Date range for image selection. Dates can be in
 +51      ; [TODATE]      internal or external FileMan format. If a date
 +52      ;               parameter is not defined or empty, then the date
 +53      ;               range remains open on the corresponding side.
 +54      ;
 +55      ;               Time parts of parameter values are ignored and both
 +56      ;               ends of the date range are included in the search.
 +57      ;               For example, in order to search images for May 21,
 +58      ;               2008, the inernal value of both parameters should
 +59      ;               be 3080521.
 +60      ;
 +61      ;               If the FROMDATE is after the TODATE, then values of
 +62      ;               the parameters are swapped.
 +63      ; 
 +64      ; [MQUE]        Flags for tasking reports and action on previously
 +65      ;               tasked reports.
 +66      ;               Q  (Default) Queue a new report task or return the status of
 +67      ;                  In Progress for a running report. If previously 
 +68      ;                  ran task is complete, then return report data.
 +69      ;               R  Stop and Requeue a running report with the 
 +70      ;                  same parameters. Existing data collected is removed 
 +71      ;                  from temporary storage.
 +72      ;               D  Stop a running or completed report and delete the data.
 +73      ;
 +74      ; Return Values
 +75      ; =============
 +76      ;     
 +77      ; Zero value of the 1st '^'-piece of the @MAGRESULTS@(0) indicates an
 +78      ; error during execution of the procedure. In this case, the array
 +79      ; is formatted as described in the comments to the RPCERRS^MAGUERR1.
 +80      ;
 +81      ; Otherwise, the array contains the requested data. See description
 +82      ; of the MAGG QUE IMAGE STATISTICS remote procedure for details.
 +83      ;
 +84      ; Notes
 +85      ; =====
 +86      ;
 +87      ; Temporary global nodes ^TMP("MAGGA03Q",$J) and ^XTMP("MAGGA03Q,DUZ")
 +88      ; are used by this procedure.
 +89      ;
STATS(MAGRY,FLAGS,FROMDATE,TODATE,MQUE) ; RPC [MAGG IMAGE STATISTICS QUE]
 +1        NEW MOTH,MERR,MAGXTN,MAGRES,MTDESC,X,Y,RC
 +2        SET MAGRY=$NAME(^TMP("MAGGA03Q",$JOB))
 +3        KILL @MAGRY
 +4        SET (RC,MERR)=0
 +5        DO CLEAR^MAGUERR(1)
 +6       ;
 +7       ;--- Validate FLAGS Parameters
 +8        SET FLAGS=$GET(FLAGS)
           IF $TRANSLATE(FLAGS,"CDESU")'=""
               DO IPVE^MAGUERR("FLAGS")
               SET MERR=1
 +9       ;--- Missing required flag(s)
 +10       IF $TRANSLATE(FLAGS,"DE")=FLAGS
               DO ERROR^MAGUERR(-6,,"D,E")
               SET MERR=1
 +11       IF $TRANSLATE(FLAGS,"SU")=FLAGS
               DO ERROR^MAGUERR(-6,,"S,U")
               SET MERR=1
 +12      ;
 +13      ;--- Validate Date Range
 +14       if $$DTRANGE^MAGUTL03(.FROMDATE,.TODATE)<0
               SET MERR=1
 +15      ;
 +16      ;--- Validate TaskMan Queing parameters
 +17       SET MQUE=$GET(MQUE)
           IF MQUE=""
               SET MQUE="Q"
 +18       IF $TRANSLATE(MQUE,"QRD")'=""
               DO IPVE^MAGUERR("MQUE")
               SET MERR=1
 +19      ;
 +20      ;--- Check if error occurred and quit if so
 +21       IF MERR
               DO ERROR^MAGUERR(-30)
               SET RC=$$FIRSTERR^MAGUERR1()
 +22       IF RC<0
               DO RPCERRS^MAGUERR1(.MAGRY,RC)
               QUIT 
 +23      ;
 +24      ;--- Create unique XTMP node
 +25       SET MAGXTN=$$TNODE(FLAGS,FROMDATE,TODATE)
 +26      ;
 +27      ;--- Resolve previously tasked report
 +28       IF $DATA(^XTMP(MAGXTN,0))
               DO RESOLVE(.RC,MAGXTN,MQUE)
 +29      ;--- If 1 is returned then the report is in progress
 +30       IF +RC=1
               MERGE @MAGRY@(0)=RC
               QUIT 
 +31      ;--- If 2 is returned then the report is complete and is returned
 +32       IF +RC=2
               MERGE @MAGRY@(0)=^XTMP(MAGXTN,"R",0)
               QUIT 
 +33      ;
 +34      ;--- Create the TaskMan parameters and queue the report
 +35       SET MTDESC="Imaging Statistics: "_FLAGS_" "_FROMDATE_" to "_TODATE
 +36       SET MOTH("ZTDTH")=$HOROLOG
 +37      ; p117 T5:  To enable a complete list of reports to be displayed 
 +38      ; in the Client Report list.  If this "I" node isn't set, the report 
 +39      ; will not show up in list until it is run. Could be minutes.  
 +40      ; Added a new status to the Delphi Window : 'Queuing'.  
 +41      ; 'Queuing' status will account for the time between the Task being
 +42      ;  created and the job being run.  The Queuing below isn't used. It
 +43      ;  is replaced with the TaskMan Task number later.
 +44      ;  Delphi uses $p(3) = '' (start time) to determine 'Queuing', and not Running.
 +45       SET ^XTMP(MAGXTN,"I",0)="Queuing"_U_$GET(DUZ)_U_U
 +46      ;
 +47       SET MAGRES=$$NODEV^XUTMDEVQ("TASK^MAGGA03Q",MTDESC,"FLAGS;FROMDATE;TODATE;MAGXTN",.MOTH)
 +48      ;--- Save thru date ^ create date ^ Task Number
 +49       SET ^XTMP(MAGXTN,0)=$$FMADD^XLFDT(DT,1)_U_DT_U_MAGRES
 +50      ;--- Save user cross reference node for report lookup by user
 +51       SET ^XTMP("MAGGA03Q",DUZ,MAGXTN)=""
 +52      ;--- Return successful queued report notification with Task ID
 +53       SET @MAGRY@(0)="1^Report Queued on Task ID: "_MAGRES
 +54       QUIT 
 +55      ;
TASK      ;
 +1        NEW MAGXTN,MAGRES
 +2        SET MAGXTN=$$TNODE(FLAGS,FROMDATE,TODATE)
 +3       ;--- Save Internal Data as follows
 +4       ;--- ^XTMP($$TNODE,"I",0)=Task Number^User ID^Start Date/Time^Complete Date/Time
 +5        SET ^XTMP(MAGXTN,"I",0)=$GET(ZTSK)_U_$GET(DUZ)_U_$$NOW^XLFDT_U
 +6       ;--- Collect Report Data from Imaging API
 +7       ; GEK BOOKMARK 1
           DO IMGQUERY^MAGGA03(.MAGRES,FLAGS,FROMDATE,TODATE)
 +8       ;P122 gek 
 +9       ;  If the variable ZSTOP = '1', then the TASK/Report was stopped,
 +10      ;  or if the Report's User Index: ^XTMP("MAGGAO3Q",DUZ,MAGXTN)  
 +11      ;  does not exist, the report was stopped.
 +12      ;  We only save report Data if report was not Stopped.
 +13      ;  This Fixes the issue of not being able to Re-Run a report that
 +14      ;  was previously canceled while it was still running.
 +15      ;  
 +16       IF '$GET(ZTSTOP)
               IF ($DATA(^XTMP("MAGGA03Q",DUZ,MAGXTN)))
                   Begin DoDot:1
 +17      ;--- Save Completed date/time of Report
 +18                   SET $PIECE(^XTMP(MAGXTN,"I",0),U,4)=$$NOW^XLFDT
 +19      ;--- Update the Save Through date to midnight
 +20                   SET $PIECE(^XTMP(MAGXTN,0),U,1)=$$FMADD^XLFDT($$DT^XLFDT(),1)
 +21      ;--- Save Report Data in temporary storage
 +22                   MERGE ^XTMP(MAGXTN,"R")=@MAGRES
 +23                   QUIT 
                   End DoDot:1
 +24       QUIT 
 +25      ;
 +26      ; Returns status of existing report based on MQUE flag
RESOLVE(RY,MAGXTN,MQUE) ;
 +1       ; if task is finished, then return the data.
 +2       ; Q flag will return a completed report or an In Progress status if still running
 +3        IF MQUE="Q"
               Begin DoDot:1
 +4                SET X=$PIECE($GET(^XTMP(MAGXTN,"I",0)),U,4)
 +5       ;/p117 T5 gek-  add $G above to stop <undef>. 
 +6       ; Occurred rarely (pre T5) when report is Re-Run. CodeCR731
 +7                IF X=""
                       SET RY="1^In Progress"
                       QUIT 
 +8                SET RY="2^Report Complete"
 +9                MERGE @MAGRY=^XTMP(MAGXTN,"R")
 +10               QUIT 
               End DoDot:1
               QUIT 
 +11      ; R flag will Stop and Requeue a running report with same parameters
 +12       IF MQUE="R"
               Begin DoDot:1
 +13               NEW MAGSTP,ZTSK
 +14               SET ZTSK=$$GETTASK(MAGXTN)
 +15      ; Try to stop the task if it's currently running
 +16               SET MAGSTP=$$ASKSTOP^%ZTLOAD(ZTSK)
 +17               IF 'MAGSTP
                       SET RY="1^Report cannot be stopped. Try again later"
                       QUIT 
 +18      ;
 +19               DO STAT^%ZTLOAD
 +20               IF 'ZTSK(0)
                       SET RY="0^Task is undefined"
                       Begin DoDot:2
 +21      ;Kill Report Data and Report's User Index
 +22                       KILL ^XTMP(MAGXTN),^XTMP("MAGGA03Q",DUZ,MAGXTN)
 +23                       SET RY="0^Okay to retask"
 +24                       QUIT 
                       End DoDot:2
                       QUIT 
 +25               IF ZTSK(1)<3
                       SET RY="1^Task In Progress : "_ZTSK
                       QUIT 
 +26      ;I ZTSK(1) is either 4 or 5,  both mean not a running task. Inactive. problem
 +27      ;Kill Report Data and Report's User Index
 +28               KILL ^XTMP(MAGXTN),^XTMP("MAGGA03Q",DUZ,MAGXTN)
 +29               SET RY="0^Okay to retask"
 +30               QUIT 
               End DoDot:1
               QUIT 
 +31      ; D flag will delete a previously ran report and stop a currently running task
 +32       IF MQUE="D"
               Begin DoDot:1
 +33               NEW MAGSTP,ZTSK
 +34               SET ZTSK=$$GETTASK(MAGXTN)
 +35               SET MAGSTP=$$ASKSTOP^%ZTLOAD(ZTSK)
 +36               IF 'MAGSTP
                       SET RY="1^Report cannot be stopped. Try again later"
                       QUIT 
 +37      ;Kill Report Data and Report's User Index
 +38               KILL ^XTMP(MAGXTN),^XTMP("MAGGA03Q",DUZ,MAGXTN)
 +39               SET RY="1^Report data deleted"
               End DoDot:1
               QUIT 
 +40       QUIT 
 +41      ;
 +42      ;***** RETURNS VARIOUS IMAGE STATISTICS DATA
 +43      ; RPC: MAGG IMAGE STATISTICS BY USER
 +44      ;
 +45      ; Return all statistics reports previously tasked for a user
 +46      ;
 +47      ; .MAGRY        Reference to a local variable where the results
 +48      ;               are returned to.
 +49      ; 
 +50      ; MAGDUZ        Internal ID of a user that has previously queued an Image Statistics 
 +51      ;               Report.
 +52      ;               The default value is the current user DUZ.
 +53      ;
 +54      ; Return Values
 +55      ; =============
 +56      ; 
 +57      ; MAGRY(0) -  ^01: 1 Successful execution of the remote procedure
 +58      ;                  0 An error occurred during the execution of the remote procedure
 +59      ;
 +60      ;             ^02: The number of reports identified for the user
 +61      ;
 +62      ; MAGRY(1..n) ^01: report FLAGS parameter
 +63      ;             ^02: report FROMDATE parameter
 +64      ;             ^03: report TODATE parameter
 +65      ;             ^04: report REPORT START DATE/TIME parameter
 +66      ;             ^05: report REPORT COMPLETE DATE/TIEM parameter
 +67      ; 
 +68      ; e.g.
 +69      ; 0)=1^5 Reports found for user IMAGING,USER
 +70      ; 1)=CDE^2900613^3100503^3100505.09053^3100505.09053
 +71      ;
GETUSRPT(MAGRY,MAGDUZ) ; RPC [MAGG IMAGE STATISTICS BY USER]
 +1        NEW MAGX,MAGCNT
 +2       ; XTMP node information.
           NEW MAGINF
 +3        IF MAGDUZ=""
               SET MAGDUZ=DUZ
 +4       ;--- Delete yesterdays temp data in ^XTMP
 +5        DO CLEARTMP(MAGDUZ)
 +6       ;
 +7        SET MAGRY=$NAME(^TMP("MAGGUSRPT",$JOB))
           SET (MAGX,MAGCNT)=0
 +8        KILL @MAGRY
 +9        FOR 
               SET MAGX=$ORDER(^XTMP("MAGGA03Q",MAGDUZ,MAGX))
               if MAGX=""
                   QUIT 
               Begin DoDot:1
 +10               IF '$DATA(^XTMP(MAGX,"I",0))
                       QUIT 
 +11               SET MAGCNT=MAGCNT+1
 +12               SET MAGINF=$GET(^XTMP(MAGX,"I",0))
 +13      ; Status of 'Queuing' is now set from the Delphi App, if the start time is '' (null).
 +14      ; Do not change next line. Any change causes list entries to not be displayed.
 +15               SET @MAGRY@(MAGCNT)=$PIECE(MAGX,"-",3)_U_$PIECE(MAGX,"-",4)_U_$PIECE(MAGX,"-",5)_U_$PIECE(MAGINF,U,3)_U_$PIECE(MAGINF,U,4)
               End DoDot:1
 +16       SET @MAGRY@(0)="1^"_MAGCNT_$SELECT(MAGCNT>1:" Reports ",1:" Report ")_"found for user "_$$GET1^DIQ(200,MAGDUZ_",",.01)
 +17       QUIT 
 +18      ;
 +19      ; Get unique XTMP node
 +20      ; Namespace + User id + Flags + From Date + To Date
TNODE(FLAG,FROMDT,TODT) ;
 +1       ;/p117 T5 GEK  THIS IS 'maggaO3q' (LETTER 'O') It should be 'magga03q' (Zero) 
 +2       ;Q "MAGGAO3Q"_"-"_DUZ_"-"_FLAG_"-"_FROMDT_"-"_TODT ; this had letter 'o'
 +3       ; this is Zero 
           QUIT "MAGGA03Q"_"-"_DUZ_"-"_FLAG_"-"_FROMDT_"-"_TODT
 +4       ;
 +5       ; Returns the Task Number from XTMP global
 +6       ; TNODE = Value created in $$TNODE
GETTASK(TNODE) ;
 +1        QUIT $SELECT('$DATA(^XTMP(TNODE,0)):0,1:+$PIECE(^XTMP(TNODE,0),U,3))
 +2       ;
 +3       ; Delete temp data from yesterday
 +4       ; 
CLEARTMP(MAGDUZ) ; Delete temp data from yesterday
 +1        NEW MAGDAT,MAGXTN
 +2        SET MAGXTN=""
 +3        FOR 
               SET MAGXTN=$ORDER(^XTMP("MAGGA03Q",MAGDUZ,MAGXTN))
               if MAGXTN=""
                   QUIT 
               Begin DoDot:1
 +4                SET MAGDAT=$PIECE($GET(^XTMP(MAGXTN,0)),U,2)
 +5       ; delete all data if created date is before today 
                   IF MAGDAT<DT
                       Begin DoDot:2
 +6                        NEW MAGSTP,ZTSK
 +7                        SET ZTSK=$$GETTASK(MAGXTN)
 +8       ; Try to stop the task if it's currently running
 +9                        SET MAGSTP=$$ASKSTOP^%ZTLOAD(ZTSK)
 +10      ; Report cannot be stopped
                           IF 'MAGSTP
                               QUIT 
 +11                       KILL ^XTMP(MAGXTN),^XTMP("MAGGA03Q",$PIECE(MAGXTN,"-",2),MAGXTN)
 +12                       QUIT 
                       End DoDot:2
 +13               QUIT 
               End DoDot:1
 +14       QUIT 
 +15      ;*****  CLNXTMP  
 +16      ;  Clean XTMP nodes. 
 +17      ;  Due to an error (rare), there may be some orphaned 
 +18      ;  report data in the XTMP global.  This routine will
 +19      ;  clear out any orphaned XTMP nodes for the MAGGA03Q
 +20      ;  and MAGGAO3Q nodes of the QA Statistics Reports.
 +21      ;  It won't hurt valid reports.  Valid reports are pointed 
 +22      ;  to from the XTMP("MAGGA03Q",DUZ,xxxx) cross reference.
CLNXTMP   ;
 +1       ; 0 = zero   O = letter O
 +2       ; make a list of the valid XTMP Nodes for Reports. These are referenced by the 
 +3       ; cross ref :    XTMP('MAGGA03Q',duz,xnode)
 +4        NEW MAGXTN,MAGDUZ,MWIN
 +5        SET MAGDUZ=""
 +6        KILL ^TMP($JOB,"MAGXTMP")
 +7       ; for all users, save reference to their Report Nodes.
 +8        FOR 
               SET MAGDUZ=$ORDER(^XTMP("MAGGA03Q",MAGDUZ))
               if MAGDUZ=""
                   QUIT 
               Begin DoDot:1
 +9                SET MAGXTN=""
 +10               FOR 
                       SET MAGXTN=$ORDER(^XTMP("MAGGA03Q",MAGDUZ,MAGXTN))
                       if MAGXTN=""
                           QUIT 
                       Begin DoDot:2
 +11      ; save list of valid XTMP Nodes.
                           SET ^TMP($JOB,"MAGXTMP",MAGXTN)=""
 +12                       QUIT 
                       End DoDot:2
 +13               QUIT 
               End DoDot:1
 +14      ; The orphaned XTMP Node used 'O' (letter),  and '0' (Zero)
 +15      ; We'll delete any nodes that aren't referenced by 
 +16      ; the cross Ref :    XTMP('MAGGA03Q',duz,xnode)
 +17       NEW XNODE,TNODE,DONE
 +18       SET DONE=0
 +19       FOR XNODE="MAGGAO3Q","MAGGA03Q"
               Begin DoDot:1
 +20               SET TNODE=XNODE
 +21               SET DONE=0
 +22               FOR 
                       SET XNODE=$ORDER(^XTMP(XNODE))
                       Begin DoDot:2
 +23      ; Quit if node is beyond QA Report Nodes.
                           IF $EXTRACT(XNODE,1,8)'=TNODE
                               SET DONE=1
                               QUIT 
 +24      ; This is Valid, we won't delete.
                           IF $DATA(^TMP($JOB,"MAGXTMP",XNODE))
                               QUIT 
 +25      ; kill the orphaned node.
 +26                       KILL ^XTMP(XNODE)
 +27                       QUIT 
                       End DoDot:2
                       if DONE
                           QUIT 
 +28               QUIT 
               End DoDot:1
 +29      ; clean up 
           KILL ^TMP($JOB,"MAGXTMP")
 +30       QUIT