def popular_packages(cls, limit: int): packages = Package.objects()\ .order_by('-total_downloads')\ .limit(limit)\ .all() return list(packages)
def main(): print("Fake data adder script") mongo_setup.global_init('pypi') t0 = datetime.datetime.now() if User.objects().count() > 0: print("Users exists, skipping...", flush=True) users = list(User.objects()) else: users = build_users() if Package.objects().count() > 0: print("Packages exists, skipping...", flush=True) packages = list(Package.objects()) else: packages = build_packages(users) if ReleaseHistory.objects().count() > 0: print("Releases exists, skipping...", flush=True) releases = list(ReleaseHistory.objects()) else: releases = build_releases(packages) if Download.objects().count() > 0: print("Downloads exists, skipping...", flush=True) downloads = list(Download.objects()) # sync_downloads(downloads) else: downloads = build_downloads(releases, packages) print("Syncing download count to packages ...", flush=True) # sync_downloads(downloads) t1 = datetime.datetime.now() dt = t1 - t0 print(f"Created {user_count:,} users, {package_count:,} packages, " f"{release_count:,} releases, {download_count:,} downloads.") print(f"Total time: {dt.total_seconds():,} sec.")
def build_packages(users): packages = [] print(f"Creating {package_count:,} fake packages...", flush=True) for i in range(0, package_count): maintainers = [] for _ in range(0, random.randint(1, 5)): u = random.choice(users) maintainers.append(u.id) p = Package() p.name = f"Package{i+1}" p.maintainers.extend(maintainers) packages.append(p) Package.objects().insert(packages) return list(Package.objects())
def package_count(cls): return Package.objects().count()
def find_package_by_name(cls, name): return Package.objects(name=name).first()
def sync_downloads(downloads): for d in downloads: Package.objects(id=d.package_id).update_one(inc__total_downloads=1)