001// Generated by delombok at Fri Oct 11 21:56:29 CEST 2019
002/**
003 * Copyright (c) 2010-2019 Mark Allen, Norbert Bartels.
004 *
005 * Permission is hereby granted, free of charge, to any person obtaining a copy
006 * of this software and associated documentation files (the "Software"), to deal
007 * in the Software without restriction, including without limitation the rights
008 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
009 * copies of the Software, and to permit persons to whom the Software is
010 * furnished to do so, subject to the following conditions:
011 *
012 * The above copyright notice and this permission notice shall be included in
013 * all copies or substantial portions of the Software.
014 *
015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
016 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
017 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
019 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
020 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
021 * THE SOFTWARE.
022 */
023package com.restfb;
024
025import java.util.ArrayList;
026import java.util.HashMap;
027import java.util.List;
028import java.util.Map;
029import com.restfb.json.Json;
030import com.restfb.json.JsonObject;
031import com.restfb.util.StringUtils;
032
033public class DebugHeaderInfo {
034  /**
035   * x-fb-debug
036   */
037  private final String debug;
038  /**
039   * x-fb-rev
040   */
041  private final String rev;
042  /**
043   * x-fb-trace-id
044   */
045  private final String traceId;
046  /**
047   * the use version
048   */
049  private final Version usedVersion;
050  /**
051   * x-app-usage
052   */
053  private final HeaderUsage appUsage;
054  /**
055   * x-page-usage
056   */
057  private final HeaderUsage pageUsage;
058  /**
059   * x-ad-account-usage
060   */
061  private final HeaderUsage adAccountUsage;
062  private final BusinessUseCaseUsage businessUseCaseUsage;
063
064  private DebugHeaderInfo(String debug, String rev, String traceId, Version version, String appUsage, String pageUsage, String adAccountUsage, String businessUsage) {
065    this.debug = debug;
066    this.rev = rev;
067    this.traceId = traceId;
068    this.usedVersion = version;
069    this.appUsage = createUsage(appUsage);
070    this.pageUsage = createUsage(pageUsage);
071    this.adAccountUsage = createUsage(adAccountUsage);
072    this.businessUseCaseUsage = createBusinessUsage(businessUsage);
073  }
074
075  /**
076   * get Facebook response header field <code>x-fb-debug</code>
077   * 
078   * @return the Facebook response header field <code>x-fb-debug</code>
079   */
080  public String getDebug() {
081    return debug;
082  }
083
084  /**
085   * get Facebook response header field <code>x-fb-rev</code>
086   * 
087   * @return the Facebook response header field <code>x-fb-rev</code>
088   */
089  public String getRev() {
090    return rev;
091  }
092
093  /**
094   * get Facebook response header field <code>x-fb-trace-id</code>
095   * 
096   * @return the Facebook response header field x-fb-trace-id
097   */
098  public String getTraceId() {
099    return traceId;
100  }
101
102  /**
103   * get the version, facebook used internally to fulfill the request
104   * 
105   * @return the version, facebook used internally to fulfill the request
106   */
107  public Version getUsedVersion() {
108    return usedVersion;
109  }
110
111  /**
112   * get Facebook response header field <code>x-app-usage</code>
113   *
114   * @return the Facebook response header field x-app-usage
115   */
116  public HeaderUsage getAppUsage() {
117    return appUsage;
118  }
119
120  /**
121   * get Facebook response header field <code>x-page-usage</code>
122   *
123   * @return the Facebook response header field x-page-usage
124   */
125  public HeaderUsage getPageUsage() {
126    return pageUsage;
127  }
128
129  /**
130   * get Facebook response header field <code>x-ad-account-usage</code>
131   * 
132   * @return the Facebook response header field x-ad-account-usage
133   */
134  public HeaderUsage getAdAccountUsage() {
135    return adAccountUsage;
136  }
137
138  public BusinessUseCaseUsage getBusinessUseCaseUsage() {
139    return businessUseCaseUsage;
140  }
141
142  private BusinessUseCaseUsage createBusinessUsage(String businessUsageInformation) {
143    if (StringUtils.isBlank(businessUsageInformation)) {
144      return null;
145    }
146    try {
147      DefaultJsonMapper jsonMapper = new DefaultJsonMapper();
148      JsonObject jsonObject = Json.parse(businessUsageInformation).asObject();
149      BusinessUseCaseUsage usage = new BusinessUseCaseUsage();
150      for (String key : jsonObject.names()) {
151        usage.put(key, jsonMapper.toJavaList(jsonObject.get(key).toString(), InnerBusinessUseCaseUsage.class));
152      }
153      return usage;
154    } catch (Exception e) {
155      return null;
156    }
157  }
158
159  private HeaderUsage createUsage(String usageInformation) {
160    if (StringUtils.isBlank(usageInformation)) {
161      return null;
162    }
163    try {
164      JsonObject jsonValue = Json.parse(usageInformation).asObject();
165      if (jsonValue.names().size() == 1 && jsonValue.contains("acc_id_util_pct")) {
166        return new HeaderUsage(jsonValue.getDouble("acc_id_util_pct", 0.0));
167      }
168      return new HeaderUsage(jsonValue.getInt("call_count", -1), jsonValue.getInt("total_time", -1), jsonValue.getInt("total_cputime", -1));
169    } catch (Exception e) {
170      return new HeaderUsage(usageInformation);
171    }
172  }
173
174
175  /**
176   * business use case based usage object.
177   *
178   * This usage object is used in Graph API 3.3+ and provides access to the complete set of inner objects.
179   */
180  public static class BusinessUseCaseUsage {
181    private Map<String, List<InnerBusinessUseCaseUsage>> usageMap = new HashMap<>();
182
183    /**
184     * add a new list of usages that belongs to the business id to the outer object
185     *
186     * @param key
187     *          the business id the list of usages is connected with
188     * @param businessUseCaseUsageList
189     *          the list of usages
190     */
191    void put(String key, List<InnerBusinessUseCaseUsage> businessUseCaseUsageList) {
192      usageMap.put(key, businessUseCaseUsageList);
193    }
194
195    /**
196     * returns the usage list that belongs to the given business id
197     *
198     * @param businessId
199     *          the business ids you like to fetch the usage list for
200     * @return the list of usages
201     */
202    public List<InnerBusinessUseCaseUsage> get(String businessId) {
203      return usageMap.get(businessId);
204    }
205
206    /**
207     * returns the list of Business Ids that are provided in the the business use case usage header
208     * 
209     * @return list of business ids
210     */
211    public List<String> getBusinessIds() {
212      return new ArrayList<>(usageMap.keySet());
213    }
214  }
215
216
217  public static class InnerBusinessUseCaseUsage {
218    /**
219     * Percentage of calls made for this business id/business ad account
220     */
221    @Facebook("call_count")
222    private Integer callCount;
223    /**
224     * Percentage of the total CPU time that has been used
225     */
226    @Facebook("total_cputime")
227    private Integer totalCputime;
228    /**
229     * Percentage of the total time that has been used
230     */
231    @Facebook("total_time")
232    private Integer totalTime;
233    /**
234     * Type of rate limit logic being applied
235     */
236    @Facebook
237    private String type;
238    /**
239     * Time in minutes to resume calls
240     */
241    @Facebook("estimated_time_to_regain_access")
242    private Integer estimatedTimeToRegainAccess;
243
244    /**
245     * Percentage of calls made for this business id/business ad account
246     */
247    @java.lang.SuppressWarnings("all")
248    public Integer getCallCount() {
249      return this.callCount;
250    }
251
252    /**
253     * Percentage of the total CPU time that has been used
254     */
255    @java.lang.SuppressWarnings("all")
256    public Integer getTotalCputime() {
257      return this.totalCputime;
258    }
259
260    /**
261     * Percentage of the total time that has been used
262     */
263    @java.lang.SuppressWarnings("all")
264    public Integer getTotalTime() {
265      return this.totalTime;
266    }
267
268    /**
269     * Type of rate limit logic being applied
270     */
271    @java.lang.SuppressWarnings("all")
272    public String getType() {
273      return this.type;
274    }
275
276    /**
277     * Time in minutes to resume calls
278     */
279    @java.lang.SuppressWarnings("all")
280    public Integer getEstimatedTimeToRegainAccess() {
281      return this.estimatedTimeToRegainAccess;
282    }
283  }
284
285
286  public static class HeaderUsage {
287    HeaderUsage(String percentage) {
288      this.percentage = percentage;
289      this.percentageOnly = true;
290      this.adAccountHeader = false;
291    }
292
293    HeaderUsage(Integer callCount, Integer totalTime, Integer totalCputime) {
294      this.callCount = callCount;
295      this.totalTime = totalTime;
296      this.totalCputime = totalCputime;
297      this.percentageOnly = false;
298      this.adAccountHeader = false;
299    }
300
301    HeaderUsage(Double percentage) {
302      this.accIdUtilPct = percentage;
303      this.percentageOnly = false;
304      this.adAccountHeader = true;
305    }
306
307    private final boolean percentageOnly;
308    private final boolean adAccountHeader;
309    private String percentage;
310    private Integer callCount;
311    private Integer totalTime;
312    private Integer totalCputime;
313    private Double accIdUtilPct;
314
315    @java.lang.SuppressWarnings("all")
316    public boolean isPercentageOnly() {
317      return this.percentageOnly;
318    }
319
320    @java.lang.SuppressWarnings("all")
321    public boolean isAdAccountHeader() {
322      return this.adAccountHeader;
323    }
324
325    @java.lang.SuppressWarnings("all")
326    public String getPercentage() {
327      return this.percentage;
328    }
329
330    @java.lang.SuppressWarnings("all")
331    public Integer getCallCount() {
332      return this.callCount;
333    }
334
335    @java.lang.SuppressWarnings("all")
336    public Integer getTotalTime() {
337      return this.totalTime;
338    }
339
340    @java.lang.SuppressWarnings("all")
341    public Integer getTotalCputime() {
342      return this.totalCputime;
343    }
344
345    @java.lang.SuppressWarnings("all")
346    public Double getAccIdUtilPct() {
347      return this.accIdUtilPct;
348    }
349  }
350
351
352  public static class DebugHeaderInfoFactory {
353    private String debug;
354    private String rev;
355    private String traceId;
356    private Version version;
357    private String appUsage;
358    private String pageUsage;
359    private String adAccountUsage;
360    private String businessUseCaseUsage;
361
362    public static DebugHeaderInfoFactory create() {
363      return new DebugHeaderInfoFactory();
364    }
365
366    public DebugHeaderInfoFactory setVersion(Version version) {
367      this.version = version;
368      return this;
369    }
370
371    public DebugHeaderInfoFactory setDebug(String debug) {
372      this.debug = debug;
373      return this;
374    }
375
376    public DebugHeaderInfoFactory setRev(String rev) {
377      this.rev = rev;
378      return this;
379    }
380
381    public DebugHeaderInfoFactory setTraceId(String traceId) {
382      this.traceId = traceId;
383      return this;
384    }
385
386    public DebugHeaderInfoFactory setAppUsage(String appUsage) {
387      this.appUsage = appUsage;
388      return this;
389    }
390
391    public DebugHeaderInfoFactory setPageUsage(String pageUsage) {
392      this.pageUsage = pageUsage;
393      return this;
394    }
395
396    public DebugHeaderInfoFactory setAdAccountUsage(String adAccountUsage) {
397      this.adAccountUsage = adAccountUsage;
398      return this;
399    }
400
401    public DebugHeaderInfoFactory setBusinessUseCaseUsage(String businessUseCaseUsage) {
402      this.businessUseCaseUsage = businessUseCaseUsage;
403      return this;
404    }
405
406    public DebugHeaderInfo build() {
407      return new DebugHeaderInfo(this.debug, this.rev, this.traceId, this.version, this.appUsage, this.pageUsage, this.adAccountUsage, this.businessUseCaseUsage);
408    }
409  }
410}