def run(self, min_equiv=360): """ @param: min_equiv = how many minutes correspond to a 100 free_time_score """ min_equiv /= 100 # SELECT screen_name, free_time_score FROM users # ORDER BY free_time_score DESC; all_users = SimUser.objects.order_by('-free_time_score') # SELECT circuit.id FROM circuits all_circuits = Circuit.objects.all() circuits_size = len(all_circuits) session_counter = 0 print colored.white("Inserting visits for user:") for usuario in progress.bar(all_users): #Probability that a user does not visit this day ~50% skip_visit = random.randint(0, 30) if skip_visit > 15: continue # Calculating users available time watch_time = usuario.free_time_score * min_equiv factor = int(0.3 * watch_time) total_time = random.randint(-factor, factor) + watch_time # Get user visit begin time in 24hrs format begin_hour = Cumulative_vec_samples(hour_x_day_accum, 1) # Create a timestamp #timestamp = datetime(self.date.year, # self.date.month, # self.date.day, # begin_hour[0], # 0, 0) while total_time > 0: # Getting the duration in minutes of the visit visit_duration = Cumulative_vec_distr(visit_duration_accum) # Creating an instance of visit visita = Visit( visitor=usuario.user, # visit random circuit, cause could visit the same circuit twice or more content_object=all_circuits[random.randint( 1, circuits_size - 1)]) # Adding visit minutes to timestamp #timestamp += timedelta(minutes=visit_duration) # Save visit to DB visita.save() # updating total_time total_time -= visit_duration session_counter += 1 if session_counter >= 5000: session_counter = 0 transaction.commit() transaction.commit()
def TransformationView(request, pk): appointment = models.Appointment.objects.get(pk=pk) new_visit = Visit(visit_date=appointment.appointment_date, visit_time=appointment.appointment_time, patient_id=appointment.patient) new_visit.save() appointment.delete() return redirect('visits:visit_detail', pk=new_visit.pk)
def index(request): # get the number the users users_count = User.objects.all().count() # get the number of the visits data rows resultcount = Visit.objects.all().count() # check if the database is already seeded if resultcount < 1000: seeder = Seed.seeder() fake = Faker() # prepare 1000 fake instance to be seeded seeder.add_entity( Visit, 1000, { 'created_at': lambda x: fake.date_time_between( datetime.datetime(2021, 4, 1, 0, 0, 0), datetime.datetime(2021, 4, 10, 23, 0, 0)), 'ip_address': lambda x: "{}.{}.{}.{}".format(random.randint( 1, 255), random.randint(1, 255), random.randint(1, 255), random.randint(1, 255) ), #Generate fake ip_address 'user': lambda x: get_user_or_none(users_count), }) # insert the fake data into our database seeder.execute() # get the client ip address using our helper client_ip = get_ip_address() # get the visitor last visit by ip_address and user_id/anynomous last_visit = Visit.objects.order_by('-created_at').filter( ip_address=client_ip, user_id=request.user.id).first() # get the time since the last visit in seconds time_since_last_visit = datetime.datetime.now().timestamp() - ( last_visit.created_at.timestamp() if last_visit != None else 0) # check if the latest visit is more than 2 hours old if time_since_last_visit > 7200: # create an instance of the visit visit = Visit(ip_address=client_ip) # check if the visit is by a user or anonymous if request.user.is_authenticated: visit.user = request.user # persiste the visit visit.save() # return the response as web page (home template) return render(request, 'home.html')
def page(request, address, component=None): ''' Serve a component page. Not a view, but used by views below ''' if component and not request.user_agent.is_bot: # Record the visit Visit.record(request, address, 'view') # Increment view count for this component component.views += 1 component.save() # Return the page of the component # If the user is authenticated, check if they have an active session for this component if request.user.is_authenticated(): session = component.session( user=request.user, required=False ) if session: # Return the current live page in the session # Ensure a trailing slash so that there is no redirection to the # trailing slash URL by the embedded server in the session location = '%s:%s/%s/' % (session.worker.ip, session.port, address) if settings.MODE == 'local': # Proxy to session response = requests.get('http://%s' % location) return HttpResponse(response.text, status=response.status_code) else: # Get Nginx to proxy from session response = django.http.HttpResponse() response['X-Accel-Redirect'] = '/internal-component-session/%s' % location return response # Otherwise, return the `index.html` for the compooent if settings.MODE == 'local': # In `local` mode serve the static file return django.views.static.serve(request, '%s/index.html' % address, document_root='/srv/stencila/store') else: # Otherwise, ask Nginx to serve it url = '/internal-component-file/%s/index.html' % address response = HttpResponse() response['X-Accel-Redirect'] = url return response
def record_visit(self): user = None if self.request.user.is_anonymous() else self.request.user visit = Visit(session_key=self.request.session.session_key, user=user, ip=self.get_client_ip()) visit.updateLocation() visit.save()
def run(self, min_equiv=360): """ @param: min_equiv = how many minutes correspond to a 100 free_time_score """ min_equiv /= 100 # SELECT screen_name, free_time_score FROM users # ORDER BY free_time_score DESC; all_users = SimUser.objects.order_by('-free_time_score') # SELECT circuit.id FROM circuits all_circuits = Circuit.objects.all() circuits_size = len(all_circuits) session_counter = 0 print colored.white("Inserting visits for user:") for usuario in progress.bar(all_users): #Probability that a user does not visit this day ~50% skip_visit = random.randint(0,30) if skip_visit > 15: continue # Calculating users available time watch_time = usuario.free_time_score * min_equiv factor = int(0.3 * watch_time) total_time = random.randint(-factor,factor) + watch_time # Get user visit begin time in 24hrs format begin_hour = Cumulative_vec_samples(hour_x_day_accum,1) # Create a timestamp #timestamp = datetime(self.date.year, # self.date.month, # self.date.day, # begin_hour[0], # 0, 0) while total_time > 0: # Getting the duration in minutes of the visit visit_duration = Cumulative_vec_distr(visit_duration_accum) # Creating an instance of visit visita = Visit( visitor = usuario.user, # visit random circuit, cause could visit the same circuit twice or more content_object = all_circuits[random.randint(1,circuits_size-1)] ) # Adding visit minutes to timestamp #timestamp += timedelta(minutes=visit_duration) # Save visit to DB visita.save() # updating total_time total_time -= visit_duration session_counter += 1 if session_counter >= 5000: session_counter = 0 transaction.commit() transaction.commit()