Esempio n. 1
0
def _generate_stats_for_datestr(datestr, kinds=None):

    if not kinds:
        kinds = ['Analytic', 'AnalyticVendor', 'AnalyticFirmware', 'Useragent']

    # update AnalyticVendor
    if 'AnalyticVendor' in kinds:
        for analytic in db.session.query(AnalyticVendor).filter(
                AnalyticVendor.datestr == datestr):
            db.session.delete(analytic)
        db.session.commit()
        for v in db.session.query(Vendor):
            _generate_stats_for_vendor(v, datestr)
        db.session.commit()

    # update AnalyticFirmware
    if 'AnalyticFirmware' in kinds:
        for analytic in db.session.query(AnalyticFirmware).filter(
                AnalyticFirmware.datestr == datestr):
            db.session.delete(analytic)
        db.session.commit()
        for fw in db.session.query(Firmware)\
                            .join(Remote).filter(Remote.name != 'deleted'):
            _generate_stats_for_firmware(fw, datestr)
        db.session.commit()

    # update Useragent
    if 'Useragent' in kinds:
        for agnt in db.session.query(Useragent).filter(
                Useragent.datestr == datestr):
            db.session.delete(agnt)
        db.session.commit()
        ua_apps = {}
        ua_fwupds = {}
        ua_distros = {}
        ua_langs = {}
        clients = db.session.query(Client.user_agent).\
                        filter(Client.datestr == datestr).all()
        for res in clients:
            ua = res[0]
            if not ua:
                continue

            # downloader app
            ua_app = _get_app_from_ua(ua)
            if ua_app not in ua_apps:
                ua_apps[ua_app] = 1
            else:
                ua_apps[ua_app] += 1

            # fwupd version
            ua_fwupd = _get_fwupd_from_ua(ua)
            if ua_fwupd not in ua_fwupds:
                ua_fwupds[ua_fwupd] = 1
            else:
                ua_fwupds[ua_fwupd] += 1

            # language and distro
            ua_lang_distro = _get_lang_distro_from_ua(ua)
            if ua_lang_distro:
                ua_lang = ua_lang_distro[0]
                ua_distro = ua_lang_distro[1]
                if ua_lang not in ua_langs:
                    ua_langs[ua_lang] = 1
                else:
                    ua_langs[ua_lang] += 1
                if ua_distro not in ua_distros:
                    ua_distros[ua_distro] = 1
                else:
                    ua_distros[ua_distro] += 1
        for ua in ua_apps:
            db.session.add(
                Useragent(UseragentKind.APP, ua, datestr, cnt=ua_apps[ua]))
        for ua in ua_fwupds:
            db.session.add(
                Useragent(UseragentKind.FWUPD, ua, datestr, cnt=ua_fwupds[ua]))
        for ua in ua_langs:
            db.session.add(
                Useragent(UseragentKind.LANG, ua, datestr, cnt=ua_langs[ua]))
        for ua in ua_distros:
            db.session.add(
                Useragent(UseragentKind.DISTRO,
                          ua,
                          datestr,
                          cnt=ua_distros[ua]))
        db.session.commit()

    # update Analytic
    if 'Analytic' in kinds:
        analytic = db.session.query(Analytic).filter(
            Analytic.datestr == datestr).first()
        if analytic:
            db.session.delete(analytic)
            db.session.commit()
        db.session.add(Analytic(datestr, len(clients)))
        db.session.commit()

    # for the log
    print('generated for %s: %s' % (datestr, ','.join(kinds)))
def _generate_stats_for_datestr(datestr):

    # update AnalyticVendor
    for analytic in db.session.query(AnalyticVendor).filter(
            AnalyticVendor.datestr == datestr):
        db.session.delete(analytic)
    db.session.commit()
    for v in db.session.query(Vendor):
        _generate_stats_for_vendor(v, datestr)
    db.session.commit()

    # update AnalyticFirmware
    for analytic in db.session.query(AnalyticFirmware).filter(
            AnalyticFirmware.datestr == datestr):
        db.session.delete(analytic)
    db.session.commit()
    for firmware_id, in db.session.query(Firmware.firmware_id)\
                                  .join(Remote)\
                                  .filter(Remote.name != 'deleted'):
        fw = db.session.query(Firmware)\
                       .filter(Firmware.firmware_id == firmware_id)\
                       .one()
        _generate_stats_for_firmware(fw, datestr)
    db.session.commit()

    # update Useragent
    for agnt in db.session.query(Useragent).filter(
            Useragent.datestr == datestr):
        db.session.delete(agnt)
    db.session.commit()
    ua_apps = {}
    ua_fwupds = {}
    ua_distros = {}
    ua_langs = {}
    clients = db.session.query(Client.user_agent).\
                    filter(Client.datestr == datestr).all()
    for res in clients:
        ua = res[0]
        if not ua:
            continue

        # downloader app
        ua_app = _get_app_from_ua(ua)
        if ua_app not in ua_apps:
            ua_apps[ua_app] = 1
        else:
            ua_apps[ua_app] += 1

        # fwupd version
        ua_fwupd = _get_fwupd_from_ua(ua)
        if ua_fwupd not in ua_fwupds:
            ua_fwupds[ua_fwupd] = 1
        else:
            ua_fwupds[ua_fwupd] += 1

        # language and distro
        ua_lang_distro = _get_lang_distro_from_ua(ua)
        if ua_lang_distro:
            ua_lang = ua_lang_distro[0]
            ua_distro = ua_lang_distro[1]
            if ua_lang not in ua_langs:
                ua_langs[ua_lang] = 1
            else:
                ua_langs[ua_lang] += 1
            if ua_distro not in ua_distros:
                ua_distros[ua_distro] = 1
            else:
                ua_distros[ua_distro] += 1
    for ua in ua_apps:
        db.session.add(
            Useragent(kind=int(UseragentKind.APP),
                      value=ua,
                      datestr=datestr,
                      cnt=ua_apps[ua]))
    for ua in ua_fwupds:
        db.session.add(
            Useragent(kind=int(UseragentKind.FWUPD),
                      value=ua,
                      datestr=datestr,
                      cnt=ua_fwupds[ua]))
    for ua in ua_langs:
        db.session.add(
            Useragent(kind=int(UseragentKind.LANG),
                      value=ua,
                      datestr=datestr,
                      cnt=ua_langs[ua]))
    for ua in ua_distros:
        db.session.add(
            Useragent(kind=int(UseragentKind.DISTRO),
                      value=ua,
                      datestr=datestr,
                      cnt=ua_distros[ua]))
    db.session.commit()

    # update Analytic
    analytic = db.session.query(Analytic).filter(
        Analytic.datestr == datestr).first()
    if analytic:
        db.session.delete(analytic)
        db.session.commit()
    db.session.add(Analytic(datestr=datestr, cnt=len(clients)))
    db.session.commit()

    # for the log
    print('generated for %s' % datestr)