2015-04-01 39 views
1

我想创建一个算法,但不知道如何开始。及时准确的连续数据算法

该算法实际上是一个接受具有一些属性createdAt值的N个对象的数组的方法。 我会将数组从旧到新排序(createdAt),然后我必须找出可用数据的一致性,也就是说,每隔一小时我至少有5条记录,并且每半小时记录两条记录。

例-testcode:

- (void) normalizeData:(NSArray*)records 
{ 
// sort the records 
NSArray* sortedRecords = [records sortWithCreatedAt]; 

// split all dates in the records, distinct them, and create a dictionary with a key for every date, for value create another dictionary with the hour as key and the records as the value. 

NSArray* distinctDates = [sortedRecords valueForKeyPath:@"@distinctUnionOfObjects.createdAt"]; // should only consider month-day-year-hour 
NSMutableDictionary* dictionary = [NSMutableDictionary dictionary]; 
for (NSDate* date in distinctDates) 
    { 
    NSString* stringDate = [date string]; 
    NSArray* recordsForDate = [sortedRecords valueForKeyPath:[NSString stringWithFormat:@"[collect].{createdAt=%@}.self", stringDate]]; // let's say you got them with this line 
    [dictionary setObject:recordsForDate forKey:date]; 
    } 

for (NSDate* keyDate in dictionary) 
    { 
    NSArray* records = [dictionary objectForKey:keyDate]; 
    Record* previousRecord = nil; 
    for (Records* record in records) 
     { 
     // I'll have to keep the previous record and compare the time difference with the new 
     NSInteger secondsAfterDate = 0; 
     if (previousRecord) 
     { 
     secondsAfterDate = [record.createdAt timeIntervalSinceDate:previousRecord.createdAt]; 
     // add logic to create trend difference in a model that has for every hour of the records count, the records and suffice description 
     // logic if the records count and timespan is suffice. 

     } 
     previousRecord = record; 
     } 
    } 
} 

我希望在该方法的过程中的任何贡献。

此外,最终目标是为处理的记录的每个结果创建一个返回(调用块处理程序)。 逻辑应该以每小时至少5条记录结束,并且在15分钟之内它们之间的时间间隔。

+0

当你的意思是,你需要5个记录每小时,你晚上11点12点和,或5分离(在有序阵列“指数”条款)值的时间差已是不到一个小时之间是什么意思? – Larme 2015-04-01 20:45:50

+0

对于每个小时(晚上11点至12点),我至少需要5条记录,时间约15分钟。这样我可以说我有足够的数据来处理趋势变化。 – 2015-04-01 20:53:58

回答

0

取的记录集(第一条记录的createdAt和最后一个记录的createdAt之差)的总时间长度,并将其离散成箱。将每个对象放在适当的bin中。然后使用两个窗口大小的滑动窗口(30分钟和60分钟)。当你沿着阵列行走时,不断评估你描述的条件是否得到满足。

请注意,对于上述方法,正确定义垃圾箱宽度作为您的时间戳过程的解决方案非常重要。既然你没有在你的帖子中表明这一点,如果这是一个问题,随时发表评论。

+0

感谢您的贡献。我会尽快检查你的建议。 – 2015-04-07 14:55:53