Beispiel #1
0
def stats_cache_into_json():
    global JxStateArray,JxStatsMap
    Today = int(time.time() / 86400.0)
    JxGraphsJSon = {}
    for (Type,List) in JxStatsMap.iteritems():
        for (k, Map) in enumerate(List):
            for (Key,String) in Map.Order +[('Other','Other')]:
                try:
                    Dict = JxStateArray[(Type,k,Key)]
                except KeyError:
                    Dict = {Today:0}
                if Today not in Dict:
                    Dict[Today] = 0
                keys = Dict.keys()		
                JxGraphsJSon[(Type,k,Key)] =  JxJSon([(Day-Today,sum([Dict[day] for day in keys if day <=Day])) for Day in range(min(keys), max(keys) + 1)]) 
    return JxGraphsJSon
Beispiel #2
0
def compute_count(): 
    """Computes the stats"""
    
    global JxStatsArray,JxPartitionLists,NoType
    
    JxCache = load_cache()
    try:
        Query = """select cards.factId, cards.id, cards.reps, cards.interval from cards, 
	cards as mCards where mCards.modified>%s and cards.factId=mCards.factId 
	group by cards.id order by cards.factId""" % JxCache['TimeCached']
        JxStatsArray = JxCache['Stats']
        JxPartitionLists = JxCache['Partitions']
        NoType = JxCache['NoType']
    except:
        Query = """select factId, id, reps, interval from cards order by factId"""
        NoType = 0 # known/seen/in deck
        for (Type,List) in JxStatsMap.iteritems():
            for (k, Map) in enumerate(List):
                for (Key,String) in Map.Order+[('Other','Other')]:
                    if k != 1:
                        JxStatsArray[(Type,k,Key)] = (0, 0, 0, 
			        len([Item for (Item,Value) in Map.Dict.iteritems() if Value == Key])) 
                    elif Type =='Word':
                        JxStatsArray[(Type,k,Key)] = (0, 0, 0, sum([Jx_Word_Occurences[Item] 
				for (Item,Value) in Map.Dict.iteritems() if Value == Key]))
                    else:
                        JxStatsArray[(Type,k,Key)] = (0, 0, 0, sum([Jx_Kanji_Occurences[Item] 
			        for (Item,Value) in Map.Dict.iteritems() if Value == Key])) 
                    for Label in ['Known','Seen','InDeck']:
                        JxPartitionLists[(Type,k,Key,Label)] = []	
			
    # we compute known/seen/in deck/total stats for each value of each map and each type
    Rows = mw.deck.s.all(Query)  
    CardState = []
    Length = len(Rows)
    Index = 0
    while True and Index<Length:
        (FactId,CardId,CardRep,Interval) = Rows[Index]
        # set the card's status                       
        if Interval > 21 and CardRep:
            CardState.append(0)
        elif CardRep:
            CardState.append(1)
        else:
            CardState.append(2)
        Index += 1
        if Index == Length: 
            # we have finished parsing the Entries.Flush the last Fact and break
            JxFlushFactStats(CardState,CardId)
            break
            # we have finished parsing the Entries, flush the Status change
        elif FactId == Rows[Index][0]:
            # Same Fact : Though it does nothing, we put this here for speed purposes because it happens a lot.
            pass
        else:                        
            # Fact change
            JxFlushFactStats(CardState,CardId)
            CardState = []
	    
    # now cache the updated stats  
    JxCache['Stats'] = JxStatsArray
    JxCache['Partitions'] = JxPartitionLists
    JxCache['NoType'] = NoType
    JxCache['TimeCached'] = time() # among the few things that coul corrupt the cache : 
    # new entries in the database before the cache was saved...sigh...
    save_cache(JxCache)