|
| 1 | +use primitives::sentry::{AggregateEvents, Event, EventAggregate}; |
| 2 | +use primitives::Channel; |
| 3 | + |
| 4 | +// @TODO: Remove attribute once we use this function! |
| 5 | +#[allow(dead_code)] |
| 6 | +pub(crate) fn reduce(channel: &Channel, initial_aggr: &mut EventAggregate, ev: &Event) { |
| 7 | + match ev { |
| 8 | + Event::Impression { publisher, .. } => { |
| 9 | + let impression = initial_aggr.events.get("IMPRESSION"); |
| 10 | + |
| 11 | + let merge = merge_impression_ev(impression, &publisher, &channel); |
| 12 | + |
| 13 | + initial_aggr.events.insert("IMPRESSION".to_owned(), merge); |
| 14 | + } |
| 15 | + Event::Close => { |
| 16 | + let creator = channel.creator.clone(); |
| 17 | + let close_event = AggregateEvents { |
| 18 | + event_counts: Some(vec![(creator.clone(), 1.into())].into_iter().collect()), |
| 19 | + event_payouts: vec![(creator, channel.deposit_amount.clone())] |
| 20 | + .into_iter() |
| 21 | + .collect(), |
| 22 | + }; |
| 23 | + initial_aggr.events.insert("CLOSE".to_owned(), close_event); |
| 24 | + } |
| 25 | + _ => {} |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +fn merge_impression_ev( |
| 30 | + impression: Option<&AggregateEvents>, |
| 31 | + earner: &str, |
| 32 | + channel: &Channel, |
| 33 | +) -> AggregateEvents { |
| 34 | + let mut impression = impression.map(Clone::clone).unwrap_or_default(); |
| 35 | + |
| 36 | + let event_count = impression |
| 37 | + .event_counts |
| 38 | + .get_or_insert_with(Default::default) |
| 39 | + .entry(earner.into()) |
| 40 | + .or_insert_with(|| 0.into()); |
| 41 | + |
| 42 | + *event_count += &1.into(); |
| 43 | + |
| 44 | + let event_payouts = impression |
| 45 | + .event_payouts |
| 46 | + .entry(earner.into()) |
| 47 | + .or_insert_with(|| 0.into()); |
| 48 | + *event_payouts += &channel.spec.min_per_impression; |
| 49 | + |
| 50 | + impression |
| 51 | +} |
| 52 | + |
| 53 | +#[cfg(test)] |
| 54 | +mod test { |
| 55 | + use super::*; |
| 56 | + use chrono::Utc; |
| 57 | + use primitives::util::tests::prep_db::DUMMY_CHANNEL; |
| 58 | + use primitives::BigNum; |
| 59 | + |
| 60 | + #[test] |
| 61 | + fn test_reduce() { |
| 62 | + let mut channel: Channel = DUMMY_CHANNEL.clone(); |
| 63 | + channel.deposit_amount = 100.into(); |
| 64 | + // make immutable again |
| 65 | + let channel = channel; |
| 66 | + |
| 67 | + let mut event_aggr = EventAggregate { |
| 68 | + channel_id: channel.id.clone(), |
| 69 | + created: Utc::now(), |
| 70 | + events: Default::default(), |
| 71 | + }; |
| 72 | + |
| 73 | + let event = Event::Impression { |
| 74 | + publisher: "myAwesomePublisher".to_string(), |
| 75 | + ad_unit: None, |
| 76 | + }; |
| 77 | + |
| 78 | + for _ in 0..101 { |
| 79 | + reduce(&channel, &mut event_aggr, &event); |
| 80 | + } |
| 81 | + |
| 82 | + assert_eq!(event_aggr.channel_id, channel.id); |
| 83 | + |
| 84 | + let impression_event = event_aggr |
| 85 | + .events |
| 86 | + .get("IMPRESSION") |
| 87 | + .expect("Should have an Impression event"); |
| 88 | + |
| 89 | + let event_counts = impression_event |
| 90 | + .event_counts |
| 91 | + .as_ref() |
| 92 | + .expect("there should be event_counts set") |
| 93 | + .get("myAwesomePublisher") |
| 94 | + .expect("There should be myAwesomePublisher event_counts key"); |
| 95 | + assert_eq!(event_counts, &BigNum::from(101)); |
| 96 | + |
| 97 | + let event_payouts = impression_event |
| 98 | + .event_counts |
| 99 | + .as_ref() |
| 100 | + .expect("there should be event_counts set") |
| 101 | + .get("myAwesomePublisher") |
| 102 | + .expect("There should be myAwesomePublisher event_payouts key"); |
| 103 | + assert_eq!(event_payouts, &BigNum::from(101)); |
| 104 | + } |
| 105 | +} |
0 commit comments