2 * Copyright (c) 2017 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
28 #import <Foundation/Foundation.h>
29 #import "SFAnalyticsSampler.h"
30 #import "SFAnalyticsMultiSampler.h"
31 #import "SFAnalyticsActivityTracker.h"
33 // this sampling interval will cause the sampler to run only at data reporting time
34 extern const NSTimeInterval SFAnalyticsSamplerIntervalOncePerReport
;
36 @interface SFAnalytics
: NSObject
38 + (instancetype
)logger
;
40 + (NSInteger
)fuzzyDaysSinceDate
:(NSDate
*)date
;
41 + (void)addOSVersionToEvent
:(NSMutableDictionary
*)event
;
42 // Help for the subclass to pick a prefered location
43 + (NSString
*)defaultAnalyticsDatabasePath
:(NSString
*)basename
;
45 // Log event-based metrics: create an event corresponding to some event in your feature
46 // and call the appropriate method based on the successfulness of that event
47 - (void)logSuccessForEventNamed
:(NSString
*)eventName
;
48 - (void)logHardFailureForEventNamed
:(NSString
*)eventName withAttributes
:(NSDictionary
*)attributes
;
49 - (void)logSoftFailureForEventNamed
:(NSString
*)eventName withAttributes
:(NSDictionary
*)attributes
;
50 // or just log an event if it is not failable
51 - (void)noteEventNamed
:(NSString
*)eventName
;
53 - (void)logResultForEvent
:(NSString
*)eventName hardFailure
:(bool)hardFailure result
:(NSError
*)eventResultError
;
54 - (void)logResultForEvent
:(NSString
*)eventName hardFailure
:(bool)hardFailure result
:(NSError
*)eventResultError withAttributes
:(NSDictionary
*)attributes
;
56 // Track the state of a named value over time
57 - (SFAnalyticsSampler
*)addMetricSamplerForName
:(NSString
*)samplerName withTimeInterval
:(NSTimeInterval
)timeInterval block
:(NSNumber
* (^)(void))block
;
58 - (SFAnalyticsSampler
*)existingMetricSamplerForName
:(NSString
*)samplerName
;
59 - (void)removeMetricSamplerForName
:(NSString
*)samplerName
;
60 // Same idea, but log multiple named values in a single block
61 - (SFAnalyticsMultiSampler
*)AddMultiSamplerForName
:(NSString
*)samplerName withTimeInterval
:(NSTimeInterval
)timeInterval block
:(NSDictionary
<NSString
*, NSNumber
*>* (^)(void))block
;
62 - (SFAnalyticsMultiSampler
*)existingMultiSamplerForName
:(NSString
*)samplerName
;
63 - (void)removeMultiSamplerForName
:(NSString
*)samplerName
;
65 // Log measurements of arbitrary things
66 // System metrics measures how much time it takes to complete the action - possibly more in the future. The return value can be ignored if you only need to execute 1 block for your activity
67 - (SFAnalyticsActivityTracker
*)logSystemMetricsForActivityNamed
:(NSString
*)eventName withAction
:(void (^)(void))action
;
68 - (void)logMetric
:(NSNumber
*)metric withName
:(NSString
*)metricName
;
72 // --------------------------------
73 // Things below are for subclasses
75 // Override to create a concrete logger instance
76 @
property (readonly
, class) NSString
* databasePath
;
79 - (void)setDateProperty
:(NSDate
*)date forKey
:(NSString
*)key
;
80 - (NSDate
*)datePropertyForKey
:(NSString
*)key
;
82 // --------------------------------
83 // Things below are for unit testing
85 - (void)removeState
; // removes DB object and any samplers