Fix 'frequency' statistics for the trigger to emit proper results
[platform/core/context/statistics-context-provider.git] / src / app / db_handle.cpp
index 2bc1b90..8297770 100644 (file)
@@ -50,6 +50,7 @@ int ctx::app_db_handle::read(const char* subject, ctx::json filter)
                query = create_sql_common_setting(filter);
 
        } else if (STR_EQ(subject, APP_SUBJ_FREQUENCY)) {
+               is_trigger_item = true;
                query = create_sql_frequency(filter);
        }
 
@@ -109,10 +110,25 @@ std::string ctx::app_db_handle::create_sql_frequency(ctx::json filter)
        std::string where_clause = create_where_clause(filter_cleaned);
 
        std::stringstream query;
+
+       query <<
+               "DELETE FROM " APP_TEMP_USAGE_FREQ ";";
+
+       query <<
+               "INSERT INTO " APP_TEMP_USAGE_FREQ \
+               " SELECT " STATS_APP_ID ", COUNT(*) AS " STATS_TOTAL_COUNT \
+               " FROM " APP_TABLE_USAGE_LOG \
+               " WHERE " << where_clause <<
+               " GROUP BY " STATS_APP_ID ";";
+
+       query <<
+               "INSERT OR IGNORE INTO " APP_TEMP_USAGE_FREQ " (" STATS_APP_ID ")" \
+               " VALUES ('" << app_id << "');";
+
        query <<
                "SELECT S." STATS_APP_ID ", S." STATS_TOTAL_COUNT ", 1+COUNT(lesser." STATS_TOTAL_COUNT ") AS Rank" \
-               " FROM " APP_VIEW_USAGE_FREQ " AS S" \
-               " LEFT JOIN " APP_VIEW_USAGE_FREQ " AS lesser" \
+               " FROM " APP_TEMP_USAGE_FREQ " AS S" \
+               " LEFT JOIN " APP_TEMP_USAGE_FREQ " AS lesser" \
                " ON S." STATS_TOTAL_COUNT " < lesser." STATS_TOTAL_COUNT \
                " WHERE S." STATS_APP_ID " = '" << app_id << "'";