]> git.saurik.com Git - apple/security.git/blob - Analytics/SFAnalytics.h
Security-58286.70.7.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
43 // Log event-based metrics: create an event corresponding to some event in your feature
44 // and call the appropriate method based on the successfulness of that event
45 - (void)logSuccessForEventNamed:(NSString*)eventName;
46 - (void)logHardFailureForEventNamed:(NSString*)eventName withAttributes:(NSDictionary*)attributes;
47 - (void)logSoftFailureForEventNamed:(NSString*)eventName withAttributes:(NSDictionary*)attributes;
48 // or just log an event if it is not failable
49 - (void)noteEventNamed:(NSString*)eventName;
50
51 - (void)logResultForEvent:(NSString*)eventName hardFailure:(bool)hardFailure result:(NSError*)eventResultError;
52
53 // Track the state of a named value over time
54 - (SFAnalyticsSampler*)addMetricSamplerForName:(NSString*)samplerName withTimeInterval:(NSTimeInterval)timeInterval block:(NSNumber* (^)(void))block;
55 - (SFAnalyticsSampler*)existingMetricSamplerForName:(NSString*)samplerName;
56 - (void)removeMetricSamplerForName:(NSString*)samplerName;
57 // Same idea, but log multiple named values in a single block
58 - (SFAnalyticsMultiSampler*)AddMultiSamplerForName:(NSString*)samplerName withTimeInterval:(NSTimeInterval)timeInterval block:(NSDictionary<NSString*, NSNumber*>* (^)(void))block;
59 - (SFAnalyticsMultiSampler*)existingMultiSamplerForName:(NSString*)samplerName;
60 - (void)removeMultiSamplerForName:(NSString*)samplerName;
61
62 // Log measurements of arbitrary things
63 // 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
64 - (SFAnalyticsActivityTracker*)logSystemMetricsForActivityNamed:(NSString*)eventName withAction:(void (^)(void))action;
65 - (void)logMetric:(NSNumber*)metric withName:(NSString*)metricName;
66
67
68
69 // --------------------------------
70 // Things below are for subclasses
71
72 // Override to create a concrete logger instance
73 @property (readonly, class) NSString* databasePath;
74
75 // Storing dates
76 - (void)setDateProperty:(NSDate*)date forKey:(NSString*)key;
77 - (NSDate*)datePropertyForKey:(NSString*)key;
78
79 // --------------------------------
80 // Things below are for unit testing
81
82 - (void)removeState; // removes DB object and any samplers
83
84 @end
85
86 #endif
87 #endif