Graf zobrazuje skutečná data – Počty útoků za den
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun Mar 01 13:04:10 2009 +0100 (2009-03-01)
changeset 40fd29fd5a8e29
parent 39 be6383d38a3a
child 41 d41dc2f3380d
Graf zobrazuje skutečná data – Počty útoků za den
java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/DenniSouhrn.java
java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/dao/SouhrnDAO.java
java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/dao/SouhrnDAO.sql.xml
java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/dto/Cil.java
java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/dto/Soucet.java
java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/grafy/CasovyGrafUtoku.java
java/HrisniciSpameri/web/graf.jspx
java/HrisniciSpameri/web/index.jsp
java/HrisniciSpameri/web/souhrn.jspx
     1.1 --- a/java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/DenniSouhrn.java	Sun Mar 01 01:08:24 2009 +0100
     1.2 +++ b/java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/DenniSouhrn.java	Sun Mar 01 13:04:10 2009 +0100
     1.3 @@ -3,6 +3,7 @@
     1.4  import cz.frantovo.hrisniciSpameri.dao.CilDAO;
     1.5  import cz.frantovo.hrisniciSpameri.dao.SouhrnDAO;
     1.6  import cz.frantovo.hrisniciSpameri.dto.Cil;
     1.7 +import cz.frantovo.hrisniciSpameri.dto.Soucet;
     1.8  import cz.frantovo.hrisniciSpameri.dto.Souhrn;
     1.9  import java.io.Serializable;
    1.10  import java.sql.Date;
    1.11 @@ -11,6 +12,7 @@
    1.12  import java.text.SimpleDateFormat;
    1.13  import java.util.Calendar;
    1.14  import java.util.Collection;
    1.15 +import java.util.HashMap;
    1.16  import java.util.logging.Level;
    1.17  import java.util.logging.Logger;
    1.18  
    1.19 @@ -89,6 +91,10 @@
    1.20          }
    1.21      }
    1.22  
    1.23 +    public String getKonecString() {
    1.24 +        return formatData.format(getKonec());
    1.25 +    }
    1.26 +
    1.27      public void setCilString(String cil) {
    1.28          try {
    1.29              this.cil = Integer.parseInt(cil);
    1.30 @@ -119,6 +125,11 @@
    1.31          setKonec(getVychoziKonec());
    1.32      }
    1.33  
    1.34 +    /**
    1.35 +     * @return Denní souhrny útoků –
    1.36 +     * obsahuje IP adresy a domény útočníků,
    1.37 +     * datum je den poslendího útoku daného útočníka na daný cíl.
    1.38 +     */
    1.39      public Collection<Souhrn> getSouhrn() {
    1.40          Collection<Souhrn> souhrn = new SouhrnDAO().getSouhrn(zacatek, konec, cil);
    1.41  
    1.42 @@ -132,6 +143,25 @@
    1.43      }
    1.44  
    1.45      /**
    1.46 +     * @return Vyšší stupeň agregace než souhrn –
    1.47 +     * obsahuje jen součty útoků pro daný den a daný cíl,
    1.48 +     * neobsahuje podrobnosti o útočnících (ip adresa, doména).
    1.49 +     */
    1.50 +    public HashMap<Cil, Collection<Soucet>> getSoucty() {
    1.51 +        HashMap<Cil, Collection<Soucet>> soucty = new SouhrnDAO().getSoucty(zacatek, konec, cil);
    1.52 +
    1.53 +        /** ne všechno musíme posílat do JSP vrstvy */
    1.54 +        for (Collection<Soucet> ss : soucty.values()) {
    1.55 +            for (Soucet s : ss) {
    1.56 +                s.getCil().setDatabaze(null);
    1.57 +                s.getCil().setSelekt(null);
    1.58 +            }
    1.59 +        }
    1.60 +
    1.61 +        return soucty;
    1.62 +    }
    1.63 +
    1.64 +    /**
    1.65       * @return Seznam všech cílů. Ale bez těchto hodnot: databáze a selekt.
    1.66       */
    1.67      public Collection<Cil> getSeznamCilu() {
     2.1 --- a/java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/dao/SouhrnDAO.java	Sun Mar 01 01:08:24 2009 +0100
     2.2 +++ b/java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/dao/SouhrnDAO.java	Sun Mar 01 13:04:10 2009 +0100
     2.3 @@ -2,6 +2,7 @@
     2.4  
     2.5  import cz.frantovo.hrisniciSpameri.dto.Cil;
     2.6  import cz.frantovo.hrisniciSpameri.dto.SitovaAdresa;
     2.7 +import cz.frantovo.hrisniciSpameri.dto.Soucet;
     2.8  import cz.frantovo.hrisniciSpameri.dto.Souhrn;
     2.9  import java.sql.Connection;
    2.10  import java.sql.Date;
    2.11 @@ -9,6 +10,7 @@
    2.12  import java.sql.ResultSet;
    2.13  import java.util.ArrayList;
    2.14  import java.util.Collection;
    2.15 +import java.util.HashMap;
    2.16  import java.util.logging.Level;
    2.17  
    2.18  /**
    2.19 @@ -21,8 +23,9 @@
    2.20  
    2.21          DENNI_SOUHRN,
    2.22          DENNI_SOUHRN_CIL,
    2.23 +        DENNI_SOUCTY,
    2.24 +        DENNI_SOUCTY_CIL
    2.25      }
    2.26 -    private static final long DEN = 1000 * 60 * 60 * 24;
    2.27  
    2.28      /**
    2.29       * Získá z databáze souhrn útoků. Buď pro konkrétní den nebo pro dané období.
    2.30 @@ -81,4 +84,63 @@
    2.31  
    2.32          return souhrn;
    2.33      }
    2.34 +
    2.35 +    /**
    2.36 +     * @param zacatek začátek období nebo konkrétní den
    2.37 +     * @param konec konec období nebo null (pokud nás zajímá konkrétní den – což nás tady většinou nezajímá)
    2.38 +     * @param cil id cíle nebo nula, pokud chceme všechny cíle
    2.39 +     * @return součty útoků za jednotlivé dny, seskupené podle cílů
    2.40 +     */
    2.41 +    public HashMap<Cil, Collection<Soucet>> getSoucty(Date zacatek, Date konec, int cil) {
    2.42 +        HashMap<Cil, Collection<Soucet>> soucty = new HashMap<Cil, Collection<Soucet>>();
    2.43 +
    2.44 +        Connection db = getSpojeni(null);
    2.45 +        if (db == null) {
    2.46 +            return null;
    2.47 +        } else {
    2.48 +            PreparedStatement ps = null;
    2.49 +            ResultSet rs = null;
    2.50 +            try {
    2.51 +                if (konec == null) {
    2.52 +                    konec = zacatek;
    2.53 +                }
    2.54 +
    2.55 +                if (cil == 0) {
    2.56 +                    ps = db.prepareStatement(getSQL(SQL.DENNI_SOUCTY));
    2.57 +                    ps.setDate(1, zacatek);
    2.58 +                    ps.setDate(2, konec);
    2.59 +                } else {
    2.60 +                    ps = db.prepareStatement(getSQL(SQL.DENNI_SOUCTY_CIL));
    2.61 +                    ps.setDate(1, zacatek);
    2.62 +                    ps.setDate(2, konec);
    2.63 +                    ps.setInt(3, cil);
    2.64 +                }
    2.65 +                rs = ps.executeQuery();
    2.66 +                while (rs.next()) {
    2.67 +                    Soucet s = new Soucet();
    2.68 +                    s.setDen(rs.getDate("den"));
    2.69 +                    s.setPocet(rs.getInt("pocet"));
    2.70 +
    2.71 +                    Cil c = new Cil();
    2.72 +                    c.setId(rs.getInt("id"));
    2.73 +                    c.setNazev(rs.getString("nazev"));
    2.74 +                    c.setUrl(rs.getString("url"));
    2.75 +                    c.setSelekt(rs.getString("selekt"));
    2.76 +                    c.setDatabaze(rs.getString("databaze"));
    2.77 +                    s.setCil(c);
    2.78 +
    2.79 +                    if (soucty.get(c) == null) {
    2.80 +                        soucty.put(c, new ArrayList<Soucet>());
    2.81 +                    }
    2.82 +
    2.83 +                    soucty.get(c).add(s);
    2.84 +                }
    2.85 +            } catch (Exception e) {
    2.86 +                log.log(Level.SEVERE, "Chyba při získávání součtů z databáze", e);
    2.87 +            } finally {
    2.88 +                zavri(db, ps, rs);
    2.89 +            }
    2.90 +        }
    2.91 +        return soucty;
    2.92 +    }
    2.93  }
    2.94 \ No newline at end of file
     3.1 --- a/java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/dao/SouhrnDAO.sql.xml	Sun Mar 01 01:08:24 2009 +0100
     3.2 +++ b/java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/dao/SouhrnDAO.sql.xml	Sun Mar 01 13:04:10 2009 +0100
     3.3 @@ -42,4 +42,39 @@
     3.4          ORDER BY max(den) DESC
     3.5          ]]>
     3.6      </entry>
     3.7 +    <entry key="DENNI_SOUCTY">
     3.8 +        <![CDATA[
     3.9 +        SELECT  den,
    3.10 +                sum(pocet) AS pocet,
    3.11 +                cil,
    3.12 +                id,
    3.13 +                nazev,
    3.14 +                url,
    3.15 +                selekt,
    3.16 +                databaze
    3.17 +        FROM    souhrn
    3.18 +                JOIN cil ON (cil.id = souhrn.cil)
    3.19 +        WHERE   den >= date_trunc('day', ?::date)
    3.20 +                AND den <= date_trunc('day', ?::date)
    3.21 +        GROUP BY den, cil, id, nazev, url, selekt, databaze
    3.22 +        ]]>
    3.23 +    </entry>
    3.24 +    <entry key="DENNI_SOUCTY_CIL">
    3.25 +        <![CDATA[
    3.26 +        SELECT  den,
    3.27 +                sum(pocet) AS pocet,
    3.28 +                cil,
    3.29 +                id,
    3.30 +                nazev,
    3.31 +                url,
    3.32 +                selekt,
    3.33 +                databaze
    3.34 +        FROM    souhrn
    3.35 +                JOIN cil ON (cil.id = souhrn.cil)
    3.36 +        WHERE   den >= date_trunc('day', ?::date)
    3.37 +                AND den <= date_trunc('day', ?::date)
    3.38 +                AND cil = ?
    3.39 +        GROUP BY den, cil, id, nazev, url, selekt, databaze
    3.40 +        ]]>
    3.41 +    </entry>
    3.42  </properties>
    3.43 \ No newline at end of file
     4.1 --- a/java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/dto/Cil.java	Sun Mar 01 01:08:24 2009 +0100
     4.2 +++ b/java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/dto/Cil.java	Sun Mar 01 13:04:10 2009 +0100
     4.3 @@ -65,4 +65,25 @@
     4.4  
     4.5      public Cil() {
     4.6      }
     4.7 +
     4.8 +    @Override
     4.9 +    public boolean equals(Object obj) {
    4.10 +        if (obj instanceof Cil) {
    4.11 +            Cil c = (Cil) obj;
    4.12 +            return c.getId() == getId();
    4.13 +        } else {
    4.14 +            return false;
    4.15 +        }
    4.16 +    }
    4.17 +
    4.18 +    @Override
    4.19 +    public int hashCode() {
    4.20 +        int hash = 5;
    4.21 +        hash = 67 * hash + this.id;
    4.22 +        hash = 67 * hash + (this.nazev != null ? this.nazev.hashCode() : 0);
    4.23 +        hash = 67 * hash + (this.url != null ? this.url.hashCode() : 0);
    4.24 +        hash = 67 * hash + (this.selekt != null ? this.selekt.hashCode() : 0);
    4.25 +        hash = 67 * hash + (this.databaze != null ? this.databaze.hashCode() : 0);
    4.26 +        return hash;
    4.27 +    }
    4.28  }
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/dto/Soucet.java	Sun Mar 01 13:04:10 2009 +0100
     5.3 @@ -0,0 +1,41 @@
     5.4 +package cz.frantovo.hrisniciSpameri.dto;
     5.5 +
     5.6 +import java.sql.Date;
     5.7 +
     5.8 +/**
     5.9 + * Ještě větší agregace než Souhrn.
    5.10 + * Obsahuje jen součet útoků na danou doménu pro daný den.
    5.11 + * Používá se pro zobrazení grafu.
    5.12 + * @see Souhrn
    5.13 + * @author fiki
    5.14 + */
    5.15 +public class Soucet {
    5.16 +
    5.17 +    private Date den;
    5.18 +    private int pocet;
    5.19 +    private Cil cil;
    5.20 +
    5.21 +    public Date getDen() {
    5.22 +        return den;
    5.23 +    }
    5.24 +
    5.25 +    public void setDen(Date den) {
    5.26 +        this.den = den;
    5.27 +    }
    5.28 +
    5.29 +    public int getPocet() {
    5.30 +        return pocet;
    5.31 +    }
    5.32 +
    5.33 +    public void setPocet(int pocet) {
    5.34 +        this.pocet = pocet;
    5.35 +    }
    5.36 +
    5.37 +    public Cil getCil() {
    5.38 +        return cil;
    5.39 +    }
    5.40 +
    5.41 +    public void setCil(Cil cil) {
    5.42 +        this.cil = cil;
    5.43 +    }
    5.44 +}
     6.1 --- a/java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/grafy/CasovyGrafUtoku.java	Sun Mar 01 01:08:24 2009 +0100
     6.2 +++ b/java/HrisniciSpameri/src/java/cz/frantovo/hrisniciSpameri/grafy/CasovyGrafUtoku.java	Sun Mar 01 13:04:10 2009 +0100
     6.3 @@ -1,12 +1,15 @@
     6.4  package cz.frantovo.hrisniciSpameri.grafy;
     6.5  
     6.6 +import cz.frantovo.hrisniciSpameri.DenniSouhrn;
     6.7 +import cz.frantovo.hrisniciSpameri.dto.Soucet;
     6.8  import de.laures.cewolf.DatasetProduceException;
     6.9  import de.laures.cewolf.DatasetProducer;
    6.10  import de.laures.cewolf.links.CategoryItemLinkGenerator;
    6.11  import de.laures.cewolf.tooltips.CategoryToolTipGenerator;
    6.12  import java.io.Serializable;
    6.13 -import java.util.Calendar;
    6.14 +import java.util.Collection;
    6.15  import java.util.Date;
    6.16 +import java.util.HashMap;
    6.17  import java.util.Map;
    6.18  import org.jfree.data.category.CategoryDataset;
    6.19  import org.jfree.data.time.Day;
    6.20 @@ -22,22 +25,30 @@
    6.21  
    6.22      private static final long serialVersionUID = -9133218147909088777L;
    6.23  
    6.24 -    public XYDataset produceDataset(Map params) throws DatasetProduceException {
    6.25 +    public XYDataset produceDataset(Map parametry) throws DatasetProduceException {
    6.26  
    6.27 -        System.setProperty("java.awt.headless", "true");
    6.28 +        DenniSouhrn ds = new DenniSouhrn();
    6.29 +        ds.setZacatekString(String.valueOf(parametry.get("zacatek")));
    6.30 +        ds.setKonecString(String.valueOf(parametry.get("konec")));
    6.31 +        ds.setCilString(String.valueOf(parametry.get("cil")));
    6.32  
    6.33 -        TimeSeries timeseries = new TimeSeries("fond", org.jfree.data.time.Day.class);
    6.34 -        TimeSeriesCollection dataset = new TimeSeriesCollection();
    6.35 +        HashMap<Integer, TimeSeries> casoveRady = new HashMap<Integer, TimeSeries>();
    6.36  
    6.37 +        for (Collection<Soucet> ss : ds.getSoucty().values()) {
    6.38  
    6.39 -        Calendar datum = Calendar.getInstance();
    6.40 -        for (int i = 0; i < 11; i++) {
    6.41 -            datum.add(Calendar.DAY_OF_YEAR, -1);
    6.42 -            double nahodneCislo = 100 - 10 * i - 20 * Math.random();
    6.43 -            timeseries.add(new Day(datum.getTime()), nahodneCislo);
    6.44 +            for (Soucet s : ss) {
    6.45 +                if (casoveRady.get(s.getCil().getId()) == null) {
    6.46 +                    casoveRady.put(s.getCil().getId(), new TimeSeries(s.getCil().getNazev(), org.jfree.data.time.Day.class));
    6.47 +                }
    6.48 +                casoveRady.get(s.getCil().getId()).add(new Day(s.getDen()), s.getPocet());
    6.49 +            }
    6.50          }
    6.51  
    6.52 -        dataset.addSeries(timeseries);
    6.53 +        TimeSeriesCollection dataset = new TimeSeriesCollection();
    6.54 +        for (TimeSeries ts : casoveRady.values()) {
    6.55 +            dataset.addSeries(ts);
    6.56 +        }
    6.57 +
    6.58          return dataset;
    6.59      }
    6.60  
     7.1 --- a/java/HrisniciSpameri/web/graf.jspx	Sun Mar 01 01:08:24 2009 +0100
     7.2 +++ b/java/HrisniciSpameri/web/graf.jspx	Sun Mar 01 13:04:10 2009 +0100
     7.3 @@ -10,16 +10,13 @@
     7.4      <jsp:useBean id="casovyGrafUtoku" class="cz.frantovo.hrisniciSpameri.grafy.CasovyGrafUtoku" />
     7.5  
     7.6  
     7.7 -    <cewolf:chart id="graf" title="Můj graf" type="timeseries" xaxislabel="Datum" yaxislabel="Počet" showlegend="false">
     7.8 +    <cewolf:chart id="graf" title="Počty útoků za den" type="timeseries" xaxislabel="Datum" yaxislabel="Počet" showlegend="true">
     7.9          <cewolf:data>
    7.10              <cewolf:producer id="casovyGrafUtoku" usecache="false">
    7.11                  <!-- TODO: rozchodit mezipaměť - cache -->
    7.12 -                <cewolf:param name="isin" value="${isin}" />
    7.13 -                <cewolf:param name="currency" value="${currency}" />
    7.14 -                <cewolf:param name="monthFrom" value="${monthFrom}" />
    7.15 -                <cewolf:param name="yearFrom" value="${yearFrom}" />
    7.16 -                <cewolf:param name="monthTo" value="${monthTo}" />
    7.17 -                <cewolf:param name="yearTo" value="${yearTo}" />
    7.18 +                <cewolf:param name="zacatek" value="${param.zacatek}" />
    7.19 +                <cewolf:param name="konec" value="${param.konec}" />
    7.20 +                <cewolf:param name="cil" value="${param.cil}" />
    7.21              </cewolf:producer>
    7.22          </cewolf:data>
    7.23          <cewolf:gradientpaint>
    7.24 @@ -28,6 +25,6 @@
    7.25          </cewolf:gradientpaint>
    7.26      </cewolf:chart>
    7.27  
    7.28 -    <cewolf:img chartid="graf" renderer="/cewolf" width="680" height="320" />
    7.29 +    <cewolf:img chartid="graf" renderer="/cewolf" width="800" height="400" />
    7.30  
    7.31  </jsp:root>
     8.1 --- a/java/HrisniciSpameri/web/index.jsp	Sun Mar 01 01:08:24 2009 +0100
     8.2 +++ b/java/HrisniciSpameri/web/index.jsp	Sun Mar 01 13:04:10 2009 +0100
     8.3 @@ -14,14 +14,11 @@
     8.4      <body>
     8.5          <h1>Hříšníci spameři</h1>
     8.6  
     8.7 -
     8.8 +        <jsp:include page="souhrn.jspx"/>
     8.9          <c:choose>
    8.10              <c:when test="${param.graf == 'ano'}">
    8.11                  <jsp:include page="graf.jspx"/>
    8.12 -            </c:when>            
    8.13 -            <c:otherwise>
    8.14 -                <jsp:include page="souhrn.jspx"/>
    8.15 -            </c:otherwise>
    8.16 +            </c:when>
    8.17          </c:choose>
    8.18  
    8.19          <div id="licence">Program je vydán pod licencí: <a href="licence.txt">GNU Affero GPL</a>. A zdrojové kódy jsou ke stažení: <a href="HrisniciSpameri-src.zip">HrisniciSpameri-src.zip</a><a href="HrisniciSpameri-src.zip.SHA-512">.</a></div>
     9.1 --- a/java/HrisniciSpameri/web/souhrn.jspx	Sun Mar 01 01:08:24 2009 +0100
     9.2 +++ b/java/HrisniciSpameri/web/souhrn.jspx	Sun Mar 01 13:04:10 2009 +0100
     9.3 @@ -1,9 +1,4 @@
     9.4  <?xml version="1.0" encoding="UTF-8"?>
     9.5 -<!-- 
     9.6 -    Document   : souhrn
     9.7 -    Created on : 11.2.2009, 18:54:42
     9.8 -    Author     : fiki
     9.9 --->
    9.10  <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
    9.11            xmlns:c="http://java.sun.com/jsp/jstl/core"
    9.12            xmlns:f="http://java.sun.com/jsp/jstl/functions"
    9.13 @@ -40,18 +35,19 @@
    9.14              <label>Začátek: <input name="zacatek" type="text" value="${denniSouhrn.zacatek}"/></label><br/>
    9.15              <label title="Pokud konec nezadáte, zobrazí se souhrn za jeden den – začátek">Konec: <input name="konec" type="text" value="${denniSouhrn.konec}"/></label><br/>
    9.16              <label>Cíl: <select name="cil">
    9.17 -                <option value="0">Všechny cíle</option>
    9.18 -                <c:forEach var="c" items="${denniSouhrn.seznamCilu}">
    9.19 -                    <c:choose>
    9.20 -                        <c:when test="${c.id == denniSouhrn.cil}">
    9.21 -                            <option value="${c.id}" selected="selected"><c:out value="${c.nazev}"/></option>
    9.22 -                        </c:when>
    9.23 -                        <c:otherwise>
    9.24 -                            <option value="${c.id}"><c:out value="${c.nazev}"/></option>
    9.25 -                        </c:otherwise>
    9.26 -                    </c:choose>
    9.27 -                </c:forEach>
    9.28 +                    <option value="0">Všechny cíle</option>
    9.29 +                    <c:forEach var="c" items="${denniSouhrn.seznamCilu}">
    9.30 +                        <c:choose>
    9.31 +                            <c:when test="${c.id == denniSouhrn.cil}">
    9.32 +                                <option value="${c.id}" selected="selected"><c:out value="${c.nazev}"/></option>
    9.33 +                            </c:when>
    9.34 +                            <c:otherwise>
    9.35 +                                <option value="${c.id}"><c:out value="${c.nazev}"/></option>
    9.36 +                            </c:otherwise>
    9.37 +                        </c:choose>
    9.38 +                    </c:forEach>
    9.39              </select></label><br/>
    9.40 +            <input name="graf" type="hidden" value="${param.graf}"/>
    9.41              <button>Zobrazit</button>
    9.42          </fieldset>
    9.43      </form>
    9.44 @@ -85,6 +81,6 @@
    9.45                  </tr>
    9.46              </c:forEach>
    9.47          </tbody>
    9.48 -    </table>
    9.49 +    </table>   
    9.50  
    9.51  </jsp:root>