Exemple #1
0
def createTabla(config, title, fluidos=None, parent=None):
    """Create TablaMEoS to add to mainwindow
        config: configparser instance with project configuration
        title: title for the table
        fluidos: optional array with meos instances to fill de table
        parent: mainwindow pointer
        """
    propiedades, keys, units = get_propiedades(config)

    # Add the unit suffix to properties title
    for i, unit in enumerate(units):
        sufx = unit.text()
        if not sufx:
            sufx = "[-]"
        propiedades[i] = propiedades[i] + os.linesep + sufx

    # Add two phases properties if requested
    if config.getboolean("MEoS", "phase"):
        for i in range(len(propiedades) - 1, -1, -1):
            if keys[i] in ThermoAdvanced.propertiesPhase():
                txt = [propiedades[i]]
                prefix = QtWidgets.QApplication.translate("pychemqt", "Liquid")
                txt.append(prefix + os.linesep + propiedades[i])
                prefix = QtWidgets.QApplication.translate("pychemqt", "Vapour")
                txt.append(prefix + os.linesep + propiedades[i])
                propiedades[i:i + 1] = txt
                units[i:i + 1] = [units[i]] * 3

    # Define common argument for TableMEoS
    kw = {}
    kw["horizontalHeader"] = propiedades
    kw["stretch"] = False
    kw["units"] = units
    kw["parent"] = parent
    kw["keys"] = keys

    if fluidos:
        # Generate a readOnly table filled of data
        tabla = TablaMEoS(len(propiedades), readOnly=True, **kw)
        data = []
        for fluido in fluidos:
            fila = _getData(fluido, keys, config.getboolean("MEoS", "phase"))
            data.append(fila)
        tabla.setData(data)

    else:
        # Generate a dinamic table empty
        columnInput = []
        for key in keys:
            if key in ["P", "T", "x", "rho", "v", "h", "s"]:
                columnInput.append(False)
            else:
                columnInput.append(True)
            if config.getboolean("MEoS", "phase") and \
                    key in ThermoAdvanced.propertiesPhase():
                columnInput.append(True)
                columnInput.append(True)
        kw["columnReadOnly"] = columnInput
        kw["dinamica"] = True

        # Discard the keys from single phase state as input values
        if config.getboolean("MEoS", "phase"):
            for i in range(len(keys) - 1, -1, -1):
                if keys[i] in ThermoAdvanced.propertiesPhase():
                    keys[i:i + 1] = [keys[i], "", ""]

        tabla = TablaMEoS(len(propiedades), filas=1, **kw)

    prefix = QtWidgets.QApplication.translate("pychemqt", "Table")
    tabla.setWindowTitle(prefix + ": " + title)
    tabla.resizeColumnsToContents()
    return tabla
    def run(self, impression_master_id=None):
        if impression_master_id:
            self.im_id = impression_master_id
            self.day_im = self.get_day_impression_by_id(self.im_id)
            self.region_rate = self.get_region_rate_by_id(self.im_id)

        if not self.day_im:
            self.logger.error("[AllotDayClientScript]: no day_impression")
            return

        # 第一天
        day_first = self.day_im[0]

        total_client = day_first.client
        clients = self.get_clients(total_client)
        for code, client, nums in clients:  # divide by region
            start = 0
            end = 0
            # 异常处理,continue
            try: # 当异常处理表有记录时,表示已经执行过了
                ExceptionContinueModel.get(ExceptionContinueModel.day_impression_id==day_first.id,
                                                    targeting_code=code,
                                                    type="allot_day_client").nums
                self.logger.debug(u"处理过了 %s %s" % (day_first.date, code))
                continue
            except ExceptionContinueModel.DoesNotExist:
                # 清除异常时的数据
                delete_exception = """
                delete t1 from bl_campaign_client as t1
                left join bl_client_masters as t2 on t2.id = t1.client_id
                where t1.day_impression_id={day_impression_id} and (t2.province_code="{targeting_code}" or t2.city_code = "{targeting_code}")
                """
                CampaignClientModel.raw(delete_exception.format(day_impression_id=day_first.id,
                                                                targeting_code=code)).execute()


                for i, per in enumerate(self.client_rate):
                    client_rate_num = nums * float(per) / 100
                    end = int(round(client_rate_num)) + start
                    for j in client[start:end]:
                        CampaignClientModel(day_impression_id=day_first.id,
                                            client_id=j.id,
                                            actual_plan_impression=i,
                                            plan_impression=i).\
                            save()
                    start = end

                ExceptionContinueModel.create(day_impression_id=day_first.id,
                                              type="allot_day_client",
                                              targeting_code=code,
                                              nums=nums).save()


        day_left = self.day_im[1:]
        ###### replace start
        try:
            if day_left:
                ExceptionContinueModel.get(ExceptionContinueModel.day_impression_id==day_left[0].id,
                                                    type="allot_day_client")
                self.logger.debug("已经分配过了")
        except ExceptionContinueModel.DoesNotExist:
            # 清除已经异常时分配的任务
            self.logger.debug("开始分配")
            # 其他天
            # 复制第一天的 client 的 80% 到其他天
            for day in day_left:
                CampaignClientModel.raw("insert into bl_campaign_client(day_impression_id, client_id, plan_impression) select %d as day_impression_id, client_id, -1 as plan_impression from bl_campaign_client where bl_campaign_client.day_impression_id = %d;" % (day.id, day_first.id)).\
                    execute()

            for day in day_left:
                if config.getboolean("cli", "replace"):
                    count = CampaignClientModel.select().\
                        where(CampaignClientModel.day_impression_id == day.id).\
                        count()
                    # 去除第一天里 20% 的client
                    # TODO: 优化 mysql rand()
                    CampaignClientModel.raw("delete from bl_campaign_client where day_impression_id = %d order by rand() limit %d" % (day.id, int(0.2 * count))).\
                        execute()
                # 去除后的 client,如果当日各个地区的大于则去除,小于则补充

                total_client = day.client

                single_region_sql_province = """
                SELECT count(1) as nums FROM `bl_campaign_client` as t1
                left join `bl_client_masters` as t2
                on t2.id = t1.client_id and t1.day_impression_id = {day_impression_id}
                where t2.province_code = "{targeting_code}"
                """
                single_region_sql_city = """
                SELECT count(1) as nums FROM `bl_campaign_client` as t1
                left join `bl_client_masters` as t2
                on t2.id = t1.client_id and t1.day_impression_id = {day_impression_id}
                where t2.province_code = "{province_code}" and t2.city_code = "{targeting_code}"
                """
                for region in self.region_rate:
                    nums = region.rate * total_client / 100
                    nums = int(round(nums))
                    if region.targeting_code[:2] == "CN": # 省
                        single_region_num = CampaignClientModel.raw(single_region_sql_province.format(day_impression_id=day.id,
                                                                                        targeting_code=region.targeting_code)).execute()
                    else: # city
                        province_code = "CN_{code}".format(code=region.targeting_code[:2])
                        single_region_num = CampaignClientModel.raw(single_region_sql_city.format(day_impression_id=day.id,
                                                                                                  targeting_code=region.targeting_code,
                                                                                                  province_code=province_code)).execute()
                    single_region_num = single_region_num.next().nums
                    if single_region_num:
                        if nums > single_region_num: # 量不足
                            diff = nums - single_region_num

                            addtion_sql_province = """
                            insert into bl_campaign_client(client_id, day_impression_id)
                            select t1.id, {day_im_id} as day_impression_id from bl_client_masters as t1
                            left join bl_campaign_client as t2 on t1.id = t2.client_id
                            where t1.province_code ="{targeting_code}" and t2.id is NULL limit {nums};
                            """
                            addtion_sql_city = """
                            insert into bl_campaign_client(client_id, day_impression_id)
                            select t1.id, {day_im_id} as day_impression_id from bl_client_masters as t1
                            left join bl_campaign_client as t2 on t1.id = t2.client_id
                            where (t1.province_code ="{province_code}" and t1.city_code ="{targeting_code}") and t2.id is NULL limit {nums};
                            """
                            # 补充,TODO 未随机
                            if region.targeting_code[:2] == "CN": # 省
                                addtion_clients = ClientMasterModel.raw(addtion_sql_province.format(day_im_id=day.id,
                                                                                    targeting_code=region.targeting_code,
                                                                                    nums=diff)).execute()
                            else:
                                province_code = "CN_{code}".format(code=region.targeting_code[:2])
                                addtion_clients = ClientMasterModel.raw(addtion_sql_city.format(day_im_id=day.id,
                                                                                    targeting_code=region.targeting_code,
                                                                                                province_code=province_code,
                                                                                    nums=diff)).execute()
                        elif nums < single_region_num: # 量过了,不删,plan_impression 为 0 即不使用
                            pass

                    else: # 没有此地区,增加
                        if nums > 0:
                            addtion_sql_province = """
                            insert into bl_campaign_client(client_id, day_impression_id)
                            select t1.id, {day_im_id} as day_impression_id from bl_client_masters as t1
                            left join bl_campaign_client as t2 on t1.id = t2.client_id
                            where t1.province_code ="{targeting_code}" and t2.id is NULL limit {nums};
                            """
                            addtion_sql_city = """
                            insert into bl_campaign_client(client_id, day_impression_id)
                            select t1.id, {day_im_id} as day_impression_id from bl_client_masters as t1
                            left join bl_campaign_client as t2 on t1.id = t2.client_id
                            where (t1.province_code ="{province_code}" and t1.city_code ="{targeting_code}") and t2.id is NULL limit {nums};
                            """
                            # 补充,TODO 未随机
                            if region.targeting_code[:2] == "CN": # 省
                                addtion_clients = ClientMasterModel.raw(addtion_sql_province.format(day_im_id=day.id,
                                                                                    targeting_code=region.targeting_code,
                                                                                    nums=nums)).execute()
                            else:
                                province_code = "CN_{code}".format(code=region.targeting_code[:2])
                                addtion_clients = ClientMasterModel.raw(addtion_sql_city.format(day_im_id=day.id,
                                                                                    targeting_code=region.targeting_code,
                                                                                                    province_code=province_code,
                                                                                    nums=nums)).execute()

        ###### replace 完毕
        self.logger.debug("开始设置剩余天数的频次")
        # 设置剩余天数的 actual_plan_impression
        for day in day_left:
            region_client_sql_province = """
            select t1.* from bl_campaign_client as t1
            left join bl_client_masters as t2 on t2.id = t1.client_id
            where t1.day_impression_id = {day_im_id} and t2.province_code = "{targeting_code}"
            """
            region_client_sql_city = """
            select t1.* from bl_campaign_client as t1
            left join bl_client_masters as t2 on t2.id = t1.client_id
            where t1.day_impression_id = {day_im_id} and (t2.province_code = "{province_code}" and t2.city_code = "{targeting_code}")
            """
            region_client_count_sql_province = """
            select count(*) as nums from bl_campaign_client as t1
            left join bl_client_masters as t2 on t2.id = t1.client_id
            where t1.day_impression_id = {day_im_id} and t2.province_code = "{targeting_code}"
            """
            region_client_count_sql_city = """
            select count(*) as nums from bl_campaign_client as t1
            left join bl_client_masters as t2 on t2.id = t1.client_id
            where t1.day_impression_id = {day_im_id} and (t2.province_code = "{province_code}" or t2.city_code = "{targeting_code}")
            """
            for region in self.region_rate: # 地区的 client
                if region.targeting_code[:2] == "CN": # 省
                    clients = CampaignClientModel.raw(region_client_sql_province.format(day_im_id=day.id,
                                                                            targeting_code=region.targeting_code)).\
                        tuples().execute()
                    nums = CampaignClientModel.raw(region_client_count_sql_province.format(day_im_id=day.id,
                                                                            targeting_code=region.targeting_code)).\
                        dicts().execute()
                else:
                    province_code = "CN_{code}".format(code=region.targeting_code[:2])
                    clients = CampaignClientModel.raw(region_client_sql_city.format(day_im_id=day.id,
                                                                                        province_code=province_code,
                                                                            targeting_code=region.targeting_code)).\
                        tuples().execute()
                    nums = CampaignClientModel.raw(region_client_count_sql_city.format(day_im_id=day.id,
                                                                                        province_code=province_code,
                                                                            targeting_code=region.targeting_code)).\
                        dicts().execute()
                nums = nums.next()["nums"]
                start = 0
                end = 0
                # 异常处理,continue
                try: # 当异常处理表有记录时,表示已经执行过了
                    ExceptionContinueModel.get(ExceptionContinueModel.day_impression_id==day.id,
                                                    targeting_code=region.targeting_code,
                                                    type="allot_day_client").nums
                    self.logger.debug(u"设置过了, %s, %s" % (day.date, region.targeting_code))
                    continue
                except ExceptionContinueModel.DoesNotExist:
                    handle_exception = """
                    update bl_campaign_client as t1 left join bl_client_masters as t2 on t2.id = t1.client_id
                    set t1.plan_impression = -1, t1.actual_plan_impression = -1
                    where t1.day_impression_id={day_impression_id} and (t2.province_code="{targeting_code}" or t2.city_code="{targeting_code}")
                    """
                    CampaignClientModel.raw(handle_exception.format(day_impression_id=day.id,
                                                                    targeting_code=region.targeting_code)).execute()

                    self.logger.debug("开始设置")
                    client = clients.cursor.fetchall() # this is a row objects

                    for i, per in enumerate(self.client_rate):
                        self.logger.debug("%s, %s" % (i, per))
                        client_rate_num = nums * float(per) / 100
                        #impression = round(client_rate_num * i)
                        end = int(round(client_rate_num)) + start
                        # TODO: 这里慢
                        for j in client[start:end]:
                                c = CampaignClientModel.get(CampaignClientModel.id == j[0])
                                c.actual_plan_impression = i
                                c.plan_impression = i
                                c.save()
                        start = end
                    self.logger.debug("%s, %s" % (day.date, region.targeting_code))
                    ExceptionContinueModel.create(day_impression_id=day.id,
                                                type="allot_day_client",
                                                targeting_code=region.targeting_code,
                                                nums=nums).save()

        # 更新 day_impression 的 impression
        for day in self.day_im:
            #count = CampaignClientModel.select().\
                #where(CampaignClientModel.day_impression_id == day.id, CampaignClientModel.plan_impression > 0).\
                #count()
            sql = """
            update bl_day_impression\
            set impression =\
            (select sum(plan_impression) from `bl_campaign_client` where day_impression_id = {day_im_id})\
            where id = {day_im_id};
            """
            #day.impression = count
            #day.save()
            CampaignClientModel.raw(sql.format(day_im_id=day.id)).execute()
Exemple #3
0
    def run(self, impression_master_id=None):
        self.im_id = impression_master_id
        self.campaign_paln_queue = get_campaign_plan_queue(self.im_id)
        self.im = self.get_impression_master_by_id(self.im_id)
        #self.result_queue = get_queue()
        #self.result_queue.use(
            #config.get("tubes", "campaign_plan_result") + "_%s" % self.im_id)

        if not self.im:
            self.logger.warning("[AllocationScript]: no impression_master")
            return None

        self.url1 = None

        def repl(matchobj):
            return "{" + matchobj.group(1) + "}"

        self.url1 = re.sub(r"%(\w+)%", repl, self.im.tracking_url_1)

        job = self.campaign_paln_queue.reserve(timeout=0)

        while job:
            value = self.json_loads(job.body)
            value["campaign_date"] = self.str2datetime(value["campaign_date"])
            now = datetime.datetime.now()
            now_hour = datetime.datetime(
                now.year, now.month, now.day, now.hour)
            # days 为正,将来的任务提前到现在,为负,执行以前的任务
            now_hour += datetime.timedelta(days=self.im.delay_days)
            value_now_hour = datetime.datetime(value["campaign_date"].year, value[
                "campaign_date"].month, value["campaign_date"].day, value["campaign_date"].hour)

            if now_hour > value_now_hour: # 清除这个小时未完成的任务
                job.delete()
                job = self.campaign_paln_queue.reserve(timeout=0)
                continue

            if now < value["campaign_date"]:
                self.logger.debug("wait") # , now, value["campaign_date"])
                continue

            url1, params = self.url1, value

            try:
                ts = time.time()
                ts -= random.randint(1500, 1800)

                url = url1.format(mac=params["mac_md5"], ip=params["ip"], ts=int(ts))
                if config.getboolean("log", "url_record"):
                    self.logger.info("[tracking url]:{url}".format(url=url))

                urllib2.urlopen(
                    url
                ).read()

                #CampaignPlanModel.raw(
                    #"UPDATE bl_campaign_plan set status=1 where id={id}".format(id=params["plan_id"])).\
                    #execute()

            except (urllib2.URLError, urllib2.HTTPError) as e:
                self.logger.error("send_request error, task: %r, exception: %r" % (params, e))

            self.logger.debug("worker work hard for %d" % params["plan_id"])

            job.delete()
            job = self.campaign_paln_queue.reserve(timeout=0)