Exemplo n.º 1
0
    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()
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
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')