def test_enum_ranking_both_side_tied(self): data = [1, 1, 3, 5, 5] self.assertListEqual([("T1", 1), ("T1", 1), ("3", 3), ("T4", 5), ("T4", 5)], list(enumerate_ranking(data))) self.assertListEqual([(1, 1), (1, 1), (3, 3), (4, 5), (4, 5)], list(enumerate_ranking(data, t_prefix=False))) self.assertListEqual([("T0", 1), ("T0", 1), ("2", 3), ("T3", 5), ("T3", 5)], list(enumerate_ranking(data, start=0))) self.assertListEqual([(0, 1), (0, 1), (2, 3), (3, 5), (3, 5)], list( enumerate_ranking(data, t_prefix=False, start=0)))
def test_enum_ranking_in_between_tied(self): data = [1, 2, 3, 3, 5] self.assertListEqual([("1", 1), ("2", 2), ("T3", 3), ("T3", 3), ("5", 5)], list(enumerate_ranking(data))) self.assertListEqual([(1, 1), (2, 2), (3, 3), (3, 3), (5, 5)], list(enumerate_ranking(data, t_prefix=False))) self.assertListEqual([("0", 1), ("1", 2), ("T2", 3), ("T2", 3), ("4", 5)], list(enumerate_ranking(data, start=0))) self.assertListEqual([(0, 1), (1, 2), (2, 3), (2, 3), (4, 5)], list( enumerate_ranking(data, t_prefix=False, start=0)))
def __init__(self, cursor, incl_not_used: bool): FeatureUsageEntry = namedtuple("FeatureUsageEntry", ["feature_name", "count", "rank"]) self.data = [] for rank, d in enumerate_ranking( cursor, is_tie=lambda cur, prv: cur[BotFeatureUsageResult.KEY] == prv[ BotFeatureUsageResult.KEY]): try: feature = BotFeature.cast(d[OID_KEY]).key self.data.append( FeatureUsageEntry(feature_name=feature, count=d[BotFeatureUsageResult.KEY], rank=rank)) except TypeError: # Skip if the feature code has been added in the newer build but not in the current executing build pass if incl_not_used: diff = {feature for feature in BotFeature }.difference({BotFeature.cast(d[OID_KEY]) for d in cursor}) last_rank = f"{'T' if len(diff) > 0 else ''}{len(self.data) + 1 if self.data else 1}" for diff_ in diff: self.data.append( FeatureUsageEntry(feature_name=diff_.key, count=0, rank=last_rank)) self.chart_label = [d.feature_name for d in self.data] self.chart_data = [d.count for d in self.data]
def __post_init__(self, org_stats: List[UserMessageStatsEntry]): self.member_stats = [] # Disabling T-prefix for datatable sorting in the webpage for rank, data in enumerate_ranking( list( sorted(org_stats, key=lambda x: x.total_count, reverse=True)), is_tie=lambda cur, prv: cur.total_count == prv.total_count, t_prefix=False): data.set_rank(rank) self.member_stats.append(data)
def __init__(self, crs, limit: Optional[int] = None): self.data = [] usage_key = UniqueKeywordCountResult.KEY_COUNT_USAGE for rank, d in enumerate_ranking(crs, is_tie=lambda cur, prv: cur[usage_key] == prv[usage_key]): self.data.append( UniqueKeywordCountEntry( word=d[OID_KEY][UniqueKeywordCountResult.KEY_WORD], word_type=d[OID_KEY][UniqueKeywordCountResult.KEY_WORD_TYPE], count_usage=d[UniqueKeywordCountResult.KEY_COUNT_USAGE], count_module=d[UniqueKeywordCountResult.KEY_COUNT_MODULE], rank=rank ) ) self.limit = limit
def get_user_daily_message( channel_data: ChannelModel, *, hours_within: Optional[int] = None, start: Optional[datetime] = None, end: Optional[datetime] = None, tz: Optional[tzinfo] = None, available_only: Optional[bool] = True) \ -> UserDailyMessageResult: available_dict = { d.user_oid: d.available for d in ProfileManager.get_channel_members( channel_data.id, available_only=available_only) } uname_dict = IdentitySearcher.get_batch_user_name(list( available_dict.keys()), channel_data, on_not_found="(N/A)") result = MessageRecordStatisticsManager.member_daily_message_count( channel_data.id, hours_within=hours_within, start=start, end=end, tzinfo_=tz) # Array for storing active member count actv_mbr = [] # Create empty arrays for the result # Each entry corresponds to daily message count in a specific date # Ordered by the date proc_count = {} proc_rank = {} for oid in uname_dict.keys(): proc_count[oid] = [] proc_rank[oid] = [] for date in result.dates: # Get the data of the corresponding date data_of_date = result.data_count[date] # Storing active member count actv_mbr.append(len(data_of_date)) # Get and sort the daily message count collated_count = { oid: data_of_date.get(oid, 0) for oid in proc_count.keys() } sorted_count = list( sorted(collated_count.items(), key=lambda x: x[1], reverse=True)) # Insert daily count and rank for rank, entry in enumerate_ranking( sorted_count, is_tie=lambda cur, prv: cur[1] == prv[1]): uid, count = entry proc_count[uid].append(count) proc_rank[uid].append(rank) entries = [ UserDailyMessageEntry(member_name=name, count_list=proc_count[oid], rank_list=proc_rank[oid], available=available_dict[oid]) for oid, name in uname_dict.items() ] return UserDailyMessageResult(label_dates=result.dates, entries=entries, active_members=actv_mbr)
def test_enum_ranking_tie_fn(self): data = [1, 2, 3, 4, 5] self.assertListEqual( [("T1", 1), ("T1", 2), ("T1", 3), ("T1", 4), ("T1", 5)], list(enumerate_ranking(data, is_tie=lambda cur, prv: True)))
def get_module_count_stats(self, channel_oid: ObjectId, limit: Optional[int] = None) \ -> Generator[Tuple[Union[int, str], AutoReplyModuleModel], None, None]: return enumerate_ranking( self._mod.get_module_count_stats(channel_oid, limit), is_tie=lambda cur, prv: cur.called_count == prv.called_count)