Skip to content

Commit

Permalink
Added pragma marks
Browse files Browse the repository at this point in the history
Defined constants DEFAULT_UPDATE_INTERVAL & EVENT_SEND_THRESHOLD
Improved namespacing by adding Countly prefix to DeviceInfo, EventQueue & ConnectionQueue
  • Loading branch information
erkanyildiz committed Dec 20, 2013
1 parent b39f0ab commit e35be92
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 42 deletions.
4 changes: 2 additions & 2 deletions Countly.h
Expand Up @@ -7,14 +7,14 @@

#import <Foundation/Foundation.h>

@class EventQueue;
@class CountlyEventQueue;

@interface Countly : NSObject {
double unsentSessionLength;
NSTimer *timer;
double lastTime;
BOOL isSuspended;
EventQueue *eventQueue;
CountlyEventQueue *eventQueue;
}

+ (Countly *)sharedInstance;
Expand Down
99 changes: 59 additions & 40 deletions Countly.m
Expand Up @@ -4,6 +4,7 @@
//
// Please visit www.count.ly for more information.

#pragma mark - Directives

#ifndef COUNTLY_DEBUG
#define COUNTLY_DEBUG 0
Expand All @@ -20,6 +21,8 @@
#endif

#define COUNTLY_VERSION "1.0"
#define COUNTLY_DEFAULT_UPDATE_INTERVAL 60.0
#define COUNTLY_EVENT_SEND_THRESHOLD 10

#import "Countly.h"
#import "Countly_OpenUDID.h"
Expand All @@ -32,6 +35,8 @@
#include <sys/sysctl.h>


#pragma mark - Category - GTMNSStringURLArgumentsAdditions

/// Utilities for encoding and decoding URL arguments.
/// This code is from the project google-toolbox-for-mac
@interface NSString (GTMNSStringURLArgumentsAdditions)
Expand Down Expand Up @@ -84,12 +89,14 @@ - (NSString*)gtm_stringByUnescapingFromURLArgument {
@end


@interface DeviceInfo : NSObject
#pragma mark - CountlyDeviceInfo

@interface CountlyDeviceInfo : NSObject
{
}
@end

@implementation DeviceInfo
@implementation CountlyDeviceInfo

+ (NSString *)udid
{
Expand Down Expand Up @@ -152,21 +159,21 @@ + (NSString *)metrics
{
NSString *result = @"{";

result = [result stringByAppendingFormat:@"\"%@\":\"%@\"", @"_device", [DeviceInfo device]];
result = [result stringByAppendingFormat:@"\"%@\":\"%@\"", @"_device", [CountlyDeviceInfo device]];

result = [result stringByAppendingFormat:@",\"%@\":\"%@\"", @"_os", @"iOS"];

result = [result stringByAppendingFormat:@",\"%@\":\"%@\"", @"_os_version", [DeviceInfo osVersion]];
result = [result stringByAppendingFormat:@",\"%@\":\"%@\"", @"_os_version", [CountlyDeviceInfo osVersion]];

NSString *carrier = [DeviceInfo carrier];
NSString *carrier = [CountlyDeviceInfo carrier];
if (carrier != nil)
result = [result stringByAppendingFormat:@",\"%@\":\"%@\"", @"_carrier", carrier];

result = [result stringByAppendingFormat:@",\"%@\":\"%@\"", @"_resolution", [DeviceInfo resolution]];
result = [result stringByAppendingFormat:@",\"%@\":\"%@\"", @"_resolution", [CountlyDeviceInfo resolution]];

result = [result stringByAppendingFormat:@",\"%@\":\"%@\"", @"_locale", [DeviceInfo locale]];
result = [result stringByAppendingFormat:@",\"%@\":\"%@\"", @"_locale", [CountlyDeviceInfo locale]];

result = [result stringByAppendingFormat:@",\"%@\":\"%@\"", @"_app_version", [DeviceInfo appVersion]];
result = [result stringByAppendingFormat:@",\"%@\":\"%@\"", @"_app_version", [CountlyDeviceInfo appVersion]];

result = [result stringByAppendingString:@"}"];

Expand All @@ -177,6 +184,9 @@ + (NSString *)metrics

@end


#pragma mark - CountlyEvent

@interface CountlyEvent : NSObject
{
}
Expand Down Expand Up @@ -219,12 +229,15 @@ - (void)dealloc

@end

@interface EventQueue : NSObject

#pragma mark - CountlyEventQueue

@interface CountlyEventQueue : NSObject

@end


@implementation EventQueue
@implementation CountlyEventQueue

- (void)dealloc
{
Expand Down Expand Up @@ -462,7 +475,10 @@ - (void)recordEvent:(NSString *)key segmentation:(NSDictionary *)segmentation co

@end

@interface ConnectionQueue : NSObject

#pragma mark - CountlyConnectionQueue

@interface CountlyConnectionQueue : NSObject
{
NSURLConnection *connection_;
UIBackgroundTaskIdentifier bgTask_;
Expand All @@ -475,19 +491,19 @@ @interface ConnectionQueue : NSObject

@end

static ConnectionQueue *s_sharedConnectionQueue = nil;
static CountlyConnectionQueue *s_sharedCountlyConnectionQueue = nil;

@implementation ConnectionQueue : NSObject
@implementation CountlyConnectionQueue : NSObject

@synthesize appKey;
@synthesize appHost;

+ (ConnectionQueue *)sharedInstance
+ (CountlyConnectionQueue *)sharedInstance
{
if (s_sharedConnectionQueue == nil)
s_sharedConnectionQueue = [[ConnectionQueue alloc] init];
if (s_sharedCountlyConnectionQueue == nil)
s_sharedCountlyConnectionQueue = [[CountlyConnectionQueue alloc] init];

return s_sharedConnectionQueue;
return s_sharedCountlyConnectionQueue;
}

- (id)init
Expand Down Expand Up @@ -527,9 +543,9 @@ - (void)beginSession
{
NSString *data = [NSString stringWithFormat:@"app_key=%@&device_id=%@&timestamp=%ld&sdk_version="COUNTLY_VERSION"&begin_session=1&metrics=%@",
appKey,
[DeviceInfo udid],
[CountlyDeviceInfo udid],
time(NULL),
[DeviceInfo metrics]];
[CountlyDeviceInfo metrics]];

[[CountlyDB sharedInstance] addToQueue:data];

Expand All @@ -540,7 +556,7 @@ - (void)updateSessionWithDuration:(int)duration
{
NSString *data = [NSString stringWithFormat:@"app_key=%@&device_id=%@&timestamp=%ld&session_duration=%d",
appKey,
[DeviceInfo udid],
[CountlyDeviceInfo udid],
time(NULL),
duration];

Expand All @@ -553,7 +569,7 @@ - (void)endSessionWithDuration:(int)duration
{
NSString *data = [NSString stringWithFormat:@"app_key=%@&device_id=%@&timestamp=%ld&end_session=1&session_duration=%d",
appKey,
[DeviceInfo udid],
[CountlyDeviceInfo udid],
time(NULL),
duration];

Expand All @@ -566,7 +582,7 @@ - (void)recordEvents:(NSString *)events
{
NSString *data = [NSString stringWithFormat:@"app_key=%@&device_id=%@&timestamp=%ld&events=%@",
appKey,
[DeviceInfo udid],
[CountlyDeviceInfo udid],
time(NULL),
events];

Expand Down Expand Up @@ -638,6 +654,9 @@ - (void)dealloc

@end


#pragma mark - Countly Core

static Countly *s_sharedCountly = nil;

@implementation Countly
Expand All @@ -657,7 +676,7 @@ - (id)init
timer = nil;
isSuspended = NO;
unsentSessionLength = 0;
eventQueue = [[EventQueue alloc] init];
eventQueue = [[CountlyEventQueue alloc] init];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didEnterBackgroundCallBack:)
Expand All @@ -677,47 +696,47 @@ - (id)init

- (void)start:(NSString *)appKey withHost:(NSString *)appHost
{
timer = [NSTimer scheduledTimerWithTimeInterval:60.0
timer = [NSTimer scheduledTimerWithTimeInterval:COUNTLY_DEFAULT_UPDATE_INTERVAL
target:self
selector:@selector(onTimer:)
userInfo:nil
repeats:YES];
lastTime = CFAbsoluteTimeGetCurrent();
[[ConnectionQueue sharedInstance] setAppKey:appKey];
[[ConnectionQueue sharedInstance] setAppHost:appHost];
[[ConnectionQueue sharedInstance] beginSession];
[[CountlyConnectionQueue sharedInstance] setAppKey:appKey];
[[CountlyConnectionQueue sharedInstance] setAppHost:appHost];
[[CountlyConnectionQueue sharedInstance] beginSession];
}

- (void)recordEvent:(NSString *)key count:(int)count
{
[eventQueue recordEvent:key count:count];

if (eventQueue.count >= 10)
[[ConnectionQueue sharedInstance] recordEvents:[eventQueue events]];
if (eventQueue.count >= COUNTLY_EVENT_SEND_THRESHOLD)
[[CountlyConnectionQueue sharedInstance] recordEvents:[eventQueue events]];
}

- (void)recordEvent:(NSString *)key count:(int)count sum:(double)sum
{
[eventQueue recordEvent:key count:count sum:sum];

if (eventQueue.count >= 10)
[[ConnectionQueue sharedInstance] recordEvents:[eventQueue events]];
if (eventQueue.count >= COUNTLY_EVENT_SEND_THRESHOLD)
[[CountlyConnectionQueue sharedInstance] recordEvents:[eventQueue events]];
}

- (void)recordEvent:(NSString *)key segmentation:(NSDictionary *)segmentation count:(int)count
{
[eventQueue recordEvent:key segmentation:segmentation count:count];

if (eventQueue.count >= 10)
[[ConnectionQueue sharedInstance] recordEvents:[eventQueue events]];
if (eventQueue.count >= COUNTLY_EVENT_SEND_THRESHOLD)
[[CountlyConnectionQueue sharedInstance] recordEvents:[eventQueue events]];
}

- (void)recordEvent:(NSString *)key segmentation:(NSDictionary *)segmentation count:(int)count sum:(double)sum
{
[eventQueue recordEvent:key segmentation:segmentation count:count sum:sum];

if (eventQueue.count >= 10)
[[ConnectionQueue sharedInstance] recordEvents:[eventQueue events]];
if (eventQueue.count >= COUNTLY_EVENT_SEND_THRESHOLD)
[[CountlyConnectionQueue sharedInstance] recordEvents:[eventQueue events]];
}

- (void)onTimer:(NSTimer *)timer
Expand All @@ -730,33 +749,33 @@ - (void)onTimer:(NSTimer *)timer
lastTime = currTime;

int duration = unsentSessionLength;
[[ConnectionQueue sharedInstance] updateSessionWithDuration:duration];
[[CountlyConnectionQueue sharedInstance] updateSessionWithDuration:duration];
unsentSessionLength -= duration;

if (eventQueue.count > 0)
[[ConnectionQueue sharedInstance] recordEvents:[eventQueue events]];
[[CountlyConnectionQueue sharedInstance] recordEvents:[eventQueue events]];
}

- (void)suspend
{
isSuspended = YES;

if (eventQueue.count > 0)
[[ConnectionQueue sharedInstance] recordEvents:[eventQueue events]];
[[CountlyConnectionQueue sharedInstance] recordEvents:[eventQueue events]];

double currTime = CFAbsoluteTimeGetCurrent();
unsentSessionLength += currTime - lastTime;

int duration = unsentSessionLength;
[[ConnectionQueue sharedInstance] endSessionWithDuration:duration];
[[CountlyConnectionQueue sharedInstance] endSessionWithDuration:duration];
unsentSessionLength -= duration;
}

- (void)resume
{
lastTime = CFAbsoluteTimeGetCurrent();

[[ConnectionQueue sharedInstance] beginSession];
[[CountlyConnectionQueue sharedInstance] beginSession];

isSuspended = NO;
}
Expand Down

0 comments on commit e35be92

Please sign in to comment.