Skip to content

Commit

Permalink
Merge pull request #13 from Hack-The-Travel/develop
Browse files Browse the repository at this point in the history
Release 0.2.1 - fix uptime
  • Loading branch information
gurza authored Aug 26, 2018
2 parents 3e0f714 + 8440aae commit f0f002e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# general things to ignore
*.py[cod]
.pytest_cache/

conf.py

Expand Down
6 changes: 6 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
Release History
===============

0.2.1 (2018-08-26)
++++++++++++++++++
**Bugfixes**
- Fix uptime output format (command /status).


0.2.0 (2018-08-26)
++++++++++++++++++

Expand Down
7 changes: 4 additions & 3 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
import telebot
import sqlite3
import time
import datetime
from utils import uptime
from conf import TOKEN, DB_NAME, ADMIN_IDS
import logging

start_time = time.time()
bot_start_time = datetime.datetime.utcnow()
bot = telebot.TeleBot(TOKEN)


Expand Down Expand Up @@ -69,11 +71,10 @@ def command_status(message):
chat_id = message.chat.id
if chat_id in ADMIN_IDS:
chats_number = execute_sql('SELECT count(1) from chat')[0][0]
uptime = time.time() - start_time
bot.send_message(
chat_id,
'\n'.join([
'up {} days, {:02d}:{:02d}'.format(int(uptime//86400), int(uptime//3600), int((uptime//60)%60)),
uptime(bot_start_time, datetime.datetime.utcnow()),
'Number of subscriptions: {}'.format(chats_number)
])
)
Expand Down
2 changes: 2 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# -*- coding: utf-8 -*-
from .test_utils import TestUtils
22 changes: 22 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
import pytest
import datetime
from utils import uptime


now = datetime.datetime.utcnow()


class TestUtils():
@pytest.mark.parametrize(
'start, check, uptime_str', (
(now, now + datetime.timedelta(hours=0.5), 'Up 0 hours.'),
(now, now + datetime.timedelta(days=1), 'Up 1 day.'),
(now, now + datetime.timedelta(days=1, hours=1), 'Up 1 day, 1 hour.'),
(now, now + datetime.timedelta(days=1, hours=2), 'Up 1 day, 2 hours.'),
(now, now + datetime.timedelta(days=2), 'Up 2 days.'),
(now, now + datetime.timedelta(days=2, hours=1), 'Up 2 days, 1 hour.'),
(now, now + datetime.timedelta(days=2, hours=2), 'Up 2 days, 2 hours.'),
))
def test_runtime(self, start, check, uptime_str):
assert uptime(start, check) == uptime_str
17 changes: 17 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
from typing import List
import datetime


def uptime(start: datetime.datetime, check: datetime.datetime) -> str:
delta: datetime.timedelta = check - start
delta_days: int = delta.days
delta_hours: int = delta.seconds // 3600
up: List[str] = []
if delta_days > 0:
up.append('{} day{}'.format(delta_days, '' if delta_days == 1 else 's'))
if delta_hours > 0:
up.append('{} hour{}'.format(delta_hours, '' if delta_hours == 1 else 's'))
if len(up) == 0:
return 'Up 0 hours.'
return 'Up {}.'.format(', '.join(up))

0 comments on commit f0f002e

Please sign in to comment.