]> git.saurik.com Git - apple/security.git/blob - Analytics/SFAnalytics.h
Security-58286.240.4.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 // this sampling interval will cause the sampler to run only at data reporting time
34 extern const NSTimeInterval SFAnalyticsSamplerIntervalOncePerReport;
35
36 @interface SFAnalytics : NSObject
37
38 + (instancetype)logger;
39
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;
44
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;
52
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;
55
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;
64
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;
69
70
71
72 // --------------------------------
73 // Things below are for subclasses
74
75 // Override to create a concrete logger instance
76 @property (readonly, class) NSString* databasePath;
77
78 // Storing dates
79 - (void)setDateProperty:(NSDate*)date forKey:(NSString*)key;
80 - (NSDate*)datePropertyForKey:(NSString*)key;
81
82 // --------------------------------
83 // Things below are for unit testing
84
85 - (void)removeState; // removes DB object and any samplers
86
87 @end
88
89 #endif
90 #endif