@@ -162,28 +162,22 @@ def migrate_usage_reports(osfid: str, until_when: str):
162162 _item_is_component = is_osf_component (_osfobj ) if _osfobj else False
163163
164164 def _each_new ():
165- # go in sorted order to build cumulative counts
166- # (only a few dozen of these per item; should be fine to sort and load all at once)
167165 _each_hit = _es6_scan_range (
168166 es6_reports .PublicItemUsageReport ,
169167 until_when = until_when ,
170168 addl_filter = {'terms' : {'item_osfid' : _synonymous_osfids (osfid )}},
171- sort = 'report_yearmonth' ,
172169 )
170+ # (only a few dozen of these per item; should be fine to load all at once)
173171 _hits = list (_each_hit )
174172 if _osfobj and not _hits :
175173 # this item has usages, but only before the monthly usage reparts started
176174 # -- create one for cumulative counts (if the object still exists)
177175 yield _backfill_old_usage_report (_osfobj , _item_is_component , until_when )
178176 else :
179- _prior_report = None
180177 for _hit in _hits :
181- yield (
182- _prior_report := _convert_public_usage_report (
183- _hit ['_source' ],
184- _prior_report ,
185- item_is_component = _item_is_component ,
186- )
178+ yield _convert_public_usage_report (
179+ _hit ['_source' ],
180+ item_is_component = _item_is_component ,
187181 )
188182
189183 _es8_bulk_save (es8_metrics .MonthlyPublicItemUsageReportEs8 , _each_new ())
@@ -223,7 +217,6 @@ def _es6_scan_range(
223217 from_when : str = '' ,
224218 until_when : str ,
225219 addl_filter = None ,
226- sort = None ,
227220):
228221 _timestamp_range = {'lt' : until_when }
229222 if from_when :
@@ -234,13 +227,10 @@ def _es6_scan_range(
234227 if addl_filter :
235228 _filters .append (addl_filter )
236229 _query_body = {'query' : {'bool' : {'filter' : _filters }}}
237- if sort :
238- _query_body ['sort' ] = sort
239230 return es6_helpers .scan (
240231 _es6_connection (),
241232 index = es6_recordtype ._template_pattern ,
242233 query = _query_body ,
243- preserve_order = bool (sort ),
244234 )
245235
246236
@@ -402,26 +392,13 @@ def _convert_preprint_metric(
402392
403393def _convert_public_usage_report (
404394 source : dict ,
405- prior_report : es8_metrics .MonthlyPublicItemUsageReportEs8 | None ,
406395 item_is_component : bool ,
407396) -> es8_metrics .MonthlyPublicItemUsageReportEs8 :
408- if prior_report is None :
409- _c_views , _c_view_sess , _c_downloads , _c_download_sess = _get_cumulative_usage (
410- osfid = source ['item_osfid' ],
411- until_when = YearMonth .from_str (source ['report_yearmonth' ]).month_end (),
412- is_preprint = ('preprint' in source .get ('item_type' , ())),
413- )
414- else :
415- _c_views = prior_report .cumulative_view_count + source .get ('view_count' , 0 )
416- _c_view_sess = prior_report .cumulative_view_session_count + (
417- source .get ('view_session_count' , 0 ) or source .get ('view_count' , 0 )
418- )
419- _c_downloads = prior_report .cumulative_download_count + source .get (
420- 'download_count' , 0
421- )
422- _c_download_sess = prior_report .cumulative_download_session_count + (
423- source .get ('download_session_count' , 0 ) or source .get ('download_count' )
424- )
397+ _c_views , _c_view_sess , _c_downloads , _c_download_sess = _get_cumulative_usage (
398+ osfid = source ['item_osfid' ],
399+ until_when = YearMonth .from_str (source ['report_yearmonth' ]).month_end (),
400+ is_preprint = ('preprint' in source .get ('item_type' , ())),
401+ )
425402 return es8_metrics .MonthlyPublicItemUsageReportEs8 (
426403 cycle_coverage = _semverish_from_yearmonth (source ['report_yearmonth' ]),
427404 item_iri = osfid_iri (source ['item_osfid' ]),
@@ -437,11 +414,11 @@ def _convert_public_usage_report(
437414 provider_ids = source .get ('provider_id' ),
438415 platform_iris = source .get ('platform_iri' ) or [website_settings .DOMAIN ],
439416 view_count = source .get ('view_count' , 0 ),
440- view_session_count = source .get ('view_session_count' , 0 ),
417+ view_session_count = source .get ('view_session_count' ) or source . get ( 'view_count' , 0 ),
441418 cumulative_view_count = _c_views ,
442419 cumulative_view_session_count = _c_view_sess or _c_views ,
443420 download_count = source .get ('download_count' , 0 ),
444- download_session_count = source .get ('download_session_count' , 0 ),
421+ download_session_count = source .get ('download_session_count' ) or source . get ( 'download_count' , 0 ),
445422 cumulative_download_count = _c_downloads ,
446423 cumulative_download_session_count = _c_download_sess or _c_downloads ,
447424 )
@@ -552,7 +529,7 @@ def _cumulative_countedusage_downloads(osfid, until_when) -> tuple[int, int]:
552529
553530
554531def _cumulative_preprint_count (preprint_metric_cls , osfid : str , until_when : str ) -> int :
555- '''aggregate views on each preprint'''
532+ '''aggregate counts on given preprint'''
556533 # copied/adapted from osf.metrics.preprint_metrics
557534 _search = (
558535 preprint_metric_cls .search ()
@@ -562,12 +539,11 @@ def _cumulative_preprint_count(preprint_metric_cls, osfid: str, until_when: str)
562539 )
563540 _search .aggs .metric ('agg_count' , 'sum' , field = 'count' )
564541 _response = _search .execute ()
565- _view_count = (
542+ return (
566543 int (_response .aggregations .agg_count .value )
567544 if hasattr (_response .aggregations , 'agg_count' )
568545 else 0
569546 )
570- return _view_count
571547
572548
573549def _synonymous_osfids (osfid : str ) -> list [str ]:
0 commit comments