Commits

Andy Mikhailenko committed 468a4b5

web: simplified the sleep-related stuff on dashboard; the code is dirty because it's likely to change (or, if not, it's not worth the time at all).

Comments (0)

Files changed (3)

 import datetime
 
 
-def format_delta(delta):
-    return unicode(delta).partition('.')[0]
+def format_delta(delta, fmt='{hours}:{minutes}'):
+    """ Formats timedelta. Allowed variable names are: `days`, `hours`,
+    `minutes`, `seconds`.
+    """
+    hours, rem = divmod(delta.seconds, 3600)
+    minutes, seconds = divmod(rem, 60)
+    return fmt.format(days=delta.days, hours=hours, minutes=minutes,
+                      seconds=seconds)
 
 
 def split_time(string):

timetra/web/__init__.py

 from timetra import storage
 from timetra.curses import CATEGORY_COLOURS
 from timetra.reporting import drift, prediction
+from timetra.utils import format_delta
 
 
 blueprint = Blueprint('timetra', __name__)
     return default_appraisal
 
 
+def approx_time(dt):
+    hour = dt.hour
+    minute = round((dt.minute / 100.),1)*100
+    if minute == 60:
+        hour += 1
+        minute = 0
+    return u'{hour}:{minute:0>2}'.format(hour=hour, minute=minute)
+
 def get_stats(facts):
     if not facts:
         return []
     next_sleep = prediction.predict_next_occurence('sleeping')
     return render_template('dashboard.html', facts=facts, stats=stats,
                            appraise_category=appraise_category,
-                           sleep_drift=sleep_drift, next_sleep=next_sleep)
+                           sleep_drift=sleep_drift, next_sleep=next_sleep,
+                           format_delta=format_delta, approx_time=approx_time)
 
 
 @blueprint.route('reports/drift/')

timetra/web/templates/dashboard.html

 {% block content %}
 
     <div class="row">
-        <div class="span5">
-            <h2>Sleeping drift</h2>
-            {% for date in sleep_drift|sort %}
-                <p title="{{ date }}">
-                    <code>
-                        {% for hour in sleep_drift[date] %}
-                            {{ hour }}
-                        {% endfor %}
-                    </code>
-                </p>
+        <div class="well">
+            <p>
+                <i class="icon-bell"></i>
+                {% if next_sleep.eta_is_negative %}
+                    Я думал, ты уже спишь ≈{{ format_delta(next_sleep.eta) }}, чтобы проснуться около {{ next_sleep.end.strftime('%H:%M') }}. Уверен, что не пора?
+                {% else %}
+                    Если
+                    <strong>
+                        {% if next_sleep.eta.total_seconds() > 60*60 %}
+                            через {{ format_delta(next_sleep.eta, '{hours}') }} ч.
+                        {% else %}
+                            в течение часа
+                        {% endif %}
+                    </strong>
+                    уснешь, проснешься около {{ approx_time(next_sleep.end) }} после {{ format_delta(next_sleep.duration, '{hours}') }}-часового сна.
+                {% endif %}
+                (См. <a href="{{ url_for('timetra.report_drift') }}">режим сна</a> и <a href="{{ url_for('timetra.report_predictions') }}">список предсказаний</a>)
+            </p>
+        </div>
+        <h2>Categories</h2>
+        <dl class="dl-horizontal">
+            {% for item in stats|reverse %}
+                <dt>{{ item.category }}</dt>
+                <dd>
+                    <div class="progress progress-{{ appraise_category(item.category) }}">
+                        <div class="bar" style="width: {{ item.percentage }}%;">
+                            {{ item.duration }}
+                        </div>
+                    </div>
+                </dd>
             {% endfor %}
-
-            <p><a href="{{ url_for('timetra.report_drift') }}">See report</a></p>
-
-            <h2>Next sleep</h2>
-            <dl class="dl-horizontal">
-                <dt>ETA</dt>
-                <dd title="{{ next_sleep.start }}"><strong>{% if next_sleep.eta_is_negative %}-{% endif %}{{ next_sleep.eta }}</strong></dd>
-                <dt>Duration</dt>
-                <dd>{{ next_sleep.duration }}</dd>
-                <dt>Wake Up</dt>
-                <dd>{{ next_sleep.end }}</dd>
-            </dl>
-
-            <p><a href="{{ url_for('timetra.report_predictions') }}">See report</a></p>
-        </div>
-        <div class="span7">
-            <h2>Categories</h2>
-            <dl class="dl-horizontal">
-                {% for item in stats %}
-                    <dt>{{ item.category }}</dt>
-                    <dd>
-                        <div class="progress progress-{{ appraise_category(item.category) }}">
-                            <div class="bar" style="width: {{ item.percentage }}%;">
-                                {{ item.duration }}
-                            </div>
-                        </div>
-                    </dd>
-                {% endfor %}
-            </dl>
-        </div>
+        </dl>
     </div>
 
     <h2>History</h2>