myexperiment-hackers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[myexperiment-hackers] [1881] branches/event_logging: Work on news gener


From: noreply
Subject: [myexperiment-hackers] [1881] branches/event_logging: Work on news generation.
Date: Wed, 22 Oct 2008 13:14:10 -0400 (EDT)

Revision
1881
Author
alekses6
Date
2008-10-22 13:14:09 -0400 (Wed, 22 Oct 2008)

Log Message

Work on news generation. 

Introduced news-generation fine tune constants in the environment_private.rb.

Performance fix: the code will now process only required number of entries for the news - as opposed to the original version (that is before event logs), where it was processing all, then chopping off required number from the head of the list.

Modified Paths

Diff

Modified: branches/event_logging/app/helpers/application_helper.rb (1880 => 1881)


--- branches/event_logging/app/helpers/application_helper.rb	2008-10-22 13:49:43 UTC (rev 1880)
+++ branches/event_logging/app/helpers/application_helper.rb	2008-10-22 17:14:09 UTC (rev 1881)
@@ -584,11 +584,17 @@
   #                           when set to "false" will pick up also all related contributors;
   # 3-4) before, after - Time objects; the news will be generated in a time slice [after..before]
   # 5) limit - number of news entries to generate
-  def news(contributor, restrict_contributor=true, before=Time.now, after=Time.now-1.week, limit=30)
+  def news(contributor, restrict_contributor=true, before=Time.now, after=Time.now-DEFAULT_NEWS_TIMEFRAME, limit=DEFAULT_NEWS_COUNT)
     hash = {}
     
-    # news_array = contributor_news(contributor, before, after, 0, (restrict_contributor ? contributor : nil)).sort! { |a, b|
-    news_array = contributor_news_from_log(contributor, before, after, restrict_contributor, false)
+    # choose news generation method: 
+    # - new (faster / more news types), but requires ActivityLogs to be enabled
+    # - old (slow), but collects news items from all across the DB
+    if USE_EVENT_LOG && GENERATE_NEWS_FROM_EVENT_LOG
+      news_array = contributor_news_from_log(contributor, before, after, restrict_contributor, false, limit)
+    else
+      news_array = contributor_news(contributor, before, after, 0, (restrict_contributor ? contributor : nil))
+    end
     
     news_array.sort! { |a, b|
       b[0] <=> a[0]
@@ -1386,8 +1392,9 @@
   # 4) contributor_news_only - set to "true" to get events relevant for the current user only;
   #                                OR "false" to get events for friends and group members where contributor is a member of, too
   # 5) return_raw_events - used for recursive calls; when set to "true" a set of events from ActivityLog table is returned;
-  #                        "false" will cause the events to be interpreted and returned as news items.
-  def contributor_news_from_log(contributor, before, after, contributor_news_only, return_raw_events)
+  #                        "false" will cause the events to be interpreted and returned as news item;
+  # 6) limit - the number of news entries to produce (the lower the value, the more positive effect on performance this has)
+  def contributor_news_from_log(contributor, before, after, contributor_news_only, return_raw_events, limit)
     
     # DEBUG
     # puts "=================================="
@@ -1440,19 +1447,24 @@
     
     
     # get events for current contributor
+    # (no need to impose order on the DB query, as more queries will be run [for "related_contributors"]
+    #  and subsequent sorting will be required anyway)
     events = ActivityLog.find(:all, :conditions => ["created_at > ? AND created_at < ? AND ((activity_loggable_type = ? AND activity_loggable_id = ?) OR (culprit_type = ? AND culprit_id = ?) OR (referenced_type = ? AND referenced_id = ?))", after, before, contributor.class.to_s, contributor.id, contributor.class.to_s, contributor.id, contributor.class.to_s, contributor.id])
     
     # if any "related_contributors", get events for each
+    # (can't use the "limit" parameter just yet, because events for "related_contributors" may be newer
+    #  than some of the contributor in the original query)
     events_for_related_contributor = []
     related_contributors.each do |c|
-      events_for_related_contributor = contributor_news_from_log(c, before, after, true, true) 
+      events_for_related_contributor = contributor_news_from_log(c, before, after, true, true, limit) 
       events.concat(events_for_related_contributor) unless events_for_related_contributor.empty? 
     end
     return events if return_raw_events
     
-    # remove any duplicates and sort by date ascending
+    # remove any duplicates (which may arise when getting same event log entry from friends' related events),  
+    # then sort by date descending
     events = events.uniq.sort { |a, b|
-      a.created_at <=> b.created_at
+      b.created_at <=> a.created_at
     }
     
     # produce news from event list 
@@ -1461,6 +1473,10 @@
     events.each do |e|
       news_item = contributor_news_from_log!(e)
       rtn << news_item unless news_item.nil?
+      
+      # no need to interpret more events than is required by the method call;
+      # -> therefore, stop once the desired number is reached
+      break if (rtn.length == limit)
     end
     
     return rtn

Modified: branches/event_logging/config/environment_private.rb.pre (1880 => 1881)


--- branches/event_logging/config/environment_private.rb.pre	2008-10-22 13:49:43 UTC (rev 1880)
+++ branches/event_logging/config/environment_private.rb.pre	2008-10-22 17:14:09 UTC (rev 1881)
@@ -73,5 +73,27 @@
 # The maximum file size allowed for workflows
 WORKFLOW_UPLOAD_MAX_BYTES = 20971520
 
+
+# =========== Settings for Event Logging and News Generation ===========
+
 # Switch event logging on or off
-USE_EVENT_LOG = true
\ No newline at end of file
+USE_EVENT_LOG = true
+
+# Choose a method of news generation:
+# "true" to generate news from event log;
+# "false" to use older (slower) system that queries multiple DB tables;
+# (this will only be considered if "USE_EVENT_LOG == true")
+GENERATE_NEWS_FROM_EVENT_LOG = true
+
+# Default timeframes for various types of news
+# [this means that only events after (Time.now - DEFAULT_<>_TIMEFRAME) will be fetched from event log]
+DEFAULT_NEWS_TIMEFRAME = 1.week
+DEFAULT_HOME_PAGE_NEWS_TIMEFRAME = DEFAULT_NEWS_TIMEFRAME
+DEFAULT_USER_NEWS_TIMEFRAME = DEFAULT_NEWS_TIMEFRAME
+DEFAULT_GROUP_NEWS_TIMEFRAME = 2.weeks
+
+# Default news entry counts
+DEFAULT_NEWS_COUNT = 30
+DEFAULT_HOME_PAGE_NEWS_COUNT = DEFAULT_NEWS_COUNT
+DEFAULT_USER_NEWS_COUNT = DEFAULT_NEWS_COUNT
+DEFAULT_GROUP_NEWS_COUNT = DEFAULT_NEWS_COUNT
\ No newline at end of file

reply via email to

[Prev in Thread] Current Thread [Next in Thread]