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@
26 #import "SFAnalyticsSQLiteStore.h"
27 #import "SFAnalyticsDefines.h"
30 NSString* const SFAnalyticsColumnEventType = @"event_type";
31 NSString* const SFAnalyticsColumnDate = @"timestamp";
32 NSString* const SFAnalyticsColumnData = @"data";
33 NSString* const SFAnalyticsUploadDate = @"upload_date";
35 @implementation SFAnalyticsSQLiteStore
37 + (instancetype)storeWithPath:(NSString*)path schema:(NSString*)schema
39 SFAnalyticsSQLiteStore* store = nil;
40 @synchronized([SFAnalyticsSQLiteStore class]) {
41 static NSMutableDictionary* loggingStores = nil;
42 static dispatch_once_t onceToken;
43 dispatch_once(&onceToken, ^{
44 loggingStores = [[NSMutableDictionary alloc] init];
47 NSString* standardizedPath = path.stringByStandardizingPath;
48 store = loggingStores[standardizedPath];
50 store = [[self alloc] initWithPath:standardizedPath schema:schema];
51 loggingStores[standardizedPath] = store;
54 if (![store openWithError:&error] && !(error && error.code == SQLITE_AUTH)) {
55 secerror("SFAnalytics: could not open db at init, will try again later. Error: %@", error);
67 - (BOOL)tryToOpenDatabase
71 if (![self openWithError:&error]) {
74 secnotice("SFAnalytics", "successfully opened analytics db");
79 - (NSInteger)successCountForEventType:(NSString*)eventType
81 if (![self tryToOpenDatabase]) {
84 return [[[[self select:@[SFAnalyticsColumnSuccessCount] from:SFAnalyticsTableSuccessCount where:@"event_type = ?" bindings:@[eventType]] firstObject] valueForKey:SFAnalyticsColumnSuccessCount] integerValue];
87 - (void)incrementSuccessCountForEventType:(NSString*)eventType
89 if (![self tryToOpenDatabase]) {
92 NSInteger successCount = [self successCountForEventType:eventType];
93 NSInteger hardFailureCount = [self hardFailureCountForEventType:eventType];
94 NSInteger softFailureCount = [self softFailureCountForEventType:eventType];
95 [self insertOrReplaceInto:SFAnalyticsTableSuccessCount values:@{SFAnalyticsColumnEventType : eventType, SFAnalyticsColumnSuccessCount : @(successCount + 1), SFAnalyticsColumnHardFailureCount : @(hardFailureCount), SFAnalyticsColumnSoftFailureCount : @(softFailureCount)}];
98 - (NSInteger)hardFailureCountForEventType:(NSString*)eventType
100 if (![self tryToOpenDatabase]) {
103 return [[[[self select:@[SFAnalyticsColumnHardFailureCount] from:SFAnalyticsTableSuccessCount where:@"event_type = ?" bindings:@[eventType]] firstObject] valueForKey:SFAnalyticsColumnHardFailureCount] integerValue];
106 - (NSInteger)softFailureCountForEventType:(NSString*)eventType
108 if (![self tryToOpenDatabase]) {
111 return [[[[self select:@[SFAnalyticsColumnSoftFailureCount] from:SFAnalyticsTableSuccessCount where:@"event_type = ?" bindings:@[eventType]] firstObject] valueForKey:SFAnalyticsColumnSoftFailureCount] integerValue];
114 - (void)incrementHardFailureCountForEventType:(NSString*)eventType
116 if (![self tryToOpenDatabase]) {
119 NSInteger successCount = [self successCountForEventType:eventType];
120 NSInteger hardFailureCount = [self hardFailureCountForEventType:eventType];
121 NSInteger softFailureCount = [self softFailureCountForEventType:eventType];
122 [self insertOrReplaceInto:SFAnalyticsTableSuccessCount values:@{SFAnalyticsColumnEventType : eventType, SFAnalyticsColumnSuccessCount : @(successCount), SFAnalyticsColumnHardFailureCount : @(hardFailureCount + 1), SFAnalyticsColumnSoftFailureCount : @(softFailureCount)}];
125 - (void)incrementSoftFailureCountForEventType:(NSString*)eventType
127 if (![self tryToOpenDatabase]) {
130 NSInteger successCount = [self successCountForEventType:eventType];
131 NSInteger hardFailureCount = [self hardFailureCountForEventType:eventType];
132 NSInteger softFailureCount = [self softFailureCountForEventType:eventType];
133 [self insertOrReplaceInto:SFAnalyticsTableSuccessCount values:@{SFAnalyticsColumnEventType : eventType, SFAnalyticsColumnSuccessCount : @(successCount), SFAnalyticsColumnHardFailureCount : @(hardFailureCount), SFAnalyticsColumnSoftFailureCount : @(softFailureCount + 1)}];
136 - (NSDictionary*)summaryCounts
138 if (![self tryToOpenDatabase]) {
139 return [NSDictionary new];
141 NSMutableDictionary* successCountsDict = [NSMutableDictionary dictionary];
142 NSArray* rows = [self selectAllFrom:SFAnalyticsTableSuccessCount where:nil bindings:nil];
143 for (NSDictionary* rowDict in rows) {
144 NSString* eventName = rowDict[SFAnalyticsColumnEventType];
146 secinfo("SFAnalytics", "ignoring entry in success counts table without an event name");
150 successCountsDict[eventName] = @{SFAnalyticsTableSuccessCount : rowDict[SFAnalyticsColumnSuccessCount], SFAnalyticsColumnHardFailureCount : rowDict[SFAnalyticsColumnHardFailureCount], SFAnalyticsColumnSoftFailureCount : rowDict[SFAnalyticsColumnSoftFailureCount]};
153 return successCountsDict;
156 - (NSArray*)deserializedRecords:(NSArray*)recordBlobs
158 if (![self tryToOpenDatabase]) {
159 return [NSArray new];
161 NSMutableArray* records = [NSMutableArray new];
162 for (NSDictionary* row in recordBlobs) {
163 NSMutableDictionary* deserializedRecord = [NSPropertyListSerialization propertyListWithData:row[SFAnalyticsColumnData] options:NSPropertyListMutableContainers format:nil error:nil];
164 [records addObject:deserializedRecord];
169 - (NSArray*)hardFailures
171 if (![self tryToOpenDatabase]) {
172 return [NSArray new];
174 return [self deserializedRecords:[self select:@[SFAnalyticsColumnData] from:SFAnalyticsTableHardFailures]];
177 - (NSArray*)softFailures
179 if (![self tryToOpenDatabase]) {
180 return [NSArray new];
182 return [self deserializedRecords:[self select:@[SFAnalyticsColumnData] from:SFAnalyticsTableSoftFailures]];
185 - (NSArray*)allEvents
187 if (![self tryToOpenDatabase]) {
188 return [NSArray new];
190 return [self deserializedRecords:[self select:@[SFAnalyticsColumnData] from:SFAnalyticsTableAllEvents]];
195 if (![self tryToOpenDatabase]) {
196 return [NSArray new];
198 return [self select:@[SFAnalyticsColumnSampleName, SFAnalyticsColumnSampleValue] from:SFAnalyticsTableSamples];
201 - (void)addEventDict:(NSDictionary*)eventDict toTable:(NSString*)table
203 if (![self tryToOpenDatabase]) {
206 NSError* error = nil;
207 NSData* serializedRecord = [NSPropertyListSerialization dataWithPropertyList:eventDict format:NSPropertyListBinaryFormat_v1_0 options:0 error:&error];
208 if(!error && serializedRecord) {
209 [self insertOrReplaceInto:table values:@{SFAnalyticsColumnDate : @([[NSDate date] timeIntervalSince1970]), SFAnalyticsColumnData : serializedRecord}];
211 if(error && !serializedRecord) {
212 secerror("Couldn't serialize failure record: %@", error);
216 - (void)addSample:(NSNumber*)value forName:(NSString*)name
218 if (![self tryToOpenDatabase]) {
221 [self insertOrReplaceInto:SFAnalyticsTableSamples values:@{SFAnalyticsColumnDate : @([[NSDate date] timeIntervalSince1970]), SFAnalyticsColumnSampleName : name, SFAnalyticsColumnSampleValue : value}];
224 - (void)removeAllSamplesForName:(NSString*)name
226 if (![self tryToOpenDatabase]) {
229 [self deleteFrom:SFAnalyticsTableSamples where:[NSString stringWithFormat:@"name == '%@'", name] bindings:nil];
232 - (NSDate*)uploadDate
234 if (![self tryToOpenDatabase]) {
235 return nil; // In other cases return default object but nil is better here to avoid entering the upload flow
237 return [self datePropertyForKey:SFAnalyticsUploadDate];
240 - (void)setUploadDate:(NSDate*)uploadDate
242 if (![self tryToOpenDatabase]) {
245 [self setDateProperty:uploadDate forKey:SFAnalyticsUploadDate];
250 if (![self tryToOpenDatabase]) {
253 [self deleteFrom:SFAnalyticsTableSuccessCount where:@"event_type like ?" bindings:@[@"%"]];
254 [self deleteFrom:SFAnalyticsTableHardFailures where:@"id >= 0" bindings:nil];
255 [self deleteFrom:SFAnalyticsTableSoftFailures where:@"id >= 0" bindings:nil];
256 [self deleteFrom:SFAnalyticsTableSamples where:@"id >= 0" bindings:nil];
257 [self deleteFrom:SFAnalyticsTableAllEvents where:@"id >= 0" bindings:nil];