]> git.saurik.com Git - apple/security.git/blob - Analytics/SFAnalytics.h
Security-59306.140.5.tar.gz
[apple/security.git] / Analytics / SFAnalytics.h
1 /*
2 * Copyright (c) 2017 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #if __OBJC2__
25 #ifndef SFAnalytics_h
26 #define SFAnalytics_h
27
28 #import <Foundation/Foundation.h>
29 #import "SFAnalyticsSampler.h"
30 #import "SFAnalyticsMultiSampler.h"
31 #import "SFAnalyticsActivityTracker.h"
32
33 NS_ASSUME_NONNULL_BEGIN
34
35 // this sampling interval will cause the sampler to run only at data reporting time
36 extern const NSTimeInterval SFAnalyticsSamplerIntervalOncePerReport;
37
38 typedef NS_ENUM(uint32_t, SFAnalyticsTimestampBucket) {
39 SFAnalyticsTimestampBucketSecond = 0,
40 SFAnalyticsTimestampBucketMinute = 1,
41 SFAnalyticsTimestampBucketHour = 2,
42 };
43
44 @protocol SFAnalyticsProtocol <NSObject>
45 + (id<SFAnalyticsProtocol> _Nullable)logger;
46
47 - (void)logResultForEvent:(NSString*)eventName
48 hardFailure:(bool)hardFailure
49 result:(NSError* _Nullable)eventResultError;
50 - (void)logResultForEvent:(NSString*)eventName
51 hardFailure:(bool)hardFailure
52 result:(NSError* _Nullable)eventResultError
53 withAttributes:(NSDictionary* _Nullable)attributes;
54
55 - (SFAnalyticsMultiSampler* _Nullable)AddMultiSamplerForName:(NSString *)samplerName
56 withTimeInterval:(NSTimeInterval)timeInterval
57 block:(NSDictionary<NSString *,NSNumber *> *(^)(void))block;
58
59 - (SFAnalyticsActivityTracker* _Nullable)logSystemMetricsForActivityNamed:(NSString*)eventName
60 withAction:(void (^ _Nullable)(void))action;
61 - (SFAnalyticsActivityTracker* _Nullable)startLogSystemMetricsForActivityNamed:(NSString *)eventName;
62 @end
63
64 @interface SFAnalytics : NSObject <SFAnalyticsProtocol>
65
66 + (instancetype _Nullable)logger;
67
68 + (NSInteger)fuzzyDaysSinceDate:(NSDate*)date;
69 + (void)addOSVersionToEvent:(NSMutableDictionary*)event;
70 // Help for the subclass to pick a prefered location
71 + (NSString *)defaultAnalyticsDatabasePath:(NSString *)basename;
72
73 - (void)dailyCoreAnalyticsMetrics:(NSString *)eventName;
74
75 // Log event-based metrics: create an event corresponding to some event in your feature
76 // and call the appropriate method based on the successfulness of that event
77 - (void)logSuccessForEventNamed:(NSString*)eventName;
78 - (void)logSuccessForEventNamed:(NSString*)eventName timestampBucket:(SFAnalyticsTimestampBucket)timestampBucket;
79
80 - (void)logHardFailureForEventNamed:(NSString*)eventName withAttributes:(NSDictionary* _Nullable)attributes;
81 - (void)logHardFailureForEventNamed:(NSString*)eventName withAttributes:(NSDictionary* _Nullable)attributes timestampBucket:(SFAnalyticsTimestampBucket)timestampBucket;
82
83 - (void)logSoftFailureForEventNamed:(NSString*)eventName withAttributes:(NSDictionary* _Nullable)attributes;
84 - (void)logSoftFailureForEventNamed:(NSString*)eventName withAttributes:(NSDictionary* _Nullable)attributes timestampBucket:(SFAnalyticsTimestampBucket)timestampBucket;
85
86 // or just log an event if it is not failable
87 - (void)noteEventNamed:(NSString*)eventName;
88 - (void)noteEventNamed:(NSString*)eventName timestampBucket:(SFAnalyticsTimestampBucket)timestampBucket;
89
90 - (void)logResultForEvent:(NSString*)eventName
91 hardFailure:(bool)hardFailure
92 result:(NSError* _Nullable)eventResultError;
93 - (void)logResultForEvent:(NSString*)eventName
94 hardFailure:(bool)hardFailure
95 result:(NSError* _Nullable)eventResultError
96 timestampBucket:(SFAnalyticsTimestampBucket)timestampBucket;
97 - (void)logResultForEvent:(NSString*)eventName
98 hardFailure:(bool)hardFailure
99 result:(NSError* _Nullable)eventResultError
100 withAttributes:(NSDictionary* _Nullable)attributes;
101 - (void)logResultForEvent:(NSString*)eventName
102 hardFailure:(bool)hardFailure
103 result:(NSError* _Nullable)eventResultError
104 withAttributes:(NSDictionary* _Nullable)attributes
105 timestampBucket:(SFAnalyticsTimestampBucket)timestampBucket;
106
107 // Track the state of a named value over time
108 - (SFAnalyticsSampler* _Nullable)addMetricSamplerForName:(NSString*)samplerName
109 withTimeInterval:(NSTimeInterval)timeInterval
110 block:(NSNumber* (^)(void))block;
111 - (SFAnalyticsSampler* _Nullable)existingMetricSamplerForName:(NSString*)samplerName;
112 - (void)removeMetricSamplerForName:(NSString*)samplerName;
113 // Same idea, but log multiple named values in a single block
114 - (SFAnalyticsMultiSampler* _Nullable)AddMultiSamplerForName:(NSString*)samplerName
115 withTimeInterval:(NSTimeInterval)timeInterval
116 block:(NSDictionary<NSString*, NSNumber*>* (^)(void))block;
117 - (SFAnalyticsMultiSampler*)existingMultiSamplerForName:(NSString*)samplerName;
118 - (void)removeMultiSamplerForName:(NSString*)samplerName;
119
120 // Log measurements of arbitrary things
121 // 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
122 - (SFAnalyticsActivityTracker* _Nullable)logSystemMetricsForActivityNamed:(NSString*)eventName
123 withAction:(void (^ _Nullable)(void))action;
124
125 // Same as above, but automatically starts the tracker, since you haven't given it any action to perform
126 - (SFAnalyticsActivityTracker* _Nullable)startLogSystemMetricsForActivityNamed:(NSString *)eventName;
127
128 - (void)logMetric:(NSNumber*)metric withName:(NSString*)metricName;
129
130
131 // --------------------------------
132 // Things below are for subclasses
133
134 // Override to create a concrete logger instance
135 @property (readonly, class, nullable) NSString* databasePath;
136
137 // Storing dates
138 - (void)setDateProperty:(NSDate* _Nullable)date forKey:(NSString*)key;
139 - (NSDate* _Nullable)datePropertyForKey:(NSString*)key;
140
141 - (void)incrementIntegerPropertyForKey:(NSString*)key;
142 - (void)setNumberProperty:(NSNumber* _Nullable)number forKey:(NSString*)key;
143 - (NSNumber * _Nullable)numberPropertyForKey:(NSString*)key;
144
145
146 // --------------------------------
147 // Things below are for unit testing
148
149 - (void)removeState; // removes DB object and any samplers
150
151 @end
152
153 NS_ASSUME_NONNULL_END
154 #endif
155 #endif