def get_team_scouting_notes_json(global_config, comp, name, store_json_file=False): global_config['logger'].debug( 'GET Team %s Scouting Notes For Competition %s', name, comp ) season = WebCommonUtils.map_comp_to_season(comp) session = DbSession.open_db_session(global_config['db_name'] + season) result = [] result.append('{ "competition" : "%s", "team" : "%s",\n' % (comp,name)) result.append(' "scouting_notes" : [\n') team_notes = DataModel.getTeamNotes(session, name, comp) for note in team_notes: result.append(' { "tag": "%s", "note": "%s" }' % (note.tag,note.data)) result.append(',\n') if len(team_notes) > 0: result = result[:-1] result.append(' ] }\n') json_str = ''.join(result) if store_json_file is True: try: FileSync.put( global_config, '%s/EventData/TeamData/team%s_scouting_notes.json' % (comp,name), 'text', json_str) except: raise session.remove() return json_str
def get_team_score_json(global_config, name, comp, store_json_file=False): global_config['logger'].debug( 'GET Team %s Score For Competition %s', name, comp ) season = WebCommonUtils.map_comp_to_season(comp) session = DbSession.open_db_session(global_config['db_name'] + season) result = [] result.append('{ "competition" : "%s", "team" : "%s", ' % (comp,name)) team_scores = DataModel.getTeamScore(session, name, comp) if len(team_scores)==1: result.append('"score": "%s" }' % team_scores[0].score) else: result.append(' "score": [') for score in team_scores: result.append(score.json()) result.append(',\n') if len(team_scores) > 0: result = result[:-1] result.append(']}') json_str = ''.join(result) if store_json_file is True: try: FileSync.put( global_config, '%s/EventData/TeamData/team%s_scouting_score.json' % (comp,name), 'text', json_str) except: raise session.remove() return json_str
def get_team_info_json(global_config, comp, name, store_json_file=False): global_config['logger'].debug( 'GET Team %s Info', name ) season = WebCommonUtils.map_comp_to_season(comp) session = DbSession.open_db_session(global_config['db_name'] + season) team_info = DataModel.getTeamInfo(session, int(name)) if team_info is None: json_str = '' else: result = [] result.append('{ "team": "%s", "team_data" : [\n' % name) result.append(' { "name": "%s", "value": "%s" }' % ('nickname', team_info.nickname)) result.append(',\n') result.append(' { "name": "%s", "value": "%s" }' % ('affiliation', team_info.fullname)) result.append(',\n') result.append(' { "name": "%s", "value": "%s" }' % ('location', team_info.location)) result.append(',\n') result.append(' { "name": "%s", "value": "%s" }' % ('rookie_season', team_info.rookie_season)) result.append(',\n') result.append(' { "name": "%s", "value": "%s" }' % ('website', team_info.website)) result.append('\n') result.append(' ] }\n') json_str = ''.join(result) if store_json_file is True: try: FileSync.put( global_config, '%s/EventData/TeamData/team%s_teaminfo.json' % (comp,name), 'text', json_str) except: raise return json_str
def get_team_score_breakdown_json(global_config, name, comp=None, store_json_file=False): global_config['logger'].debug( 'GET Team Score Breakdown: %s', name ) if comp == None: comp = global_config['this_competition'] + global_config['this_season'] season = global_config['this_season'] else: season = WebCommonUtils.map_comp_to_season(comp) session = DbSession.open_db_session(global_config['db_name'] + season) attrdef_filename = WebCommonUtils.get_attrdef_filename(comp=comp) attr_definitions = AttributeDefinitions.AttrDefinitions(global_config) attr_definitions.parse(attrdef_filename) result = [] result.append('{ "score_breakdown": [\n') team_attributes = DataModel.getTeamAttributesInOrder(session, name, comp) for attribute in team_attributes: attr_def = attr_definitions.get_definition( attribute.attr_name ) if attr_def: try: stat_type = attr_def['Statistic_Type'] except: stat_type = 'Total' weight = int(float(attr_def['Weight'])) if weight != 0: if stat_type == 'Average': value = int(attribute.cumulative_value/attribute.num_occurs) else: value = int(attribute.cumulative_value) data_str = '{"attr_name": "%s", "raw_score": %d, "weighted_score": %d}' % (attribute.attr_name,int(value),int(weight*value)) result.append(data_str) result.append(',\n') if len(team_attributes) > 0: result = result[:-1] result.append('\n') result.append(']}') json_str = ''.join(result) if store_json_file is True: try: FileSync.put( global_config, '%s/EventData/TeamData/team%s_scouting_scorebreakdown.json' % (comp,name), 'text', json_str) except: raise session.remove() return json_str
def create_picklist_json(global_config, comp=None, store_json_file=False): global_config['logger'].debug( 'Create Picklist Json' ) global local_picklist store_data_to_file = False if comp == None: comp = global_config['this_competition'] + global_config['this_season'] season = global_config['this_season'] else: season = WebCommonUtils.map_comp_to_season(comp) session = DbSession.open_db_session(global_config['db_name'] + season) result = [] result.append('{ "picklist": [\n') local_picklist = DataModel.getTeamsInRankOrder(session, comp, True) rank = 1 for team in local_picklist: # round the score to an integer value team.score = float(int(team.score)) if team.score > 0: row = '{ "rank" : %d, "team" : %d, "score" : %d, "competition" : "%s" }' % (rank, team.team, int(team.score), team.competition) result.append(row) result.append(',\n') rank += 1 if len(result) > 0: result = result[:-1] result.append(']}') json_str = ''.join(result) if store_json_file is True: try: FileSync.put( global_config, '%s/EventData/%s.json' % (comp,'picklist'), 'text', json_str) except: raise session.remove() return json_str
def get_team_list_json(global_config, comp, store_json_file=False): global team_info_dict global_config['logger'].debug( 'GET Team List For Competition %s', comp ) season = WebCommonUtils.map_comp_to_season(comp) session = DbSession.open_db_session(global_config['db_name'] + season) result = [] result.append('{ "teams" : [\n') team_list = DataModel.getTeamsInNumericOrder(session, comp) for team in team_list: team_info = None # TODO - Remove this hardcoded number for the valid team number. This check prevents # requesting information for invalid team numbers, which has been known to happen when # tablet operators enter bogus team numbers by mistake if team.team < 10000: team_info = DataModel.getTeamInfo(session, int(team.team)) if team_info: result.append(' { "team_number": "%s", "nickname": "%s" }' % (team.team,team_info.nickname)) result.append(',\n') else: result.append(' { "team_number": "%s", "nickname": "%s" }' % (team.team,'Unknown')) result.append(',\n') if len(team_list) > 0: result = result[:-1] result.append(' ] }\n') json_str = ''.join(result) else: json_str = get_team_list_json_from_tba(global_config, comp) if store_json_file is True: try: FileSync.put( global_config, '%s/EventData/%s.json' % (comp,'teams'), 'text', json_str) except: raise return json_str
def get_team_list_json_from_tba(global_config, comp): global_config['logger'].debug( 'GET Team List For Competition From TBA %s', comp ) result = [] result.append('{ "teams" : ') event_code = WebCommonUtils.map_comp_to_event_code(comp) season = WebCommonUtils.map_comp_to_season(comp) url_str = '/api/v2/event/%s%s/teams' % (season,event_code.lower()) try: # retrieve the string itself as a formatted json string event_data = TbaIntf.get_from_tba(url_str) except: event_data = '[ ]' result.append( event_data ) result.append(' }\n') return ''.join(result)
def get_team_list_json_from_tba(global_config, comp): global_config['logger'].debug( 'GET Team List For Competition From TBA %s', comp ) web.header('Content-Type', 'application/json') result = [] result.append('{ "teams" : ') event_code = WebCommonUtils.map_comp_to_event_code(comp) season = WebCommonUtils.map_comp_to_season(comp) url_str = 'http://www.thebluealliance.com/api/v2/event/%s%s/teams?X-TBA-App-Id=frc1073:scouting-system:v01' % (season,event_code) event_data = '' try: event_data = urllib2.urlopen(url_str).read() except: event_data = '[ ]' pass result.append( event_data ) result.append(' }\n') return ''.join(result)
def get_team_attr_rankings_json(global_config, comp=None, attr_name=None): global_config['logger'].debug( 'GET Team Attribute Rankings Json' ) store_data_to_file = False if comp == None: comp = global_config['this_competition'] + global_config['this_season'] season = global_config['this_season'] else: season = WebCommonUtils.map_comp_to_season(comp) session = DbSession.open_db_session(global_config['db_name'] + season) attrdef_filename = WebCommonUtils.get_attrdef_filename(comp=comp) attr_definitions = AttributeDefinitions.AttrDefinitions() attr_definitions.parse(attrdef_filename) attr_def = attr_definitions.get_definition(attr_name) try: stat_type = attr_def['Statistic_Type'] except: stat_type = 'Total' web.header('Content-Type', 'application/json') result = [] result.append('{ "attr_name" : "%s",\n' % attr_name) # add the columns bases on the attribute definition type result.append(' "columns" : [\n') result.append(' { "sTitle": "Team" }') result.append(',\n') columns = [] if attr_def['Type'] == 'Map_Integer': map_values = attr_def['Map_Values'].split(':') for map_value in map_values: item_name = map_value.split('=')[0] columns.append(item_name) result.append(' { "sTitle": "%s" }' % item_name) result.append(',\n') result = result[:-1] result.append('\n') result.append(' ],\n') if stat_type == 'Average': team_rankings = DataModel.getTeamAttributesInAverageRankOrder(session, comp, attr_name) else: team_rankings = DataModel.getTeamAttributesInRankOrder(session, comp, attr_name) result.append(' "rankings" : [\n') for team_attr in team_rankings: data_str = ' [ %d,' % team_attr.team value_dict = DataModel.mapAllValuesToDict(attr_def, team_attr.all_values) for column in columns: try: value = value_dict[column] except: value = 0 data_str += ' %d,' % value data_str = data_str.rstrip(',') data_str += ' ]' result.append(data_str) result.append(',\n') if len(team_rankings) > 0: result = result[:-1] result.append('\n') result.append(' ]\n}') json_str = ''.join(result) if store_data_to_file is True: try: file_name = 'attrrankings_%s' % attr_name FileSync.put( global_config, '%s/EventData/%s.json' % (comp,file_name), 'text', json_str) except: raise session.remove() return json_str
def get_team_scouting_data_summary_json(global_config, comp, name, attr_filter=[], filter_name=None, store_json_file=False): global_config['logger'].debug( 'GET Team %s Scouting Data For Competition %s', name, comp ) season = WebCommonUtils.map_comp_to_season(comp) session = DbSession.open_db_session(global_config['db_name'] + season) if global_config['attr_definitions'] == None: return None attrdef_filename = WebCommonUtils.get_attrdef_filename(comp=comp) attr_definitions = AttributeDefinitions.AttrDefinitions(global_config) attr_definitions.parse(attrdef_filename) result = [] result.append('{ "competition" : "%s", "team" : "%s",\n' % (comp,name)) result.append(' "scouting_data_summary" : [\n') team_attributes = DataModel.getTeamAttributesInOrder(session, name, comp) if len(team_attributes) > 0: some_attr_added = False for attribute in team_attributes: attr_def = attr_definitions.get_definition( attribute.attr_name ) include_attr = False if attr_def: if attr_def.has_key('Include_In_Team_Display') \ and attr_def['Include_In_Team_Display'] == 'Yes': include_attr = True elif attr_def.has_key('Include_In_Report') \ and attr_def['Include_In_Report'] == 'Yes': include_attr = True elif attr_def.has_key('Weight') \ and attr_def['Weight'] != '0': include_attr = True # if an attribute filter has been provided, only include the attribute data if the # attribute is in the filter if len(attr_filter) > 0: if attr_def['Name'] not in attr_filter: include_attr = False if include_attr == True: some_attr_added = True if attr_def.has_key('Display_Name'): attr_name = attr_def['Display_Name'] else: attr_name = attr_def['Name'] category = attr_def.get('Sub_Category', '') result.append(' { "name": "%s", "matches": "%s", "cumulative_value": "%s", "average_value": "%s", "all_values": "%s", "category": "%s" }' % \ (attr_name,str(attribute.num_occurs),str(attribute.cumulative_value),str(round(attribute.avg_value,1)),\ DataModel.mapAllValuesToShortenedString(attr_def, attribute.all_values), category) ) result.append(',\n') if some_attr_added: result = result[:-1] result.append(' ] }\n') json_str = ''.join(result) if store_json_file is True: try: FileSync.put( global_config, '%s/EventData/TeamData/team%s_scouting_data_summary.json' % (comp,name), 'text', json_str) except: raise session.remove() return json_str
def get_team_rankings_json(global_config, comp=None, attr_filters=[], filter_name=None, thumbnails = False, store_json_file=False): global_config['logger'].debug( 'GET Team Rankings Json' ) store_data_to_file = False if comp == None: comp = global_config['this_competition'] + global_config['this_season'] season = global_config['this_season'] else: season = WebCommonUtils.map_comp_to_season(comp) session = DbSession.open_db_session(global_config['db_name'] + season) result = [] result.append('{ "rankings": [\n') rank_added = False if len(attr_filters) == 0: team_rankings = DataModel.getTeamsInRankOrder(session, comp, False) for team in team_rankings: # round the score to an integer value team.score = float(int(team.score)) if team.score > 0: thumbnails_snippet = '' if thumbnails: thumbnails_snippet = ',\n' + get_team_scouting_thumbnails_json_snippet(global_config, comp, str(team.team)) result.append( ' { "score": %0.1f, "competition": "%s", "team": %d%s }' % (team.score, comp, team.team,thumbnails_snippet)) result.append(',\n') rank_added = True else: # we'll need the attribute definitions in order to retrieve the correct attribute value # and attribute weighting attrdef_filename = './config/' + global_config['attr_definitions'] attr_definitions = AttributeDefinitions.AttrDefinitions(global_config) attr_definitions.parse(attrdef_filename) team_rank_dict = dict() for attr_filter in attr_filters: try: attr_name, attr_value = attr_filter.split('=') except: attr_name = attr_filter attr_value = None attr_def = attr_definitions.get_definition(attr_name) if attr_value is None: team_rankings = DataModel.getTeamAttributesInRankOrder(session, comp, attr_name, False) for team in team_rankings: try: stat_type = attr_def['Statistic_Type'] except: stat_type = 'Total' weight = int(float(attr_def['Weight'])) if stat_type == 'Average': score = int(team.cumulative_value/team.num_occurs*weight) else: score = int(team.cumulative_value*weight) try: team_rank_dict[team.team] += score except: team_rank_dict[team.team] = score else: team_rankings = DataModel.getTeamAttributesWithValue(session, comp, attr_name, attr_value, False) for team in team_rankings: score = team.all_values.count(attr_value) try: team_rank_dict[team.team] += score except: team_rank_dict[team.team] = score sorted_team_rank = sorted(team_rank_dict.items(), key=operator.itemgetter(1)) for team, score in sorted_team_rank: # round the score to an integer value score = float(int(score)) if score > 0: thumbnails_snippet = '' if thumbnails: thumbnails_snippet = ',\n' + get_team_scouting_thumbnails_json_snippet(global_config, comp, str(team)) result.append( ' { "score": %0.1f, "competition": "%s", "team": %d%s }' % (score, comp, team, thumbnails_snippet)) result.append(',\n') rank_added = True if rank_added == True: result = result[:-1] result.append(']}') json_str = ''.join(result) if store_json_file is True: try: if filter_name is None: file_name = 'scoutingrankings' else: file_name = 'scoutingrankings_%s' % filter_name FileSync.put( global_config, '%s/EventData/%s.json' % (comp,file_name), 'text', json_str) except: raise return json_str