Skip to content

Commit

Permalink
Create tag database from existing interval database on startup
Browse files Browse the repository at this point in the history
- Closes #224

Signed-off-by: Thomas Lauf <thomas.lauf@tngtech.com>
  • Loading branch information
lauft committed Jun 8, 2019
1 parent eeb9bc4 commit b15cddd
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 11 deletions.
41 changes: 30 additions & 11 deletions src/Database.cpp
Expand Up @@ -375,22 +375,41 @@ void Database::initializeTagDatabase ()

if (!File::read (_location + "/tags.data", content))
{
return;
}
auto intervals = getAllInclusions (*this);

if (intervals.empty ())
{
return;
}

auto* json = (json::object*) json::parse (content);
std::cout << "Tag info database does not exist. Recreating from interval data..." << std::endl ;

for (auto& pair : json->_data)
for (auto& interval : intervals)
{
for (auto& tag : interval.tags ())
{
_tagInfoDatabase.incrementTag (tag);
}
}
}
else
{
auto key = pair.first;
auto* value = (json::object*) pair.second;
auto iter = value->_data.find ("count");
if (iter == value->_data.end ())
auto *json = (json::object *) json::parse (content);

for (auto &pair : json->_data)
{
throw format ("Failed to find \"count\" member for tag \"{1}\" in tags database. Database corrupted?", key);
auto key = pair.first;
auto *value = (json::object *) pair.second;
auto iter = value->_data.find ("count");

if (iter == value->_data.end ())
{
throw format ("Failed to find \"count\" member for tag \"{1}\" in tags database. Database corrupted?", key);
}

auto number = dynamic_cast<json::number *> (iter->second);
_tagInfoDatabase.add (key, TagInfo{(unsigned int) number->_dvalue});
}
auto number = dynamic_cast<json::number *> (iter->second);
_tagInfoDatabase.add (key, TagInfo {(unsigned int) number->_dvalue});
}
}

Expand Down
34 changes: 34 additions & 0 deletions test/cli.t
Expand Up @@ -30,6 +30,9 @@ import os
import shutil
import sys
import unittest
import json

from datetime import datetime, timedelta

# Ensure python finds the local simpletap module
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
Expand All @@ -52,6 +55,37 @@ class TestCLI(TestCase):

assert os.path.isdir(self.t.env["TIMEWARRIORDB"])
assert os.path.exists(self.t.env["TIMEWARRIORDB"])
assert os.path.exists(os.path.join(self.t.env["TIMEWARRIORDB"], "data"))
assert os.path.exists(os.path.join(self.t.env["TIMEWARRIORDB"], "data", "tags.data"))
assert not os.path.exists(os.path.join(self.t.env["TIMEWARRIORDB"], "data", "undo.data"))

def test_tag_database_is_recreated(self):
"""Verify that calling 'timew' recreates tag database"""
now_utc = datetime.now().utcnow()

four_hours_before_utc = now_utc - timedelta(hours=4)
three_hours_before_utc = now_utc - timedelta(hours=3)
two_hours_before_utc = now_utc - timedelta(hours=2)
one_hour_before_utc = now_utc - timedelta(hours=1)

self.t("track {:%Y-%m-%dT%H:%M:%S} - {:%Y-%m-%dT%H:%M:%S} FOO".format(four_hours_before_utc, three_hours_before_utc))
self.t("track {:%Y-%m-%dT%H:%M:%S} - {:%Y-%m-%dT%H:%M:%S} BAR".format(three_hours_before_utc, two_hours_before_utc))
self.t("track {:%Y-%m-%dT%H:%M:%S} - {:%Y-%m-%dT%H:%M:%S} FOO".format(two_hours_before_utc, one_hour_before_utc))

os.remove(os.path.join(self.t.env["TIMEWARRIORDB"], "data", "tags.data"))

assert not os.path.exists(os.path.join(self.t.env["TIMEWARRIORDB"], "data", "tags.data"))

self.t.runError("")

assert os.path.exists(os.path.join(self.t.env["TIMEWARRIORDB"], "data", "tags.data"))

with open(os.path.join(self.t.env["TIMEWARRIORDB"], "data", "tags.data")) as f:
data = json.load(f)
self.assertIn("FOO", data)
self.assertEqual(data["FOO"]["count"], 2)
self.assertIn("BAR", data)
self.assertEqual(data["BAR"]["count"], 1)

def test_TimeWarrior_without_command_without_active_time_tracking(self):
"""Call 'timew' without active time tracking"""
Expand Down

0 comments on commit b15cddd

Please sign in to comment.