public static Collection<Statistic> merge(Collection<Statistic>... results) {
    Collection<Statistic> newResults = new ArrayList<Statistic>();
    if (results.length == 0) {
      return Collections.emptySet();
    } else if (results.length == 1) {
      return results[0];
    } else {

      List<String> indivNames = new ArrayList<String>();
      for (Collection<Statistic> result : results) {
        for (Statistic individual : result) {
          if (!indivNames.contains(individual.name)) {
            indivNames.add(individual.name);
          }
        }
      }

      for (String indivName : indivNames) {
        Statistic indivStat = new Statistic(indivName);
        for (Collection<Statistic> result : results) {
          for (Statistic individual : result) {
            if (indivName.equals(individual.name)) {
              indivStat.add(individual);
            }
          }
        }
        newResults.add(indivStat);
      }
      return newResults;
    }
  }
 public static Statistic total(Collection<Statistic>... results) {
   Collection<Statistic> merged = merge(results);
   Statistic total = new Statistic("");
   for (Statistic individual : merged) {
     total.add(individual);
   }
   return total;
 }
Esempio n. 3
0
  public String toString() {
    final StringBuilder sb = new StringBuilder();

    for (final Statistic m : Statistic.values()) {
      sb.append(m.toString());
      sb.append("=");
      sb.append(statsCurrent.getStatistic(m));
      sb.append(", ");
    }

    if (sb.length() > 2) {
      sb.delete(sb.length() - 2, sb.length());
    }

    return sb.toString();
  }
Esempio n. 4
0
  private void resetDailyStats() {
    try {
      final Map<String, String> emailValues = new LinkedHashMap<>();
      for (final Statistic statistic : Statistic.values()) {
        final String key = statistic.getLabel(PwmConstants.DEFAULT_LOCALE);
        final String value = statsDaily.getStatistic(statistic);
        emailValues.put(key, value);
      }

      AlertHandler.alertDailyStats(pwmApplication, emailValues);
    } catch (Exception e) {
      LOGGER.error("error while generating daily alert statistics: " + e.getMessage());
    }

    currentDailyKey = new DailyKey(new Date());
    statsDaily = new StatisticsBundle();
    LOGGER.debug("reset daily statistics");
  }
Esempio n. 5
0
  public int outputStatsToCsv(
      final OutputStream outputStream, final Locale locale, final boolean includeHeader)
      throws IOException {
    LOGGER.trace("beginning output stats to csv process");
    final Date startTime = new Date();

    final StatisticsManager statsManger = pwmApplication.getStatisticsManager();
    final CSVPrinter csvPrinter = Helper.makeCsvPrinter(outputStream);

    if (includeHeader) {
      final List<String> headers = new ArrayList<>();
      headers.add("KEY");
      headers.add("YEAR");
      headers.add("DAY");
      for (Statistic stat : Statistic.values()) {
        headers.add(stat.getLabel(locale));
      }
      csvPrinter.printRecord(headers);
    }

    int counter = 0;
    final Map<StatisticsManager.DailyKey, String> keys =
        statsManger.getAvailableKeys(PwmConstants.DEFAULT_LOCALE);
    for (final StatisticsManager.DailyKey loopKey : keys.keySet()) {
      counter++;
      final StatisticsBundle bundle = statsManger.getStatBundleForKey(loopKey.toString());
      final List<String> lineOutput = new ArrayList<>();
      lineOutput.add(loopKey.toString());
      lineOutput.add(String.valueOf(loopKey.year));
      lineOutput.add(String.valueOf(loopKey.day));
      for (final Statistic stat : Statistic.values()) {
        lineOutput.add(bundle.getStatistic(stat));
      }
      csvPrinter.printRecord(lineOutput);
    }

    csvPrinter.flush();
    LOGGER.trace(
        "completed output stats to csv process; output "
            + counter
            + " records in "
            + TimeDuration.fromCurrent(startTime).asCompactString());
    return counter;
  }
Esempio n. 6
0
  private void publishStatisticsToCloud()
      throws URISyntaxException, IOException, PwmUnrecoverableException {
    final StatsPublishBean statsPublishData;
    {
      final StatisticsBundle bundle = getStatBundleForKey(KEY_CUMULATIVE);
      final Map<String, String> statData = new HashMap<>();
      for (final Statistic loopStat : Statistic.values()) {
        statData.put(loopStat.getKey(), bundle.getStatistic(loopStat));
      }
      final Configuration config = pwmApplication.getConfig();
      final List<String> configuredSettings = new ArrayList<>();
      for (final PwmSetting pwmSetting : config.nonDefaultSettings()) {
        if (!pwmSetting.getCategory().hasProfiles() && !config.isDefaultValue(pwmSetting)) {
          configuredSettings.add(pwmSetting.getKey());
        }
      }
      final Map<String, String> otherData = new HashMap<>();
      otherData.put(
          StatsPublishBean.KEYS.SITE_URL.toString(),
          config.readSettingAsString(PwmSetting.PWM_SITE_URL));
      otherData.put(
          StatsPublishBean.KEYS.SITE_DESCRIPTION.toString(),
          config.readSettingAsString(PwmSetting.PUBLISH_STATS_SITE_DESCRIPTION));
      otherData.put(
          StatsPublishBean.KEYS.INSTALL_DATE.toString(),
          PwmConstants.DEFAULT_DATETIME_FORMAT.format(pwmApplication.getInstallTime()));

      try {
        otherData.put(
            StatsPublishBean.KEYS.LDAP_VENDOR.toString(),
            pwmApplication
                .getProxyChaiProvider(config.getDefaultLdapProfile().getIdentifier())
                .getDirectoryVendor()
                .toString());
      } catch (Exception e) {
        LOGGER.trace("unable to read ldap vendor type for stats publication: " + e.getMessage());
      }

      statsPublishData =
          new StatsPublishBean(
              pwmApplication.getInstanceID(),
              new Date(),
              statData,
              configuredSettings,
              PwmConstants.BUILD_NUMBER,
              PwmConstants.BUILD_VERSION,
              otherData);
    }
    final URI requestURI = new URI(PwmConstants.PWM_URL_CLOUD + "/rest/pwm/statistics");
    final HttpPost httpPost = new HttpPost(requestURI.toString());
    final String jsonDataString = JsonUtil.serialize(statsPublishData);
    httpPost.setEntity(new StringEntity(jsonDataString));
    httpPost.setHeader("Accept", PwmConstants.AcceptValue.json.getHeaderValue());
    httpPost.setHeader("Content-Type", PwmConstants.ContentTypeValue.json.getHeaderValue());
    LOGGER.debug(
        "preparing to send anonymous statistics to "
            + requestURI.toString()
            + ", data to send: "
            + jsonDataString);
    final HttpResponse httpResponse =
        PwmHttpClient.getHttpClient(pwmApplication.getConfig()).execute(httpPost);
    if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
      throw new IOException(
          "http response error code: " + httpResponse.getStatusLine().getStatusCode());
    }
    LOGGER.info("published anonymous statistics to " + requestURI.toString());
    try {
      localDB.put(
          LocalDB.DB.PWM_STATS,
          KEY_CLOUD_PUBLISH_TIMESTAMP,
          String.valueOf(System.currentTimeMillis()));
    } catch (LocalDBException e) {
      LOGGER.error(
          "unexpected error trying to save last statistics published time to LocalDB: "
              + e.getMessage());
    }
  }
  public static Collection<Statistic> parse(File inFile)
      throws IOException, XmlPullParserException {
    Collection<Statistic> results = new ArrayList<Statistic>();
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    try {
      fis = new FileInputStream(inFile);
      bis = new BufferedInputStream(fis);
      XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
      factory.setNamespaceAware(true);
      factory.setValidating(false);
      XmlPullParser parser = factory.newPullParser();
      parser.setInput(bis, null);

      // check that the first tag is <javancss>
      expectNextTag(parser, "javancss");

      // skip until we get to the <packages> tag
      while (parser.getDepth() > 0
          && (parser.getEventType() != XmlPullParser.START_TAG
              || !"packages".equals(parser.getName()))) {
        parser.next();
      }
      while (parser.getDepth() > 0
          && (parser.getEventType() != XmlPullParser.START_TAG
              || !"package".equals(parser.getName()))) {
        parser.next();
      }
      while (parser.getDepth() >= 2
          && parser.getEventType() == XmlPullParser.START_TAG
          && "package".equals(parser.getName())) {
        Map<String, String> data = new HashMap<String, String>();
        String lastTag = null;
        String lastText = null;
        int depth = parser.getDepth();
        while (parser.getDepth() >= depth) {
          parser.next();
          switch (parser.getEventType()) {
            case XmlPullParser.START_TAG:
              lastTag = parser.getName();
              break;
            case XmlPullParser.TEXT:
              lastText = parser.getText();
              break;
            case XmlPullParser.END_TAG:
              if (parser.getDepth() == 4 && lastTag != null && lastText != null) {
                data.put(lastTag, lastText);
              }
              lastTag = null;
              lastText = null;
              break;
          }
        }
        if (data.containsKey("name")) {
          Statistic s = new Statistic(data.get("name"));
          s.setClasses(Long.valueOf(data.get("classes")));
          s.setFunctions(Long.valueOf(data.get("functions")));
          s.setNcss(Long.valueOf(data.get("ncss")));
          s.setJavadocs(Long.valueOf(data.get("javadocs")));
          s.setJavadocLines(Long.valueOf(data.get("javadoc_lines")));
          s.setSingleCommentLines(Long.valueOf(data.get("single_comment_lines")));
          s.setMultiCommentLines(Long.valueOf(data.get("multi_comment_lines")));
          results.add(s);
        }
        parser.next();
      }

    } catch (XmlPullParserException e) {
      throw new IOException2(e);
    } finally {
      if (bis != null) {
        bis.close();
      }
      if (fis != null) {
        fis.close();
      }
    }
    return results;
  }