32.4.2. 단계별 가이드

이는 고객이 원하는 사용자 정의 분석을 수행하기 위한 단계였습니다.

  1. 전체 애플리케이션 목록을 검색합니다. 이 작업에는 페이지 페이지가 필요합니다.

    def api_call_applications_list(domain, provider_key)
      done = false
      res = Array.new
      page = 1
    
          while !done
        url = "https://#{domain}/admin/api/applications.xml?provider_key=#{provider_key}&page=#{page}&per_page=100"
        page += 1
        response = RestClient.get url
        raise Exception.new("Wrong response code (#{response.code}) in request #{url}") if response.code!=200
        document = Nokogiri::XML(response.to_str)    done = document.xpath("applications/@current_page").text == document.xpath("applications/@total_pages").text
        document.xpath("//application").each do |item|
          app = Hash.new
          app["created_at"] = DateTime.parse(item.xpath("created_at").text)
          app["plan_name"] = item.xpath("plan/name").text
          app["service_id"] = item.xpath("plan/service_id").text
          app["account_id"] = item.xpath("user_account_id").text
          app["id"] = item.xpath("id").text
          res << app
        end
      end
      return res
    end
  2. 기준을 충족하지 않는 애플리케이션을 필터링합니다. 즉, 계획이 "평가"여야 하며 10일 이상 최신이어야 합니다.

    def filter_applications(domain, provider_key, plan_name, num_of_days)
      res = api_call_applications_list(domain, provider_key)
      res.each do |item|
        res.delete(item) if item["plan_name"] != plan_name
        res.delete(item) if item["created_at"] > (DateTime.now - num_of_days)
      end
      return res
    end
  3. 그런 다음 기준을 충족하는 각 애플리케이션에 대해 사용량을 가져옵니다. 즉, 애플리케이션이 지난 10일 동안 얼마나 많은 히트를 수행했는지를 의미합니다.

    def api_call_application_usage(domain, provider_key, application_id, metric, from, to, granularity)
      url = "https://#{domain}/stats/applications/#{application_id}/usage.xml?provider_key=#{provider_key}&metric_name=#{metric}&since=#{from}&until=#{to}&granularity=#{granularity}"
      response = RestClient.get url
      raise Exception.new("Wrong response code (#{response.code}) in request #{url}") if response.code!=200
      document = Nokogiri::XML(response.to_str)
      return document.xpath("//usage/data/values").text.split(",")
    end
  4. 개발자의 정보가 account 오브젝트에 저장되어 있기 때문에 애플리케이션을 계정에 상호 참조합니다.

    def api_call_account_read(domain, provider_key, account_id)
      url = "https://#{domain}/admin/api/accounts/#{account_id}.xml?provider_key=#{provider_key}"
      response = RestClient.get url
      raise Exception.new("Wrong response code (#{response.code}) in request #{url}") if response.code!=200
      document = Nokogiri::XML(response.to_str)
      account = Hash.new
      account["email"] = document.xpath("//users/user/email").text
      account["name"] = document.xpath("//users/user/first_name").text + " " + document.xpath("//users/user/last_name").text
      return account
    end

    이제 모든 것을 함께 배치하면 이제 스크립트가 완료됩니다. 3scale의 기본 제공 분석에서 아직 제공되지 않은 정보를 가져오는 스크립트가 있습니다. 전체 스크립트를 gist로 가져올 수도 있습니다.