def packet_graphs(packet_id, info=None): stats = packet_stats(packet_id) fresh = [] misc = [] upper = [] # Make a rolling sum of signatures over time agg = lambda l, attr, date: l.append( (l[-1] if l else 0) + len(stats['dates'][date][attr])) dates = list(stats['dates'].keys()) for date in dates: agg(fresh, 'fresh', date) agg(misc, 'misc', date) agg(upper, 'upper', date) # Stack misc and upper on top of fresh for a nice stacked line graph for i in range(len(dates)): misc[i] = misc[i] + fresh[i] upper[i] = upper[i] + misc[i] return render_template( 'packet_stats.html', info=info, data=json.dumps({ 'dates': dates, 'accum': { 'fresh': fresh, 'misc': misc, 'upper': upper, }, 'daily': {} }), fresh=stats['freshman'], packet=Packet.by_id(packet_id), )
def sign(packet_id, info): packet = Packet.by_id(packet_id) if packet is not None and packet.is_open(): was_100 = packet.is_100() if app.config["REALM"] == "csh": # Check if the CSHer is an upperclassman and if so, sign that row for sig in filter(lambda sig: sig.member == info["uid"], packet.upper_signatures): sig.signed = True app.logger.info( "Member {} signed packet {} as an upperclassman".format( info["uid"], packet_id)) return commit_sig(packet, was_100, info["uid"]) # The CSHer is a misc so add a new row db.session.add(MiscSignature(packet=packet, member=info["uid"])) app.logger.info("Member {} signed packet {} as a misc".format( info["uid"], packet_id)) return commit_sig(packet, was_100, info["uid"]) else: # Check if the freshman is onfloor and if so, sign that row for sig in filter(lambda sig: sig.freshman_username == info["uid"], packet.fresh_signatures): sig.signed = True app.logger.info("Freshman {} signed packet {}".format( info["uid"], packet_id)) return commit_sig(packet, was_100, info["uid"]) app.logger.warn("Failed to add {}'s signature to packet {}".format( info["uid"], packet_id)) return "Error: Signature not valid. Reason: Unknown"
def upperclassmen_total(info=None): open_packets = Packet.open_packets() # Sum up the signed packets per upperclassman upperclassmen = dict() misc = dict() for packet in open_packets: for sig in packet.upper_signatures: if sig.member not in upperclassmen: upperclassmen[sig.member] = 0 if sig.signed: upperclassmen[sig.member] += 1 for sig in packet.misc_signatures: misc[sig.member] = 1 + misc.get(sig.member, 0) return render_template('upperclassmen_totals.html', info=info, num_open_packets=len(open_packets), upperclassmen=sorted(upperclassmen.items(), key=itemgetter(1), reverse=True), misc=sorted(misc.items(), key=itemgetter(1), reverse=True))
def packet_stats(packet_id): """ Gather statistics for a packet in the form of number of signatures per day Return format: { packet_id, freshman: { name, rit_username, }, dates: { <date>: { upper: [ uid ], misc: [ uid ], fresh: [ freshman_username ], }, }, } """ packet = Packet.by_id(packet_id) dates = [packet.start.date() + timedelta(days=x) for x in range(0, (packet.end-packet.start).days + 1)] print(dates) upper_stats = {date: list() for date in dates} for uid, date in map(lambda sig: (sig.member, sig.updated), filter(lambda sig: sig.signed, packet.upper_signatures)): upper_stats[date.date()].append(uid) fresh_stats = {date: list() for date in dates} for username, date in map(lambda sig: (sig.freshman_username, sig.updated), filter(lambda sig: sig.signed, packet.fresh_signatures)): fresh_stats[date.date()].append(username) misc_stats = {date: list() for date in dates} for uid, date in map(lambda sig: (sig.member, sig.updated), packet.misc_signatures): misc_stats[date.date()].append(uid) total_stats = dict() for date in dates: total_stats[date.isoformat()] = { 'upper': upper_stats[date], 'fresh': fresh_stats[date], 'misc': misc_stats[date], } return { 'packet_id': packet_id, 'freshman': { 'name': packet.freshman.name, 'rit_username': packet.freshman.rit_username, }, 'dates': total_stats, }
def get_packet_by_id(packet_id: int) -> dict: """ Return the scores of the packet in question """ packet = Packet.by_id(packet_id) return { 'required': vars(packet.signatures_required()), 'received': vars(packet.signatures_received()), }
def packets(info=None): open_packets = Packet.open_packets() # Pre-calculate and store the return values of did_sign(), signatures_received(), and signatures_required() for packet in open_packets: packet.did_sign_result = packet.did_sign(info['uid'], app.config['REALM'] == 'csh') packet.signatures_received_result = packet.signatures_received() packet.signatures_required_result = packet.signatures_required() open_packets.sort(key=packet_sort_key, reverse=True) return render_template('active_packets.html', info=info, packets=open_packets)
def sig2dict(sig): """ A utility function for upperclassman stats. Converts an UpperSignature to a dictionary with the date and the packet. """ packet = Packet.by_id(sig.packet_id) return { 'date': sig.updated.date(), 'packet': { 'id': packet.id, 'freshman_username': packet.freshman_username, }, }
def upperclassman(uid, info=None): open_packets = Packet.open_packets() # Pre-calculate and store the return value of did_sign() for packet in open_packets: packet.did_sign_result = packet.did_sign(uid, True) signatures = sum(map(lambda packet: 1 if packet.did_sign_result else 0, open_packets)) open_packets.sort(key=lambda packet: packet.freshman_username) open_packets.sort(key=lambda packet: packet.did_sign_result, reverse=True) return render_template("upperclassman.html", info=info, open_packets=open_packets, member=uid, signatures=signatures)
def create_new_packets(base_date: date, freshmen_list: dict): packet_start_time = time(hour=19) packet_end_time = time(hour=21) start = datetime.combine(base_date, packet_start_time) end = datetime.combine(base_date, packet_end_time) + timedelta(days=14) print('Fetching data from LDAP...') all_upper = list( filter( lambda member: not ldap.is_intromember(member) and not ldap. is_on_coop(member), ldap.get_active_members())) rtp = ldap.get_active_rtps() three_da = ldap.get_3das() webmaster = ldap.get_webmasters() c_m = ldap.get_constitutional_maintainers() w_m = ldap.get_wiki_maintainers() drink = ldap.get_drink_admins() # Packet starting notifications packets_starting_notification(start) # Create the new packets and the signatures for each freshman in the given CSV print('Creating DB entries and sending emails...') for freshman in Freshman.query.filter( Freshman.rit_username.in_(freshmen_list)).all(): packet = Packet(freshman=freshman, start=start, end=end) db.session.add(packet) send_start_packet_mail(packet) packet_starting_notification(packet) for member in all_upper: sig = UpperSignature(packet=packet, member=member.uid) sig.eboard = ldap.get_eboard_role(member) sig.active_rtp = member.uid in rtp sig.three_da = member.uid in three_da sig.webmaster = member.uid in webmaster sig.c_m = member.uid in c_m sig.w_m = member.uid in w_m sig.drink_admin = member.uid in drink db.session.add(sig) for onfloor_freshman in Freshman.query.filter_by(onfloor=True).filter( Freshman.rit_username != freshman.rit_username).all(): db.session.add( FreshSignature(packet=packet, freshman=onfloor_freshman)) db.session.commit()
def upperclassmen_total(info=None): open_packets = Packet.open_packets() # Sum up the signed packets per upperclassman upperclassmen = dict() for packet in open_packets: for sig in chain(packet.upper_signatures, packet.misc_signatures): if sig.member not in upperclassmen: upperclassmen[sig.member] = 0 if isinstance(sig, MiscSignature): upperclassmen[sig.member] += 1 elif sig.signed: upperclassmen[sig.member] += 1 return render_template("upperclassmen_totals.html", info=info, num_open_packets=len(open_packets), upperclassmen=sorted(upperclassmen.items(), key=itemgetter(1), reverse=True))
def freshman_packet(packet_id, info=None): packet = Packet.by_id(packet_id) if packet is None: return 'Invalid packet or freshman', 404 else: can_sign = packet.is_open() # If the packet is open and the user is an off-floor freshman set can_sign to False if packet.is_open() and app.config['REALM'] != 'csh': if info['uid'] not in map(lambda sig: sig.freshman_username, packet.fresh_signatures): can_sign = False return render_template('packet.html', info=info, packet=packet, can_sign=can_sign, did_sign=packet.did_sign(info['uid'], app.config['REALM'] == 'csh'), required=packet.signatures_required(), received=packet.signatures_received(), upper=packet.upper_signatures)