Skip to content

Commit

Permalink
Better handling of missing identifiers.
Browse files Browse the repository at this point in the history
  • Loading branch information
xant-tv committed Sep 8, 2022
1 parent 3da3822 commit ff9a051
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
15 changes: 9 additions & 6 deletions src/bot/core/cogs/clan.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from db.query.admins import get_admin_by_id
from db.query.clans import get_all_clans_in_guild, get_clan_in_guild
from db.query.members import get_members_matching_by_all_ids, get_member_by_id
from util.data import make_empty_structure, make_structure, append_frames, create_merge_id, coalesce_shared_ids, format_clan_list
from util.data import make_empty_structure, make_structure, append_frames, coalesce_shared_ids, format_clan_list
from util.encrypt import generate_local
from util.enum import AuditRecordType
from util.local import file_path, delete_file, write_file
Expand Down Expand Up @@ -102,10 +102,13 @@ async def members(self, ctx: discord.ApplicationContext, filter: str):
detail_map['join_date'].append(join_date)
detail_map['last_online'].append(member.get('lastOnlineStatusChange'))
details = make_structure(detail_map)
create_merge_id(details)

# Extract database member information.
records = get_members_matching_by_all_ids(DATABASE, details['bnet_id'].dropna().to_list(), details['destiny_id'].dropna().to_list())
records = get_members_matching_by_all_ids(
DATABASE,
details['bnet_id'].dropna().to_list(),
details['destiny_id'].dropna().to_list()
)
struct = make_empty_structure()
if records:
for user_id in records.get('discord_id'):
Expand All @@ -126,7 +129,6 @@ async def members(self, ctx: discord.ApplicationContext, filter: str):
records_map['discord_name'].append(user_discord_name)
records_map['discord_role'].append(user_discord_role)
struct = make_structure(records)
create_merge_id(struct)
for property in ['discord_name', 'discord_role']:
struct[property] = struct['discord_id'].map(
dict(
Expand All @@ -140,8 +142,9 @@ async def members(self, ctx: discord.ApplicationContext, filter: str):
# Structure and append additional details.
clan_members = details
if not struct.empty:
clan_members = clan_members.merge(struct, how='outer', on=['merge_id'], suffixes=['_api', '_db'])
coalesce_shared_ids(clan_members)
clan_members_by_bnet = clan_members.merge(struct, how='outer', on=['bnet_id'], suffixes=['_api', '_db'])
clan_members_by_destiny = clan_members.merge(struct, how='outer', on=['destiny_id'], suffixes=['_api', '_db'])
clan_members = coalesce_shared_ids(clan_members_by_bnet, clan_members_by_destiny)
clan_members['clan_id'] = clan_id
clan_members['clan_name'] = clan_name
members = append_frames(members, clan_members)
Expand Down
17 changes: 12 additions & 5 deletions src/util/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,19 @@ def make_structure(data) -> pd.DataFrame:
def append_frames(*frames) -> pd.DataFrame:
return pd.concat(frames, axis=0, ignore_index=True)

def create_merge_id(df: pd.DataFrame):
df['merge_id'] = np.where(df['bnet_id'].notnull(), df['bnet_id'], df['destiny_id'])
def coalesce_shared_ids(df_bnet: pd.DataFrame, df_destiny: pd.DataFrame):
"""Join all the various data structures to retain."""

def coalesce_shared_ids(df: pd.DataFrame):
df['bnet_id'] = np.where(df['bnet_id_api'].notnull(), df['bnet_id_api'], df['bnet_id_db'])
df['destiny_id'] = np.where(df['destiny_id_api'].notnull(), df['destiny_id_api'], df['destiny_id_db'])
# Handle separate merge suffixes.
df_bnet['destiny_id'] = np.where(df_bnet['destiny_id_api'].notnull(), df_bnet['destiny_id_api'], df_bnet['destiny_id_db'])
df_destiny['bnet_id'] = np.where(df_destiny['bnet_id_api'].notnull(), df_destiny['bnet_id_api'], df_destiny['bnet_id_db'])

# Magic merging of things (assumes bungie_name will always be present, which will probably also break at some point).
df = pd.concat([df_bnet, df_destiny])
df.sort_values(by=['bungie_name', 'bnet_id', 'destiny_id'], inplace=True) # Sorts duplicates where any missing keys are below complete records.
df.drop_duplicates(subset=['bungie_name'], inplace=True) # Drops duplicates maintaining the most complete record set.

return df

# Processing functionality.
def format_clan_list(df: pd.DataFrame):
Expand Down

0 comments on commit ff9a051

Please sign in to comment.