Class: Lhj::AppleAnalyticsHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/lhj/helper/apple/analytics/apple_analytics_helper.rb

Class Method Summary collapse

Class Method Details

.download_csv(url, path) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/lhj/helper/apple/analytics/apple_analytics_helper.rb', line 61

def self.download_csv(url, path)
  name = "#{rand(36 ** 8).to_s(36)}.csv.gz"
  key = "#{path}/#{name}"
  file = File.join(Lhj::Config.instance.home_dir, key)
  save_file(url, file)
  Lhj::OSS::Helper.instance.upload(key, file)
  File.delete(file) if File.exist?(file)
  key
end

.notify(keys, conversation_id) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/lhj/helper/apple/analytics/apple_analytics_helper.rb', line 81

def self.notify(keys, conversation_id)
  title = 'AppStore数据分析'
  markdown = "### 数据来源于App Store \n"
  keys.each do |k|
    a = k.split('/')
    name = a[3].gsub('_', ' ')
    markdown += "- [#{name}](#{Lhj::OSS::Helper.instance.object_url(k)}) \n"
  end
  Lhj::Dingtalk.send_markdown_message_with_api(conversation_id, title, markdown, '')
end

.read_all_report(id, date, conversation_id) ⇒ Object

Parameters:

  • id: (String)

    the app resource ID from the List Apps response.

  • date: (String)

    format YYYY-MM-DD

  • conversation_id: (String)

    robot conversation id



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/lhj/helper/apple/analytics/apple_analytics_helper.rb', line 11

def self.read_all_report(id, date, conversation_id)
  keys = []
  url = "https://api.appstoreconnect.apple.com/v1/apps/#{id}/analyticsReportRequests"
  reports_res = Lhj::ConnectAPI.get(url)
  reports_res_body = JSON.parse(reports_res.body)
  reports_res_body['data'].each do |json|
    if json['attributes']['accessType'] == 'ONGOING'
      keys += read_reports(id, json['relationships']['reports']['links']['related'], date)
    end
  end
  notify(keys, conversation_id) if keys.length.positive?
end

.read_instances(url, date, path) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/lhj/helper/apple/analytics/apple_analytics_helper.rb', line 40

def self.read_instances(url, date, path)
  keys = []
  req_url = "#{url}?filter%5BprocessingDate%5D=#{date}&filter%5Bgranularity%5D=DAILY"
  instances_res = Lhj::ConnectAPI.get(req_url)
  instances_res_body = JSON.parse(instances_res.body)
  instances_res_body['data'].each do |json|
    keys += read_segments(json['relationships']['segments']['links']['related'], path)
  end
  keys
end

.read_reports(id, url, date) ⇒ Array

Parameters:

Returns:

  • (Array)


28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lhj/helper/apple/analytics/apple_analytics_helper.rb', line 28

def self.read_reports(id, url, date)
  keys = []
  reports_res = Lhj::ConnectAPI.get(url)
  reports_res_body = JSON.parse(reports_res.body)
  reports_res_body['data'].each do |json|
    name = json['attributes']['name'].gsub(/\s/, '_')
    path = "analytics/#{id}/#{json['attributes']['category']}/#{name}/#{date}"
    keys += read_instances(json['relationships']['instances']['links']['related'], date, path)
  end
  keys
end

.read_segments(url, path) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/lhj/helper/apple/analytics/apple_analytics_helper.rb', line 51

def self.read_segments(url, path)
  keys = []
  segment_res = Lhj::ConnectAPI.get(url)
  segment_res_body = JSON.parse(segment_res.body)
  segment_res_body['data'].each do |json|
    keys << download_csv(json['attributes']['url'], path)
  end
  keys
end

.save_file(url, file) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/lhj/helper/apple/analytics/apple_analytics_helper.rb', line 71

def self.save_file(url, file)
  http_client = Faraday.new
  response = http_client.get(url) do |req|
    req.options[:timeout] = 300
    req.options[:open_timeout] = 20
  end
  FileUtils.mkdir_p(File.dirname(file)) unless File.directory?(File.dirname(file))
  File.open(file, 'wb') { |fp| fp.write(response.body) }
end