-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetasomeDataPointStore.m
More file actions
310 lines (234 loc) · 9.1 KB
/
MetasomeDataPointStore.m
File metadata and controls
310 lines (234 loc) · 9.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
//
// MetasomeDataPointStore.m
// Metasome
//
// Created by Omar Metwally on 8/20/13.
// Copyright (c) 2013 Logisome. All rights reserved.
//
#import "MetasomeDataPointStore.h"
#import "MetasomeDatapoint.h"
@implementation MetasomeDataPointStore
@synthesize toGraph, since;
+(MetasomeDataPointStore *)sharedStore
{
static MetasomeDataPointStore *sharedStore = nil;
if (!sharedStore) {
sharedStore = [[super allocWithZone:nil] init];
}
return sharedStore;
}
+(id)allocWithZone:(NSZone *)zone
{
return [self sharedStore];
}
-(id)init
{
self = [super init];
if (self) {
// Read in Metasome.xcdatamodeld
model = [NSManagedObjectModel mergedModelFromBundles:nil];
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
// Where does the SQLite file go?
NSString *path = [self itemArchivePath];
NSURL *storeURL = [NSURL fileURLWithPath:path];
NSError *error = nil;
if (![psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
[NSException raise:@"Open failed" format:@"Reason: %@", [error localizedDescription]];
}
// Create the managed object context
context = [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:psc];
// The managed object context can manage undo, but we don't need it
[context setUndoManager:nil];
since = nil;
[self loadAllPoints];
}
return self;
}
-(MetasomeDataPoint *)addPointWithName:(NSString *)pName value:(float)pValue date:(float)pDate options:(int)optionsValue fromApi:(NSString *)apiName
{
// derive the hour of the day to insert into "hour" column
NSDate *tempDate = [NSDate dateWithTimeIntervalSince1970:pDate];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:tempDate];
int newHour = [components hour];
float red = 0.0;
float green = 0.0;
float blue = 0.0;
// set the order
double order;
if ([allPoints count] == 0) {
order = 1.0;
} else {
order = [[allPoints lastObject] orderingValue] + 1.0;
}
// create point object and add to 'allPoints' array
MetasomeDataPoint *newPoint = [NSEntityDescription insertNewObjectForEntityForName:@"DataPoints" inManagedObjectContext:context];
[newPoint setParameterName:pName];
[newPoint setParameterValue:pValue];
[newPoint setPDate:pDate];
[newPoint setOrderingValue:order];
[newPoint setHour:newHour];
[newPoint setOptions:optionsValue];
if (apiName) {
[newPoint setApi:apiName];
}
// set the points' RGB values
if ( newHour>= 4 && newHour < 9 )
red = 1.0;
else if ( newHour >=9 && newHour < 19 )
green = 1.0;
else
blue = 1.0;
[newPoint setRed:red];
[newPoint setGreen:green];
[newPoint setBlue:blue];
[allPoints addObject:newPoint];
// method returns a pointer to a MetasomeDataPoint object
return newPoint;
}
-(void)removePoint:(MetasomeDataPoint *)p
{
[context deleteObject:p];
[allPoints removeObjectIdenticalTo:p];
}
-(void)moveItemAtIndex:(int)from toIndex:(int)to
{
if (from == to) return;
MetasomeDataPoint *p = [allPoints objectAtIndex:from];
[allPoints removeObjectAtIndex:from];
[allPoints insertObject:p atIndex:to];
// Compte a new orderingValue fro the object that was moved
double lowerBound = 0.0;
// Is there an object before it in the array?
if (to > 0) {
lowerBound = [[allPoints objectAtIndex:to - 1] orderingValue];
} else {
lowerBound = [[allPoints objectAtIndex:1] orderingValue] - 2.0;
}
double upperBound = 0.0;
// Is there an object after it in the array?
if (to < [allPoints count] - 1) {
upperBound = [[allPoints objectAtIndex:to + 1] orderingValue];
} else {
upperBound = [[allPoints objectAtIndex:to -1] orderingValue] + 2.0;
}
double newOrderValue = (lowerBound + upperBound) / 2.0;
[p setOrderingValue:newOrderValue];
}
-(NSString *)itemArchivePath
{
NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// Get the one and only document directory
NSString *documentDirectory = [documentDirectories objectAtIndex:0];
return [documentDirectory stringByAppendingPathComponent:@"datapoints.data"];
}
-(BOOL)saveChanges
{
NSError *err = nil;
BOOL successful = [context save:&err];
if (!successful) {
NSLog(@"Error saving: %@", [err localizedDescription]);
}
return successful;
}
-(NSArray *)pointsWithParameterName:(NSString *)pn fromDate:(NSDate *)fromDate toDate:(NSDate *)toDate
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *e = [[model entitiesByName] objectForKey:@"DataPoints"];
[request setEntity:e];
NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"pDate" ascending:YES];
NSPredicate *p = [NSPredicate predicateWithFormat:@"(parameterName like %@) AND (pDate > %f)", pn, fromDate.timeIntervalSince1970];
[request setPredicate:p];
[request setSortDescriptors:[NSArray arrayWithObject:sd]];
NSError *error;
NSArray *result = [context executeFetchRequest:request error:&error];
if (!result) {
[NSException raise:@"Fetch failed" format:@"Reason: %@", [error localizedDescription]];
}
return result;
}
-(void)deletePointsFromApi:(NSString *)apiName fromDate:(NSDate *)fromDate toDate:(NSDate *)toDate
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *e = [[model entitiesByName] objectForKey:@"DataPoints"];
[request setEntity:e];
NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"pDate" ascending:YES];
NSPredicate *p;
if (fromDate) {
p = [NSPredicate predicateWithFormat:@"(api like %@) AND (pDate > %f)", apiName, fromDate.timeIntervalSince1970];
} else {
p = [NSPredicate predicateWithFormat:@"api like %@", apiName];
}
[request setPredicate:p];
[request setSortDescriptors:[NSArray arrayWithObject:sd]];
NSError *error;
NSArray *result = [context executeFetchRequest:request error:&error];
if (!result) {
[NSException raise:@"Fetch failed" format:@"Reason: %@", [error localizedDescription]];
}
for (NSManagedObject *obj in result) {
[context deleteObject:obj];
}
[self saveChanges];
}
-(void)loadAllPoints
{
if (!allPoints) {
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *e = [[model entitiesByName] objectForKey:@"DataPoints"];
[request setEntity:e];
NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"pDate" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sd]];
[request setIncludesPropertyValues:NO]; // only create pointers but does not fetch objects values
NSError *error;
NSArray *result = [context executeFetchRequest:request error:&error];
if (!result) {
[NSException raise:@"Fetch failed" format:@"Reason: %@", [error localizedDescription]];
}
allPoints = [[NSMutableArray alloc] initWithArray:result];
}
}
-(NSMutableArray *)allPoints
{
return allPoints;
}
-(void)deleteAllPoints
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *e = [[model entitiesByName] objectForKey:@"DataPoints"];
[request setEntity:e];
[request setIncludesPropertyValues:NO]; // only creates pointers but does not fetch object values
NSError *error;
NSArray *result = [context executeFetchRequest:request error:&error];
for (NSManagedObject *obj in result) {
[context deleteObject:obj];
}
[self saveChanges];
}
-(void)deleteTodayPoints
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *e = [[model entitiesByName] objectForKey:@"DataPoints"];
[request setEntity:e];
// yesterday's time stamp
float pYesterdayDate = [[NSDate date] timeIntervalSince1970]- 86400;
NSPredicate *p = [NSPredicate predicateWithFormat:@"pDate > %f", pYesterdayDate];
[request setPredicate:p];
NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"pDate" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sd]];
NSError *error;
NSArray *result = [context executeFetchRequest:request error:&error];
if (!result) {
[NSException raise:@"Fetch failed" format:@"Reason: %@", [error localizedDescription]];
}
for (NSManagedObject *obj in result) {
[context deleteObject:obj];
}
[self saveChanges];
}
-(NSManagedObjectContext *)context
{
return context;
}
@end