void LsHelper::parseResult(ChecksT& checks)
{
  JsonHelper json(m_socketHandler->lastResult());

  QScriptValueIterator entryIter(json.data());
  while (entryIter.hasNext()) {
    entryIter.next();
    if (entryIter.flags() & QScriptValue::SkipInEnumeration) continue;
    QScriptValueIterator fieldIter(entryIter.value());

    QStringList fields;
    fields.clear();
    while (fieldIter.hasNext()) {
      fieldIter.next();
      if (fieldIter.flags() & QScriptValue::SkipInEnumeration) continue;
      fields.push_back(fieldIter.value().toString());
    }

    CheckT check;
    switch( fields.size() ) {
    case 6: // host
      check.id = check.host = fields[0].toStdString();
      check.status = fields[1].toInt();
      check.last_state_change = fields[2].toStdString();
      check.check_command = fields[3].toStdString();
      check.alarm_msg = fields[4].toStdString();
      check.host_groups = fields[5].toStdString();
      break;

    case 7: // service
      check.host = fields[0].toStdString();
      check.id = ID_PATTERN.arg(check.host.c_str(), fields[1]).toLower().toStdString();
      check.status = fields[2].toInt();
      check.last_state_change = fields[3].toStdString();
      check.check_command = fields[4].toStdString();
      check.alarm_msg = fields[5].toStdString();
      check.host_groups = fields[6].toStdString();
      break;

    default:
      qDebug()<< "unexpected entry: "<< entryIter.value().toString();
      continue;
      break;
    }

    checks.insert(check.id, check);
  }
}
void MxYeeDeyMittraFracs<DIM, Scalar>::setMatrix() {
  //matrix = Teuchos::rcp(new Epetra_CrsMatrix(Copy, bfield->getMap(), 1, true));
  //

  MxGridFieldIter<DIM> fieldIter(field);

  double minNonzeroFrac = 1.0;
  int numZeroFracs = 0;

  MxIndex ind;
  size_t comp;
  double val;
  Scalar mval;
  MxDimVector<int, DIM> cell;
  for (fieldIter.begin(); !fieldIter.atEnd(); fieldIter.bump()) {
    ind = fieldIter.getGlobCompIndx();
    comp = fieldIter.getComp();
    cell = fieldIter.getCell();
    val = field->getCompFrac(comp, cell, "pec");
    //std::cout << val << "\n";
    if (val != 0) {
      if (val < minFrac) val = minFrac;
      if (val < minNonzeroFrac) minNonzeroFrac = val;
    }
    else
      numZeroFracs++;

    //if (val != 0) {
      MxUtil::convertScalar(val, mval);
      MxCrsMatrix<Scalar>::insertRowValues(ind, 1, &ind, &mval);
    //}
  }

  std::cout << "  min nonzero frac is: " << minNonzeroFrac << "\n";
  std::cout << "  number of zeros is: " << numZeroFracs << "\n";

  //matrix->FillComplete(bfield->getMap(), bfield->getMap());
  MxCrsMatrix<Scalar>::fillComplete(field->getMap(), field->getMap());
}
void MxYeeDeyMittraFracs<DIM, Scalar>::setMatrixInverse() {
  //matrix = Teuchos::rcp(new Epetra_CrsMatrix(Copy, bfield->getMap(), 1, true));
  //

  MxGridFieldIter<DIM> fieldIter(field);

  double minNonzeroFrac = 1.0;
  int numZeroFracs = 0;

  MxIndex ind;
  size_t comp;
  double val;
  Scalar mval;
  MxDimVector<int, DIM> cell;

  srand(time(NULL) + this->getRangeMap()->getComm()->myPID());
  double rnd, df, nval;

  for (fieldIter.begin(); !fieldIter.atEnd(); fieldIter.bump()) {
    ind = fieldIter.getGlobCompIndx();
    comp = fieldIter.getComp();
    cell = fieldIter.getCell();
    val = field->getCompFrac(comp, cell, "pec");

    // pertub testing
    if (mRandomize) {
      if (val != 0 && val != 1) {
        rnd = (2 * double(rand() - RAND_MAX / 2)) / double(RAND_MAX);
        df = mRandomScale * rnd;
        //nval = val + df;
        nval = val*pow(10.0, df);
        if (nval < 0) val = 0;
        else if (nval > 1) val = 1;
        else {
          //std::cout << "original=" << val << " new=" << nval << "\n";
          val = nval;
        }
      }
    }

    if (val == 0) 
      numZeroFracs++;
    else if (val < minFrac) {
      val = 1.0 / minFrac;
      if (val < minNonzeroFrac) minNonzeroFrac = minFrac;
    }
    else {
      if (val < minNonzeroFrac) minNonzeroFrac = val;
      val = 1.0 / val;
    }

    MxUtil::convertScalar(val, mval);
    MxCrsMatrix<Scalar>::insertRowValues(ind, 1, &ind, &mval);
  }

  std::cout << "  min nonzero frac is: " << minNonzeroFrac << "\n";
  std::cout << "  number of zeros is: " << numZeroFracs << "\n";

  //matrix->FillComplete(bfield->getMap(), bfield->getMap());
  MxCrsMatrix<Scalar>::fillComplete(field->getMap(), field->getMap());
}