Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

"""Generate some stats on the bookmarks in the syste 

 

Stats we want to track 

 

- total bookmarks per day 

- total # of tags in the system per day 

- unique...not sure 

 

- per user - number of bookmarks they have that day 

 

- the popularity tracking numbers...let's show most popular by clicks? not 

really stats 

 

- outstanding invites 

- invites sent but not accepted 

 

# do the users thing as an hourly job, but assign a letter per hour of the day 

# and run it that way. on hour 0 run A users, on hour 1 run B users, on hour 

# 23 run xzy users. 

 

""" 

from datetime import datetime 

 

from sqlalchemy import Column 

from sqlalchemy import DateTime 

from sqlalchemy import Integer 

from sqlalchemy import Unicode 

 

from bookie.models import Base 

from bookie.models import DBSession 

from bookie.models import BmarkMgr 

from bookie.models import TagMgr 

from bookie.models.queue import ImportQueueMgr 

 

 

IMPORTER_CT = 'importer_queue' 

TOTAL_CT = 'user_bookmarks' 

UNIQUE_CT = 'unique_bookmarks' 

TAG_CT = 'total_tags' 

 

 

class StatBookmarkMgr(object): 

    """Handle our agg stuff for the stats on bookmarks""" 

 

    @staticmethod 

    def get_stat(start, end, *stats): 

        """Fetch the records from the stats table for these guys""" 

        qry = StatBookmark.query 

        qry = qry.filter(StatBookmark.tstamp > start) 

        qry = qry.filter(StatBookmark.tstamp <= end) 

 

        if stats: 

            qry = qry.filter(StatBookmark.attrib.in_(stats)) 

 

        # order things up by their date so they're grouped together 

        qry.order_by(StatBookmark.tstamp) 

        return qry.all() 

 

    @staticmethod 

    def count_unique_bookmarks(): 

        """Count the unique number of bookmarks in the system""" 

        total = BmarkMgr.count(distinct=True) 

        stat = StatBookmark(attrib=UNIQUE_CT, data=total) 

        DBSession.add(stat) 

 

    @staticmethod 

    def count_total_bookmarks(): 

        """Count the total number of bookmarks in the system""" 

        total = BmarkMgr.count() 

        stat = StatBookmark(attrib=TOTAL_CT, data=total) 

        DBSession.add(stat) 

 

    @staticmethod 

    def count_total_tags(): 

        """Count the total number of tags in the system""" 

        total = TagMgr.count() 

        stat = StatBookmark(attrib=TAG_CT, data=total) 

        DBSession.add(stat) 

 

    @staticmethod 

    def count_importer_depth(): 

        """Mark how deep the importer queue is at the moment""" 

        total = ImportQueueMgr.size() 

        stat = StatBookmark(attrib=IMPORTER_CT, data=total) 

        DBSession.add(stat) 

 

 

class StatBookmark(Base): 

    """First stats we track are the counts of things. 

 

    """ 

    __tablename__ = 'stats_bookmarks' 

 

    id = Column(Integer, autoincrement=True, primary_key=True) 

    tstamp = Column(DateTime, default=datetime.utcnow) 

    attrib = Column(Unicode(100), nullable=False) 

    data = Column(Integer, nullable=False, default=0) 

 

    def __init__(self, **kwargs): 

        self.attrib = kwargs.get('attrib', 'unknown') 

        self.data = kwargs.get('data', 0)