Пример #1
0
def start(click, **kwargs):
    config = Config()
    db = Database()
    path_profile = get_profile_and_projects_from_path(config.root_dir())
    project = path_profile['current_project']
    # Check if timers are still open in same profile
    open_timer = db.open_timer()
    if not open_timer.empty:
        sure = click.confirm(
            f'{open_timer.to_string()}\n These timers are still open. Would you like to stop them?'
        )
        if sure:
            # Stop timer
            for index, entry in open_timer.iterrows():
                del entry['id']
                _entry = {
                    **entry, 'timestamp': datetime.now(),
                    'action': ACTION_TYPES['end']
                }
                db.insert(_entry)

    if kwargs.get('ago'):
        time = pd.Timestamp('now') - pd.Timedelta(kwargs.get('ago'))
    else:
        time = datetime.now()
    entry = {
        'timestamp':
        time,
        'profile':
        kwargs.get('profile', path_profile['profile'])
        or path_profile['profile'],
        'project':
        kwargs.get('project', project) or project,
        'path':
        getcwd(),
        'message':
        kwargs.get('message'),
        'service':
        kwargs.get('service') or config.default_service()['id'],
        'action':
        ACTION_TYPES['start']
    }
    db.insert(entry)
    click.echo(
        f"Started timer for {path_profile['profile']}/{'/'.join(path_profile['projects'])} on {time}"
    )
    log.debug('starting timer call')
    pass
Пример #2
0
class Timer:
    def __init__(self):
        self.project = Project().current_project
        self.account = Project().current_account
        self.current_path = Project().current_path
        self.config = Config()
        self.db = Database()

    def entry(self, action, *args, **kwargs):
        timestamp = pd.Timestamp.now()
        df = pd.DataFrame({
            'project': [kwargs.get('project', self.project)],
            'account': [kwargs.get('account', self.account)],
            'path': [kwargs.get('path', self.current_path)],
            'service':
            [kwargs.get('service',
                        self.config.default_service()['id'])],
            'message': [kwargs.get('message', None)],
            'timestamp': [timestamp],
            'action': [action],
        })
        return df

    def end_timer(self):
        # First end all open timer
        timers = self.db.open_timer()
        if not timers.empty:
            if len(timers.index) > 1:
                for index, timer in timers.reset_index().iterrows():
                    entry = self.entry(END_TIMER_ACTION,
                                       project=timer['project'],
                                       account=timer['account'],
                                       path=timer['path'])
                    self.db.insert(entry)
            else:
                val = timers.reset_index().iloc[0]
                self.db.insert(
                    self.entry(END_TIMER_ACTION,
                               project=val['project'],
                               account=val['account'],
                               path=val['path']))
            return timers.reset_index().to_dict('records')
        else:
            log.debug(f'No timer was started for {self.current_path}')

    def start_time(self, message, service, account, project):
        # Close all timer
        self.end_timer()

        params = {}

        if message:
            params['message'] = message
        if service:
            params['service'] = service
        if account:
            params['account'] = account
        if project:
            params['project'] = project

        entry = self.entry(START_TIMER_ACTION, **params)
        try:
            self.db.insert(entry)
        except:
            self.db.save(entry)

        return {
            'project': self.project,
            'account': self.account,
            'path': self.current_path,
            'timestamp': entry['timestamp']
        }