32.2. 사용자 정의 프로시저에서 3scale 애플리케이션 분석 추출
애플리케이션 분석을 추출하려면 먼저 ActiveDocs 작업을 시작합니다. 3scale ActiveDocs는 계정 설정 > 통합 > 3scale API 문서 아래에 있는 관리 포털에서 사용할 수 있습니다. 3scale에는 ActiveDocs로 사용할 수 있는 모든 API가 있어 브라우저에서 시도할 수 있습니다. 이를 통해 필요에 가장 적합한 요청을 찾고 요청 (curl-like)을 가져오고 응답을 조작할 수 있습니다. 다음 그림에서는 ActiveDocs 예제를 제공합니다.The following figure provides an ActiveDocs example:
이는 스크립트가 분석을 추출할 모든 애플리케이션을 가져오는 API 요청의 ActiveDocs입니다.
- ActiveDocs로 조사를 완료한 후 선택한 스크립팅 언어로 요청을 지정합니다. 예에서는 Ruby를 사용합니다.
- 필요한 것을 정확히 수행하는 스크립트가 있을 때까지 반복합니다. 확장된 분석의 예는 스크립트를 gist로 사용할 수 있습니다. 당신은 당신의 계정에서 그것을 시도 할 수 있습니다.
ActiveDocs를 사용하면 API에서 수행할 수 있는 작업을 빠르게 이해할 수 있습니다. 그런 다음 원하는 작업에 필요한 3 또는 4 개의 요청을 검색하고 스크립트를 함께 배치하는 것이 중요합니다.
다음 절차에서는 고객이 원하는 사용자 지정 분석을 수행하는 단계를 보여줍니다.
절차
전체 애플리케이션 목록을 검색합니다. 이 작업을 수행하려면 페이지 지정이 필요합니다.
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기준을 충족하지 않는 애플리케이션을 필터링하십시오. 즉 계획은 "검토"여야 하며 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기준을 충족하는 각 애플리케이션에 대해 애플리케이션을 지난 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개발자의 정보가 계정 오브젝트에 저장되기 때문에 애플리케이션에 대한 교차 참조를 제공합니다.
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로 가져올 수도 있습니다.