def __init__(self, account_id): self.account_id = account_id self.local_dates = LocalDates(account_id) self.budget_commander = BudgetCommander(account_id) self.budget = self.budget_commander.budget self.cost = self.budget_commander.this_month_spend self.main()
def dateRangeFromDays(date_range, account_id): if date_range.find(',') > 0: return date_range #if it has a comma it's already been formatted if date_range.lower() == "today": return datetime.strftime(LocalDates(account_id).today, '%Y%m%d') + ", " + datetime.strftime( LocalDates(account_id).today, '%Y%m%d') if date_range == "THIS_MONTH": days = LocalDates(account_id).yesterday.date().day - 1 assert days != 0 #we can't get this month on the 1st else: days = int(date_range.split("_")[1]) start_date = datetime.strftime( LocalDates(account_id).today - timedelta(days), '%Y%m%d') end_date = datetime.strftime(LocalDates(account_id).yesterday, '%Y%m%d') return start_date + ", " + end_date
def accountRanToday(self, account_id): """Returns True if the account ran today""" query = 'SELECT ad_performance_report_processed_at FROM accounts where id = "%s"' % ( account_id) last_run_time = pd.read_sql_query( query, Database().createEngine( ))["ad_performance_report_processed_at"].values[0] if not last_run_time: return False return LocalDates( account_id).today.date() == datetime.utcfromtimestamp( last_run_time.tolist() / 1e9).date()
def __init__(self, account_id): self.account_id = account_id self.budget_commander = BudgetCommander(account_id) self.local_dates = LocalDates(account_id) if not self.budget_commander.user_settings['emergency_stop']: Log("info", "Emergency stop is disabled.", "", self.account_id) return self.budget = self.budget_commander.budget if self.budget_commander.this_month_spend >= self.budget: Log("info", "this month spend (%s) is over this month's budget (%s). Exiting." %(self.budget_commander.this_month_spend, self.budget), "", self.account_id) return self.costs = GetCostFromApi(account_id) self.today_cost = self.costs.today_cost self.day_budget_percentage = self.costs.day_budget_percentage self.day_limit = self.getDayLimit() self.main()
def accountShouldRun(self, account_id): """ For the nightly shedule Whether the account should run - Don't run before 3am because the data may not be ready - Run if the account hasn't ran today already """ if self.run_all_active_accounts: return True if LocalDates(account_id).today.hour < 3: return False if self.accountRanToday(account_id): return False return True
def __init__(self, account_id, budget_group_id=None): self.account_id = account_id self.local_dates = LocalDates(account_id) self.budget_group_id = budget_group_id self.budget_group_info = self.getBudgetGroupInfo() self.envvars = (Settings()).getEnv() self.createBudgetCommanderTable() self.account_info = self.getAccountInfo() self.name = self.account_info["name"] self.google_id = self.account_info["google_id"] self.currency_symbol = (Currency()).getSymbol(account_id) self.user_settings = self.getBudgetCommanderSettings() self.username = self.getUserName() self.budget = self.getBudget() self.this_month_spend = self.getThisMonthSpend() self.last_month_spend = self.getLastMonthSpend() self.under_budget = self.accountIsUnderBudget(self.budget, self.this_month_spend)
def createDfWithAllDateRanges(self, account_id): all_df = None settings = Settings() for date_range in settings.date_ranges: if date_range == "THIS_MONTH" and LocalDates( account_id).is_first_of_month: continue report = Report(account_id, date_range, self.options) report.createAccountDirectory() report.createReportDirectory() report.downloadReport(account_id, report.where_string) df = report.convertCsvToDataframe() df["date_range"] = date_range if df.shape[0] == 0: print("%s df is empty" % (date_range)) continue if functions.dfIsEmpty(all_df): all_df = df.copy() else: all_df = all_df.append(df.copy()) if functions.dfIsEmpty(all_df): Log("info", "%s report is empty" % (self.report_name), "", self.account_id) return all_df = report.stringifyIds(all_df) return all_df.reset_index()
def __init__(self, account_id): self.account_id = account_id self.local_dates = LocalDates(account_id) self.df = self.main(account_id) self.today_cost = self.getTodayCost() self.day_budget_percentage = self.getBudgetPercentage()
def addProcessedAtTimestamp(account_id): today = LocalDates(account_id).today Log('info', "storing local time: %s" % (today), '', account_id) query = "update accounts set ad_performance_report_processed_at = '%s' where id = '%s'" % ( today, account_id) Database().executeQuery(query)
def __init__(self, account_id): self.account_id = account_id self.local_dates = LocalDates(account_id) self.envvars = (Settings()).getEnv() self.budget_commander = BudgetCommander(account_id) self.user_settings = self.budget_commander.user_settings