def createAction()

in s2rest_play/app/org/apache/s2graph/rest/play/controllers/CounterController.scala [99:172]


  def createAction(service: String, action: String) = Action(s2parse.json) { implicit request =>
    counterModel.findByServiceAction(service, action, useCache = false) match {
      case None =>
        val body = request.body
        val version = (body \ "version").asOpt[Int].map(_.toByte).getOrElse(counter.VERSION_2)
        val autoComb = (body \ "autoComb").asOpt[Boolean].getOrElse(true)
        val dimension = (body \ "dimension").asOpt[String].getOrElse("")
        val useProfile = (body \ "useProfile").asOpt[Boolean].getOrElse(false)
        val bucketImpId = (body \ "bucketImpId").asOpt[String]

        val useExact = (body \ "useExact").asOpt[Boolean].getOrElse(true)
        val useRank = (body \ "useRank").asOpt[Boolean].getOrElse(true)

        val intervalUnit = (body \ "intervalUnit").asOpt[String]
        // 2 day
        val ttl = (body \ "ttl").asOpt[Int].getOrElse(2 * 24 * 60 * 60)
        val dailyTtl = (body \ "dailyTtl").asOpt[Int]
        val regionMultiplier = (body \ "regionMultiplier").asOpt[Int].getOrElse(1)

        val rateAction = (body \ "rateAction").asOpt[Map[String, String]]
        val rateBase = (body \ "rateBase").asOpt[Map[String, String]]
        val rateThreshold = (body \ "rateThreshold").asOpt[Int]

        val rateActionId = {
          for {
            actionMap <- rateAction
            service <- actionMap.get("service")
            action <- actionMap.get("action")
            policy <- counterModel.findByServiceAction(service, action)
          } yield {
            policy.id
          }
        }
        val rateBaseId = {
          for {
            actionMap <- rateBase
            service <- actionMap.get("service")
            action <- actionMap.get("action")
            policy <- counterModel.findByServiceAction(service, action)
          } yield {
            policy.id
          }
        }

        val hbaseTable = {
          Seq(tablePrefixMap(version), service, ttl) ++ dailyTtl mkString "_"
        }

        // find label
        val itemType = Label.findByName(action, useCache = false) match {
          case Some(label) =>
            ItemType.withName(label.tgtColumnType.toUpperCase)
          case None =>
            val strItemType = (body \ "itemType").asOpt[String].getOrElse("STRING")
            ItemType.withName(strItemType.toUpperCase)
        }
        val policy = Counter(useFlag = true, version, service, action, itemType, autoComb = autoComb, dimension,
          useProfile = useProfile, bucketImpId, useRank = useRank, ttl, dailyTtl, Some(hbaseTable), intervalUnit,
          rateActionId, rateBaseId, rateThreshold)

        if (rateAction.isEmpty) {
          // prepare exact storage
          exactCounter(version).prepare(policy)
        }
        if (useRank || rateAction.isDefined) {
          // prepare ranking storage
          rankingCounter(version).prepare(policy)
        }
        counterModel.createServiceAction(policy)
        Ok(Json.toJson(Map("msg" -> s"created $service/$action")))
      case Some(policy) =>
        Ok(Json.toJson(Map("msg" -> s"already exist $service/$action")))
    }
  }