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
40 seccritical("Cannot init db with empty path");
43 if (![schema length]) {
44 seccritical("Cannot init db without schema");
48 SFAnalyticsSQLiteStore* store = nil;
49 @synchronized([SFAnalyticsSQLiteStore class]) {
50 static NSMutableDictionary* loggingStores = nil;
51 static dispatch_once_t onceToken;
52 dispatch_once(&onceToken, ^{
53 loggingStores = [[NSMutableDictionary alloc] init];
56 NSString* standardizedPath = path.stringByStandardizingPath;
57 store = loggingStores[standardizedPath];
59 store = [[self alloc] initWithPath:standardizedPath schema:schema];
60 loggingStores[standardizedPath] = store;
63 if (![store openWithError:&error] && !(error && error.code == SQLITE_AUTH)) {
64 secerror("SFAnalytics: could not open db at init, will try again later. Error: %@", error);
76 - (BOOL)tryToOpenDatabase
80 if (![self openWithError:&error]) {
83 secnotice("SFAnalytics", "successfully opened analytics db");
88 - (NSInteger)successCountForEventType:(NSString*)eventType
90 if (![self tryToOpenDatabase]) {
93 return [[[[self select:@[SFAnalyticsColumnSuccessCount] from:SFAnalyticsTableSuccessCount where:@"event_type = ?" bindings:@[eventType]] firstObject] valueForKey:SFAnalyticsColumnSuccessCount] integerValue];
96 - (void)incrementSuccessCountForEventType:(NSString*)eventType
98 if (![self tryToOpenDatabase]) {
101 NSInteger successCount = [self successCountForEventType:eventType];
102 NSInteger hardFailureCount = [self hardFailureCountForEventType:eventType];
103 NSInteger softFailureCount = [self softFailureCountForEventType:eventType];
104 [self insertOrReplaceInto:SFAnalyticsTableSuccessCount values:@{SFAnalyticsColumnEventType : eventType, SFAnalyticsColumnSuccessCount : @(successCount + 1), SFAnalyticsColumnHardFailureCount : @(hardFailureCount), SFAnalyticsColumnSoftFailureCount : @(softFailureCount)}];
107 - (NSInteger)hardFailureCountForEventType:(NSString*)eventType
109 if (![self tryToOpenDatabase]) {
112 return [[[[self select:@[SFAnalyticsColumnHardFailureCount] from:SFAnalyticsTableSuccessCount where:@"event_type = ?" bindings:@[eventType]] firstObject] valueForKey:SFAnalyticsColumnHardFailureCount] integerValue];
115 - (NSInteger)softFailureCountForEventType:(NSString*)eventType
117 if (![self tryToOpenDatabase]) {
120 return [[[[self select:@[SFAnalyticsColumnSoftFailureCount] from:SFAnalyticsTableSuccessCount where:@"event_type = ?" bindings:@[eventType]] firstObject] valueForKey:SFAnalyticsColumnSoftFailureCount] integerValue];
123 - (void)incrementHardFailureCountForEventType:(NSString*)eventType
125 if (![self tryToOpenDatabase]) {
128 NSInteger successCount = [self successCountForEventType:eventType];
129 NSInteger hardFailureCount = [self hardFailureCountForEventType:eventType];
130 NSInteger softFailureCount = [self softFailureCountForEventType:eventType];
131 [self insertOrReplaceInto:SFAnalyticsTableSuccessCount values:@{SFAnalyticsColumnEventType : eventType, SFAnalyticsColumnSuccessCount : @(successCount), SFAnalyticsColumnHardFailureCount : @(hardFailureCount + 1), SFAnalyticsColumnSoftFailureCount : @(softFailureCount)}];
134 - (void)incrementSoftFailureCountForEventType:(NSString*)eventType
136 if (![self tryToOpenDatabase]) {
139 NSInteger successCount = [self successCountForEventType:eventType];
140 NSInteger hardFailureCount = [self hardFailureCountForEventType:eventType];
141 NSInteger softFailureCount = [self softFailureCountForEventType:eventType];
142 [self insertOrReplaceInto:SFAnalyticsTableSuccessCount values:@{SFAnalyticsColumnEventType : eventType, SFAnalyticsColumnSuccessCount : @(successCount), SFAnalyticsColumnHardFailureCount : @(hardFailureCount), SFAnalyticsColumnSoftFailureCount : @(softFailureCount + 1)}];
145 - (NSDictionary*)summaryCounts
147 if (![self tryToOpenDatabase]) {
148 return [NSDictionary new];
150 NSMutableDictionary* successCountsDict = [NSMutableDictionary dictionary];
151 NSArray* rows = [self selectAllFrom:SFAnalyticsTableSuccessCount where:nil bindings:nil];
152 for (NSDictionary* rowDict in rows) {
153 NSString* eventName = rowDict[SFAnalyticsColumnEventType];
155 secinfo("SFAnalytics", "ignoring entry in success counts table without an event name");
159 successCountsDict[eventName] = @{SFAnalyticsTableSuccessCount : rowDict[SFAnalyticsColumnSuccessCount], SFAnalyticsColumnHardFailureCount : rowDict[SFAnalyticsColumnHardFailureCount], SFAnalyticsColumnSoftFailureCount : rowDict[SFAnalyticsColumnSoftFailureCount]};
162 return successCountsDict;
165 - (NSArray*)deserializedRecords:(NSArray*)recordBlobs
167 if (![self tryToOpenDatabase]) {
168 return [NSArray new];
170 NSMutableArray* records = [NSMutableArray new];
171 for (NSDictionary* row in recordBlobs) {
172 NSMutableDictionary* deserializedRecord = [NSPropertyListSerialization propertyListWithData:row[SFAnalyticsColumnData] options:NSPropertyListMutableContainers format:nil error:nil];
173 [records addObject:deserializedRecord];
178 - (NSArray*)hardFailures
180 if (![self tryToOpenDatabase]) {
181 return [NSArray new];
183 return [self deserializedRecords:[self select:@[SFAnalyticsColumnData] from:SFAnalyticsTableHardFailures]];
186 - (NSArray*)softFailures
188 if (![self tryToOpenDatabase]) {
189 return [NSArray new];
191 return [self deserializedRecords:[self select:@[SFAnalyticsColumnData] from:SFAnalyticsTableSoftFailures]];
194 - (NSArray*)allEvents
196 if (![self tryToOpenDatabase]) {
197 return [NSArray new];
199 return [self deserializedRecords:[self select:@[SFAnalyticsColumnData] from:SFAnalyticsTableAllEvents]];
204 if (![self tryToOpenDatabase]) {
205 return [NSArray new];
207 return [self select:@[SFAnalyticsColumnSampleName, SFAnalyticsColumnSampleValue] from:SFAnalyticsTableSamples];
210 - (void)addEventDict:(NSDictionary*)eventDict toTable:(NSString*)table
212 if (![self tryToOpenDatabase]) {
215 NSError* error = nil;
216 NSData* serializedRecord = [NSPropertyListSerialization dataWithPropertyList:eventDict format:NSPropertyListBinaryFormat_v1_0 options:0 error:&error];
217 if(!error && serializedRecord) {
218 [self insertOrReplaceInto:table values:@{SFAnalyticsColumnDate : @([[NSDate date] timeIntervalSince1970]), SFAnalyticsColumnData : serializedRecord}];
220 if(error && !serializedRecord) {
221 secerror("Couldn't serialize failure record: %@", error);
225 - (void)addSample:(NSNumber*)value forName:(NSString*)name
227 if (![self tryToOpenDatabase]) {
230 [self insertOrReplaceInto:SFAnalyticsTableSamples values:@{SFAnalyticsColumnDate : @([[NSDate date] timeIntervalSince1970]), SFAnalyticsColumnSampleName : name, SFAnalyticsColumnSampleValue : value}];
233 - (void)removeAllSamplesForName:(NSString*)name
235 if (![self tryToOpenDatabase]) {
238 [self deleteFrom:SFAnalyticsTableSamples where:[NSString stringWithFormat:@"name == '%@'", name] bindings:nil];
241 - (NSDate*)uploadDate
243 if (![self tryToOpenDatabase]) {
244 return nil; // In other cases return default object but nil is better here to avoid entering the upload flow
246 return [self datePropertyForKey:SFAnalyticsUploadDate];
249 - (void)setUploadDate:(NSDate*)uploadDate
251 if (![self tryToOpenDatabase]) {
254 [self setDateProperty:uploadDate forKey:SFAnalyticsUploadDate];
259 if (![self tryToOpenDatabase]) {
262 [self deleteFrom:SFAnalyticsTableSuccessCount where:@"event_type like ?" bindings:@[@"%"]];
263 [self deleteFrom:SFAnalyticsTableHardFailures where:@"id >= 0" bindings:nil];
264 [self deleteFrom:SFAnalyticsTableSoftFailures where:@"id >= 0" bindings:nil];
265 [self deleteFrom:SFAnalyticsTableSamples where:@"id >= 0" bindings:nil];
266 [self deleteFrom:SFAnalyticsTableAllEvents where:@"id >= 0" bindings:nil];