Beispiel #1
0
    def add_fact(self, fact, start_time, end_time, temporary = False):
        fact = Fact(fact, start_time = start_time, end_time = end_time)
        start_time = fact.start_time or dt.datetime.now().replace(second = 0, microsecond = 0)

        self.start_transaction()
        result = self.__add_fact(fact.serialized_name(), start_time, end_time, temporary)
        self.end_transaction()

        if result:
            self.facts_changed()
        return result
Beispiel #2
0
    def add_fact(self, fact, start_time, end_time, temporary = False):
        start_time = self.__round_seconds(start_time)
        end_time = self.__round_seconds(end_time)
        fact = Fact(fact, start_time = start_time, end_time = end_time)

        self.start_transaction()
        result = self.__add_fact(fact.serialized_name(), fact.start_time, fact.end_time, temporary)
        self.end_transaction()

        if result:
            self.facts_changed()
        return result
Beispiel #3
0
    def add_fact(self, fact, start_time, end_time, temporary=False):
        start_time = self.__round_seconds(start_time)
        end_time = self.__round_seconds(end_time)
        fact = Fact(fact, start_time=start_time, end_time=end_time)

        self.start_transaction()
        result = self.__add_fact(fact.serialized_name(), fact.start_time,
                                 fact.end_time, temporary)
        self.end_transaction()

        if result:
            self.facts_changed()
        return result
Beispiel #4
0
    def add_fact(self, fact, start_time, end_time, temporary=False):
        fact = Fact(fact, start_time=start_time, end_time=end_time)
        start_time = fact.start_time or dt.datetime.now().replace(
            second=0, microsecond=0)

        self.start_transaction()
        result = self.__add_fact(fact.serialized_name(), start_time, end_time,
                                 temporary)
        self.end_transaction()

        if result:
            self.facts_changed()
        return result
Beispiel #5
0
    def __solve_overlaps(self, start_time, end_time):
        """finds facts that happen in given interval and shifts them to
        make room for new fact
        """
        if end_time is None or start_time is None:
            return

        # possible combinations and the OR clauses that catch them
        # (the side of the number marks if it catches the end or start time)
        #             |----------------- NEW -----------------|
        #      |--- old --- 1|   |2 --- old --- 1|   |2 --- old ---|
        # |3 -----------------------  big old   ------------------------ 3|
        query = """
                   SELECT a.*, b.name, c.name as category
                     FROM facts a
                LEFT JOIN activities b on b.id = a.activity_id
                LEFT JOIN categories c on b.category_id = c.id
                    WHERE (end_time > ? and end_time < ?)
                       OR (start_time > ? and start_time < ?)
                       OR (start_time < ? and end_time > ?)
                 ORDER BY start_time
                """
        conflicts = self.fetchall(query, (start_time, end_time,
                                          start_time, end_time,
                                          start_time, end_time))

        for fact in conflicts:
            # won't eliminate as it is better to have overlapping entries than loosing data
            if start_time < fact["start_time"] and end_time > fact["end_time"]:
                continue

            # split - truncate until beginning of new entry and create new activity for end
            if fact["start_time"] < start_time < fact["end_time"] and \
               fact["start_time"] < end_time < fact["end_time"]:

                logging.info("splitting %s" % fact["name"])
                # truncate until beginning of the new entry
                self.execute("""UPDATE facts
                                   SET end_time = ?
                                 WHERE id = ?""", (start_time, fact["id"]))
                fact_name = fact["name"]

                # create new fact for the end
                new_fact = Fact(fact["name"],
                                category = fact["category"],
                                description = fact["description"])
                new_fact_id = self.__add_fact(new_fact.serialized_name(), end_time, fact["end_time"])

                # copy tags
                tag_update = """INSERT INTO fact_tags(fact_id, tag_id)
                                     SELECT ?, tag_id
                                       FROM fact_tags
                                      WHERE fact_id = ?"""
                self.execute(tag_update, (new_fact_id, fact["id"])) #clone tags

                if trophies:
                    trophies.unlock("split")

            # overlap start
            elif start_time < fact["start_time"] < end_time:
                logging.info("Overlapping start of %s" % fact["name"])
                self.execute("UPDATE facts SET start_time=? WHERE id=?",
                             (end_time, fact["id"]))

            # overlap end
            elif start_time < fact["end_time"] < end_time:
                logging.info("Overlapping end of %s" % fact["name"])
                self.execute("UPDATE facts SET end_time=? WHERE id=?",
                             (start_time, fact["id"]))