@@ -143,22 +143,22 @@ def serialize_global(local_stock, global_stock):
143143 )
144144 portfolio_percentage_str = (
145145 "{:.2f}" .format (round (portfolio_percentage , 4 ))
146- if portfolio_percentage != "N/A"
146+ if portfolio_percentage != "N/A" and type ( portfolio_percentage ) == float
147147 else "N/A"
148148 )
149149 ownership_percentage_str = (
150150 "{:.2f}" .format (round (ownership_percentage , 4 ))
151- if ownership_percentage != "N/A"
151+ if ownership_percentage != "N/A" and type ( ownership_percentage ) == float
152152 else "N/A"
153153 )
154154 gain_value_str = (
155155 "{:.2f}" .format (round (gain_value , 2 ))
156- if update and buy_timeseries != "N/A"
156+ if update and buy_timeseries != "N/A" and type ( gain_value ) == float
157157 else "N/A"
158158 )
159159 gain_percent_str = (
160160 "{:.2f}" .format (round (gain_percent , 2 ))
161- if update and buy_timeseries != "N/A"
161+ if update and buy_timeseries != "N/A" and type ( gain_percent ) == float
162162 else "N/A"
163163 )
164164
@@ -303,13 +303,21 @@ def serialize_local(
303303
304304
305305def analyze_total (cik , stocks , access_number ):
306- market_values = []
306+ market_map = []
307307 for key in stocks :
308308 stock = stocks [key ]
309309 value = stock .get ("market_value" , 0 )
310- market_values .append (value )
311-
310+ market_map .append ({"cusip" : key , "market_value" : value })
311+
312+ market_values = [stock ["market_value" ] for stock in market_map ]
313+ top_holdings = [
314+ stock ["cusip" ]
315+ for stock in sorted (market_map , key = lambda x : x ["market_value" ], reverse = True )[
316+ :5
317+ ][: min (5 , len (market_map ))]
318+ ]
312319 total = sum (market_values )
320+
313321 database .edit_filing (
314322 {
315323 "cik" : cik ,
@@ -319,6 +327,7 @@ def analyze_total(cik, stocks, access_number):
319327 {
320328 "$set" : {
321329 "market_value" : total ,
330+ "top_holdings" : top_holdings ,
322331 }
323332 },
324333 )
@@ -621,14 +630,27 @@ def sort_pipeline(
621630 reverse : bool ,
622631 unavailable : bool ,
623632 additional : list = [],
633+ stock_structure : str = "array" ,
624634 collection_search = database .search_filers ,
635+ match_query = {},
625636):
626637 if limit < 0 :
627638 raise ValueError
628639
629640 pipeline = [
630- {"$match" : {"cik" : cik }},
641+ {
642+ "$match" : {"cik" : cik , ** match_query },
643+ },
631644 ]
645+
646+ if stock_structure == "dict" :
647+ pipeline .extend (
648+ [
649+ {"$set" : {"stocks" : {"$objectToArray" : "$stocks" }}},
650+ {"$set" : {"stocks" : "$stocks.v" }},
651+ ]
652+ )
653+
632654 if additional :
633655 pipeline .extend (additional )
634656
@@ -646,15 +668,18 @@ def sort_pipeline(
646668 if unavailable is False :
647669 sort_query = f"${ sort } "
648670
671+ pipeline .append ({"$project" : {"_id" : 1 }})
649672 cursor = collection_search (pipeline )
650673 results = [result for result in cursor ]
651674 if not cursor or not results :
652675 raise LookupError
653676 count = len (results )
654677
678+ pipeline .pop (- 1 )
655679 pipeline .append (
656680 {"$sort" : {sort : 1 if reverse else - 1 , "_id" : 1 }},
657681 )
682+
658683 if unavailable is False :
659684 sort_stage = pipeline .pop (- 1 )
660685 pipeline .extend (
0 commit comments