Package coprs :: Package logic :: Module stat_logic
[hide private]
[frames] | no frames]

Source Code for Module coprs.logic.stat_logic

  1  from collections import defaultdict 
  2   
  3  from sqlalchemy.orm.exc import NoResultFound 
  4   
  5  from coprs import app 
  6  from coprs import db 
  7  from coprs.models import CounterStat 
  8  from coprs import helpers 
  9  from coprs.helpers import REPO_DL_STAT_FMT, CHROOT_REPO_MD_DL_STAT_FMT, string_dt_to_unixtime, \ 
 10      CHROOT_RPMS_DL_STAT_FMT, PROJECT_RPMS_DL_STAT_FMT, is_ip_from_builder_net 
 11  from coprs.helpers import CounterStatType 
 12  from coprs.rmodels import TimedStatEvents 
13 14 15 -class CounterStatLogic(object):
16 17 @classmethod
18 - def get(cls, name):
19 """ 20 :param name: counter name 21 :return: 22 """ 23 return CounterStat.query.filter(CounterStat.name == name)
24 25 @classmethod
26 - def get_multiply_same_type(cls, counter_type, names_list):
27 return ( 28 CounterStat.query 29 .filter(CounterStat.counter_type == counter_type) 30 .filter(CounterStat.name.in_(names_list)) 31 )
32 33 @classmethod
34 - def add(cls, name, counter_type):
35 csl = CounterStat(name=name, counter_type=counter_type) 36 db.session.add(csl) 37 return csl
38 39 @classmethod
40 - def incr(cls, name, counter_type):
41 """ 42 Warning: dirty method: does commit if missing stat record. 43 """ 44 try: 45 csl = CounterStatLogic.get(name).one() 46 csl.counter = CounterStat.counter + 1 47 except NoResultFound: 48 csl = CounterStatLogic.add(name, counter_type) 49 csl.counter = 1 50 51 db.session.add(csl) 52 return csl
53 54 @classmethod
55 - def get_copr_repo_dl_stat(cls, copr):
56 # chroot -> stat_name 57 chroot_by_stat_name = {} 58 for chroot in copr.active_chroots: 59 kwargs = { 60 "copr_user": copr.user.name, 61 "copr_project_name": copr.name, 62 "copr_name_release": chroot.name_release 63 } 64 chroot_by_stat_name[REPO_DL_STAT_FMT.format(**kwargs)] = chroot.name_release 65 66 # [{counter: <value>, name: <stat_name>}, ...] 67 stats = cls.get_multiply_same_type(counter_type=helpers.CounterStatType.REPO_DL, 68 names_list=chroot_by_stat_name.keys()) 69 70 # need: {chroot -> value, ... } 71 repo_dl_stats = defaultdict(int) 72 for stat in stats: 73 repo_dl_stats[chroot_by_stat_name[stat.name]] = stat.counter 74 75 return repo_dl_stats
76
77 78 -def handle_logstash(rc, ls_data):
79 """ 80 :param rc: connection to redis 81 :type rc: StrictRedis 82 83 :param ls_data: log stash record 84 :type ls_data: dict 85 """ 86 dt_unixtime = string_dt_to_unixtime(ls_data["@timestamp"]) 87 app.logger.debug("got ls_data: {}".format(ls_data)) 88 89 # don't count statistics from builders 90 if "clientip" in ls_data: 91 if is_ip_from_builder_net(ls_data["clientip"]): 92 return 93 94 if "tags" not in ls_data: 95 return 96 97 tags = set(ls_data["tags"]) 98 if "frontend" in tags and "repo_dl": 99 name = REPO_DL_STAT_FMT.format(**ls_data) 100 CounterStatLogic.incr(name=name, counter_type=CounterStatType.REPO_DL) 101 db.session.commit() 102 103 if "backend" in tags and "repomdxml" in tags: 104 if "copr_user" in ls_data: 105 key = CHROOT_REPO_MD_DL_STAT_FMT.format(**ls_data) 106 TimedStatEvents.add_event(rc, key, timestamp=dt_unixtime) 107 108 if "backend" in tags and "rpm" in tags: 109 key_chroot = CHROOT_RPMS_DL_STAT_FMT.format(**ls_data) 110 key_project = PROJECT_RPMS_DL_STAT_FMT.format(**ls_data) 111 TimedStatEvents.add_event(rc, key_chroot, timestamp=dt_unixtime) 112 TimedStatEvents.add_event(rc, key_project, timestamp=dt_unixtime)
113