@@ -251,52 +251,66 @@ def _path_is_valid(self, path: list) -> bool:
251251 return False
252252 return True
253253
254- def get_count (self , tiered_keys : list , id ) -> int :
254+ def get_count (self , tiered_keys : list , id ) -> int | None :
255255 if len (tiered_keys ) == 0 :
256256 if id == "Research and Development" :
257- return (
258- PSC .objects .filter (Q (code__startswith = "A" ))
259- .annotate (code_prefix = Substr ("code" , 1 , 2 ))
260- .values ("code_prefix" )
261- .distinct ()
262- .count ()
263- )
257+ return self .get_research_and_dev_count ("A" )
264258 elif id == "Service" :
265- return (
266- PSC .objects .filter (Q (code__regex = r"^[B-Z]$" ))
267- .annotate (code_prefix = Substr ("code" , 1 , 1 ))
268- .values ("code_prefix" )
269- .distinct ()
270- .count ()
271- )
259+ return self .get_service_count (r"^[B-Z]" , is_regex = True )
272260 elif id == "Product" :
273- return (
274- PSC .objects .filter (Q (code__regex = r"^\d\d$" ))
275- .annotate (code_prefix = Substr ("code" , 1 , 2 ))
276- .values ("code_prefix" )
277- .distinct ()
278- .count ()
279- )
261+ return self .get_product_count (r"^\d\d" , is_regex = True )
280262 else :
281263 if id .startswith ("A" ):
282264 return self .get_research_and_dev_count (id )
283- elif len (id ) == 1 :
284- new_length = 2
285- else :
286- new_length = 4
287- all_codes = (
288- PSC .objects .filter (code__startswith = id )
289- .annotate (code_prefix = Substr ("code" , 1 , new_length ), code_len = Length ("code" ))
290- .filter (code_len__gte = new_length )
291- )
292- return all_codes .values ("code_prefix" ).distinct ().count ()
265+ elif re .search (r"^[B-Z]" , id ):
266+ return self .get_service_count (id )
267+ elif re .search (r"^\d\d" , id ):
268+ return self .get_product_count (id )
293269
294- def get_research_and_dev_count (self , id ):
295- new_length = len (id ) + 1
296- filters = [Q (code__startswith = id ), ~ Q (code__endswith = 0 )]
270+ def get_tier_count (self , tier_length , id , is_regex = False , is_r_and_d = False ) -> int :
271+ if is_regex :
272+ filters = [Q (code__regex = id )]
273+ else :
274+ filters = [Q (code__startswith = id )]
275+ if is_r_and_d :
276+ filters .append (~ Q (code__endswith = 0 ))
297277 all_codes = (
298278 PSC .objects .filter (* filters )
299- .annotate (code_prefix = Substr ("code" , 1 , new_length ), code_len = Length ("code" ))
300- .filter (code_len__gte = new_length )
279+ .annotate (code_prefix = Substr ("code" , 1 , tier_length ), code_len = Length ("code" ))
280+ .filter (code_len__gte = tier_length )
301281 )
302282 return all_codes .values ("code_prefix" ).distinct ().count ()
283+
284+ def get_research_and_dev_count (self , id ) -> int :
285+ # Will always need the lowest tier count
286+ count = self .get_tier_count (4 , id , is_r_and_d = True )
287+ if len (id ) == 3 : # ex: AA1
288+ return count
289+
290+ count += self .get_tier_count (3 , id , is_r_and_d = True )
291+ if len (id ) == 2 : # ex: AA
292+ return count
293+
294+ count += self .get_tier_count (2 , id , is_r_and_d = True ) # id = Research and Development
295+ return count
296+
297+ def get_service_count (self , id , is_regex = False ) -> int :
298+ count = self .get_tier_count (4 , id , is_regex )
299+ if len (id ) == 2 : # ex: B5
300+ return count
301+
302+ count += self .get_tier_count (2 , id , is_regex )
303+
304+ if len (id ) == 1 : # ex: B
305+ return count
306+
307+ count += self .get_tier_count (1 , id , is_regex ) # id = Service
308+ return count
309+
310+ def get_product_count (self , id , is_regex = False ) -> int :
311+ count = self .get_tier_count (4 , id , is_regex )
312+ if len (id ) == 2 : # ex: 10
313+ return count
314+
315+ count += self .get_tier_count (2 , id , is_regex ) # id = Product
316+ return count
0 commit comments