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

33 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-19 18:38 +0100

1from portality.dao import DomainObject 

2from portality.models.cache import Cache 

3from portality.models import Journal, Article 

4 

5 

6class JournalArticle(DomainObject): 

7 __type__ = 'journal,article' 

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

9 

10 @classmethod 

11 def site_statistics(cls): 

12 

13 stats = Cache.get_site_statistics() 

14 if stats is not None: 

15 return stats 

16 

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

18 # cache a new set 

19 

20 # prep the query and result objects 

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

22 "journals" : "0", 

23 "countries" : "0", 

24 "abstracts" : "0", 

25 "new_journals" : "0", 

26 "no_apc" : "0" 

27 } 

28 

29 # get the journal data 

30 q = JournalStatsQuery() 

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

32 

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

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

35 

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

37 for b in apc_buckets: 

38 if b.get("key") == "No": 

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

40 break 

41 

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

43 if len(nj_stat) > 0: 

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

45 

46 # get the article data 

47 qa = ArticleStatsQuery() 

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

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

50 

51 # now cache and return 

52 Cache.cache_site_statistics(stats) 

53 

54 return stats 

55 

56 

57class JournalStatsQuery(object): 

58 stats = { 

59 "track_total_hits" : True, 

60 "query": { 

61 "bool": { 

62 "must": [ 

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

64 ] 

65 } 

66 }, 

67 "size": 0, 

68 "aggs": { 

69 "countries" : { 

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

71 }, 

72 "apcs" : { 

73 "terms" : {"field" : "index.has_apc.exact"} 

74 }, 

75 "creation" : { 

76 "date_range" : { 

77 "field" : "created_date", 

78 "ranges" : [ 

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

80 ] 

81 } 

82 } 

83 } 

84 } 

85 

86 

87class ArticleStatsQuery(object): 

88 q = { 

89 "track_total_hits" : True, 

90 "query": { 

91 "bool": { 

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

93 } 

94 }, 

95 "size": 0 

96 }