Returns audit entry counts grouped by user and calendar day.
182564
def CountAPIAuditEntriesByUserAndDay(self,
                                       min_timestamp=None,
                                       max_timestamp=None):
    """"""
    results = collections.Counter()
    for entry in self.api_audit_entries:
      if min_timestamp is not None and entry.timestamp < min_timestamp:
        continue

      if max_timestamp is not None and entry.timestamp > max_timestamp:
        continue

      # Truncate DateTime by removing the time-part to allow grouping by date.
      day = rdfvalue.RDFDatetime.FromDate(entry.timestamp.AsDatetime().date())
      results[(entry.username, day)] += 1

    return dict(results)
943880
def get_allEvents(self):
        '''
        
        '''

        if not hasattr(self,'allEvents'):
            timeFilters = {'endTime__gte': timezone.now()}
            if getConstant('registration__displayLimitDays') or 0 > 0:
                timeFilters['startTime__lte'] = timezone.now() + timedelta(days=getConstant('registration__displayLimitDays'))

            # Get the Event listing here to avoid duplicate queries
            self.allEvents = Event.objects.filter(
                **timeFilters
            ).filter(
                Q(instance_of=PublicEvent) |
                Q(instance_of=Series)
            ).annotate(
                **self.get_annotations()
            ).exclude(
                Q(status=Event.RegStatus.hidden) |
                Q(status=Event.RegStatus.regHidden) |
                Q(status=Event.RegStatus.linkOnly)
            ).order_by(*self.get_ordering())

        return self.allEvents
1177023
def get_audits():
    
    audits = []
    audits.append(TemplatedFile('/etc/securetty', SecureTTYContext(),
                                template_dir=TEMPLATES_DIR,
                                mode=0o0400, user='root', group='root'))
    return audits
1014681
def data(self, data):
        """"""
        if self.state == STATE_SOURCE_ID:
            self.context.audit_record.source_id = int(data)  # Audit ids can be 64 bits
        elif self.state == STATE_DATETIME:
            dt = datetime.datetime.strptime(data, "%Y-%m-%dT%H:%M:%S")
            self.get_parent_element().datetimestamp = dt
        elif self.state == STATE_REASON_FOR_CHANGE:
            self.context.audit_record.reason_for_change = data.strip() or None  # Convert a result of '' to None.
        self.state = STATE_NONE
1087816
def active(self, registered_only=True):
        ""
        visitors = self.filter(
            expiry_time__gt=timezone.now(),
            end_time=None
        )
        if registered_only:
            visitors = visitors.filter(user__isnull=False)
        return visitors
1132564
def users_with_birthday(self, month, day):
        """"""

        users = User.objects.filter(properties___birthday__month=month, properties___birthday__day=day)
        results = []
        for user in users:
            # TODO: permissions system
            results.append(user)

        return results
1173835
protected function aggregateUsers()
    {
        $userIdFieldName = self::USER_ID_FIELD;
        $visitorIdFieldName = self::VISITOR_ID_FIELD;

        /** @var \Zend_Db_Statement $query */
        $query = $this->getLogAggregator()->queryVisitsByDimension(
            array(self::USER_ID_FIELD),
            "log_visit.$userIdFieldName IS NOT NULL AND log_visit.$userIdFieldName != ''",
            array("LOWER(HEX($visitorIdFieldName)) as $visitorIdFieldName")
        );

        if ($query === false) {
            return;
        }

        $rowsCount = 0;
        while ($row = $query->fetch()) {
            $rowsCount++;
            $this->arrays->sumMetricsVisits($row[$userIdFieldName], $row);
            $this->rememberVisitorId($row);
        }
    }
871650
def user_data(user):
    
    # For the dasboard, indicators are computed on a daily basis
    # and by taking into account empty time windows
    _range = _group_range(user.records, 'day')
    export = OrderedDict([
        ('name', 'me'),
        ('date_range', [x.strftime('%Y-%m-%d') for x in _range]),
        ('groupby', 'day'),
        ('indicators', OrderedDict()),
        ('agg', OrderedDict())
    ])

    class Indicator(object):
        def __init__(self, name, function, interaction=None, **args):
            self.name = name
            self.function = function
            self.interaction = interaction
            self.args = args

    I = Indicator
    import bandicoot.individual as iv

    indicators_list = [
        I('nb_out_call', iv.number_of_interactions, 'call', direction='out'),
        I('nb_out_text', iv.number_of_interactions, 'text', direction='out'),
        I('nb_inc_call', iv.number_of_interactions, 'call', direction='in'),
        I('nb_inc_text', iv.number_of_interactions, 'text', direction='in'),
        I('nb_out_all', iv.number_of_interactions, 'callandtext', direction='out'),
        I('nb_inc_all', iv.number_of_interactions, 'callandtext', direction='in'),
        I('nb_all', iv.number_of_interactions, 'callandtext'),
        I('response_delay', iv.response_delay_text, 'callandtext'),
        I('response_rate', iv.response_rate_text, 'callandtext'),
        I('call_duration', iv.call_duration, 'call'),
        I('percent_initiated_interactions', iv.percent_initiated_interactions, 'call'),
        I('percent_initiated_conversations', iv.percent_initiated_interactions, 'callandtext'),
        I('active_day', iv.active_days, 'callandtext'),
        I('number_of_contacts', iv.number_of_contacts, 'callandtext'),
        I('percent_nocturnal', iv.percent_nocturnal, 'callandtext'),
        I('balance_of_contacts', iv.balance_of_contacts, 'callandtext', weighted=False),
    ]

    ARGS = {'groupby': 'day', 'summary': None, 'filter_empty': False}
    for i in indicators_list:
        arguments = i.args
        arguments.update(ARGS)
        rv = i.function(user, interaction=i.interaction, **arguments)
        export['indicators'][i.name] = rv['allweek']['allday'][i.interaction]

    # Format percentages from [0, 1] to [0, 100]
    with_percentage = ['percent_initiated_interactions', 'percent_nocturnal',
                       'response_rate', 'balance_of_contacts',
                       'percent_initiated_conversations']

    def apply_percent(d):
        if isinstance(d, list):
            return [apply_percent(dd) for dd in d]
        elif d is None:
            return d
        else:
            return 100. * d

    for i in with_percentage:
        export['indicators'][i] = apply_percent(export['indicators'][i])

    # Day by day network
    def groupby_day_correspondent(r):
        return (r.datetime.strftime('%Y-%m-%d'), r.correspondent_id)
    it = itertools.groupby(user.records, groupby_day_correspondent)
    export['network'] = [list(key) + [len(list(value))] for key, value in it]

    return export
642145
def absence_count(self):
        
        # FIXME: remove recursive dep
        from ..eighth.models import EighthSignup

        return EighthSignup.objects.filter(user=self, was_absent=True, scheduled_activity__attendance_taken=True).count()
286778
def get_all_days():
    
    weekly = get_weekly()
    daily = get_daily()

    return [w * (d / 100.0) for w in weekly for d in daily]
704935
def stats(self, start_date=None, end_date=None, registered_only=False):
        
        pageviews = self.filter(
            visitor__start_time__lt=end_date,
            visitor__start_time__gte=start_date,
        ).select_related('visitor')

        stats = {
            'total': 0,
            'unique': 0,
        }

        stats['total'] = total_views = pageviews.count()
        unique_count = 0

        if not total_views:
            return stats

        # Registered user sessions
        registered_pageviews = pageviews.filter(visitor__user__isnull=False)
        registered_count = registered_pageviews.count()

        if registered_count:
            registered_unique_count = registered_pageviews.values(
                'visitor', 'url').distinct().count()

            # Update the total unique count...
            unique_count += registered_unique_count

            stats['registered'] = {
                'total': registered_count,
                'unique': registered_unique_count,
            }

        if TRACK_ANONYMOUS_USERS and not registered_only:
            guest_pageviews = pageviews.filter(visitor__user__isnull=True)
            guest_count = guest_pageviews.count()

            if guest_count:
                guest_unique_count = guest_pageviews.values(
                    'visitor', 'url').distinct().count()

                # Update the total unique count...
                unique_count += guest_unique_count

                stats['guests'] = {
                    'total': guest_count,
                    'unique': guest_unique_count,
                }

        # Finish setting the total visitor counts
        stats['unique'] = unique_count

        return stats
609647
def get_hours_data(self, entries, date_headers):
        """"""
        project_totals = get_project_totals(
            entries, date_headers, total_column=False) if entries else []

        data_map = {}
        for rows, totals in project_totals:
            for user, user_id, periods in rows:
                for period in periods:
                    day = period['day']
                    if day not in data_map:
                        data_map[day] = {'billable': 0, 'nonbillable': 0}
                    data_map[day]['billable'] += period['billable']
                    data_map[day]['nonbillable'] += period['nonbillable']

        return data_map
1354285
def getComponentFilters(self,component,event=None,dateTime=None):
        '''
        
        '''

        # Limit to the staff member or staff category specified by the rule.
        if component.staffMember:
            filters = Q(pk=component.staffMember.pk)
        else:
            filters = Q(eventstaffmember__category=component.staffCategory)

        # Handle 'Always' and 'EventOnly' rules first, because they do not require an analysis of intervals.
        if component.admissionRule == 'EventOnly' and event:
            # Skip the analysis of intervals and include only those who are staffed for the event.
            return Q(filters & Q(eventstaffmember__event=event))
        elif component.admissionRule in ['Always','EventOnly']:
            # If 'Always' or no event is specified, include all associated staff
            return Q(filters)

        # Start with the event occurrence intervals, or with the specified time.
        if event:
            intervals = [(x.startTime,x.endTime) for x in event.eventoccurrence_set.all()]
        elif dateTime:
            intervals = [(dateTime,dateTime)]
        else:
            raise ValueError(_('Must provide either an event or a datetime to get interval queries.'))

        if component.admissionRule == 'Day':
            # The complete days of each event occurrence
            intervals = [
                (self.getDayStart(x[0]),self.getDayStart(x[1]) + timedelta(days=1)) for x in intervals
            ]
        elif component.admissionRule == 'Week':
            # The complete weeks of each event occurrence
            intervals = [
                (
                    self.getDayStart(x[0]) - timedelta(days=x[0].weekday()),
                    self.getDayStart(x[1]) - timedelta(days=x[1].weekday() - 7)
                ) for x in intervals
            ]
        elif component.admissionRule == 'Month':
            # The complete month of each event occurrence
            intervals = [
                (
                    self.getDayStart(x[0]).replace(day=1),
                    self.getDayStart(x[1]).replace(day=1) + relativedelta(months=1)
                ) for x in intervals
            ]
        elif component.admissionRule == 'Year':
            # The complete years of each event occurrence
            intervals = [
                (
                    self.getDayStart(x[0]).replace(month=1,day=1),
                    self.getDayStart(x[1]).replace(year=x[1].year + 1,month=1,day=1)
                ) for x in intervals
            ]
        else:
            # This is a failsafe that will always evaluate as False.
            return Q(pk__isnull=True)

        # Use intervaltree to create the most parsimonious set of intervals for this interval
        # and then filter on those intervals
        intervals = [sorted(x) for x in intervals]
        tree = IntervalTree.from_tuples(intervals)
        tree.merge_overlaps()

        # Since we are OR appending, start with something that is always False.
        intervalFilters = Q(pk__isnull=True)

        for item in tree.items():
            intervalFilters = intervalFilters | Q(
                Q(eventstaffmember__event__eventoccurrence__endTime__gte=item[0]) &
                Q(eventstaffmember__event__eventoccurrence__startTime__lte=item[1])
            )

        return Q(filters & intervalFilters)
1334497
def query_by_user(self, user_id, end_time=None, start_time=None):
        
        path = {}
        data = {}
        params = {}

        # REQUIRED - PATH - user_id
        """ID"""
        path["user_id"] = user_id

        # OPTIONAL - start_time
        """The beginning of the time range from which you want events."""
        if start_time is not None:
            params["start_time"] = start_time

        # OPTIONAL - end_time
        """The end of the time range from which you want events."""
        if end_time is not None:
            params["end_time"] = end_time

        self.logger.debug("GET /api/v1/audit/authentication/users/{user_id} with query params: {params} and form data: {data}".format(params=params, data=data, **path))
        return self.generic_request("GET", "/api/v1/audit/authentication/users/{user_id}".format(**path), data=data, params=params, no_data=True)
224529
def find_entries(self, users, start, *args, **kwargs):
        
        forever = kwargs.get('all', False)
        for user in users:
            if forever:
                entries = Entry.objects.filter(user=user).order_by('start_time')
            else:
                entries = Entry.objects.filter(
                    user=user, start_time__gte=start).order_by(
                    'start_time')
            yield entries
54053
def uniquelogins(sessions):
    
    # sessions = LoginSession.query.order_by(LoginSession.started_at.asc()).all()
    if not sessions:
        return [], [], []
    dates = {}
    for session in sessions:
        user = session.user
        # time value is discarded to aggregate on days only
        date = session.started_at.strftime("%Y/%m/%d")

        if date not in dates:
            dates[date] = set()  # we want unique users on a given day
            dates[date].add(user)
        else:
            dates[date].add(user)

    daily = []
    weekly = []
    monthly = []

    for date in sorted(dates.keys()):
        # print u"{} : {}".format(date, len(dates[date]))
        date_epoch = unix_time_millis(datetime.strptime(date, "%Y/%m/%d"))
        daily.append({"x": date_epoch, "y": len(dates[date])})

    # first_day = data[0]['x']
    # last_day = data[-1]['x']

    daily_serie = pd.Series(dates)
    # convert the index to Datetime type
    daily_serie.index = pd.DatetimeIndex(daily_serie.index)
    # calculate the values instead of users lists
    daily_serie = daily_serie.apply(lambda x: len(x))

    # GroupBy Week/month, Thanks Panda
    weekly_serie = daily_serie.groupby(pd.Grouper(freq="W")).aggregate(numpysum)
    monthly_serie = daily_serie.groupby(pd.Grouper(freq="M")).aggregate(numpysum)

    for date, value in weekly_serie.items():
        try:
            value = int(value)
        except ValueError:
            continue
        date_epoch = unix_time_millis(date)
        weekly.append({"x": date_epoch, "y": value})

    for date, value in monthly_serie.items():
        try:
            value = int(value)
        except ValueError:
            continue
        date_epoch = unix_time_millis(date)
        monthly.append({"x": date_epoch, "y": value})

    return daily, weekly, monthly
953025
def date_totals(entries, by):
    """"""
    date_dict = {}
    for date, date_entries in groupby(entries, lambda x: x['date']):
        if isinstance(date, datetime.datetime):
            date = date.date()
        d_entries = list(date_entries)

        if by == 'user':
            name = ' '.join((d_entries[0]['user__first_name'],
                             d_entries[0]['user__last_name']))
        elif by == 'project':
            name = d_entries[0]['project__name']
        else:
            name = d_entries[0][by]

        pk = d_entries[0][by]
        hours = get_hours_summary(d_entries)
        date_dict[date] = hours
    return name, pk, date_dict