def setUp(self): """Set up tests.""" self.app = create_app("testing") self.app_context = self.app.app_context() self.app_context.push() db.create_all() # Initialize a DB? self.client = self.app.test_client()
def main(): """Code to query DB for a week of counts, sum them, and store result.""" # NOTE: This works as expected if script is scheduled in UTC today = datetime.date.today() weekday = today.isoweekday() if weekday != 1: # If not Monday, abandon script and exit msg = ("Day of week is {} -- not Monday. " "Weekly count script exited.").format(weekday) LOGGER.warning(msg) sys.exit() monday = today - datetime.timedelta(days=7) sunday = today - datetime.timedelta(days=1) # Put last Monday and yesterday's dates into format: 2019-01-30 monday = monday.isoformat() sunday = sunday.isoformat() # Create an app context and store the data in the database app = create_app('production') with app.app_context(): date_range = DailyTotal.day.between(monday, sunday) LOGGER.info('MONDAY: {}'.format(monday)) LOGGER.info('SUNDAY: {}'.format(sunday)) LOGGER.info('DATE_RANGE {}'.format(date_range)) week_list = DailyTotal.query.filter(date_range).all() LOGGER.info('COUNTS FOR WEEK {}'.format(week_list)) week_total = 0 if not week_list: # On a query failure, log an error msg = "Weekly count query failed for {}!".format(monday) LOGGER.warning(msg) return for day in week_list: week_total += day.count weekly_count = WeeklyTotal(monday=monday, count=week_total) db.session.add(weekly_count) try: db.session.commit() msg = ( "Successfully wrote count for {} in WeeklyTotal table." ).format(monday) LOGGER.info(msg) # Catch error and attempt to recover by resetting staged changes. except sqlalchemy.exc.SQLAlchemyError as error: db.session.rollback() msg = ("Yikes! Failed to write data for {week} in " "WeeklyTotal table: {err}").format(week=monday, err=error) LOGGER.warning(msg)
def main(): """Core program.""" # Get the milestone we need from the command line. if len(sys.argv) != 2: sys.exit('BYE: too many, too few arguments.') milestone = sys.argv[1] # Check we have the right argument. if milestone in MILESTONES: # make sure the code is a string. urlcode = str(MILESTONES[milestone][0]) status = MILESTONES[milestone][1] else: sys.exit('BYE: Not a valid argument.') # Extract data from GitHub url = urljoin(URL_REPO, urlcode) json_response = get_remote_file(url) issues_count = extract_issues_count(json_response, status) # Compute the date now = newtime(datetime.datetime.now().isoformat(timespec='seconds')) # Create an app context and store the data in the database app = create_app() with app.app_context(): iss_count = IssuesCount(timestamp=now, count=issues_count, milestone=milestone) db.session.add(iss_count) try: db.session.commit() msg = ("Successfully wrote MILESTONE {milestone} count for {now} " "to IssuesCount table.").format(milestone=milestone, now=now) LOGGER.info(msg) # Catch error and attempt to recover by resetting staged changes. except sqlalchemy.exc.SQLAlchemyError as error: db.session.rollback() msg = ("Yikes! Failed to write MILESTONE {milestone} count for " "{now} in IssuesCount table.").format(milestone=milestone, now=now) LOGGER.warning(msg)
def main(): """Core program to fetch and process data from GitHub.""" # NOTE: This works as expected if script is scheduled in UTC today = datetime.date.today() yesterday = today - datetime.timedelta(days=1) yesterday = yesterday.isoformat() # Insert yesterday's date into search query in format: 2019-01-30 query = QUERY.format(yesterday=yesterday) url = urljoin(SEARCH_URL, query) json_response = get_remote_file(url) issue_count = get_issue_count(json_response) if not issue_count: # If results are incomplete, retry after 3 min time.sleep(360) issue_count = get_issue_count(json_response) if not issue_count: # On a second failure, log an error msg = "Daily count failed for {yesterday}!".format( yesterday=yesterday ) LOGGER.warning(msg) return # Create an app context and store the data in the database app = create_app('production') with app.app_context(): total = DailyTotal(day=yesterday, count=issue_count) db.session.add(total) try: db.session.commit() msg = "Successfully wrote {day} data in DailyTotal table.".format( count=issue_count, day=yesterday) LOGGER.info(msg) # Catch error and attempt to recover by resetting staged changes. except sqlalchemy.exc.SQLAlchemyError as error: db.session.rollback() msg = ("Yikes! Failed to write data for {day} in " "DailyTotal table: {err}").format(day=yesterday, err=error) LOGGER.warning(msg)
def setUp(self): """Set up tests.""" self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() self.client = self.app.test_client()
def setUp(self): """Set up tests.""" self.app = create_app(test_config={}) self.client = self.app.test_client()