def METHOD_NAME(user: Optional[User], start_date: datetime.date, end_date: datetime.date):
	query = db.session \
		.query(PackageDailyStats.package_id, PackageDailyStats.date,
			(PackageDailyStats.platform_minetest + PackageDailyStats.platform_other).label("downloads"))
	if user:
		query = query.filter(PackageDailyStats.package.has(author_id=user.id))
	all_stats = query \
		.filter(PackageDailyStats.package.has(state=PackageState.APPROVED),
				PackageDailyStats.date >= start_date, PackageDailyStats.date <= end_date) \
		.order_by(db.asc(PackageDailyStats.package_id), db.asc(PackageDailyStats.date)) \
		.all()
	stats_by_package = {}
	for stat in all_stats:
		bucket = stats_by_package.get(stat.package_id, [])
		stats_by_package[stat.package_id] = bucket
		bucket.append(stat)
	package_title_by_id = {}
	pkg_query = user.packages if user else Package.query
	for package in pkg_query.filter_by(state=PackageState.APPROVED).all():
		if user:
			package_title_by_id[package.id] = package.title
		else:
			package_title_by_id[package.id] = package.get_id()
	result = {}
	for package_id, stats in stats_by_package.items():
		i = 0
		row = []
		result[package_title_by_id[package_id]] = row
		for date in daterange(start_date, end_date):
			if i >= len(stats):
				row.append(0)
				continue
			stat = stats[i]
			if stat.date == date:
				row.append(stat.downloads)
				i += 1
			elif stat.date > date:
				row.append(0)
			else:
				raise Exception(f"Invalid logic, expected stat {stat.date} to be later than {date}")
	return result