32.2. 在自定义过程中提取 3scale 应用程序分析

要提取应用程序分析,首先要使用 ActiveDocs。3scale ActiveDocs 在您的管理门户中包括在 Account Settings > Integrate > 3scale API Docs 下。3scale 将其所有 API 都作为 ActiveDocs 提供,因此您可以从浏览器中尝试它们。这样,您可以找到最能满足您需求的请求,获取请求(curl)并获取响应。下图提供了一个 ActiveDocs 示例:

DIY 分析

这是 API 请求的 ActiveDocs,用于获取脚本将要提取分析的所有应用的 ActiveDocs。

  • 使用 ActiveDocs 完成研究后,使用所选脚本语言指定请求。这个示例使用 Ruby。
  • 重复上述操作,直到您拥有一个可以正常工作的脚本。例如扩展分析,该脚本 以 gist 用户身份提供。您可以在您的帐户中试用。

ActiveDocs 可让您快速了解 API 能够做什么。然后,需要了解您想要执行的任务需要哪 3 个或 4 个请求,并将脚本放在一起。

以下流程演示了实现示例客户所需自定义分析的步骤。

流程

  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. 将应用交叉引用帐户,因为开发人员的信息存储在帐户对象中。

    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
  5. 将所有内容放在一起以完成脚本。您有一个获取 3scale 内置分析中没有的信息的脚本。您还可以 以 gist 形式获得完整的脚本