def run(self, user_id): self.user = User.get_by_id(user_id) if not self.user.verify_user(): # not really much we can do about this... log.info("Missing tokens for user %r", self.user) self.set_status(0, 0, "You need to log out and log back in again.") return self.fk = flickr.FlickrAPI(self.user.flickr_token) self.fb_user = fb.GraphUser(access_token=self.user.fb_access_token) photosets = self.fk.photosets_getList()[0] self.synced_photos = 0 self.total_photos = sum(int(pset.get("photos")) for pset in photosets) self.set_status(self.synced_photos, self.total_photos, "syncing Flickr photos to Facebook") settings = UserSetting.multiget( user_id=user_id, settings=[UserSettingConst.FB_PRIVACY, UserSettingConst.FLICKR_SET_SYNCING] ) self.fb_privacy = settings.get(UserSettingConst.FB_PRIVACY, "FB_DEFAULT") self.flickr_settings = settings.get(UserSettingConst.FLICKR_SET_SYNCING, {}) select_sets = self.flickr_settings.get("select_sets", False) selected_sets = self.flickr_settings.get("selected_sets", []) for photoset in photosets: if not select_sets or photoset.get("id") in selected_sets: self.sync_photoset(photoset) log.info("Finished job %r for user %r", self.job.jid, self.user.id) # once we are all done, let's submit the task to rerun in an hour. self.resubmit(delay=60 * 60)
def run(self, user_id): self.user = User.get_by_id(user_id) if not self.user.verify_user(): # not really much we can do about this... log.info("Missing tokens for user %r", self.user) self.set_status(0, 0, "You need to log out and log back in again.") return self.fk = flickr.FlickrAPI(self.user.flickr_token) self.fb_user = fb.GraphUser(access_token=self.user.fb_access_token) photosets = self.fk.photosets_getList()[0] self.synced_photos = 0 self.total_photos = sum(int(pset.get('photos')) for pset in photosets) self.set_status( self.synced_photos, self.total_photos, "syncing Flickr photos to Facebook") settings = UserSetting.multiget( user_id=user_id, settings=[UserSettingConst.FB_PRIVACY, UserSettingConst.FLICKR_SET_SYNCING]) self.fb_privacy = settings.get(UserSettingConst.FB_PRIVACY, 'FB_DEFAULT') self.flickr_settings = settings.get(UserSettingConst.FLICKR_SET_SYNCING, {}) select_sets = self.flickr_settings.get('select_sets', False) selected_sets = self.flickr_settings.get('selected_sets', []) for photoset in photosets: if not select_sets or photoset.get('id') in selected_sets: self.sync_photoset(photoset) log.info("Finished job %r for user %r", self.job.jid, self.user.id) # once we are all done, let's submit the task to rerun in an hour. self.resubmit(delay=60*60)
def stats(self): c.user_count = Session.query(User).count() c.sync_count = Session.query(SyncRecord).count() c.async_tasks_count = Session.query(AsyncTask).count() data = Session.query(SyncRecord.user_id, func.count(1), func.sum(SyncRecord.transfer_in), func.sum(SyncRecord.transfer_out))\ .filter(SyncRecord.type == SyncRecord.TYPE_PHOTO)\ .group_by(SyncRecord.user_id).all() c.user_stats = [] c.total_count = 0 c.total_cost = 0 c.total_tin = 0 c.total_tout = 0 for user_id, count, tin, tout in data: tin = tin or 0 tout = tout or 0 bandwidth_cost = cost.get_bandwidth_cost(tin, tout) c.total_cost += bandwidth_cost c.total_tout += int(tout) c.total_tin += int(tin) c.total_count += count c.user_stats.append([ User.get_by_id(user_id), count, round(tin / 1024 / 1024, 2), round(tout / 1024 / 1024, 2), bandwidth_cost ]) c.total_tout = round(c.total_tout / 1024. / 1024, 2) c.total_tin = round(c.total_tin / 1024. / 1024, 2) order_by = int(request.GET.get('order_by', 4)) c.user_stats.sort(key=lambda d: d[order_by], reverse=True) return render('/admin/stats.mako')
def stats(self): c.user_count = Session.query(User).count() c.sync_count = Session.query(SyncRecord).count() c.async_tasks_count = Session.query(AsyncTask).count() data = Session.query(SyncRecord.user_id, func.count(1), func.sum(SyncRecord.transfer_in), func.sum(SyncRecord.transfer_out))\ .filter(SyncRecord.type == SyncRecord.TYPE_PHOTO)\ .group_by(SyncRecord.user_id).all() c.user_stats = [] c.total_count = 0 c.total_cost = 0 c.total_tin = 0 c.total_tout = 0 for user_id, count, tin, tout in data: tin = tin or 0 tout = tout or 0 bandwidth_cost = cost.get_bandwidth_cost(tin,tout) c.total_cost += bandwidth_cost c.total_tout += int(tout) c.total_tin += int(tin) c.total_count += count c.user_stats.append([User.get_by_id(user_id), count, round(tin/1024/1024, 2), round(tout/1024/1024, 2), bandwidth_cost]) c.total_tout = round(c.total_tout/1024./1024, 2) c.total_tin = round(c.total_tin/1024./1024, 2) order_by = int(request.GET.get('order_by', 4)) c.user_stats.sort(key=lambda d: d[order_by], reverse=True) return render('/admin/stats.mako')