*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   
*
*  2015-11-06 
*  2015-11-07  vysledky se zaznamenavaji do dbf, opakovane uspesne requesty s kratkou dobou obsluhy se pisi na 1 radek
*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   

#DEFINE CRLF CHR(13)+CHR(10)

DO (LOWER(JUSTPATH(sys(16))) + "\http_ppgm")

#DEFINE LOG_FILENAME m.gchom_path + "test_datastore"
#DEFINE CASOVY_LIMIT 2.0
#DEFINE PERIODA_MERENI 5

DIMENSION hranice(11)
hranice(1) =  0.02
hranice(2) =  0.04
hranice(3) =  0.06
hranice(4) =  0.10
hranice(5) =  0.20
hranice(6) =  0.50
hranice(7) =  2.00 
hranice(8) =  4.00
hranice(9) =  9.99

IF .not.  FILE(LOG_FILENAME + ".dbf")
    CREATE TABLE (LOG_FILENAME) (cas C(19), status C(33), trvani Y, ;
           pocet1 I, pocet2 I, pocet3 I, pocet4 I, pocet5 I, pocet6 I, pocet7 I, pocet8 I, pocet9 I, pocet_nad I, comment C(111))
    USE 
ENDIF  

DO WHILE .t.
    tt_trvani = SECONDS()
    json = http_get("current_time")
    http_status = get_string_item(m.json, "http_status")
    DO zaznamenej WITH m.http_status, SECONDS() - m.tt_trvani, m.json
    IF INKEY(PERIODA_MERENI) = 113 && male "q"
        EXIT 
    ENDIF 
    DO generuj_vysledek
    CLOSE DATABASES 
ENDDO 
WAIT "hotovo" WINDOW AT 22,22 TIMEOUT 2
CLEAR 


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
PROCEDURE zaznamenej
LPARAMETERS status, trvani, json
? STR(m.trvani, 7,3), m.status
cas = TTOC(DATETIME(),3)
USE (LOG_FILENAME) ALIAS logfile
IF  "200" $ m.status .and. "OK" $ m.status .and. m.trvani < CASOVY_LIMIT
      *      zapis do spolecneho radku
    LOCATE FOR cas = LEFT(m.cas, 13) + "  "
    IF .not. FOUND()
        APPEND BLANK 
        REPLACE cas WITH LEFT(m.cas, 13), status WITH m.status
    ENDIF 

ELSE 
    APPEND BLANK 
    REPLACE cas WITH m.cas, status WITH m.status
ENDIF 
DO zapis_trvani WITH m.trvani, m.json
USE IN logfile


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
PROCEDURE zapis_trvani
LPARAMETERS trvani, json
sloupec = ""
FOR ii = 1 TO 8
    IF m.trvani < hranice(m.ii)
        sloupec = "pocet" + STR(m.ii,1)
        REPLACE trvani WITH m.trvani, &sloupec WITH &sloupec + 1, comment WITH zkrat_string(m.json)
        EXIT 
    ENDIF 
ENDFOR 
IF EMPTY(sloupec)
    REPLACE trvani WITH m.trvani, pocet9 WITH pocet9 + 1, comment WITH zkrat_string(m.json)
ENDIF 


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
FUNCTION zkrat_string (retezec)
pomm = STRTRAN(m.retezec, CHR(13), " ")
pomm = STRTRAN(m.pomm,    CHR(10), " ")
pomm = STRTRAN(m.pomm,    CHR(9) , " ")
pomm = STRTRAN(m.pomm,    "   "  , " ")
pomm = STRTRAN(m.pomm,    "  "   , " ")
pomm = STRTRAN(m.pomm,    "  "   , " ")
RETURN m.pomm


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
PROCEDURE generuj_vysledek
had = CREATEOBJECT("had", "Generovani reportu .html")
had.SHOW()
SELECT  cas, status, trvani, comment, ;
        pocet1, pocet2, pocet3, pocet4, pocet5, pocet6, pocet7, pocet8, pocet9, ;
        pocet1+ pocet2+ pocet3+ pocet4+ pocet5+ pocet6+ pocet7+ pocet8+ pocet9 as pocet, pocet1 as pocet_max, ;
        "    " as class1, "    " as class2, "    " as class3, "    " as class4, "    " as class5, "    " as class6, "    " as class7, "    " as class8, "    " as class9 ;
        FROM (LOG_FILENAME) INTO TABLE (LOG_FILENAME + "_procenta")
had.set_had(33)
REPLACE ALL pocet1 WITH ROUND(pocet1 / pocet * 100, 0), ;
            pocet2 WITH ROUND(pocet2 / pocet * 100, 0), ;
            pocet3 WITH ROUND(pocet3 / pocet * 100, 0), ;
            pocet4 WITH ROUND(pocet4 / pocet * 100, 0), ;
            pocet5 WITH ROUND(pocet5 / pocet * 100, 0), ;
            pocet6 WITH ROUND(pocet6 / pocet * 100, 0), ;
            pocet7 WITH ROUND(pocet7 / pocet * 100, 0), ;
            pocet8 WITH ROUND(pocet8 / pocet * 100, 0), ;
            pocet9 WITH ROUND(pocet9 / pocet * 100, 0)
REPLACE FOR "200" $ status .and. "OK" $ status comment WITH "", trvani WITH 0, status WITH ""
REPLACE ALL pocet_max WITH MAX( pocet1, pocet2, pocet3, pocet4, pocet5, pocet6, pocet7, pocet8, pocet9 ) * .9
had.set_had(77)
FOR ii = 1 TO 9
    cislo = STR(m.ii,1)
    REPLACE FOR BETWEEN(pocet&cislo, pocet_max, 99)  class&cislo WITH "fat_"
    REPLACE FOR BETWEEN(pocet&cislo, 1, pocet_max/4) class&cislo WITH "min_"
ENDFOR 
had.set_had(88)
DO html_ppgm
USE 
RELEASE had

