@@ -22,13 +22,12 @@ def parse_args():
2222 return parser .parse_args ()
2323
2424
25- def latest_complete_week_end ():
26- today = date .today ()
27- return today - timedelta (days = today .weekday () + 1 )
25+ def latest_complete_period_end ():
26+ return date .today () - timedelta (days = 1 )
2827
2928
3029def fetch_daily_downloads (package_name , weeks ):
31- end = latest_complete_week_end ()
30+ end = latest_complete_period_end ()
3231 start = end - timedelta (days = (weeks * 7 ) - 1 )
3332 encoded = urllib .parse .quote (package_name , safe = "" )
3433 url = "https://api.npmjs.org/downloads/range/{start}:{end}/{package}" .format (
@@ -45,13 +44,9 @@ def fetch_daily_downloads(package_name, weeks):
4544 return payload .get ("downloads" , [])
4645
4746
48- def week_start (day ):
49- return day - timedelta (days = day .weekday ())
50-
51-
5247def aggregate_weeks (daily_rows , weeks ):
53- end = latest_complete_week_end ()
54- first_week = week_start ( end ) - timedelta (days = (weeks - 1 ) * 7 )
48+ end = latest_complete_period_end ()
49+ first_week = end - timedelta (days = (weeks * 7 ) - 1 )
5550 buckets = []
5651 totals = {}
5752 for i in range (weeks ):
@@ -61,9 +56,9 @@ def aggregate_weeks(daily_rows, weeks):
6156
6257 for row in daily_rows :
6358 day = datetime .strptime (row ["day" ], "%Y-%m-%d" ).date ()
64- start = week_start (day )
65- if start in totals :
66- totals [start ] += int (row .get ("downloads" , 0 ))
59+ index = (day - first_week ). days // 7
60+ if 0 <= index < weeks :
61+ totals [buckets [ index ] ] += int (row .get ("downloads" , 0 ))
6762
6863 return [(start , totals [start ]) for start in buckets ]
6964
@@ -178,7 +173,7 @@ def y_for(value):
178173
179174 return """<svg xmlns="http://www.w3.org/2000/svg" width="{width}" height="{height}" viewBox="0 0 {width} {height}" role="img" aria-labelledby="title desc">
180175 <title id="title">{package_name} weekly npm downloads</title>
181- <desc id="desc">Weekly npm downloads over the last {weeks} full weeks . Latest full week : {latest} downloads.</desc>
176+ <desc id="desc">Weekly npm downloads over the last {weeks} complete 7-day periods . Latest period : {latest} downloads.</desc>
182177 <defs>
183178 <linearGradient id="line" x1="0" x2="1" y1="0" y2="0">
184179 <stop offset="0%" stop-color="#22d3ee" />
@@ -193,7 +188,7 @@ def y_for(value):
193188 <rect width="{width}" height="{height}" rx="16" fill="#0b1020" />
194189 <rect x="1" y="1" width="{inner_w}" height="{inner_h}" rx="15" fill="none" stroke="#26324a" />
195190 <text x="{left}" y="30" font-family="Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, Segoe UI, sans-serif" font-size="18" font-weight="700" fill="#f8fafc">npm weekly downloads</text>
196- <text x="{left}" y="50" font-family="Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, Segoe UI, sans-serif" font-size="12" fill="#8b9ab8">{package_name} · latest full week {latest_fmt} · {total_fmt} over {weeks} weeks · through {through}</text>
191+ <text x="{left}" y="50" font-family="Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, Segoe UI, sans-serif" font-size="12" fill="#8b9ab8">{package_name} · latest 7-day period {latest_fmt} · {total_fmt} over {weeks} periods · through {through}</text>
197192 <g font-family="Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, Segoe UI, sans-serif">
198193 {grid}
199194 <line x1="{left}" y1="{baseline:.1f}" x2="{right_x}" y2="{baseline:.1f}" stroke="#3a4867" stroke-width="1.2" />
0 commit comments