summaryrefslogtreecommitdiff
path: root/src/task-factory.c
blob: 2e011d305091e1655cdb8fc645705557531d63dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
     * Copyright (c) 2018 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Flora License, Version 1.1 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://floralicense.org/license/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <glib.h>
#include <stdlib.h>
#include "task-factory.h"
#include "config.h"
#include "report-generator.h"
#include "report-generator-system.h"
#include "report-generator-load-avg.h"
#include "report-generator-process.h"
#include "report-generator-top.h"
#include "log.h"
#include "err-check.h"
#include "macros.h"
#include "ipc.h"

struct base_task
{
    task_t task;
	report_generator_t *report_gen;
	JsonBuilder *builder;
	JsonGenerator *generator;
};

static task_t *create_system_report_task();
static task_t *create_load_avg_report_task();
static task_t *create_process_report_task(const config_data_process_t *config);
static task_t *create_top_report_task(struct config_data_top options);

static void base_task_init(struct base_task *base, report_generator_t *generator);
static void base_task_execute(task_t *task);
static void base_task_release(task_t *task);

task_t *task_factory_create_task(const config_t *config)
{
    switch (config->scope)
    {
        case SYSTEM:
            return create_system_report_task();
        case LOAD_AVG:
            return create_load_avg_report_task();
        case PROCESS:
            return create_process_report_task(&config->data.process);
        case TOP:
            return create_top_report_task(config->data.top);
        default:
            return NULL;
    }
}

static void base_task_init(struct base_task *base, report_generator_t *generator)
{
	base->task.execute = base_task_execute;
	base->task.release = base_task_release;
	base->builder = json_builder_new();
	base->generator = json_generator_new();
	base->report_gen = generator;
}

static void base_task_execute(task_t *task)
{
	struct base_task *_base_task = container_of(task, struct base_task, task);

	json_builder_reset(_base_task->builder);

    if (report_generator_refresh((report_generator_t*)_base_task->report_gen) != true) {
		return;
	}

	report_generator_generate((report_generator_t*)_base_task->report_gen, _base_task->builder);

	JsonNode *root = json_builder_get_root(_base_task->builder);
	json_generator_set_root(_base_task->generator, root);
	char *json_report = json_generator_to_data(_base_task->generator, NULL);

	ipc_send_report(json_report);
	g_free(json_report);
}

static void base_task_release(task_t *task)
{
	struct base_task *_base_task = container_of(task, struct base_task, task);
	report_generator_release(_base_task->report_gen);
	g_object_unref(_base_task->builder);
	g_object_unref(_base_task->generator);
}

static task_t *create_system_report_task()
{
    struct base_task *_task = (struct base_task*)g_malloc(sizeof(struct base_task));

    report_generator_system_t *generator = report_generator_system_new_generator();
	if (!generator) {
		free(_task);
		return NULL;
	}

	base_task_init(_task, (report_generator_t*)generator);

    return &_task->task;
}

static task_t *create_load_avg_report_task()
{
    struct base_task *_task = (struct base_task*)g_malloc(sizeof(struct base_task));

    report_generator_load_avg_t *generator = report_generator_load_avg_new_generator();
	if (!generator) {
		free(_task);
		return NULL;
	}

	base_task_init(_task, (report_generator_t*)generator);

    return &_task->task;
}

static bool _report_generator_process_filter_cb(struct process *proc, void *user_data)
{
	config_data_process_t *data = user_data;

	if (data->n_filters == 0) {
		return true;
	}

	for (int i = 0; i < data->n_filters; ++i) {
		struct filter *filter = &data->filters[i];
		if (filter->app_id && process_get_appid(proc)) {
			if (strcmp(filter->app_id, process_get_appid(proc)) == 0)
				return true;
		}
		if (filter->exe && process_get_exe(proc)) {
			if (strcmp(filter->exe, process_get_exe(proc)) == 0)
				return true;
		}
	}
	return false;
}

static task_t *create_process_report_task(const config_data_process_t *config)
{
    struct base_task *_task = (struct base_task*)g_malloc(sizeof(struct base_task));

    report_generator_process_t *generator = report_generator_process_new_generator();
	if (!generator) {
		free(_task);
		return NULL;
	}

	report_generator_process_set_filter(generator,
			_report_generator_process_filter_cb,
			config);

	base_task_init(_task, (report_generator_t*)generator);

    return &_task->task;
}

static task_t *create_top_report_task(struct config_data_top options)
{
    report_generator_top_type_e type;

    switch (options.subject) {
       case TOP_SUBJECT_APPS:
          type = REPORT_GENERATOR_TOP_TYPE_APPS;
          break;
       case TOP_SUBJECT_ALL:
          type = REPORT_GENERATOR_TOP_TYPE_ALL;
          break;
       default:
          return NULL;
    }

    struct base_task *_task = (struct base_task*)g_malloc(sizeof(struct base_task));

    report_generator_top_t *generator = report_generator_top_new_generator(type, options.top);
	if (!generator) {
		free(_task);
		return NULL;
	}

	base_task_init(_task, (report_generator_t*)generator);

    return &_task->task;
}