Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature-9077: Missing stats for daily check in scanning returning fro… #9092

Closed
wants to merge 8 commits into from
122 changes: 99 additions & 23 deletions app/api/custom/check_in_stats.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime

from flask import Blueprint, request
from sqlalchemy import desc
from sqlalchemy import asc, desc, func

from app.api.helpers.permissions import jwt_required
from app.api.helpers.static import STATION_TYPE
Expand Down Expand Up @@ -30,11 +30,10 @@ def get_registration_stats(event_id):
"""
# check if event is existed
event = Event.query.filter(Event.id == event_id).first()
current_time = datetime.datetime.utcnow().date()
total_attendee = TicketHolder.query.filter(TicketHolder.event_id == event_id).count()
if event is None:
return {"message": "Event can not be found."}, 404
stations = Station.query.filter(Station.event_id == event_id).all()
total_attendee = TicketHolder.query.filter(TicketHolder.event_id == event_id).count()
registration_stations = [
station.id
for station in stations
Expand All @@ -50,24 +49,96 @@ def get_registration_stats(event_id):
for station in stations
if station.station_type == STATION_TYPE.get('check out')
]
data = {
"total_attendee": total_attendee,
"registration_stations": registration_stations,
"check_in_stations": check_in_stations,
"check_out_stations": check_out_stations,
}

if request.args.get('today_only'):
results = {}
date = datetime.date.today().strftime("%Y-%m-%d")
data["date"] = date
results[date] = get_data_by_date(data)
else:
stationIds = []
for station in stations:
stationIds.append(station.id)
date_list = list(
zip(
*db.session.query(func.date(UserCheckIn.created_at))
.distinct()
.filter(
UserCheckIn.station_id.in_(stationIds),
)
.order_by(asc(func.date(UserCheckIn.created_at)))
.all()
)
)
dates = list(
map(
str,
date_list[0] if date_list else [],
)
)

if len(dates) == 0:
return {
"2023-10-17": {
"total_attendee": total_attendee,
"total_registered": 0,
"total_not_checked_in": total_attendee - 0,
"total_track_checked_in": 0,
"total_track_checked_out": 0,
"total_session_checked_in": 0,
"total_session_checked_out": 0,
"track_stats": [],
"session_stats": [
{
"check_in": 0,
"check_out": 0,
"manual_count": {},
"session_id": request.args.get('session_ids'),
"session_name": "",
"speakers": [],
"track_name": "",
}
],
}
}, 200

results = {}
for date in dates:
data["date"] = date
results[date] = get_data_by_date(data)
return results, 200


def get_data_by_date(data):
"""
Get data by date
@param data
@return: result
"""
registered_attendee = (
UserCheckIn.query.with_entities(UserCheckIn.ticket_holder_id)
.filter(
UserCheckIn.station_id.in_(registration_stations),
UserCheckIn.created_at >= current_time,
UserCheckIn.station_id.in_(data['registration_stations']),
func.date(UserCheckIn.created_at) == data['date'],
)
.group_by(UserCheckIn.ticket_holder_id)
.count()
)

check_in_attendee = UserCheckIn.query.filter(
UserCheckIn.station_id.in_(check_in_stations),
UserCheckIn.created_at >= current_time,
UserCheckIn.station_id.in_(data['check_in_stations']),
func.date(UserCheckIn.created_at) == data['date'],
)

check_out_attendee = UserCheckIn.query.filter(
UserCheckIn.station_id.in_(check_out_stations),
UserCheckIn.created_at >= current_time,
UserCheckIn.station_id.in_(data['check_out_stations']),
func.date(UserCheckIn.created_at) == data['date'],
)

session_checked_in = check_in_attendee.with_entities(
Expand All @@ -86,9 +157,9 @@ def get_registration_stats(event_id):
Session.id.in_(
[user_check_in.session_id for user_check_in in session_checked_in]
),
UserCheckIn.station_id.in_(check_in_stations),
UserCheckIn.station_id.in_(data['check_in_stations']),
Session.id == UserCheckIn.session_id,
UserCheckIn.created_at >= current_time,
func.date(UserCheckIn.created_at) == data['date'],
)

track_checked_in_count = (
Expand All @@ -101,9 +172,9 @@ def get_registration_stats(event_id):
Session.id.in_(
[user_check_in.session_id for user_check_in in session_checked_out]
),
UserCheckIn.station_id.in_(check_out_stations),
UserCheckIn.station_id.in_(data['check_out_stations']),
UserCheckIn.session_id == Session.id,
UserCheckIn.created_at >= current_time,
func.date(UserCheckIn.created_at) == data['date'],
)

track_checked_out_count = (
Expand All @@ -115,30 +186,32 @@ def get_registration_stats(event_id):
track_stat = []
if request.args.get('session_ids'):
session_stat = get_session_stats(
request.args.get('session_ids'), session_checked_in, session_checked_out
request.args.get('session_ids'),
session_checked_in,
session_checked_out,
data['date'],
)
if request.args.get('track_ids'):
track_stat = get_track_stats(
request.args.get('track_ids'),
check_in_attendee,
check_out_attendee,
current_time,
data['date'],
)

return {
"total_attendee": total_attendee,
"total_attendee": data['total_attendee'],
"total_registered": registered_attendee,
"total_not_checked_in": total_attendee - registered_attendee,
"total_not_checked_in": data['total_attendee'] - registered_attendee,
"total_track_checked_in": track_checked_in_count,
"total_track_checked_out": track_checked_out_count,
"total_session_checked_in": session_checked_in_count,
"total_session_checked_out": session_checked_out_count,
"session_stats": session_stat,
"track_stats": track_stat,
}, 200
}


def get_session_stats(session_ids, session_checked_in, session_checked_out):
def get_session_stats(session_ids, session_checked_in, session_checked_out, date):
"""
Get session stats
@param session_ids: session id to get
Expand Down Expand Up @@ -181,7 +254,10 @@ def get_session_stats(session_ids, session_checked_in, session_checked_out):

stationStorePaxs = (
db.session.query(StationStorePax)
.filter(StationStorePax.session_id == session_id)
.filter(
StationStorePax.session_id == session_id,
func.date(StationStorePax.created_at) == date,
)
.order_by(desc("created_at"))
.all()
)
Expand Down Expand Up @@ -231,7 +307,7 @@ def get_track_stats(track_ids, check_in_attendee, check_out_attendee, current_ti
),
UserCheckIn.id.in_([user_check_in.id for user_check_in in check_in_attendee]),
Session.id == UserCheckIn.session_id,
UserCheckIn.created_at >= current_time,
func.date(UserCheckIn.created_at) == current_time,
Session.track_id == track_id,
)

Expand All @@ -249,7 +325,7 @@ def get_track_stats(track_ids, check_in_attendee, check_out_attendee, current_ti
[user_check_in.id for user_check_in in check_out_attendee]
),
UserCheckIn.session_id == Session.id,
UserCheckIn.created_at >= current_time,
func.date(UserCheckIn.created_at) == current_time,
Session.track_id == track_id,
)

Expand Down
42 changes: 22 additions & 20 deletions docs/api/blueprint/user_check_in.apib
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,28 @@ Get registration stats.

+ Response 200 (application/json)

{
"session_stats": [
{
"check_in": 0,
"check_out": 0,
"manual_count": {},
"session_id": "1",
"session_name": "example",
"speakers": [],
"track_name": "example"
}
],
"total_attendee": 0,
"total_not_checked_in": 0,
"total_registered": 0,
"total_session_checked_in": 0,
"total_session_checked_out": 0,
"total_track_checked_in": 0,
"total_track_checked_out": 0,
"track_stats": []
{
"2023-10-17": {
"session_stats": [
{
"check_in": 0,
"check_out": 0,
"manual_count": {},
"session_id": "1",
"session_name": "",
"speakers": [],
"track_name": ""
}
],
"total_attendee": 0,
"total_not_checked_in": 0,
"total_registered": 0,
"total_session_checked_in": 0,
"total_session_checked_out": 0,
"total_track_checked_in": 0,
"total_track_checked_out": 0,
"track_stats": []
}
}

## Create User Check In [/v1/user-check-in]
Expand Down
4 changes: 2 additions & 2 deletions migrations/versions/rev-2023-08-17-15:38:43-bce7acfe5a4f_.py
This conversation was marked as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""empty message

Revision ID: bce7acfe5a4f
Revises: 24271525a263
Revises: 1af4cc4f7cd5
Create Date: 2023-08-17 15:38:43.387065

"""
Expand All @@ -12,7 +12,7 @@

# revision identifiers, used by Alembic.
revision = 'bce7acfe5a4f'
down_revision = '24271525a263'
down_revision = '1af4cc4f7cd5'


def upgrade():
Expand Down
18 changes: 14 additions & 4 deletions tests/all/integration/api/helpers/test_csv_jobs_util.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
import unittest
from datetime import datetime

from app.api.helpers.csv_jobs_util import *
import pytz

from app.api.helpers.csv_jobs_util import (
export_attendees_csv,
export_orders_csv,
export_sessions_csv,
export_speakers_csv,
)
from app.models import db
from app.models.custom_form import ATTENDEE_CUSTOM_FORM
from tests.all.integration.auth_helper import create_user
from tests.all.integration.utils import OpenEventTestCase
from tests.factories import common
from tests.factories.attendee import AttendeeFactory
from tests.factories.custom_form import CustomFormFactory
from tests.factories.event import EventFactoryBasic
from tests.factories.order import OrderFactory
from tests.factories.session import SessionSubFactory
from tests.factories.speaker import SpeakerFactory
from app.models.custom_form import ATTENDEE_CUSTOM_FORM


class TestExportCSV(OpenEventTestCase):
def test_export_orders_csv(self):
"""Method to check the orders data export"""

with self.app.test_request_context():
test_order = OrderFactory(created_at=datetime.now())
event = EventFactoryBasic()
test_order = OrderFactory(created_at=datetime.now(), event_id=event.id)
test_order.amount = 2
field_data = export_orders_csv([test_order])
assert field_data[1][2] == 'initializing'
Expand All @@ -34,7 +43,8 @@ def test_export_attendees_csv(self):
test_attendee.order = test_order
custom_forms = CustomFormFactory()
field_data = export_attendees_csv(
[test_attendee], [custom_forms], ATTENDEE_CUSTOM_FORM)
[test_attendee], [custom_forms], ATTENDEE_CUSTOM_FORM
)
# new export_attendees_csv will return list of dictionary for csv_writer
assert field_data[0].get("Tax ID") == "tax id"

Expand Down
40 changes: 21 additions & 19 deletions tests/all/integration/api/users_check_in/test_users_check_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,27 @@ def test_get_registration_stats(db, client, jwt):
)

result = {
"session_stats": [
{
"check_in": 0,
"check_out": 0,
"manual_count": {},
"session_id": "1",
"session_name": "example",
"speakers": [],
"track_name": "example",
}
],
"total_attendee": 0,
"total_not_checked_in": 0,
"total_registered": 0,
"total_session_checked_in": 0,
"total_session_checked_out": 0,
"total_track_checked_in": 0,
"total_track_checked_out": 0,
"track_stats": [],
"2023-10-17": {
"session_stats": [
{
"check_in": 0,
"check_out": 0,
"manual_count": {},
"session_id": "1",
"session_name": "",
"speakers": [],
"track_name": "",
}
],
"total_attendee": 0,
"total_not_checked_in": 0,
"total_registered": 0,
"total_session_checked_in": 0,
"total_session_checked_out": 0,
"total_track_checked_in": 0,
"total_track_checked_out": 0,
"track_stats": [],
}
}

assert response.status_code == 200
Expand Down