Fix 'frequency' statistics for the trigger to emit proper results
[platform/core/context/statistics-context-provider.git] / src / social / db_handle.cpp
index ea0d7da..1062bbd 100644 (file)
@@ -39,6 +39,7 @@ int ctx::social_db_handle::read(const char* subject, ctx::json filter)
                query = create_sql_freq_address(filter);
 
        } else if (STR_EQ(subject, SOCIAL_SUBJ_FREQUENCY)) {
+               is_trigger_item = true;
                query = create_sql_frequency(filter);
        }
 
@@ -110,19 +111,34 @@ std::string ctx::social_db_handle::create_sql_frequency(ctx::json filter)
                return "";
        }
 
+       /* TODO: Enable after fixing the log aggregator
        if (filter.get(NULL, STATS_DAY_OF_WEEK, &week_str))
                filter_cleaned.set(NULL, STATS_DAY_OF_WEEK, week_str);
 
        if (filter.get(NULL, STATS_TIME_OF_DAY, &time_of_day))
                filter_cleaned.set(NULL, STATS_TIME_OF_DAY, time_of_day);
-
-       std::string where_clause = create_where_clause(filter_cleaned);
+       */
 
        std::stringstream query;
+
+       query <<
+               "DELETE FROM " SOCIAL_TEMP_CONTACT_FREQ ";";
+
+       query <<
+               "INSERT INTO " SOCIAL_TEMP_CONTACT_FREQ \
+               " SELECT " SOCIAL_ADDRESS ", COUNT(*) AS " STATS_TOTAL_COUNT \
+               " FROM " SOCIAL_TABLE_CONTACT_LOG \
+               " WHERE " << create_where_clause(filter_cleaned) <<
+               " GROUP BY " SOCIAL_ADDRESS ";";
+
+       query <<
+               "INSERT OR IGNORE INTO " SOCIAL_TEMP_CONTACT_FREQ " (" SOCIAL_ADDRESS ")" \
+               " VALUES ('" << address << "');";
+
        query <<
                "SELECT S." SOCIAL_ADDRESS ", S." STATS_TOTAL_COUNT ", 1+COUNT(lesser." STATS_TOTAL_COUNT ") AS Rank" \
-               " FROM " SOCIAL_VIEW_CONTACT_FREQ " AS S" \
-               " LEFT JOIN " SOCIAL_VIEW_CONTACT_FREQ " AS lesser" \
+               " FROM " SOCIAL_TEMP_CONTACT_FREQ " AS S" \
+               " LEFT JOIN " SOCIAL_TEMP_CONTACT_FREQ " AS lesser" \
                " ON S." STATS_TOTAL_COUNT " < lesser." STATS_TOTAL_COUNT \
                " WHERE S." SOCIAL_ADDRESS " = '" << address << "'";