def run(self): finished = 0 def producer(q, data_points_with_actions): for data_point_with_actions in data_points_with_actions: thread = _DataPointThread(data_point_with_actions) thread.start() q.put(thread, True) def consumer(q, data_points_count): while finished <= data_points_count: thread = q.get(True) thread.join() finished += 1 Logger.Info('%s - AggregationController._UserThread.run - started' % __name__) Logger.Debug('%s - AggregationController._UserThread.run - started with user: %s' % (__name__, self.user)) all_data_points_with_actions = [] dc = DashboardsController(self.user) for dashboard in dc.get_live_dashboard(): for collection in dashboard['collections']: actions = collection['actions'] for data_point in collection['data_points']: all_data_points_with_actions.append({'data_point':data_point, 'actions':actions}) if all_data_points_with_actions: q = Queue(1) producer_thread = threading.Thread(target=producer, args=(q, all_data_points_with_actions)) consumer_thread = threading.Thread(target=consumer, args=(q, len(all_data_points_with_actions))) producer_thread.start() consumer_thread.start() Logger.Info('%s - AggregationController._UserThread.run - finished' % __name__)
def aggregate(self): Logger.Info('%s - AggregationController.aggregate - stated' % __name__) for user in self.users: all_data_points_with_actions = [] dc = DashboardsController(user) for dashboard in dc.get_live_dashboard(): for collection in dashboard['collections']: actions = collection['actions'] for data_point in collection['data_points']: all_data_points_with_actions.append({'data_point':data_point, 'actions':actions}) if all_data_points_with_actions: for data_point_with_actions in all_data_points_with_actions: run_aggregator_for_data_point(data_point_with_actions['data_point'], data_point_with_actions['actions']) #Threading removed # finished = 0 # def producer(q, users): # for user in users: # thread = _UserThread(user) # thread.start() # q.put(thread, True) # def consumer(q, users_count): # while finished <= users_count: # thread = q.get(True) # thread.join() # finished += 1 # q = Queue(1) # producer_thread = threading.Thread(target=producer, args=(q, self.users)) # consumer_thread = threading.Thread(target=consumer, args=(q, len(self.users))) # producer_thread.start() # consumer_thread.start() Logger.Info('%s - AggregationController.aggregate - finished' % __name__)
def delete_insight(request, insight_id): Logger.Info('%s - dashboard_delete - started' % __name__) Logger.Debug('%s - dashboard_delete - started with id:%s' % (__name__, id)) dc = DashboardsController(request.user) db = dc.get_dashboard_by_id(insight_id) if db and db['username'] == request.user.username: db.delete() Logger.Info('%s - dashboard_delete - finished' % __name__) return redirect(user_home, request.user.username)
def load_dashboards(request, username, count=None): Logger.Info('%s - load_dashboards - started' % __name__) user = UserController.GetUserByUserName(username) dc = DashboardsController(user) saved_dashboards = dc.get_saved_dashboards() if count: saved_dashboards = saved_dashboards[:int(count)] Logger.Info('%s - load_dashboards - finished' % __name__) return JSONResponse({ 'dashboards':saved_dashboards })
def create_project_insight(request, project_id): project = ProjectsController.GetProjectById(request.company, request.user, project_id) if not project: raise Http404 dc = DashboardsController(request.user) db = dc.create_new_dashboard_from_template(1) ActivityRecordsController.RecordInsightCreated(request.user, request.company, project) ProjectsController.AddInsightToProject(request.company, project.project_id, db.id) return redirect('/dashboard/%s?next=/%s/%s/projects/%s&company_id=%s&project_id=%s' % (db.id, settings.SITE_URLS['company_prefix'], request.company.id, project_id, request.company.id, project_id))
def load_project_insight(request, project_id, insight_id): project = ProjectsController.GetProjectById(request.company, request.user, project_id) if not project: raise Http404 db = DashboardsController.GetDashboardById(insight_id) if db.username == request.user.username: ActivityRecordsController.RecordInsightEdited(request.user, request.company, project, db) return redirect('/dashboard/%s?next=/%s/%s/projects/%s&company_id=%s&project_id=%s' % (db.id, settings.SITE_URLS['company_prefix'], request.company.id, project_id, request.company.id, project_id)) ActivityRecordsController.RecordInsightRemixed(request.user, request.company, project, db) dc = DashboardsController(request.user) db = dc.create_new_dashboard_from_dashboard(insight_id) ProjectsController.AddInsightToProject(request.company, project.project_id, db.id) return redirect('/dashboard/%s?next=/%s/%s/projects/%s&company_id=%s&project_id=%s' % (db.id, settings.SITE_URLS['company_prefix'], request.company.id, project_id, request.company.id, project_id))
def create_from_template(request): template_name = request.POST.get('template_name') if not template_name: Logger.Warn('%s - create_from_template - No template_name supplied in POST' % __name__) messages.error(request, 'Sorry, we could not create that insight for you. Please try again later.') return redirect(community_page) template = [t for t in settings.DASHBOARD_TEMPLATES if t['name'] == template_name][0] template = template['template'] template['username'] = request.user.username for collection in template['collections']: for visualization in collection['visualizations']: for element in visualization['elements']: if element['name'] == 'colorscheme': element['value'] = element['values'][randint(0, len(element['values']) - 1)] dc = DashboardsController(request.user) db = dc.create_new_dashboard_from_settings(template) return redirect('/dashboard/%s' % db.id)
def change_subscription(request): Logger.Info("%s - change_subscription - started" % __name__) user = request.user if request.method == 'GET': if not 'subscription_id' in request.GET: uc = UserController(user) user_subscriptions = uc.get_user_subscriptions() current_active_subscription_name = user_subscriptions['active_subscription'] available_subscriptions = [settings.SUBSCRIPTIONS_SETTINGS['subscriptions'][sub] for sub in settings.SUBSCRIPTIONS_SETTINGS['subscriptions'].keys() if sub != current_active_subscription_name] Logger.Info('%s - change_subscription - finished' % __name__) return render_to_response( 'thecommunity/account_page/_old/account_page_user_account_management_list_available_subscriptions.html', { 'subscriptions':available_subscriptions } ) else: subscription_id = request.GET['subscription_id'] subscription = [sub for sub in settings.SUBSCRIPTIONS_SETTINGS['subscriptions'].values() if sub['id'] == subscription_id][0] uc = UserController(user) subscription_migration_direction = uc.subscription_migration_direction(subscription['id']) template_name = 'thedashboard/change_subscriptions/%s' % subscription['templates'][subscription_migration_direction] Logger.Info('%s - change_subscription - finished' % __name__) return render_to_response( template_name, { 'subscription':subscription, 'show_credit_card_form':uc.need_to_ask_for_credit_card_details() }, context_instance=RequestContext(request) ) else: if 'credit_card_number' in request.POST: credit_card_number = request.POST['credit_card_number'] if credit_card_number != '0' and credit_card_number != '1': #TODO: this is just for the tests Logger.Info('%s - change_subscription - finished' % __name__) return JSONResponse({'errors':['Enter credit card number "1" to pass and "0" to fail']}) first_name = request.POST['first_name'].strip() last_name = request.POST['last_name'].strip() if not first_name or not last_name: Logger.Info('%s - change_subscription - finished' % __name__) return JSONResponse({'errors':['You must provide your first and last name']}) user.first_name = first_name user.last_name = last_name user.save() credit_card_data = { 'number':credit_card_number, 'expiry_month':request.POST['credit_card_expiry_month'], 'expiry_year':int(request.POST['credit_card_expiry_year']) } else: credit_card_data = None new_subscription_id = request.POST['subscription_id'] uc = UserController(user) subscription_changed = uc.change_user_subscription( new_subscription_id, credit_card_data ) if not subscription_changed: Logger.Info('%s - change_subscription - finished' % __name__) return JSONResponse({'errors':['There was an error process your card details, please try again later']}) maximum_number_of_dashboards = settings.SUBSCRIPTIONS_SETTINGS['subscriptions'][new_subscription_id]['config']['number_of_saved_dashboards'] dc = DashboardsController(user) dc.delete_dashboards_to_match_subscription(maximum_number_of_dashboards) Logger.Info('%s - change_subscription - finished' % __name__) return JSONResponse()
def community_values(self): dc = DashboardsController(self.user) return { 'number_of_insights':len(dc.get_saved_dashboards()) }