Example #1
0
 def get_view_count(self):
     try:
         redis = RedisService().redis
         result = redis.get(self.view_count_redis_key)
         if not result:
             return 0
         return int(result.decode('utf-8'))
     except KeyError:
         return 0
Example #2
0
    def handle(self, *args, **options):
        # connect to API
        d = get_driver()
        teams = d.teams.get_teams()

        # outer vars
        all_usernames = []
        all_user_statuses = {}
        all_response = []

        for team in teams:
            if team['display_name'] == 'Codefund':
                continue

            # pull back users on team
            print(team['display_name'])
            all_users = []
            cont = True
            per_page = 60
            page = 0
            while cont:
                params = {'in_team':team['id'], 'sort':'last_activity_at', 'per_page': per_page, 'page': page}
                users = d.users.get_users(params=params)
                all_users += users
                cont = len(users) == per_page
                page += 1
                #for testing on small amount of data
                #if settings.DEBUG:
                #    cont = False
            for user in users:
                pass

            # get through all users
            print(f"- {len(all_users)}")
            users_status = d.client.post('/users/status/ids', [ele['id'] for ele in all_users])
            all_response += users_status
            users_status_by_id = { ele['user_id']: ele for ele in users_status }
            all_usernames += [ele['username'].lower() for ele in all_users]

            # iterate through each one, and sync it to our DB
            for user in all_users:
                last_activity_at_1 = user['last_activity_at']
                last_activity_at_2 = users_status_by_id[user['id']]['last_activity_at']
                status = users_status_by_id[user['id']]['status']
                last_activity_at = max(last_activity_at_2, last_activity_at_1)
                username = user['username']
                timestamp = int(last_activity_at/1000)
                timestamp = timezone.datetime.utcfromtimestamp(timestamp).replace(tzinfo=pytz.utc)
                all_user_statuses[username] = (status, timestamp, user['id'])

            # look for manual statuses set by /api/v0.1/chat route and clean them up if needed
            redis = RedisService().redis
            for ele in all_response:

                user_id = ele['user_id']
                status = ele['status']
                if status == 'offline':
                    continue

                # user has been offline for 10 mins
                # update mattermost, and redis
                max_delta = (10*60)

                # calc it
                now = timezone.now().timestamp()
                last_action_mattermost = int(ele['last_activity_at']/1000)
                redis_response = redis.get(user_id)
                last_seen_gc = int(float(redis_response)) if redis_response else now

                # do update
                is_away_mm = ((now - last_action_mattermost) > max_delta)
                is_away_gc = (now - last_seen_gc) > max_delta
                manual = ele['manual']
                update_ele = (manual and is_away_gc) or (not manual and is_away_mm)
                if update_ele or settings.DEBUG:
                    new_status = 'offline' 
                    d.client.put(f'/users/{user_id}/status', {'user_id': user_id, 'status': new_status})
                    redis.set(user_id, 0)
        # update all usernames
        profiles = Profile.objects.filter(handle__in=all_usernames)
        for profile in profiles:
            profile.last_chat_status, profile.last_chat_seen, profile.chat_id = all_user_statuses[profile.handle]
        bulk_update(profiles, update_fields=['last_chat_seen', 'last_chat_status', 'chat_id'])