def test_get_curr_time_diff(self): start = arrow.Arrow(2017, 8, 14, 12, 0) end = arrow.Arrow(2017, 8, 14, 13, 0) self.assertEqual(ArrowUtil.get_curr_time_diff(start=start, stop=end), 60) self.assertEqual( ArrowUtil.get_curr_time_diff(start=start, stop=end, base_hour=True), 1 )
def test_is_between(self): self.assertEqual( ArrowUtil.is_between((10, 0), (20, 0), now=arrow.Arrow(2017, 8, 14, 12, 0)), True) self.assertEqual( ArrowUtil.is_between((0, 0), (24, 0), now=arrow.Arrow(2017, 8, 14, 12, 0)), True) self.assertEqual( ArrowUtil.is_between((10, 0), (24, 0), now=arrow.Arrow(2017, 8, 14, 9, 0)), False) self.assertEqual( ArrowUtil.is_between((10, 0), (20, 0), now=arrow.Arrow(2017, 8, 14, 10, 0)), True) self.assertEqual( ArrowUtil.is_between((10, 0), (20, 0), now=arrow.Arrow(2017, 8, 14, 20, 0)), True)
def total_chart(self): records = [] for i in range(-6, 1, 1): records.append(self.__get_total_score(i)) date = [-6, -5, -4, -3, -2, -1, 0] x_ticks = ArrowUtil.format_weekly_date() legend = self.column_list data = [] for l in legend: data.append(list(map(lambda x: x[l], records))) f_name = "total_weekly_report.png" title = "Total Report" Plot.make_line( date, data, f_name, legend=legend, x_ticks=x_ticks, x_label="Total Point", y_label="Days", title=title, ) self.slackbot.file_upload(f_name, title=title, comment=MsgResource.TOTAL_REPORT)
def is_holiday(self): record = self.data_handler.read_record() holiday = record.get("Holiday", None) if holiday is None: return not ArrowUtil.is_weekday() else: return holiday
def commit(self, timely="daily"): events = self.github.get_user(self.username).get_events() if isinstance(timely, int): point_start = self.__time_point(timely) point_end = self.__time_point(timely + 1) commit_count = self.__get_event_count(events, point_start, point_end) return commit_count elif timely == "daily": point_start = self.__time_point(0) point_end = self.__time_point(1) commit_count = self.__get_event_count(events, point_start, point_end) if commit_count == 0: self.slackbot.send_message( text=MsgResource.GITHUB_COMMIT_EMPTY) else: self.slackbot.send_message( text=MsgResource.GITHUB_COMMIT_EXIST( commit_count=commit_count)) elif timely == "weekly": commit_count_list = [] for i in range(-6, 1, 1): record = self.data_handler.read_record(days=i) commit_count_list.append(record.get("Github", 0)) date = [-6, -5, -4, -3, -2, -1, 0] x_ticks = ArrowUtil.format_weekly_date() y_ticks = [i for i in range(max(commit_count_list) + 1)] f_name = "github_weekly_commit.png" title = "Github Commit" Plot.make_bar( date, commit_count_list, f_name, x_ticks=x_ticks, y_ticks=y_ticks, x_label="Commit Count", title=title, ) self.slackbot.file_upload(f_name, title=title, comment=MsgResource.GITHUB_COMMIT_WEEKLY) elif timely == "ten_days": commit_count_list = [] for i in range(-9, 1, 1): point_start = self.__time_point(i) point_end = self.__time_point(i + 1) commit_count_list.append( self.__get_event_count(events, point_start, point_end)) return commit_count_list
def commit(self, timely="daily"): events = self.github.get_user(self.username).get_events() if isinstance(timely, int): point_start = self.__time_point(timely) point_end = self.__time_point(timely + 1) commit_count = self.__get_event_count(events, point_start, point_end) return commit_count elif timely == "daily": point_start = self.__time_point(0) point_end = self.__time_point(1) commit_count = self.__get_event_count(events, point_start, point_end) if commit_count == 0: self.slackbot.send_message(text=MsgResource.GITHUB_COMMIT_EMPTY) else: self.slackbot.send_message( text=MsgResource.GITHUB_COMMIT_EXIST(commit_count=commit_count) ) elif timely == "weekly": commit_count_list = [] for i in range(-6, 1, 1): record = self.data_handler.read_record(days=i) commit_count_list.append(record.get("Github", 0)) date = [-6, -5, -4, -3, -2, -1, 0] x_ticks = ArrowUtil.format_weekly_date() y_ticks = [i for i in range(max(commit_count_list) + 1)] f_name = "github_weekly_commit.png" title = "Github Commit" Plot.make_bar( date, commit_count_list, f_name, x_ticks=x_ticks, y_ticks=y_ticks, x_label="Commit Count", title=title, ) self.slackbot.file_upload( f_name, title=title, comment=MsgResource.GITHUB_COMMIT_WEEKLY ) elif timely == "ten_days": commit_count_list = [] for i in range(-9, 1, 1): point_start = self.__time_point(i) point_end = self.__time_point(i + 1) commit_count_list.append( self.__get_event_count(events, point_start, point_end) ) return commit_count_list
def test_is_between(self): self.assertEqual( ArrowUtil.is_between((10, 0), (20, 0), now=arrow.Arrow(2017, 8, 14, 12, 0)), True, ) self.assertEqual( ArrowUtil.is_between((0, 0), (24, 0), now=arrow.Arrow(2017, 8, 14, 12, 0)), True, ) self.assertEqual( ArrowUtil.is_between((10, 0), (24, 0), now=arrow.Arrow(2017, 8, 14, 9, 0)), False, ) self.assertEqual( ArrowUtil.is_between((10, 0), (20, 0), now=arrow.Arrow(2017, 8, 14, 10, 0)), True, ) self.assertEqual( ArrowUtil.is_between((10, 0), (20, 0), now=arrow.Arrow(2017, 8, 14, 20, 0)), True, )
def test_is_today_day_of_week(self): today_day_of_week = arrow.now().weekday() + 1 self.assertEqual(ArrowUtil.is_today_day_of_week([0]), True) self.assertEqual(ArrowUtil.is_today_day_of_week([today_day_of_week]), True) self.assertEqual( ArrowUtil.is_today_day_of_week([today_day_of_week, 4, 5]), True ) self.assertEqual(ArrowUtil.is_today_day_of_week([10]), False) if ArrowUtil.is_weekday(): self.assertEqual(ArrowUtil.is_today_day_of_week([8]), True) else: self.assertEqual(ArrowUtil.is_today_day_of_week([9]), True)
def edit_attention(self, category, data, days=0): record = self.read_record(days=days) activity_data = record.get("activity", {}) assert category in activity_data latest_data = activity_data[category][-1] task_end_time = arrow.get(latest_data["end_time"]) current_time = data["time"] if ArrowUtil.get_curr_time_diff(task_end_time, current_time) < 40: latest_data["score"] = data["score"] else: pass self.edit_record(("activity", activity_data), days=days)
def total_score(self): today_data = self.__get_total_score() self.data_handler.edit_summary(today_data) color = MsgResource.SCORE_COLOR(today_data["total"]) today_data["Color"] = color yesterday_data = self.__get_total_score(-1) for k, v in today_data.items(): if isinstance(v, float): y_point = yesterday_data.get(k, False) if not y_point: continue else: diff = v - float(y_point) diff = round(diff * 100) / 100 if diff > 0: diff = "+" + str(diff) else: diff = str(diff) today_data[k] = str(v) + " (" + diff + ")" elif isinstance(v, bool): if v: today_data[k] = "O" else: today_data[k] = "X" record = self.data_handler.read_record() activity = record.get("activity", {}) # Sleep Time go_to_bed = activity.get("go_to_bed", None) wake_up = activity.get("wake_up", None) if go_to_bed is not None and wake_up is not None: go_to_bed_time = arrow.get(go_to_bed) wake_up_time = arrow.get(wake_up) sleep_hour = ArrowUtil.get_curr_time_diff(start=go_to_bed_time, stop=wake_up_time, base_hour=True) today_data["Sleep"] = (go_to_bed_time.format("HH:mm") + " ~ " + wake_up_time.format("HH:mm") + " : " + str(sleep_hour) + "h (" + str(today_data["Sleep"]) + ")") # Working Hour in_company = activity.get("in_company", None) out_company = activity.get("out_company", None) if in_company is not None and out_company is not None: in_company_time = arrow.get(in_company) out_company_time = arrow.get(out_company) working_hour = ArrowUtil.get_curr_time_diff(start=in_company_time, stop=out_company_time, base_hour=True) today_data["Working Hour"] = (in_company_time.format("HH:mm") + " ~ " + out_company_time.format("HH:mm") + " : " + str(working_hour) + "h") attachments = MsgTemplate.make_summary_template(today_data) self.slackbot.send_message(attachments=attachments)
def total_score(self): today_data = self.__get_total_score() self.data_handler.edit_summary(today_data) color = MsgResource.SCORE_COLOR(today_data["total"]) today_data["Color"] = color yesterday_data = self.__get_total_score(-1) for k, v in today_data.items(): if isinstance(v, float): y_point = yesterday_data.get(k, False) if not y_point: continue else: diff = v - float(y_point) diff = round(diff * 100) / 100 if diff > 0: diff = "+" + str(diff) else: diff = str(diff) today_data[k] = str(v) + " (" + diff + ")" elif isinstance(v, bool): if v: today_data[k] = "O" else: today_data[k] = "X" record = self.data_handler.read_record() activity = record.get("activity", {}) # Sleep Time go_to_bed = activity.get("go_to_bed", None) wake_up = activity.get("wake_up", None) if go_to_bed is not None and wake_up is not None: go_to_bed_time = arrow.get(go_to_bed) wake_up_time = arrow.get(wake_up) sleep_hour = ArrowUtil.get_curr_time_diff( start=go_to_bed_time, stop=wake_up_time, base_hour=True ) today_data["Sleep"] = ( go_to_bed_time.format("HH:mm") + " ~ " + wake_up_time.format("HH:mm") + " : " + str(sleep_hour) + "h (" + str(today_data["Sleep"]) + ")" ) # Working Hour in_company = activity.get("in_company", None) out_company = activity.get("out_company", None) if in_company is not None and out_company is not None: in_company_time = arrow.get(in_company) out_company_time = arrow.get(out_company) working_hour = ArrowUtil.get_curr_time_diff( start=in_company_time, stop=out_company_time, base_hour=True ) today_data["Working Hour"] = ( in_company_time.format("HH:mm") + " ~ " + out_company_time.format("HH:mm") + " : " + str(working_hour) + "h" ) attachments = MsgTemplate.make_summary_template(today_data) self.slackbot.send_message(attachments=attachments)