]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Minor] Lua_task: Add function to get scan time
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 15 Apr 2019 11:21:21 +0000 (12:21 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 15 Apr 2019 11:21:21 +0000 (12:21 +0100)
src/lua/lua_task.c

index 7bc4438cdeca5ff84cdfc335075fbf47fa8b4259..b1aabfddaf8ed6504f5318e58df0df35c0d2c3c4 100644 (file)
@@ -696,7 +696,20 @@ LUA_FUNCTION_DEF (task, get_date);
  * @return {string} if of a message
  */
 LUA_FUNCTION_DEF (task, get_message_id);
+/***
+ * @method task:get_timeval()
+ * Returns the timestamp for a task start processing time.
+ * @return {table} table with fields as described in `struct timeval` in C
+ */
 LUA_FUNCTION_DEF (task, get_timeval);
+/***
+ * @method task:get_scan_time([set])
+ * Returns 2 floating point numbers: scan real time and scan virtual time.
+ * If `set` is `true`, then the finishing time is also set (enabled by default).
+ * This function should be normally called on idempotent phase.
+ * @return {number,number} real and virtual times in seconds with floating point
+ */
+LUA_FUNCTION_DEF (task, get_scan_time);
 /***
  * @method task:get_metric_result()
  * Get full result of a metric as a table:
@@ -1102,6 +1115,7 @@ static const struct luaL_reg tasklib_m[] = {
        LUA_INTERFACE_DEF (task, get_date),
        LUA_INTERFACE_DEF (task, get_message_id),
        LUA_INTERFACE_DEF (task, get_timeval),
+       LUA_INTERFACE_DEF (task, get_scan_time),
        LUA_INTERFACE_DEF (task, get_metric_result),
        LUA_INTERFACE_DEF (task, get_metric_score),
        LUA_INTERFACE_DEF (task, get_metric_action),
@@ -4401,6 +4415,34 @@ lua_task_get_timeval (lua_State *L)
        return 1;
 }
 
+static gint
+lua_task_get_scan_time (lua_State *L)
+{
+       LUA_TRACE_POINT;
+       struct rspamd_task *task = lua_check_task (L, 1);
+       gboolean set = TRUE;
+
+       if (task != NULL) {
+               if (lua_isboolean (L, 2)) {
+                       set = lua_toboolean (L, 2);
+               }
+
+               rspamd_task_set_finish_time (task);
+               lua_pushnumber (L, task->time_real_finish - task->time_real);
+               lua_pushnumber (L, task->time_virtual_finish - task->time_virtual);
+
+               if (!set) {
+                       /* Reset to nan to allow further calcs in rspamd_task_set_finish_time */
+                       task->time_real_finish = NAN;
+               }
+       }
+       else {
+               return luaL_error (L, "invalid arguments");
+       }
+
+       return 1;
+}
+
 static gint
 lua_task_get_size (lua_State *L)
 {