_binary From 748851cb026b1456678aa47a692bc70e963e71db Mon Sep 17 00:00:00 2001
From: Pragya Rana <pragya.rana9@gmail.com>
Date: Fri, 12 May 2017 12:20:01 -0700
Subject: [PATCH] Add Differential Privacy in Query Statistics

* Implemented differential privacy in query statistics page.
  Shows fuzzified views if Differential Privacy is enabled.
* Fixed a minor issue from previous commit 9c505c078:
  Adding Differential Privacy in Group Statistics
---
 src/controllers/components/CrawlComponent.php  | 34 ++++++++++++++++++++++++++
 src/controllers/components/SocialComponent.php |  6 +++--
 2 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/src/controllers/components/CrawlComponent.php b/src/controllers/components/CrawlComponent.php
index 9d9b8b83..b5f9a427 100644
--- a/src/controllers/components/CrawlComponent.php
+++ b/src/controllers/components/CrawlComponent.php
@@ -128,6 +128,40 @@ class CrawlComponent extends Component implements CrawlConstants
                             $impression_model->getStatistics(C\QUERY_IMPRESSION,
                                 $period, $filter);
                     }
+                    // Add Differential Privacy for query
+                    // statistics if enabled
+                    if (C\DIFFERENTIAL_PRIVACY) {
+                        $i = 0;
+                        foreach ($periods as $period) {
+                            if (!empty($data["STATISTICS"][$period])) {
+                                foreach ($data['STATISTICS'][$period] as
+                                    $item_name =>$item_data) {
+                                    $view_stat = $impression_model->getImpressionStat(
+                                        $item_data[0]['ID'], C\QUERY_IMPRESSION, $period);
+                                    $fuzzy_views = $view_stat[1];
+                                    if ($view_stat[0] != $item_data[0]['NUM_VIEWS'] ||
+                                        $tmp_data[$item_name][$i - 1] > $fuzzy_views) {
+                                        $fuzzy_views = $parent->addDifferentialPrivacy($item_data[0]['NUM_VIEWS']);
+                                        /* Make sure each time period's
+                                        fuzzified view is at least as large as
+                                        previous time period's value */
+                                        if ($i > 0) {
+                                            if ($tmp_data[$item_name][$i - 1] > $fuzzy_views) {
+                                                $fuzzy_views = $tmp_data[$item_name][$i - 1];
+                                            }
+                                        }
+                                        $impression_model->updateImpressionStat($item_data[0]['ID'],
+                                            C\QUERY_IMPRESSION, $period, $item_data[0]['NUM_VIEWS'], $fuzzy_views);
+                                    }
+                                    $data["STATISTICS"][$period][$item_name][0]['NUM_VIEWS']=
+                                        ($fuzzy_views == 0)?
+                                        tl('managegroups_element_no_activity'):$fuzzy_views;
+                                    $tmp_data[$item_name][$i] = $fuzzy_views;
+                                }
+                                $i++;
+                            }
+                        }
+                    }
                     break;
                 case "resume":
                     $crawl_params = [];
diff --git a/src/controllers/components/SocialComponent.php b/src/controllers/components/SocialComponent.php
index bf824cff..b9d63cfe 100644
--- a/src/controllers/components/SocialComponent.php
+++ b/src/controllers/components/SocialComponent.php
@@ -705,7 +705,8 @@ class SocialComponent extends Component implements CrawlConstants
                                             $data["STATISTICS"][$field][$period][0]['ID'],
                                             $field, $period);
                                         $fuzzy_views = $view_stat[1];
-                                        if ($view_stat[0] != $data["STATISTICS"][$field][$period][0]['NUM_VIEWS']) {
+                                        if ($view_stat[0] != $data["STATISTICS"][$field][$period][0]['NUM_VIEWS'] ||
+                                            $tmp_data[$i - 1] > $fuzzy_views) {
                                             $fuzzy_views = $parent->addDifferentialPrivacy(
                                                 $data["STATISTICS"][$field][$period][0]['NUM_VIEWS']);
                                             /* Make sure each time period's
@@ -736,7 +737,8 @@ class SocialComponent extends Component implements CrawlConstants
                                             $item_data) {
                                             $view_stat = $impression_model->getImpressionStat($item_data[0]['ID'], $field, $period);
                                             $fuzzy_views = $view_stat[1];
-                                            if ($view_stat[0] != $item_data[0]['NUM_VIEWS']) {
+                                            if ($view_stat[0] != $item_data[0]['NUM_VIEWS'] ||
+                                                $tmp_data[$item_name][$i - 1] > $fuzzy_views) {
                                                 $fuzzy_views = $parent->addDifferentialPrivacy($item_data[0]['NUM_VIEWS']);
                                                 if ($i > 0) {
                                                     if ($tmp_data[$item_name][$i - 1] > $fuzzy_views) {
-- 
2.11.0 (Apple Git-81)

