Ejemplo n.º 1
0
    def timezone(self, value):
        """Set Property: timezone
        """
        tz_name = value
        if not validate_tz_name(tz_name):
            raise ValueError("Invalid timezone name assigned: {}".format(tz_name))

        self.__timezone = tz_name
    def _collect(
        self,
        auth_type_use,
        start_date,
        end_date,
        request_params=None,
        request_retry=None,
        request_action=TuneV3LogsAdvertisersActions.FIND
    ):
        """Collect data: TUNE Advertiser Stats Clicks.

        Args:
            start_date:
            end_date:
            request_params:
            request_retry:
            request_action:

        Returns:

        """
        auth_value = None
        if auth_type_use == TuneV2AuthenticationTypes.API_KEY:
            auth_value = self.api_key
        elif auth_type_use == TuneV2AuthenticationTypes.SESSION_TOKEN:
            auth_value = self.session_token

        dict_request_params = {
            auth_type_use: auth_value,
            "timezone": self.timezone,
            "fields": (
                "created,"
                "ad_network_id,"
                "campaign.id,"
                "campaign.name,"
                "publisher.id,"
                "publisher.name,"
                "publisher_ref_id,"
                "publisher_sub_site.id,"
                "publisher_sub_site.ref,"
                "publisher_sub_site.name,"
                "publisher_sub_campaign.id,"
                "publisher_sub_campaign.ref,"
                "publisher_sub_campaign.name,"
                "request_url,"
                "site.id,"
                "site.mobile_app_type,"
                "site.name,"
                "site.package_name,"
                "site.store_app_id"
            ),
            "filter": "({})".format(self._FILTER_NOT_DEBUG_NOR_TEST_DATA),
            "start_date": start_date,
            "end_date": end_date,
            "debug": 0
        }

        if request_params:
            if "fields" in request_params and request_params["fields"]:
                dict_request_params["fields"] = \
                    request_params["fields"]

            if "group" in request_params and request_params["group"]:
                dict_request_params["group"] = \
                    request_params["group"]

            if "timestamp" in request_params and request_params["timestamp"]:
                dict_request_params["timestamp"] = \
                    request_params["timestamp"]

            if "filter" in request_params and request_params["filter"]:
                dict_request_params["filter"] = "({} AND {})".format(
                    request_params["filter"], self._FILTER_NOT_DEBUG_NOR_TEST_DATA
                )

            if "limit" in request_params:
                dict_request_params["limit"] = \
                    int(request_params["limit"])

            if "debug" in request_params:
                dict_request_params["debug"] = \
                    int(request_params["debug"])

            timezone = None
            if "timezone" in request_params:
                timezone = request_params["timezone"]

            if timezone:
                if not validate_tz_name(timezone):
                    return TuneReportingError(error_message="Invalid Timezone: {}".format(timezone))
                self.timezone = timezone
                dict_request_params["timezone"] = \
                    self.timezone

        self.logger.debug(("TMC v3 Logs Advertisers Clicks: "
                           "Action '{}', Params: {}").format(request_action, str(dict_request_params)))

        self.logger.debug(("TMC v3 Logs Advertisers Clicks: " "Timezone: {}").format(self.timezone))

        try:
            if request_action == TuneV3LogsAdvertisersActions.FIND:
                request_params["sorts"] = "created desc"

                self._find_v3(request_params=dict_request_params, request_retry=request_retry)
            elif request_action == TuneV3LogsAdvertisersActions.EXPORT:
                self._export_v3_download_csv(request_params=dict_request_params, request_retry=request_retry)

        except TuneRequestBaseError as tmc_req_ex:
            self.logger.error(
                "TMC v3 Logs Advertisers Clicks: Failed",
                extra=tmc_req_ex.to_dict(),
            )
            raise

        except TuneReportingError as tmc_rep_ex:
            self.logger.error(
                "TMC v3 Logs Advertisers Clicks: Failed",
                extra=tmc_rep_ex.to_dict(),
            )
            raise

        except Exception as ex:
            print_traceback(ex)

            self.logger.error("TMC v3 Logs Advertisers Clicks: Failed: {}".format(get_exception_message(ex)))

            raise TuneReportingError(
                error_message=("TMC v3 Logs Advertisers Clicks: Failed: {}").format(get_exception_message(ex)),
                errors=ex
            )
    def _map_request_params(self,
                            auth_type_use,
                            start_date,
                            end_date,
                            request_params=None):
        """Build Request Paramaters

        Args:
            start_date:
            end_date:
            request_params:

        Returns:

        """
        auth_value = None
        if auth_type_use == TuneV2AuthenticationTypes.API_KEY:
            auth_value = self.api_key
        elif auth_type_use == TuneV2AuthenticationTypes.SESSION_TOKEN:
            auth_value = self.session_token

        dict_request_params = {
            auth_type_use:
            auth_value,
            "source":
            "multiverse",
            "response_timezone":
            self.timezone,
            "timestamp":
            "datehour",
            "group": ("advertiser_id,"
                      "country_id,"
                      "currency_code,"
                      "is_reengagement,"
                      "platform,"
                      "publisher_id,"
                      "publisher_sub_ad_id,"
                      "publisher_sub_adgroup_id,"
                      "publisher_sub_campaign_id,"
                      "publisher_sub_publisher_id,"
                      "publisher_sub_site_id,"
                      "purchase_validation_status,"
                      "site_id"),
            "fields": ("ad_clicks,"
                       "ad_clicks_unique,"
                       "ad_impressions,"
                       "ad_impressions_unique,"
                       "ad_network_id,"
                       "advertiser_id,"
                       "conversions,"
                       "country.code,"
                       "country.name,"
                       "currency_code,"
                       "date_hour,"
                       "events,"
                       "installs,"
                       "is_reengagement,"
                       "payouts,"
                       "publisher.name,"
                       "publisher_id,"
                       "publisher_sub_ad.ref,"
                       "publisher_sub_adgroup.ref,"
                       "publisher_sub_campaign.ref,"
                       "publisher_sub_publisher.ref,"
                       "publisher_sub_site.ref,"
                       "site.mobile_app_type,"
                       "site.package_name,"
                       "site.store_app_id,"
                       "site_id"),
            "filter":
            "({})".format(self._FILTER_NOT_DEBUG_NOR_TEST_DATA),
            "start_date":
            start_date,
            "end_date":
            end_date,
            "debug":
            0
        }

        if request_params:
            self.logger.debug(
                "TuneV2AdvertiserStatsActuals: Request",
                extra={'request_params': safe_dict(request_params)})

            if "fields" in request_params and \
                    request_params["fields"]:
                dict_request_params["fields"] = \
                    request_params["fields"]

            if "group" in request_params and \
                    request_params["group"]:
                dict_request_params["group"] = \
                    request_params["group"]

            if "timestamp" in request_params and \
                    request_params["timestamp"]:
                dict_request_params["timestamp"] = \
                    request_params["timestamp"]

            if "filter" in request_params and \
                    request_params["filter"]:
                dict_request_params["filter"] = "({} AND {})".format(
                    request_params["filter"],
                    self._FILTER_NOT_DEBUG_NOR_TEST_DATA)

            if "format" in request_params:
                dict_request_params["format"] = \
                    request_params["format"]

            if "offset" in request_params:
                dict_request_params["offset"] = \
                    int(request_params["offset"])

            if "page" in request_params:
                dict_request_params["page"] = \
                    int(request_params["page"])

            if "limit" in request_params:
                dict_request_params["limit"] = \
                    int(request_params["limit"])

            if "debug" in request_params:
                dict_request_params["debug"] = \
                    int(request_params["debug"])

            response_timezone = None
            if "timezone" in request_params:
                response_timezone = request_params["timezone"]
            if "response_timezone" in request_params:
                response_timezone = request_params["response_timezone"]

            if response_timezone:
                if not validate_tz_name(response_timezone):
                    return TuneReportingError(
                        error_message="Invalid Timezone: {}".format(
                            response_timezone))
                self.timezone = response_timezone
                dict_request_params["response_timezone"] = \
                    self.timezone

        self.logger.debug(("TuneV2AdvertiserStatsActuals: "
                           "Timezone: {}").format(self.timezone))

        return dict_request_params