1 | from datetime import * |
---|
2 | from time import strftime |
---|
3 | import calendar, operator |
---|
4 | from galaxy.web.base.controller import * |
---|
5 | import galaxy.model |
---|
6 | from galaxy.model.orm import * |
---|
7 | import pkg_resources |
---|
8 | pkg_resources.require( "SQLAlchemy >= 0.4" ) |
---|
9 | import sqlalchemy as sa |
---|
10 | import logging |
---|
11 | log = logging.getLogger( __name__ ) |
---|
12 | |
---|
13 | class Users( BaseController ): |
---|
14 | @web.expose |
---|
15 | def registered_users( self, trans, **kwd ): |
---|
16 | message = util.restore_text( kwd.get( 'message', '' ) ) |
---|
17 | num_users = trans.sa_session.query( galaxy.model.User ).count() |
---|
18 | return trans.fill_template( '/webapps/reports/registered_users.mako', num_users=num_users, message=message ) |
---|
19 | @web.expose |
---|
20 | def registered_users_per_month( self, trans, **kwd ): |
---|
21 | message = util.restore_text( kwd.get( 'message', '' ) ) |
---|
22 | q = sa.select( ( sa.func.date_trunc( 'month', sa.func.date( galaxy.model.User.table.c.create_time ) ).label( 'date' ), |
---|
23 | sa.func.count( galaxy.model.User.table.c.id ).label( 'num_users' ) ), |
---|
24 | from_obj = [ galaxy.model.User.table ], |
---|
25 | group_by = [ sa.func.date_trunc( 'month', sa.func.date( galaxy.model.User.table.c.create_time ) ) ], |
---|
26 | order_by = [ sa.desc( 'date' ) ] ) |
---|
27 | users = [] |
---|
28 | for row in q.execute(): |
---|
29 | users.append( ( row.date.strftime( "%Y-%m" ), |
---|
30 | row.num_users, |
---|
31 | row.date.strftime( "%B" ), |
---|
32 | row.date.strftime( "%Y" ) ) ) |
---|
33 | return trans.fill_template( '/webapps/reports/registered_users_per_month.mako', |
---|
34 | users=users, |
---|
35 | message=message ) |
---|
36 | @web.expose |
---|
37 | def specified_month( self, trans, **kwd ): |
---|
38 | message = util.restore_text( kwd.get( 'message', '' ) ) |
---|
39 | # If specified_date is not received, we'll default to the current month |
---|
40 | specified_date = kwd.get( 'specified_date', datetime.utcnow().strftime( "%Y-%m-%d" ) ) |
---|
41 | specified_month = specified_date[ :7 ] |
---|
42 | year, month = map( int, specified_month.split( "-" ) ) |
---|
43 | start_date = date( year, month, 1 ) |
---|
44 | end_date = start_date + timedelta( days=calendar.monthrange( year, month )[1] ) |
---|
45 | month_label = start_date.strftime( "%B" ) |
---|
46 | year_label = start_date.strftime( "%Y" ) |
---|
47 | q = sa.select( ( sa.func.date_trunc( 'day', sa.func.date( galaxy.model.User.table.c.create_time ) ).label( 'date' ), |
---|
48 | sa.func.count( galaxy.model.User.table.c.id ).label( 'num_users' ) ), |
---|
49 | whereclause = sa.and_( galaxy.model.User.table.c.create_time >= start_date, |
---|
50 | galaxy.model.User.table.c.create_time < end_date ), |
---|
51 | from_obj = [ galaxy.model.User.table ], |
---|
52 | group_by = [ sa.func.date_trunc( 'day', sa.func.date( galaxy.model.User.table.c.create_time ) ) ], |
---|
53 | order_by = [ sa.desc( 'date' ) ] ) |
---|
54 | users = [] |
---|
55 | for row in q.execute(): |
---|
56 | users.append( ( row.date.strftime( "%Y-%m-%d" ), |
---|
57 | row.date.strftime( "%d" ), |
---|
58 | row.num_users, |
---|
59 | row.date.strftime( "%A" ) ) ) |
---|
60 | return trans.fill_template( '/webapps/reports/registered_users_specified_month.mako', |
---|
61 | month_label=month_label, |
---|
62 | year_label=year_label, |
---|
63 | month=month, |
---|
64 | users=users, |
---|
65 | message=message ) |
---|
66 | @web.expose |
---|
67 | def specified_date( self, trans, **kwd ): |
---|
68 | message = util.restore_text( kwd.get( 'message', '' ) ) |
---|
69 | # If specified_date is not received, we'll default to the current month |
---|
70 | specified_date = kwd.get( 'specified_date', datetime.utcnow().strftime( "%Y-%m-%d" ) ) |
---|
71 | year, month, day = map( int, specified_date.split( "-" ) ) |
---|
72 | start_date = date( year, month, day ) |
---|
73 | end_date = start_date + timedelta( days=1 ) |
---|
74 | day_of_month = start_date.strftime( "%d" ) |
---|
75 | day_label = start_date.strftime( "%A" ) |
---|
76 | month_label = start_date.strftime( "%B" ) |
---|
77 | year_label = start_date.strftime( "%Y" ) |
---|
78 | q = sa.select( ( sa.func.date_trunc( 'day', sa.func.date( galaxy.model.User.table.c.create_time ) ).label( 'date' ), |
---|
79 | galaxy.model.User.table.c.email ), |
---|
80 | whereclause = sa.and_( galaxy.model.User.table.c.create_time >= start_date, |
---|
81 | galaxy.model.User.table.c.create_time < end_date ), |
---|
82 | from_obj = [ galaxy.model.User.table ], |
---|
83 | order_by = [ galaxy.model.User.table.c.email ] ) |
---|
84 | users = [] |
---|
85 | for row in q.execute(): |
---|
86 | users.append( ( row.email ) ) |
---|
87 | return trans.fill_template( '/webapps/reports/registered_users_specified_date.mako', |
---|
88 | specified_date=start_date, |
---|
89 | day_label=day_label, |
---|
90 | month_label=month_label, |
---|
91 | year_label=year_label, |
---|
92 | day_of_month=day_of_month, |
---|
93 | users=users, |
---|
94 | message=message ) |
---|
95 | @web.expose |
---|
96 | def last_access_date( self, trans, **kwd ): |
---|
97 | message = util.restore_text( kwd.get( 'message', '' ) ) |
---|
98 | not_logged_in_for_days = kwd.get( 'not_logged_in_for_days', 90 ) |
---|
99 | if not not_logged_in_for_days: |
---|
100 | not_logged_in_for_days = 0 |
---|
101 | cutoff_time = datetime.utcnow() - timedelta( days=int( not_logged_in_for_days ) ) |
---|
102 | now = strftime( "%Y-%m-%d %H:%M:%S" ) |
---|
103 | users = [] |
---|
104 | for user in trans.sa_session.query( galaxy.model.User ) \ |
---|
105 | .filter( galaxy.model.User.table.c.deleted==False ) \ |
---|
106 | .order_by( galaxy.model.User.table.c.email ): |
---|
107 | if user.galaxy_sessions: |
---|
108 | last_galaxy_session = user.galaxy_sessions[ 0 ] |
---|
109 | if last_galaxy_session.update_time < cutoff_time: |
---|
110 | users.append( ( user.email, last_galaxy_session.update_time.strftime( "%Y-%m-%d" ) ) ) |
---|
111 | else: |
---|
112 | # The user has never logged in |
---|
113 | users.append( ( user.email, "never logged in" ) ) |
---|
114 | users = sorted( users, key=operator.itemgetter( 1 ), reverse=True ) |
---|
115 | return trans.fill_template( '/webapps/reports/users_last_access_date.mako', |
---|
116 | users=users, |
---|
117 | not_logged_in_for_days=not_logged_in_for_days, |
---|
118 | message=message ) |
---|