*
* This program creates a rank list of the most annoyed users (because of 
* performance) of a specific day.
* A user is more annoyed if he has to wait a lot (average response time is high)
* many times (high number of dialog steps).
*
REPORT ZUSRANOY LINE-SIZE 120.

DATA: BEGIN OF APPLICATION_STATISTIC OCCURS 200.
        INCLUDE STRUCTURE SAPWLUSTCX.
DATA: END OF APPLICATION_STATISTIC.
DATA: BEGIN OF INSTANCES OCCURS 10.
        INCLUDE STRUCTURE RZLLIAPSRV.
DATA: END OF INSTANCES.
DATA: AVG_RESPTIM TYPE I, DEST(8), ANNOYANCE TYPE F, IANOY TYPE I, I
      TYPE I.
DATA: BEGIN OF RESULT OCCURS 100,
    ACCOUNT LIKE APPLICATION_STATISTIC-ACCOUNT,
    ENTRY_ID LIKE APPLICATION_STATISTIC-ENTRY_ID,
    DCOUNT LIKE APPLICATION_STATISTIC-DCOUNT,
    RESPTI LIKE APPLICATION_STATISTIC-RESPTI,
    AVG_RESPTIM TYPE I,
    IANOY TYPE I,
END OF RESULT.

PARAMETERS: DATE TYPE D DEFAULT SY-DATUM.

CALL FUNCTION 'SMLG_GET_DEFINED_SERVERS'
     TABLES: INSTANCES = INSTANCES.

INSTANCES = 'db_server_host_name'.
APPEND INSTANCES.

LOOP AT INSTANCES.
  DEST = INSTANCES.
  CALL FUNCTION 'SAPWL_WORKLOAD_GET_STATISTIC' 
       EXPORTING
            PERIODTYPE            = 'D'
            HOSTID                = DEST
            STARTDATE             = DATE
       TABLES
            APPLICATION_STATISTIC = APPLICATION_STATISTIC
       EXCEPTIONS
            NO_DATA_FOUND         = 1.

  LOOP AT APPLICATION_STATISTIC.
    IF APPLICATION_STATISTIC-TTYPE = '01'
      AND APPLICATION_STATISTIC-ACCOUNT <> 'SAP*'
      AND APPLICATION_STATISTIC-ACCOUNT <> 'SAPSYS'
      IF APPLICATION_STATISTIC-ENTRY_ID <> 'CPIC/RFC'
          AND APPLICATION_STATISTIC-ENTRY_ID <> 'Login_Pw'
          AND APPLICATION_STATISTIC-ENTRY_ID <> 'MainMenu'
          AND APPLICATION_STATISTIC-ENTRY_ID <> 'Logoff'
          AND APPLICATION_STATISTIC-ENTRY_ID <> 'SAPMSYST'.
        AVG_RESPTIM =
        APPLICATION_STATISTIC-RESPTI / APPLICATION_STATISTIC-DCOUNT.
        MOVE-CORRESPONDING APPLICATION_STATISTIC TO RESULT.
        IF AVG_RESPTIM < 1500. AVG_RESPTIM = 0. ENDIF.
        RESULT-AVG_RESPTIM = AVG_RESPTIM.
        ANNOYANCE =  AVG_RESPTIM * AVG_RESPTIM *
     AVG_RESPTIM  *  AVG_RESPTIM  * APPLICATION_STATISTIC-DCOUNT *
     APPLICATION_STATISTIC-DCOUNT * APPLICATION_STATISTIC-DCOUNT /
     100000000000000000.
        IANOY = ANNOYANCE.
        RESULT-IANOY = IANOY.
        APPEND RESULT.
      ENDIF.
    ENDIF.
  ENDLOOP.
ENDLOOP.
SORT RESULT BY IANOY DESCENDING.
WRITE: / 'USER' COLOR 4,
          8 'PROGRAM/TRANSACTION' COLOR 4,
          30 'DIALOG STEP #' COLOR 4,
          46 'AVRG RESP TIME' COLOR 4,
          64 'ANNOYANCE FACTOR' COLOR 4.
SKIP.


LOOP AT RESULT.
  I = I + 1.
  WRITE: / RESULT-ACCOUNT, RESULT-ENTRY_ID, 25(15) RESULT-DCOUNT,
    47 RESULT-AVG_RESPTIM , 64 RESULT-IANOY.
  IF I >= 30. EXIT. ENDIF.
ENDLOOP.