Exemplo n.º 1
0
  /**
   * @param response
   * @param type
   * @param alphaList
   * @param title
   * @param xTitle
   * @param yTitle
   */
  protected void createChart(
      final CollStatInfo csi,
      final Vector<Pair<String, Integer>> list,
      final String xTitle,
      final String yTitle) {
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    for (Pair<String, Integer> p : list) {
      dataset.addValue(p.second, p.first, "");
    }

    // if (StringUtils.isEmpty(csi.getInstName()))
    // {
    //    csi.setInstName(getProviderNameFromInstCode(csi.getProviderId()));
    // }

    JFreeChart chart =
        ChartFactory.createBarChart(
            csi.getTitle(), xTitle, yTitle, dataset, PlotOrientation.VERTICAL, true, true, false);

    // chart.getCategoryPlot().setRenderer(new CustomColorBarChartRenderer());

    chart.setBackgroundPaint(new Color(228, 243, 255));

    try {
      Integer hashCode = csi.hashCode();
      csi.setChartFileName(hashCode + ".png");
      DataOutputStream dos =
          new DataOutputStream(
              new FileOutputStream(new File("reports/charts/" + csi.getChartFileName())));
      ChartUtilities.writeChartAsPNG(dos, chart, 700, 600);

    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
Exemplo n.º 2
0
  /** @param response */
  protected boolean generateChart(
      final CollStatInfo csi, final HashMap<StatType, CollStatSQLDefs> statTypeHash) {
    System.out.println(csi.getTitle());

    int total = csi.getValue(StatType.eTotalNumRecords);

    Vector<Pair<String, Integer>> pairs = new Vector<Pair<String, Integer>>();

    double totalPercent = 0.0;
    int cnt = 0;
    int goodCnt = 0;
    for (StatType type : StatType.values()) {
      if (type == StatType.eTotalNumRecords) continue;

      CollStatSQLDefs csqd = statTypeHash.get(type);
      double dVal = (double) csi.getValue(type);
      int val = (int) (((dVal / (double) total) * 100.0));

      if (val > 0) {
        goodCnt++;
      }

      // System.out.println(csqd.getName()+"  "+val+ "  " + csi.getValue(csqd.getType())+" / "+
      // total);

      pairs.add(new Pair<String, Integer>(csqd.getName(), val));

      if (type.ordinal() < StatType.eHasYearOnly.ordinal()) {
        totalPercent += val;
        cnt++;
      }
    }
    totalPercent = Math.max(totalPercent, 0.0);
    pairs.add(new Pair<String, Integer>("Average", (int) totalPercent / cnt));

    // System.out.println("goodCnt: "+goodCnt);
    if (goodCnt > 0) {
      createChart(csi, pairs, "Statistic", "Percent");
      return true;
    }
    return false;
  }
Exemplo n.º 3
0
  /** @return */
  @SuppressWarnings("unchecked")
  public List<CollStatInfo> loadInstCodesAndtotals() {
    XStream xstream = new XStream();
    CollStatInfo.config(xstream);

    String xml = "";
    try {
      xml =
          FileUtils.readFileToString(
              new File(
                  XMLHelper.getConfigDirPath(
                      "../src/edu/ku/brc/specify/toycode/mexconabio/collstatinfo.xml")),
              "UTF8");
    } catch (IOException e) {
      e.printStackTrace();
    }

    List<CollStatInfo> list = (List<CollStatInfo>) xstream.fromXML(xml);
    for (CollStatInfo csi : list) {
      instHashMap.put(csi.getProviderId(), csi);
      institutions.add(csi);
    }
    return list;
  }
Exemplo n.º 4
0
  /* (non-Javadoc)
   * @see edu.ku.brc.specify.toycode.mexconabio.AnalysisBase#process(int, int)
   */
  @Override
  public void process(int type, int options) {
    if (loadInstsFromDB) {
      discoverInstCodesAndTotals();
    } else {
      loadInstCodesAndtotals();
    }

    for (CollStatSQLDefs csqd : getStatSQL()) {
      if (csqd.getType() == StatType.eTotalNumRecords) continue;

      System.out.println(csqd.getType().toString());
      for (Object[] row : BasicSQLUtils.query(dbSrcConn, csqd.getSQL())) {
        // String  instCode = (String)row[0];
        Integer providerId = (Integer) row[0];
        Long count = (Long) row[1];

        CollStatInfo csi = instHashMap.get(providerId);
        if (csi != null) {
          csi.setValue(csqd.getType(), count.intValue());
        } else {
          System.err.println("Couldn't find institution[" + providerId + "]");
        }
      }
    }

    for (CollStatInfo csi : institutions) {
      System.out.println("\n-------------------" + csi.getTitle() + "-------------------");
      for (CollStatSQLDefs csqd : getStatSQL()) {
        System.out.println(
            String.format("%15s %10d", csqd.getType().toString(), csi.getValue(csqd.getType())));
      }
    }

    saveInstitutions();
  }
Exemplo n.º 5
0
  private void saveInstitutions() {
    XStream xstream = new XStream();
    CollStatInfo.config(xstream);
    File file =
        new File(
            XMLHelper.getConfigDirPath(
                "../src/edu/ku/brc/specify/toycode/mexconabio/collstatinfo.xml"));
    System.out.println(file.getAbsolutePath());
    try {
      Writer writer =
          new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));
      xstream.toXML(institutions, writer);

    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Exemplo n.º 6
0
  public void discoverInstCodesAndTotals() {
    System.out.println("Getting Institutions");
    String sql =
        "SELECT * FROM (SELECT data_provider_id, COUNT(*) AS cnt FROM raw GROUP BY data_provider_id) T1 WHERE cnt > 1 ORDER BY cnt DESC";
    for (Object[] row : BasicSQLUtils.query(dbSrcConn, sql)) {
      CollStatInfo csi = new CollStatInfo();
      csi.setProviderId((Integer) row[0]);
      csi.setTotalNumRecords(((Long) row[1]).intValue());

      if (StringUtils.isEmpty(csi.getInstName())) {
        csi.setInstName(getProviderNameFromInstCode(csi.getProviderId()));
      }
      instHashMap.put(csi.getProviderId(), csi);
      institutions.add(csi);
    }
    System.out.println("Done Getting Institutions");
  }
Exemplo n.º 7
0
  public void createCharts() {
    loadInstCodesAndtotals();

    // for (CollStatInfo csi : institutions)
    // {
    //    csi.setInstName(getProviderNameFromInstCode(csi.getProviderId()));
    // }

    List<CollStatSQLDefs> statTypes = getStatSQL();
    HashMap<StatType, CollStatSQLDefs> statTypeHash = new HashMap<StatType, CollStatSQLDefs>();
    for (CollStatSQLDefs cs : statTypes) {
      statTypeHash.put(cs.getType(), cs);
    }

    CollStatInfo totals = new CollStatInfo(" Totals");
    for (CollStatInfo csi : institutions) {
      for (CollStatSQLDefs csqd : statTypes) {
        StatType type = csqd.getType();
        int totVal = totals.getValue(type) + csi.getValue(type);
        totals.setValue(type, totVal);
      }
    }

    try {
      FileUtils.deleteDirectory(new File("reports/charts/"));
    } catch (IOException e) {
      e.printStackTrace();
    }

    Collections.sort(
        institutions,
        new Comparator<CollStatInfo>() {
          @Override
          public int compare(CollStatInfo o1, CollStatInfo o2) {
            return o1.getInstName().compareToIgnoreCase(o2.getInstName());
            // Integer cnt1 = o1.getTotalNumRecords();
            // Integer cnt2 = o2.getTotalNumRecords();
            // return cnt2.compareTo(cnt1);
          }
        });

    institutions.insertElementAt(totals, 0);
    CollStatInfo clsi = totals;

    // tblWriter.logHdr(titles);

    int i = 0;
    for (CollStatInfo csi : institutions) {
      if (StringUtils.isEmpty(csi.getInstName())) {
        csi.setInstName(getProviderNameFromInstCode(csi.getProviderId()));
      }

      String title = csi.getTitle() + " - " + csi.getTotalNumRecords();

      if (i == 0) {
        startLogging("reports", "charts", clsi.hashCode() + ".html", title, false);
        tblWriter.startTable();
        tblInstHash.put(tblWriter, csi);

      } else {
        tblWriter.endTable();
        startNewDocument(csi.hashCode() + ".html", title, false);
        tblInstHash.put(tblWriter, csi);
        tblWriter.startTable();
      }

      if (generateChart(csi, statTypeHash)) {
        int total = csi.getValue(StatType.eTotalNumRecords);

        tblWriter.setHasLines();
        tblWriter.print("<TR><TD>");
        tblWriter.print(String.format("<img src=\"%s\">", csi.getChartFileName()));
        tblWriter.println("<BR><BR><BR><BR></TD><TD>");
        tblWriter.startTable();
        tblWriter.logHdr("Stat", "Percent");

        int rowCnt = 0;
        int cnt = 0;
        double totalPercent = 0.0;
        for (StatType type : StatType.values()) {
          if (type == StatType.eTotalNumRecords) continue;

          CollStatSQLDefs csqd = statTypeHash.get(type);

          double dVal = (double) csi.getValue(type);
          double val = (((dVal / (double) total) * 100.0));

          if (type.ordinal() < StatType.eHasYearOnly.ordinal()) {
            tblWriter.print(String.format("<TR class=\"%s\">", (rowCnt % 2 == 0 ? "od" : "ev")));
            totalPercent += val;
            cnt++;
          } else {
            tblWriter.print(String.format("<TR>", csqd.getName()));
          }
          tblWriter.println(
              String.format(
                  "<TD>%s</TD><TD style=\"text-align:right\">%6.2f</TD></TR>",
                  csqd.getName(), val));
          rowCnt++;
        }
        totalPercent = Math.max(totalPercent, 0.0);
        double avePercent = (totalPercent / (double) cnt);
        tblWriter.println(
            String.format(
                "<TR><TD>Average</TD><TD style=\"text-align:right\">%6.2f</TD></TR>", avePercent));
        csi.setAveragePercent(avePercent);

        tblWriter.endTable();
        tblWriter.println("</TD></TR>");
      }

      i++;

      /*if (i % 25 == 0)
      {
          tblWriter.endTable();
          startNewDocument("institutions"+i+".html", "Institutions " + i, false);
          tblWriter.setHasLines();
      }*/

      // if (i == 100) break;
    }
    tblWriter.endTable();

    Vector<CollStatInfo> sortedByAvesList = new Vector<CollStatInfo>(institutions);
    Collections.sort(
        sortedByAvesList,
        new Comparator<CollStatInfo>() {
          @Override
          public int compare(CollStatInfo o1, CollStatInfo o2) {
            Double i1 = o1.getAveragePercent();
            Double i2 = o2.getAveragePercent();
            int rv = i2.compareTo(i1);
            if (rv == 0) {
              Integer cnt1 = o1.getTotalNumRecords();
              Integer cnt2 = o2.getTotalNumRecords();
              return cnt2.compareTo(cnt1);
            }
            return rv;
          }
        });

    Integer rank = 0;
    String average = "";
    startNewDocument("SortedByAverages.html", " Sorted By Averages", false);
    sortByTblWriter = tblWriter;
    tblWriter.startTable();
    tblWriter.logHdr("Rank", "Institution", "Num of Records", "Percentage");
    for (CollStatInfo csi : sortedByAvesList) {
      String aveStr = String.format("%8.2f", csi.getAveragePercent());
      Integer cnt = csi.getTotalNumRecords();
      if (!aveStr.equals(average)) {
        rank++;
        average = aveStr;
      }

      String fileName = StringUtils.replace(csi.getChartFileName(), "png", "html");
      String link = String.format("<a href=\"%s\">%s</>", fileName, csi.getTitle());
      tblWriter.log(rank.toString(), link, cnt.toString(), aveStr);
    }
    tblWriter.endTable();

    // tblWriter.println("</BODY></HTML>");
    endLogging(true);

    // saveInstitutions();
  }