/** {@inheritDoc} */
 @Override
 public void close() throws Exception {
   super.close();
   if (csvReader != null) csvReader.close();
   csvReader = null;
 }
Exemplo n.º 2
0
  @Override
  public void runJob() {
    addLog("开始更新基线", LOG_TYPE_SUCCESS);
    Calendar cl = Calendar.getInstance();
    long times = cl.getTimeInMillis();
    TaskInfoDto taskInfo = (TaskInfoDto) this.params.get(TASKINFO_KEY);

    String[] strs = taskInfo.getConfigDesc().split(";");

    // splunk服务器连接信息
    String[] serCfg = strs[0].split(",");
    if (serCfg.length != 4) {
      addLog("连接服务器参数不对:" + strs[0], LOG_TYPE_ERROR);
      return;
    }

    // 基本查询条件
    String baseStr = strs[2];

    String baseStr2 = strs.length < 14 ? "" : strs[13];

    // 统计值字段名
    String cntCol = strs[3];

    // 时间字段名
    String timeCol = strs[4];

    try {
      // 连接参数
      ServiceArgs loginArgs = new ServiceArgs();
      loginArgs.setUsername(serCfg[2]);
      loginArgs.setPassword(serCfg[3]);
      loginArgs.setHost(serCfg[0]);
      loginArgs.setPort(NumberUtils.toInt(serCfg[1]));

      // 连接
      Service service = Service.connect(loginArgs);

      // 延迟
      int delayMi = NumberUtils.toInt(strs[5], 0);

      String searchStr = createNSearchStr(baseStr, cntCol, timeCol, delayMi, baseStr2);

      this.addLog("查询:" + searchStr, LOG_TYPE_SUCCESS);

      // 查询
      Job nJob = service.search(searchStr); // 最近1分钟

      ResultsReader results;
      Event et;
      Iterator<Event> ie;
      InputStream is;
      ////////////////////////////////////////////////////////// 最近1分钟
      while (!nJob.isDone()) {
        Thread.sleep(1000);
      }
      is = nJob.getResults(new Args("output_mode", "json"));

      results = createResultsReader(ResultsReaderJson.class, is);

      List<String> nNumVal1 = new ArrayList<String>();
      List<String> nNumVal2 = new ArrayList<String>();
      List<String> nNumVal3 = new ArrayList<String>();
      List<Date> nDate = new ArrayList<Date>();

      if (nJob.getResultCount() > 0) {
        ie = results.iterator();

        while (ie.hasNext()) {
          if (strs.length > 12 && "1".equals(strs[12])) {

            et = ie.next();
            nNumVal1.add(et.get("my_cnt1"));
            nNumVal2.add(et.get("my_cnt2"));
            nDate.add(convertDate(et.get("my_time")));

          } else if (strs.length > 12 && "2".equals(strs[12])) {
            while (ie.hasNext()) {
              et = ie.next();
              nNumVal1.add(et.get("my_cnt1"));
              nNumVal2.add(et.get("my_cnt2"));
              nNumVal3.add(et.get("my_cnt3"));
              nDate.add(convertDate(et.get("my_time")));
            }
          } else {

            et = ie.next();
            nNumVal1.add(et.get("my_cnt1"));
            nDate.add(convertDate(et.get("my_time")));
          }
        }
      }
      is.close();
      nJob.cancel();

      ////////////////////////////////////////////////////////// 最近1分钟 end
      String curTime;
      BizMiDto dto;
      BizMiDao bdao = new BizMiDao();
      double rr;
      if (!nNumVal1.isEmpty()) {
        for (int i = 0; i < nNumVal1.size(); i++) {
          cl.set(Calendar.SECOND, 0);
          if (nDate.get(i) != null) {
            cl.setTime(nDate.get(i));
            curTime = DateUtil.datetimeFormat.format(nDate.get(i));
          } else {
            cl.add(Calendar.MINUTE, -delayMi);
            curTime = DateUtil.datetimeFormat.format(cl.getTime());
          }

          try {
            dto = bdao.findByTimeAndMi(strs[7], strs[8], curTime, strs[6]);
            if (dto == null) {
              this.addLog(
                  "更新出错,找不到更新点:" + strs[7] + "::" + strs[8] + "::" + curTime + "::" + strs[6],
                  LOG_TYPE_ERROR);
              this.addLog("更新出错,找不到更新点", LOG_TYPE_ERROR);
            }
            dto.setVal(ckVal(nNumVal1.get(i)));
            bdao.updateByTimeAndMi(dto);
          } catch (Exception e) {
            addLog("更新入口VAL:" + formatExpection(e), LOG_TYPE_ERROR);
          }

          if (!nNumVal2.isEmpty()) {
            try {
              dto = bdao.findByTimeAndMi(strs[7], strs[9], curTime, strs[6]);
              if (dto == null) {
                this.addLog(
                    "更新出错,找不到更新点:" + strs[7] + "::" + strs[9] + "::" + curTime + "::" + strs[6],
                    LOG_TYPE_ERROR);
              }
              dto.setVal(ckVal(nNumVal2.get(i)));
              bdao.updateByTimeAndMi(dto);
            } catch (Exception e) {
              addLog("更新出口VAL:" + formatExpection(e), LOG_TYPE_ERROR);
            }

            try {
              dto = bdao.findByTimeAndMi(strs[7], strs[10], curTime, strs[6]);
              if (dto == null) {
                this.addLog(
                    "更新出错,找不到更新点:" + strs[7] + "::" + strs[10] + "::" + curTime + "::" + strs[6],
                    LOG_TYPE_ERROR);
              }
              rr =
                  NumberUtils.toDouble(nNumVal2.get(i), 0)
                      / NumberUtils.toDouble(nNumVal1.get(i), 0);
              if (rr == Double.NaN) {
                dto.setVal(String.valueOf(0));
              } else {
                dto.setVal(String.valueOf(rr));
              }
              bdao.updateByTimeAndMi(dto);
            } catch (Exception e) {
              addLog("更新响应率:" + formatExpection(e), LOG_TYPE_ERROR);
            }
          }

          if (!nNumVal3.isEmpty()) {
            dto = bdao.findByTimeAndMi(strs[7], strs[11], curTime, strs[6]);
            dto.setVal(ckVal(nNumVal3.get(i)));
            bdao.updateByTimeAndMi(dto);
            try {
              bdao.saveByTable(dto, strs[6]);
            } catch (Exception e) {
              addLog("更新响应时间:" + formatExpection(e), LOG_TYPE_ERROR);
            }
          }
        }
      }

      addLog("任务耗时:" + (Calendar.getInstance().getTimeInMillis() - times) + "毫秒", LOG_TYPE_SUCCESS);

    } catch (Exception e) {
      e.printStackTrace();
      addLog(formatExpection(e), LOG_TYPE_ERROR);
    }
  }