data prepared, task setup

This commit is contained in:
Connor Turland 2017-03-05 11:50:56 -05:00
parent 529dec09a3
commit 495e8e47e7
11 changed files with 441 additions and 9 deletions

View file

@ -3,10 +3,6 @@ class ApplicationMailer < ActionMailer::Base
default from: 'team@metamaps.cc'
layout 'mailer'
def deliver
raise NotImplementedError('Please use Mailboxer to send your emails.')
end
class << self
def mail_for_notification(notification)
case notification.notification_code

View file

@ -0,0 +1,10 @@
# frozen_string_literal: true
class MapActivityMailer < ApplicationMailer
default from: 'team@metamaps.cc'
def daily_summary(user, summary_data)
@user = user
@summary_data = summary_data
mail(to: user.email, subject: 'some subject line')
end
end

View file

@ -41,7 +41,8 @@ class Mapping < ApplicationRecord
topic2: mappable.topic2.filtered,
mapping_id: id
)
Events::SynapseAddedToMap.publish!(mappable, map, user, nil)
meta = { 'mapping_id': id }
Events::SynapseAddedToMap.publish!(mappable, map, user, meta)
end
end

View file

@ -0,0 +1,90 @@
class MapActivityService
def self.summarize_data(map, user, till = DateTime.now)
results = {
stats: {}
}
since = till - 24.hours
message_count = Message.where(resource: map)
.where("created_at > ? AND created_at < ?", since, till)
.where.not(user: user).count
if message_count > 0
results[:stats][:messages_sent] = message_count
end
moved_count = Event.where(kind: 'topic_moved_on_map', map: map)
.where("created_at > ? AND created_at < ?", since, till)
.where.not(user: user).group(:eventable_id).count
if moved_count.keys.length > 0
results[:stats][:topics_moved] = moved_count.keys.length
end
topics_added_events = Event.where(kind: 'topic_added_to_map', map: map)
.where("created_at > ? AND created_at < ?", since, till)
.where.not(user: user)
.order(:created_at)
topics_removed_events = Event.where(kind: 'topic_removed_from_map', map: map)
.where("created_at > ? AND created_at < ?", since, till)
.where.not(user: user)
.order(:created_at)
topics_added_to_include = {}
topics_added_events.each do |ta|
num_adds = topics_added_events.where(eventable_id: ta.eventable_id).count
num_removes = topics_removed_events.where(eventable_id: ta.eventable_id).count
topics_added_to_include[ta.eventable_id] = ta if num_adds > num_removes
end
if topics_added_to_include.keys.length > 0
results[:stats][:topics_added] = topics_added_to_include.keys.length
results[:topics_added] = topics_added_to_include.values
end
topics_removed_to_include = {}
topics_removed_events.each do |ta|
num_adds = topics_added_events.where(eventable_id: ta.eventable_id).count
num_removes = topics_removed_events.where(eventable_id: ta.eventable_id).count
topics_removed_to_include[ta.eventable_id] = ta if num_removes > num_adds
end
if topics_removed_to_include.keys.length > 0
results[:stats][:topics_removed] = topics_removed_to_include.keys.length
results[:topics_removed] = topics_removed_to_include.values
end
synapses_added_events = Event.where(kind: 'synapse_added_to_map', map: map)
.where("created_at > ? AND created_at < ?", since, till)
.where.not(user: user)
.order(:created_at)
synapses_removed_events = Event.where(kind: 'synapse_removed_from_map', map: map)
.where("created_at > ? AND created_at < ?", since, till)
.where.not(user: user)
.order(:created_at)
synapses_added_to_include = {}
synapses_added_events.each do |ta|
num_adds = synapses_added_events.where(eventable_id: ta.eventable_id).count
num_removes = synapses_removed_events.where(eventable_id: ta.eventable_id).count
synapses_added_to_include[ta.eventable_id] = ta if num_adds > num_removes
end
if synapses_added_to_include.keys.length > 0
results[:stats][:synapses_added] = synapses_added_to_include.keys.length
results[:synapses_added] = synapses_added_to_include.values
end
synapses_removed_to_include = {}
synapses_removed_events.each do |ta|
num_adds = synapses_added_events.where(eventable_id: ta.eventable_id).count
num_removes = synapses_removed_events.where(eventable_id: ta.eventable_id).count
synapses_removed_to_include[ta.eventable_id] = ta if num_removes > num_adds
end
if synapses_removed_to_include.keys.length > 0
results[:stats][:synapses_removed] = synapses_removed_to_include.keys.length
results[:synapses_removed] = synapses_removed_to_include.values
end
results
end
end

View file

@ -0,0 +1,7 @@
<!DOCTYPE html>
<div style="padding: 16px; background: white; text-align: left;">
<hr>
<p style="font-size: 12px;">Make sense with Metamaps</p>
<%= render partial: 'shared/mailer_unsubscribe_link' %>
</div>

View file

@ -15,8 +15,6 @@ MAP_ACCESS_REQUEST = 'ACCESS_REQUEST'
MAP_INVITE_TO_EDIT = 'INVITE_TO_EDIT'
# these ones are new
# this one's a catch all for occurences on the map
# MAP_ACTIVITY = 'MAP_ACTIVITY'
# MAP_RECEIVED_TOPIC
# MAP_LOST_TOPIC
# MAP_TOPIC_MOVED

20
lib/tasks/emails.rake Normal file
View file

@ -0,0 +1,20 @@
namespace :metamaps do
desc "delivers recent map activity digest emails to users"
task deliver_map_activity_emails: :environment do
summarize_map_activity
end
def summarize_map_activity
Map.all.find_each do |map|
map.followers.each do |user|
# add logging and rescue-ing
# and a notification of failure
next unless MapPolicy.new(user, map).show? # just in case the permission changed
next unless user.emails_allowed
summary_data = MapActivityService.summarize_data(map, user)
next if summary_data[:stats].blank?
MapActivityMailer.daily_summary(user, summary_data).deliver_later
end
end
end
end

View file

@ -0,0 +1,8 @@
# frozen_string_literal: true
FactoryGirl.define do
factory :message do
association :resource, factory: :map
user
sequence(:message) { |n| "Cool Message ##{n}" }
end
end

View file

@ -0,0 +1,6 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe MapActivityMailer, type: :mailer do
end

View file

@ -0,0 +1,10 @@
# frozen_string_literal: true
# Preview all emails at http://localhost:3000/rails/mailers/map_activity_mailer
class MapActivityMailerPreview < ActionMailer::Preview
def daily_summary
user = User.first
map = Map.first
summary_data = MapActivityService.summarize_data(map, user)
MapActivityMailer.daily_summary(user, summary_data)
end
end

View file

@ -0,0 +1,286 @@
require 'rails_helper'
RSpec.describe MapActivityService do
let(:map) { create(:map, created_at: 1.week.ago) }
let(:other_user) { create(:user) }
let(:email_user) { create(:user) }
it 'includes nothing if nothing happened' do
response = MapActivityService.summarize_data(map, email_user)
expect(response[:stats]).to eq({})
end
describe 'topics added to map' do
it 'includes a topic added within the last 24 hours' do
topic = create(:topic)
mapping = create(:mapping, user: other_user, map: map, mappable: topic, created_at: 6.hours.ago)
event = Event.find_by(kind: 'topic_added_to_map', eventable_id: topic.id)
event.update_columns(created_at: 6.hours.ago)
response = MapActivityService.summarize_data(map, email_user)
expect(response[:stats][:topics_added]).to eq(1)
expect(response[:topics_added]).to eq([event])
end
it 'includes a topic added, then removed, then re-added within the last 24 hours' do
topic = create(:topic)
mapping = create(:mapping, user: other_user, map: map, mappable: topic, created_at: 6.hours.ago)
Event.find_by(kind: 'topic_added_to_map', eventable_id: topic.id).update_columns(created_at: 6.hours.ago)
mapping.updated_by = other_user
mapping.destroy
Event.find_by(kind: 'topic_removed_from_map', eventable_id: topic.id).update_columns(created_at: 5.hours.ago)
mapping2 = create(:mapping, user: other_user, map: map, mappable: topic, created_at: 4.hours.ago)
event = Event.where("meta->>'mapping_id' = ?", mapping2.id.to_s).first
event.update_columns(created_at: 4.hours.ago)
response = MapActivityService.summarize_data(map, email_user)
expect(response[:stats][:topics_added]).to eq(1)
expect(response[:topics_added]).to eq([event])
end
it 'excludes a topic removed then re-added within the last 24 hours' do
topic = create(:topic)
mapping = create(:mapping, user: other_user, map: map, mappable: topic, created_at: 25.hours.ago)
Event.find_by(kind: 'topic_added_to_map', eventable_id: topic.id).update_columns(created_at: 25.hours.ago)
mapping.updated_by = other_user
mapping.destroy
Event.find_by(kind: 'topic_removed_from_map', eventable_id: topic.id).update_columns(created_at: 6.hours.ago)
mapping2 = create(:mapping, user: other_user, map: map, mappable: topic, created_at: 5.hours.ago)
Event.where(kind: 'topic_added_to_map').where("meta->>'mapping_id' = ?", mapping2.id.to_s).first.update_columns(created_at: 5.hours.ago)
response = MapActivityService.summarize_data(map, email_user)
expect(response[:stats]).to eq ({})
end
it 'excludes a topic added outside the last 24 hours' do
topic = create(:topic)
mapping = create(:mapping, user: other_user, map: map, mappable: topic, created_at: 25.hours.ago)
Event.find_by(kind: 'topic_added_to_map', eventable_id: topic.id).update_columns(created_at: 25.hours.ago)
response = MapActivityService.summarize_data(map, email_user)
expect(response[:stats]).to eq ({})
end
it 'excludes topics added by the user who will receive the data' do
topic = create(:topic)
topic2 = create(:topic)
mapping = create(:mapping, user: other_user, map: map, mappable: topic, created_at: 5.hours.ago)
event = Event.find_by(kind: 'topic_added_to_map', eventable_id: topic.id)
event.update_columns(created_at: 5.hours.ago)
mapping2 = create(:mapping, user: email_user, map: map, mappable: topic2, created_at: 5.hours.ago)
Event.find_by(kind: 'topic_added_to_map', eventable_id: topic2.id).update_columns(created_at: 5.hours.ago)
response = MapActivityService.summarize_data(map, email_user)
expect(response[:stats][:topics_added]).to eq(1)
expect(response[:topics_added]).to eq([event])
end
end
describe 'topics moved on map' do
it 'includes ones moved within the last 24 hours' do
topic = create(:topic)
event = Events::TopicMovedOnMap.publish!(topic, map, other_user, {})
event.update(created_at: 6.hours.ago)
response = MapActivityService.summarize_data(map, email_user)
expect(response[:stats][:topics_moved]).to eq(1)
end
it 'only includes each topic that was moved in the count once' do
topic = create(:topic)
topic2 = create(:topic)
event = Events::TopicMovedOnMap.publish!(topic, map, other_user, {})
event.update(created_at: 6.hours.ago)
event2 = Events::TopicMovedOnMap.publish!(topic, map, other_user, {})
event2.update(created_at: 5.hours.ago)
event3 = Events::TopicMovedOnMap.publish!(topic2, map, other_user, {})
event3.update(created_at: 5.hours.ago)
response = MapActivityService.summarize_data(map, email_user)
expect(response[:stats][:topics_moved]).to eq(2)
end
it 'excludes ones moved outside the last 24 hours' do
topic = create(:topic)
event = Events::TopicMovedOnMap.publish!(topic, map, other_user, {})
event.update(created_at: 25.hours.ago)
response = MapActivityService.summarize_data(map, email_user)
expect(response[:stats]).to eq ({})
end
it 'excludes ones moved by the user who will receive the data' do
topic = create(:topic)
event = Events::TopicMovedOnMap.publish!(topic, map, email_user, {})
event.update(created_at: 5.hours.ago)
response = MapActivityService.summarize_data(map, email_user)
expect(response[:stats]).to eq ({})
end
end
describe 'topics removed from map' do
it 'includes a topic removed within the last 24 hours' do
topic = create(:topic)
mapping = create(:mapping, user: other_user, map: map, mappable: topic, created_at: 25.hours.ago)
Event.find_by(kind: 'topic_added_to_map', eventable_id: topic.id).update_columns(created_at: 25.hours.ago)
mapping.updated_by = other_user
mapping.destroy
event = Event.find_by(kind: 'topic_removed_from_map', eventable_id: topic.id)
event.update_columns(created_at: 6.hours.ago)
response = MapActivityService.summarize_data(map, email_user)
expect(response[:stats][:topics_removed]).to eq(1)
expect(response[:topics_removed]).to eq([event])
end
it 'excludes a topic removed outside the last 24 hours' do
topic = create(:topic)
mapping = create(:mapping, user: other_user, map: map, mappable: topic, created_at: 26.hours.ago)
Event.find_by(kind: 'topic_added_to_map', eventable_id: topic.id).update_columns(created_at: 26.hours.ago)
mapping.updated_by = other_user
mapping.destroy
Event.find_by(kind: 'topic_removed_from_map', eventable_id: topic.id).update_columns(created_at: 25.hours.ago)
response = MapActivityService.summarize_data(map, email_user)
expect(response[:stats]).to eq ({})
end
it 'excludes topics removed by the user who will receive the data' do
topic = create(:topic)
topic2 = create(:topic)
mapping = create(:mapping, user: other_user, map: map, mappable: topic, created_at: 25.hours.ago)
Event.find_by(kind: 'topic_added_to_map', eventable_id: topic.id).update_columns(created_at: 25.hours.ago)
mapping2 = create(:mapping, user: email_user, map: map, mappable: topic2, created_at: 25.hours.ago)
Event.find_by(kind: 'topic_added_to_map', eventable_id: topic2.id).update_columns(created_at: 25.hours.ago)
mapping.updated_by = other_user
mapping.destroy
mapping2.updated_by = email_user
mapping2.destroy
event = Event.find_by(kind: 'topic_removed_from_map', eventable_id: topic.id)
event.update_columns(created_at: 5.hours.ago)
Event.find_by(kind: 'topic_removed_from_map', eventable_id: topic2.id).update_columns(created_at: 5.hours.ago)
response = MapActivityService.summarize_data(map, email_user)
expect(response[:stats][:topics_removed]).to eq(1)
expect(response[:topics_removed]).to eq([event])
end
end
describe 'synapses added to map' do
it 'includes a synapse added within the last 24 hours' do
synapse = create(:synapse)
mapping = create(:mapping, user: other_user, map: map, mappable: synapse, created_at: 6.hours.ago)
event = Event.find_by(kind: 'synapse_added_to_map', eventable_id: synapse.id)
event.update_columns(created_at: 6.hours.ago)
response = MapActivityService.summarize_data(map, email_user)
expect(response[:stats][:synapses_added]).to eq(1)
expect(response[:synapses_added]).to eq([event])
end
it 'includes a synapse added, then removed, then re-added within the last 24 hours' do
synapse = create(:synapse)
mapping = create(:mapping, user: other_user, map: map, mappable: synapse, created_at: 6.hours.ago)
Event.find_by(kind: 'synapse_added_to_map', eventable_id: synapse.id).update_columns(created_at: 6.hours.ago)
mapping.updated_by = other_user
mapping.destroy
Event.find_by(kind: 'synapse_removed_from_map', eventable_id: synapse.id).update_columns(created_at: 5.hours.ago)
mapping2 = create(:mapping, user: other_user, map: map, mappable: synapse, created_at: 4.hours.ago)
event = Event.where(kind: 'synapse_added_to_map').where("meta->>'mapping_id' = ?", mapping2.id.to_s).first
event.update_columns(created_at: 4.hours.ago)
response = MapActivityService.summarize_data(map, email_user)
expect(response[:stats][:synapses_added]).to eq(1)
expect(response[:synapses_added]).to eq([event])
end
it 'excludes a synapse removed then re-added within the last 24 hours' do
synapse = create(:synapse)
mapping = create(:mapping, user: other_user, map: map, mappable: synapse, created_at: 25.hours.ago)
Event.find_by(kind: 'synapse_added_to_map', eventable_id: synapse.id).update_columns(created_at: 25.hours.ago)
mapping.updated_by = other_user
mapping.destroy
Event.find_by(kind: 'synapse_removed_from_map', eventable_id: synapse.id).update_columns(created_at: 6.hours.ago)
mapping2 = create(:mapping, user: other_user, map: map, mappable: synapse, created_at: 5.hours.ago)
Event.where(kind: 'synapse_added_to_map').where("meta->>'mapping_id' = ?", mapping2.id.to_s).first.update_columns(created_at: 5.hours.ago)
response = MapActivityService.summarize_data(map, email_user)
expect(response[:stats]).to eq ({})
end
it 'excludes a synapse added outside the last 24 hours' do
synapse = create(:synapse)
mapping = create(:mapping, user: other_user, map: map, mappable: synapse, created_at: 25.hours.ago)
Event.find_by(kind: 'synapse_added_to_map', eventable_id: synapse.id).update_columns(created_at: 25.hours.ago)
response = MapActivityService.summarize_data(map, email_user)
expect(response[:stats]).to eq ({})
end
it 'excludes synapses added by the user who will receive the data' do
synapse = create(:synapse)
synapse2 = create(:synapse)
mapping = create(:mapping, user: other_user, map: map, mappable: synapse, created_at: 5.hours.ago)
event = Event.find_by(kind: 'synapse_added_to_map', eventable_id: synapse.id)
event.update_columns(created_at: 5.hours.ago)
mapping2 = create(:mapping, user: email_user, map: map, mappable: synapse2, created_at: 5.hours.ago)
Event.find_by(kind: 'synapse_added_to_map', eventable_id: synapse2.id).update_columns(created_at: 5.hours.ago)
response = MapActivityService.summarize_data(map, email_user)
expect(response[:stats][:synapses_added]).to eq(1)
expect(response[:synapses_added]).to eq([event])
end
end
describe 'synapses removed from map' do
it 'includes a synapse removed within the last 24 hours' do
synapse = create(:synapse)
mapping = create(:mapping, user: other_user, map: map, mappable: synapse, created_at: 25.hours.ago)
Event.find_by(kind: 'synapse_added_to_map', eventable_id: synapse.id).update_columns(created_at: 25.hours.ago)
mapping.updated_by = other_user
mapping.destroy
event = Event.find_by(kind: 'synapse_removed_from_map', eventable_id: synapse.id)
event.update_columns(created_at: 6.hours.ago)
response = MapActivityService.summarize_data(map, email_user)
expect(response[:stats][:synapses_removed]).to eq(1)
expect(response[:synapses_removed]).to eq([event])
end
it 'excludes a synapse removed outside the last 24 hours' do
synapse = create(:synapse)
mapping = create(:mapping, user: other_user, map: map, mappable: synapse, created_at: 25.hours.ago)
Event.find_by(kind: 'synapse_added_to_map', eventable_id: synapse.id).update_columns(created_at: 25.hours.ago)
mapping.updated_by = other_user
mapping.destroy
Event.find_by(kind: 'synapse_removed_from_map', eventable_id: synapse.id).update_columns(created_at: 25.hours.ago)
response = MapActivityService.summarize_data(map, email_user)
expect(response[:stats]).to eq ({})
end
it 'excludes synapses removed by the user who will receive the data' do
synapse = create(:synapse)
synapse2 = create(:synapse)
mapping = create(:mapping, user: other_user, map: map, mappable: synapse, created_at: 25.hours.ago)
Event.find_by(kind: 'synapse_added_to_map', eventable_id: synapse.id).update_columns(created_at: 25.hours.ago)
mapping2 = create(:mapping, user: email_user, map: map, mappable: synapse2, created_at: 25.hours.ago)
Event.find_by(kind: 'synapse_added_to_map', eventable_id: synapse2.id).update_columns(created_at: 25.hours.ago)
mapping.updated_by = other_user
mapping.destroy
mapping2.updated_by = email_user
mapping2.destroy
event = Event.find_by(kind: 'synapse_removed_from_map', eventable_id: synapse.id)
event.update_columns(created_at: 5.hours.ago)
Event.find_by(kind: 'synapse_removed_from_map', eventable_id: synapse2.id).update_columns(created_at: 5.hours.ago)
response = MapActivityService.summarize_data(map, email_user)
expect(response[:stats][:synapses_removed]).to eq(1)
expect(response[:synapses_removed]).to eq([event])
end
end
describe 'messages in the map chat' do
it 'counts messages within the last 24 hours' do
create(:message, resource: map, created_at: 6.hours.ago)
create(:message, resource: map, created_at: 5.hours.ago)
response = MapActivityService.summarize_data(map, email_user)
expect(response[:stats][:messages_sent]).to eq(2)
end
it 'does not count messages outside the last 24 hours' do
create(:message, resource: map, created_at: 25.hours.ago)
create(:message, resource: map, created_at: 5.hours.ago)
response = MapActivityService.summarize_data(map, email_user)
expect(response[:stats][:messages_sent]).to eq(1)
end
it 'does not count messages sent by the person who will receive the data' do
create(:message, resource: map, created_at: 5.hours.ago, user: other_user)
create(:message, resource: map, created_at: 5.hours.ago, user: email_user)
response = MapActivityService.summarize_data(map, email_user)
expect(response[:stats][:messages_sent]).to eq(1)
end
end
end