report-json-serializer: app and top serializers 37/183337/5
authorMichal Kolodziejski <m.kolodziejs@samsung.com>
Wed, 4 Jul 2018 13:41:46 +0000 (15:41 +0200)
committerMichal Kolodziejski <m.kolodziejs@samsung.com>
Thu, 5 Jul 2018 14:11:25 +0000 (16:11 +0200)
Change-Id: Iecc773aecbc6f44d404e7edd4eea13e62868683f
Signed-off-by: Michal Kolodziejski <m.kolodziejs@samsung.com>
src/config-deserializer.c
src/json-schema-defs.h
src/report-json-serializer.c
src/report-json-serializer.h

index aa00d1a..18596bd 100644 (file)
@@ -92,14 +92,14 @@ static void config_array_iterate_func(JsonArray *array, guint index, JsonNode *e
         const gchar *target = json_object_get_string_member(entry, SCHEMA_TARGET);
         configs[index].data.system.options = config_parse_options(target);
     }
-    else if (g_strcmp0(type, SCHEMA_TYPE_APPS) == 0)
+    else if (g_strcmp0(type, SCHEMA_TYPE_PROCESS) == 0)
     {
         configs[index].scope = APPS;
 
         const gchar *target = json_object_get_string_member(entry, SCHEMA_TARGET);
         configs[index].data.apps.options = config_parse_options(target);
 
-        const gchar *app_id = json_object_get_string_member(entry, SCHEMA_APP_ID);
+        const gchar *app_id = json_object_get_string_member(entry, SCHEMA_ID);
         snprintf(configs[index].data.apps.app_id, APP_ID_REGEX_MAX_LEN + 1, "%s", app_id);
     }
     else if (g_strcmp0(type, SCHEMA_TYPE_LOAD_AVG) == 0)
index 13f0248..596ea6d 100644 (file)
@@ -21,7 +21,7 @@
 #define SCHEMA_TYPE_TOP "TOP"
 #define SCHEMA_TYPE_SYSTEM "SYSTEM"
 #define SCHEMA_TYPE_LOAD_AVG "LOAD_AVG"
-#define SCHEMA_TYPE_APPS "APPS"
+#define SCHEMA_TYPE_PROCESS "PROCESS"
 
 #define SCHEMA_TARGET "target"
 #define SCHEMA_TARGET_CPU "CPU"
 
 #define SCHEMA_FREQUENCY "frequency"
 #define SCHEMA_TOP "top"
-#define SCHEMA_APP_ID "app_id"
+#define SCHEMA_ID "id"
 
 #define SCHEMA_RESULT_DATA_SYSTEM "system_data"
 #define SCHEMA_RESULT_DATA_LOAD_AVG "load_avg_data"
-#define SCHEMA_RESULT_DATA_APPS "apps_data"
+#define SCHEMA_RESULT_DATA_PROCESS "process_data"
 #define SCHEMA_RESULT_DATA_TOP "top_data"
 
+#define SCHEMA_RESULT_PID "pid"
 #define SCHEMA_RESULT_TIME "time"
 #define SCHEMA_RESULT_USAGE "usage"
 #define SCHEMA_RESULT_AVG_ONE "one_min_avg"
index f6b03fc..9a2f06c 100644 (file)
@@ -23,11 +23,11 @@ static char *_serialize(JsonBuilder *builder)
 {
        JsonGenerator *gen = json_generator_new();
        if (!gen) {
-               g_object_unref(builder); \
+               g_object_unref(builder);
                return NULL;
        }
 
-       JsonNode *root = json_builder_get_root(builder); \
+       JsonNode *root = json_builder_get_root(builder);
        json_generator_set_root(gen, root);
        char *str = json_generator_to_data(gen, NULL);
        g_object_unref(gen);
@@ -46,6 +46,18 @@ func_name(param_type *report) \
        return ret; \
 }
 
+#define IMPLEMENT_ARRAY_SERIALIZER_FUNC(func_name, param_type, serializer) \
+char* \
+func_name(param_type **reports) \
+{\
+       JsonBuilder *builder = json_builder_new(); \
+       if (!builder) return NULL; \
+       serializer(builder, reports); \
+       char *ret = _serialize(builder); \
+       g_object_unref(builder); \
+       return ret; \
+}
+
 static void system_load_average_report_to_json_object(JsonBuilder *builder, struct system_load_average_report *report)
 {
        json_builder_begin_object(builder);
@@ -115,16 +127,148 @@ static void system_cpu_usage_report_to_json_object(JsonBuilder *builder, struct
        json_builder_end_object(builder);
 }
 
+static void top_cpu_usage_reports_to_json_object(JsonBuilder *builder, struct app_cpu_usage_report **reports)
+{
+       json_builder_begin_object(builder);
+
+       json_builder_set_member_name(builder, SCHEMA_TYPE);
+       json_builder_add_string_value(builder, SCHEMA_TYPE_PROCESS);
+
+       json_builder_set_member_name(builder, SCHEMA_TARGET);
+       json_builder_add_string_value(builder, SCHEMA_TARGET_CPU);
+
+       json_builder_set_member_name(builder, SCHEMA_RESULT_DATA_TOP);
+       json_builder_begin_array(builder);
+
+       int i = 0;
+       while(reports[i])
+       {
+               json_builder_begin_object(builder);
+
+               json_builder_set_member_name(builder, SCHEMA_ID);
+               json_builder_add_string_value(builder, reports[i]->app_id);
+
+               json_builder_set_member_name(builder, SCHEMA_RESULT_PID);
+               json_builder_add_double_value(builder, reports[i]->process_report.pid);
+
+               json_builder_set_member_name(builder, SCHEMA_RESULT_RESULT);
+               json_builder_begin_object(builder);
+
+               json_builder_set_member_name(builder, SCHEMA_RESULT_TIME);
+               json_builder_add_double_value(builder, reports[i]->process_report.time);
+               json_builder_set_member_name(builder, SCHEMA_RESULT_USAGE);
+               json_builder_add_double_value(builder, reports[i]->process_report.usage);
+
+               json_builder_end_object(builder);
+
+               json_builder_end_object(builder);
+               i++;
+       }
+
+       json_builder_end_array(builder);
+       json_builder_end_object(builder);
+}
+
+static void top_memory_usage_reports_to_json_object(JsonBuilder *builder, struct app_memory_usage_report **reports)
+{
+       json_builder_begin_object(builder);
+
+       json_builder_set_member_name(builder, SCHEMA_TYPE);
+       json_builder_add_string_value(builder, SCHEMA_TYPE_PROCESS);
+
+       json_builder_set_member_name(builder, SCHEMA_TARGET);
+       json_builder_add_string_value(builder, SCHEMA_TARGET_MEMORY);
+
+       json_builder_set_member_name(builder, SCHEMA_RESULT_DATA_TOP);
+       json_builder_begin_array(builder);
+
+       int i = 0;
+       while(reports[i])
+       {
+               json_builder_begin_object(builder);
+
+               json_builder_set_member_name(builder, SCHEMA_ID);
+               json_builder_add_string_value(builder, reports[i]->app_id);
+
+               json_builder_set_member_name(builder, SCHEMA_RESULT_PID);
+               json_builder_add_double_value(builder, reports[i]->process_report.pid);
+
+               json_builder_set_member_name(builder, SCHEMA_RESULT_RESULT);
+               json_builder_begin_object(builder);
+
+               json_builder_set_member_name(builder, SCHEMA_RESULT_TIME);
+               json_builder_add_double_value(builder, reports[i]->process_report.time);
+               json_builder_set_member_name(builder, SCHEMA_RESULT_USAGE);
+               json_builder_add_double_value(builder, reports[i]->process_report.usage);
+
+               json_builder_end_object(builder);
+
+               json_builder_end_object(builder);
+               i++;
+       }
+
+       json_builder_end_array(builder);
+       json_builder_end_object(builder);
+}
+
 static void apps_cpu_usage_report_to_json_object(JsonBuilder *builder, struct app_cpu_usage_report *report)
 {
-       //TODO
-       //implement after report API changes
+       json_builder_begin_object(builder);
+
+       json_builder_set_member_name(builder, SCHEMA_TYPE);
+       json_builder_add_string_value(builder, SCHEMA_TYPE_PROCESS);
+
+       json_builder_set_member_name(builder, SCHEMA_TARGET);
+       json_builder_add_string_value(builder, SCHEMA_TARGET_CPU);
+
+       json_builder_set_member_name(builder, SCHEMA_RESULT_DATA_PROCESS);
+       json_builder_begin_object(builder);
+
+       json_builder_set_member_name(builder, SCHEMA_ID);
+       json_builder_add_string_value(builder, report->app_id);
+       json_builder_set_member_name(builder, SCHEMA_RESULT_PID);
+       json_builder_add_double_value(builder, report->process_report.pid);
+
+       json_builder_set_member_name(builder, SCHEMA_RESULT_RESULT);
+       json_builder_begin_object(builder);
+       json_builder_set_member_name(builder, SCHEMA_RESULT_TIME);
+       json_builder_add_double_value(builder, report->process_report.time);
+       json_builder_set_member_name(builder, SCHEMA_RESULT_USAGE);
+       json_builder_add_double_value(builder, report->process_report.usage);
+
+       json_builder_end_object(builder);
+       json_builder_end_object(builder);
+       json_builder_end_object(builder);
 }
 
 static void apps_memory_usage_report_to_json_object(JsonBuilder *builder, struct app_memory_usage_report *report)
 {
-       //TODO
-       //implement after report API changes
+       json_builder_begin_object(builder);
+
+       json_builder_set_member_name(builder, SCHEMA_TYPE);
+       json_builder_add_string_value(builder, SCHEMA_TYPE_PROCESS);
+
+       json_builder_set_member_name(builder, SCHEMA_TARGET);
+       json_builder_add_string_value(builder, SCHEMA_TARGET_MEMORY);
+
+       json_builder_set_member_name(builder, SCHEMA_RESULT_DATA_PROCESS);
+       json_builder_begin_object(builder);
+
+       json_builder_set_member_name(builder, SCHEMA_ID);
+       json_builder_add_string_value(builder, report->app_id);
+       json_builder_set_member_name(builder, SCHEMA_RESULT_PID);
+       json_builder_add_double_value(builder, report->process_report.pid);
+
+       json_builder_set_member_name(builder, SCHEMA_RESULT_RESULT);
+       json_builder_begin_object(builder);
+       json_builder_set_member_name(builder, SCHEMA_RESULT_TIME);
+       json_builder_add_double_value(builder, report->process_report.time);
+       json_builder_set_member_name(builder, SCHEMA_RESULT_USAGE);
+       json_builder_add_double_value(builder, report->process_report.usage);
+
+       json_builder_end_object(builder);
+       json_builder_end_object(builder);
+       json_builder_end_object(builder);
 }
 
 IMPLEMENT_SERIALIZER_FUNC(
@@ -152,3 +296,13 @@ IMPLEMENT_SERIALIZER_FUNC(
                struct app_memory_usage_report,
                apps_memory_usage_report_to_json_object)
 
+IMPLEMENT_ARRAY_SERIALIZER_FUNC(
+               report_json_serializer_serialize_top_cpu_usage_reports,
+               struct app_cpu_usage_report,
+               top_cpu_usage_reports_to_json_object)
+
+IMPLEMENT_ARRAY_SERIALIZER_FUNC(
+               report_json_serializer_serialize_top_memory_usage_reports,
+               struct app_memory_usage_report,
+               top_memory_usage_reports_to_json_object)
+
index 6b0f926..f335b37 100644 (file)
@@ -14,7 +14,6 @@
  * limitations under the License.
  */
 
-
 #ifndef __REPORT_JSON_SERIALIZER_H_
 #define __REPORT_JSON_SERIALIZER_H_
 
@@ -22,6 +21,9 @@
 
 /**
  * @brief Serializes system_load_average_report to json string
+ *
+ * @param[in] report Report structure with usage data.
+ *
  * @return dynamically allocated string on NULL on error.
  * @remark returned value should be released with @free
  */
@@ -29,6 +31,9 @@ char *report_json_serializer_serialize_system_load_average_report(struct system_
 
 /**
  * @brief Serializes system_memory_usage_report to json string
+ *
+ * @param[in] report Report structure with usage data.
+ *
  * @return dynamically allocated string on NULL on error.
  * @remark returned value should be released with @free
  */
@@ -36,6 +41,9 @@ char *report_json_serializer_serialize_system_memory_usage_report(struct system_
 
 /**
  * @brief Serializes system_cpus_usage_report to json string
+ *
+ * @param[in] report Report structure with usage data.
+ *
  * @return dynamically allocated string on NULL on error.
  * @remark returned value should be released with @free
  */
@@ -43,6 +51,9 @@ char *report_json_serializer_serialize_system_cpu_usage_report(struct system_cpu
 
 /**
  * @brief Serializes system_cpus_usage_report to json string
+ *
+ * @param[in] report Report structure with usage data.
+ *
  * @return dynamically allocated string on NULL on error.
  * @remark returned value should be released with @free
  */
@@ -50,6 +61,9 @@ char *report_json_serializer_serialize_system_cpus_usage_report(struct system_pe
 
 /**
  * @brief Serializes process_memory_usage_report to json string
+ *
+ * @param[in] report Report structure with usage data.
+ *
  * @return dynamically allocated string on NULL on error.
  * @remark returned value should be released with @free
  */
@@ -57,6 +71,9 @@ char *report_json_serializer_serialize_process_cpu_usage_report(struct process_c
 
 /**
  * @brief Serializes process_memory_usage_report to json string
+ *
+ * @param[in] report Report structure with usage data.
+ *
  * @return dynamically allocated string on NULL on error.
  * @remark returned value should be released with @free
  */
@@ -64,6 +81,9 @@ char *report_json_serializer_serialize_process_memory_usage_report(struct proces
 
 /**
  * @brief Serializes app_system_cpu_usage_report to json string
+ *
+ * @param[in] report Report structure with usage data.
+ *
  * @return dynamically allocated string on NULL on error.
  * @remark returned value should be released with @free
  */
@@ -71,9 +91,32 @@ char *report_json_serializer_serialize_apps_cpu_usage_report(struct app_cpu_usag
 
 /**
  * @brief Serializes app_memory_usage_report to json string
+ *
+ * @param[in] report Report structure with usage data.
+ *
  * @return dynamically allocated string on NULL on error.
  * @remark returned value should be released with @free
  */
 char *report_json_serializer_serialize_apps_memory_usage_report(struct app_memory_usage_report *report);
 
+/**
+ * @brief Serializes app_system_cpu_usage_reports to json string
+ *
+ * @param[in] report NULL-terminated array of report structures with usage data.
+ *
+ * @return dynamically allocated string on NULL on error.
+ * @remark returned value should be released with @free
+ */
+char *report_json_serializer_serialize_top_cpu_usage_reports(struct app_cpu_usage_report **reports);
+
+/**
+ * @brief Serializes app_memory_usage_reports to json string
+ *
+ * @param[in] report NULL-terminated array of report structures with usage data.
+ *
+ * @return dynamically allocated string on NULL on error.
+ * @remark returned value should be released with @free
+ */
+char *report_json_serializer_serialize_top_memory_usage_reports(struct app_memory_usage_report **reports);
+
 #endif