# HG changeset patch # User bsw # Date 1338317540 -7200 # Node ID 42d070777353b25c7cd42a90e8204443c0f2050d # Parent 8af806af86a08b1dcc621669f96533f618e7933e Optimized fetching of calculated state_time_left attribute of issue diff -r 8af806af86a0 -r 42d070777353 model/issue.lua --- a/model/issue.lua Tue May 29 20:45:58 2012 +0200 +++ b/model/issue.lua Tue May 29 20:52:20 2012 +0200 @@ -1,6 +1,14 @@ Issue = mondelefant.new_class() Issue.table = 'issue' +local new_selector = Issue.new_selector + +function Issue:new_selector() + local selector = new_selector(self) + selector:add_field("coalesce(issue.fully_frozen + issue.voting_time, issue.half_frozen + issue.verification_time, issue.accepted + issue.discussion_time, issue.created + issue.admission_time) - now()", "state_time_left") + return selector +end + Issue:add_reference{ mode = 'm1', to = "Area", @@ -244,26 +252,6 @@ return Issue:get_state_name_for_state(self.state) end -function Issue.object_get:state_time_left() - local state = self.state - local last_event_time - local duration - if state == "new" then - last_event_time = self.created - duration = self.admission_time - elseif state == "accepted" then - last_event_time = self.accepted - duration = self.discussion_time - elseif state == "frozen" then - last_event_time = self.half_frozen - duration = self.verification_time - elseif state == "voting" then - last_event_time = self.fully_frozen - duration = self.voting_time - end - return db:query{ "SELECT ?::timestamp + ?::interval - CURRENT_TIMESTAMP(0) as time_left", last_event_time, duration }[1].time_left -end - function Issue.object_get:next_states() local state = self.state local next_states