Coverage for portality / models / search.py: 45%

29 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-05-04 09:41 +0100

1from portality.dao import DomainObject 

2from portality.models import Journal, Article 

3 

4 

5class JournalArticle(DomainObject): 

6 __type__ = 'journal,article' 

7 __readonly__ = True # TODO actually heed this attribute in all DomainObject methods which modify data 

8 

9 @classmethod 

10 def site_statistics(cls): 

11 

12 # stats = Cache.get_site_statistics() 

13 # if stats is not None: 

14 # return stats 

15 

16 # we didn't get anything from the cache, so we need to generate and 

17 # cache a new set 

18 

19 # prep the query and result objects 

20 stats = { # Note these values all have to be strings 

21 "journals" : "0", 

22 "countries" : "0", 

23 "abstracts" : "0", 

24 "new_journals" : "0", 

25 "no_apc" : "0" # this is now used to represent "no charges", which means no apc and no other charges 

26 } 

27 

28 # get the journal data 

29 q = JournalStatsQuery() 

30 journal_data = Journal.query(q=q.stats) 

31 

32 stats["journals"] = "{0:,}".format(journal_data.get("hits", {}).get("total", {}).get('value', 0)) 

33 stats["countries"] = "{0:,}".format(len(journal_data.get("aggregations", {}).get("countries", {}).get("buckets", []))) 

34 

35 # apc_buckets = journal_data.get("aggregations", {}).get("apcs", {}).get("buckets", []) 

36 # for b in apc_buckets: 

37 # if b.get("key") == "No": 

38 # stats["no_apc"] = "{0:,}".format(b.get("doc_count")) 

39 # break 

40 

41 nj_stat = journal_data.get("aggregations", {}).get("creation", {}).get("buckets", []) 

42 if len(nj_stat) > 0: 

43 stats["new_journals"] = "{0:,}".format(nj_stat[0].get("doc_count", 0)) 

44 

45 # count the journals with no fees 

46 qc = JournalChargesQuery() 

47 no_fees = Journal.query(q=qc.query()) 

48 stats["no_apc"] = "{0:,}".format(no_fees.get("hits", {}).get("total", {}).get('value', 0)) 

49 

50 # get the article data 

51 qa = ArticleStatsQuery() 

52 article_data = Article.query(q=qa.q) 

53 stats["abstracts"] = "{0:,}".format(article_data.get("hits", {}).get("total", {}).get('value', 0)) 

54 

55 # now cache and return 

56 # Cache.cache_site_statistics(stats) 

57 

58 return stats 

59 

60 

61class JournalChargesQuery(object): 

62 def query(self): 

63 return { 

64 "track_total_hits": True, 

65 "query": { 

66 "bool": { 

67 "must": [ 

68 {"term": {"admin.in_doaj": True}}, 

69 {"term": {"bibjson.apc.has_apc": False}}, 

70 {"term": {"bibjson.other_charges.has_other_charges": False}} 

71 ] 

72 } 

73 }, 

74 "size": 0 

75 } 

76 

77class JournalStatsQuery(object): 

78 stats = { 

79 "track_total_hits" : True, 

80 "query": { 

81 "bool": { 

82 "must": [ 

83 {"term": {"admin.in_doaj": True}} 

84 ] 

85 } 

86 }, 

87 "size": 0, 

88 "aggs": { 

89 "countries" : { 

90 "terms" : {"field" : "index.country.exact", "size" : 500} 

91 }, 

92 "creation" : { 

93 "date_range" : { 

94 "field" : "created_date", 

95 "ranges" : [ 

96 {"from" : "now-1M"} 

97 ] 

98 } 

99 } 

100 } 

101 } 

102 

103 

104class ArticleStatsQuery(object): 

105 q = { 

106 "track_total_hits" : True, 

107 "query": { 

108 "bool": { 

109 "must": {"term": {"admin.in_doaj": "true"}} 

110 } 

111 }, 

112 "size": 0 

113 }