From 241e460605a23b103e2c5d44ecce2c7dd28813e8 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Thu, 4 Feb 2021 10:04:39 +0100
Subject: [PATCH] MOD: Add the update count in the repo module for downloaded
 karas

- Add events and handler in the lkt_queue
- Display the new data with lkt
- Now printings lines of files alongside the function names
---
 inc/lektor/common.h      | 12 +++++------
 inc/lektor/queue.h       | 14 ++++++------
 src/base/commands.c      |  2 +-
 src/base/common.c        |  7 +++---
 src/database/queue.c     | 10 ++++-----
 src/main/lkt.c           | 46 ++++++++++++++++++++++++++++++++--------
 src/main/server.c        |  1 +
 src/module/module_repo.c |  3 +++
 src/net/listen.c         |  8 +++----
 9 files changed, 69 insertions(+), 34 deletions(-)

diff --git a/inc/lektor/common.h b/inc/lektor/common.h
index 62ee6ccd..00f213ad 100644
--- a/inc/lektor/common.h
+++ b/inc/lektor/common.h
@@ -89,12 +89,12 @@ enum log_level {
     __LAST_UNUSED_LOG_LEVEL,    /* Usefull to enable all by default */
 };
 extern enum log_level __log_level;
-void __lkt_log(enum log_level, const char *section, const char *func, const char *format, ...);
-#define LOG_DEBUG(section, ...) __lkt_log(DEBUG, section, __func__, __VA_ARGS__)
-#define LOG_INFO( section, ...) __lkt_log(INFO,  section, __func__, __VA_ARGS__)
-#define LOG_WARN( section, ...) __lkt_log(WARN,  section, __func__, __VA_ARGS__)
-#define LOG_ERROR(section, ...) __lkt_log(ERROR, section, __func__, __VA_ARGS__)
-#define LOG_FATAL(...)          { __lkt_log(ERROR, "FATAL", __func__, __VA_ARGS__); exit(1); }
+void __lkt_log(enum log_level, const char *section, const char *func, uint64_t line, const char *format, ...);
+#define LOG_DEBUG(section, ...) __lkt_log(DEBUG, section, __func__, __LINE__, __VA_ARGS__)
+#define LOG_INFO( section, ...) __lkt_log(INFO,  section, __func__, __LINE__, __VA_ARGS__)
+#define LOG_WARN( section, ...) __lkt_log(WARN,  section, __func__, __LINE__, __VA_ARGS__)
+#define LOG_ERROR(section, ...) __lkt_log(ERROR, section, __func__, __LINE__, __VA_ARGS__)
+#define LOG_FATAL(...)          { __lkt_log(ERROR, "FATAL", __func__, __LINE__, __VA_ARGS__); exit(1); }
 
 #define SELF_EXECUTABLE_LINUX       "/proc/self/exe"
 #define SELF_EXECUTABLE_FREEBSD     "/proc/curproc/file"
diff --git a/inc/lektor/queue.h b/inc/lektor/queue.h
index 464e3f8a..65eb7b08 100644
--- a/inc/lektor/queue.h
+++ b/inc/lektor/queue.h
@@ -48,15 +48,17 @@ enum lkt_event_type {
     lkt_event_prop_time         = (1 <<  8), // size_t
     lkt_event_skip_current      = (1 <<  9), // NULL
     lkt_event_db_updating       = (1 << 10), // size_t, `LKT_DB_UPDATING_*`
-    lkt_event_db_update_total   = (1 << 11), // size_t, update udapte_total reset update_current
+    lkt_event_db_update_total   = (1 << 11), // size_t, increments udapte_total
     lkt_event_db_update_tick    = (1 << 12), // NULL, increment the `update_current`
 };
 
-#define lkt_event_play ( lkt_event_play_pos    | lkt_event_play_file    \
-                       | lkt_event_play_next   | lkt_event_play_prev    \
-                       | lkt_event_play_toggle | lkt_event_skip_current )
-#define lkt_event_prop ( lkt_event_prop_vol    | lkt_event_prop_dur     \
-                       | lkt_event_prop_time                            )
+#define lkt_event_play   ( lkt_event_play_pos    | lkt_event_play_file      \
+                         | lkt_event_play_next   | lkt_event_play_prev      \
+                         | lkt_event_play_toggle | lkt_event_skip_current   )
+#define lkt_event_prop   ( lkt_event_prop_vol    | lkt_event_prop_dur       \
+                         | lkt_event_prop_time                              )
+#define lkt_event_update ( lkt_event_db_updating | lkt_event_db_update_tick \
+                         | lkt_event_db_update_total                        )
 
 typedef struct {
     unsigned int type;
diff --git a/src/base/commands.c b/src/base/commands.c
index fb9ea207..daeabc74 100644
--- a/src/base/commands.c
+++ b/src/base/commands.c
@@ -155,7 +155,7 @@ command_stats(struct lkt_state *srv, size_t c, char UNUSED *args[LKT_MESSAGE_ARG
      *              the sdl2 module and any player module) */
     out->data_len = safe_snprintf(out->data, LKT_MESSAGE_MAX,
                                   "__is_updating: %d\n" /* Custom field here */
-                                  "___update_progress: %ld:%ld\n"
+                                  "__update_progress: %ld:%ld\n"
                                   "db_update: %ld\n"
                                   "uptime: %ld\n"
                                   "artists: %d\n"       /* Number of authors */
diff --git a/src/base/common.c b/src/base/common.c
index 0d24a7f9..c297695b 100644
--- a/src/base/common.c
+++ b/src/base/common.c
@@ -53,7 +53,8 @@ __set_assert(void)
 enum log_level __log_level = __LAST_UNUSED_LOG_LEVEL; /* All by default */
 
 void
-__lkt_log(enum log_level level, const char *section, const char *func, const char *format, ...)
+__lkt_log(enum log_level level, const char *section, const char *func, uint64_t line_number,
+          const char *format, ...)
 {
     char line[LKT_MESSAGE_MAX];
     va_list ap;
@@ -69,10 +70,10 @@ __lkt_log(enum log_level level, const char *section, const char *func, const cha
     if (level > __log_level)
         return;
 
-    safe_snprintf(line, LKT_MESSAGE_MAX, " %c [%02d:%02d:%02d] %-10s %s: %s\n",
+    safe_snprintf(line, LKT_MESSAGE_MAX, " %c [%02d:%02d:%02d] %-10s %s(+%ld): %s\n",
                   level == ERROR ? '!' : level == WARN ? '*' : level == INFO ? '.' : ' ',
                   hour, min, sec,
-                  section, func, format);
+                  section, func, line_number, format);
 
     va_start(ap, format);
     vfprintf(stderr, line, ap);
diff --git a/src/database/queue.c b/src/database/queue.c
index 23bafe1a..a9aba984 100644
--- a/src/database/queue.c
+++ b/src/database/queue.c
@@ -621,17 +621,17 @@ database_queue_move(volatile sqlite3 *db, int from, int to)
         "  SELECT position, kara_id, priority FROM queue_tmp ORDER BY priority, position;"
         /* Clean queue_tmp */
         "DELETE FROM sqlite_sequence WHERE name = 'queue_tmp';"
-        
+
         "DELETE FROM queue_tmp;";
 
     char SQL[LKT_MAX_SQLITE_STATEMENT];
 
     SQLITE_EXEC(db, "BEGIN TRANSACTION;", error_no_rollback);
-        safe_snprintf(SQL, LKT_MAX_SQLITE_STATEMENT, SQL_TEMPLATE_PART_1,to,from, from, to, to, to);
-        SQLITE_EXEC(db, SQL, error);
+    safe_snprintf(SQL, LKT_MAX_SQLITE_STATEMENT, SQL_TEMPLATE_PART_1, to, from, from, to, to, to);
+    SQLITE_EXEC(db, SQL, error);
 
-        safe_snprintf(SQL, LKT_MAX_SQLITE_STATEMENT, SQL_TEMPLATE_PART_2, to, from);
-        SQLITE_EXEC(db, SQL, error);
+    safe_snprintf(SQL, LKT_MAX_SQLITE_STATEMENT, SQL_TEMPLATE_PART_2, to, from);
+    SQLITE_EXEC(db, SQL, error);
     SQLITE_EXEC(db, "COMMIT;", error);
     return true;
 error:
diff --git a/src/main/lkt.c b/src/main/lkt.c
index 6a4f6c5c..814178c0 100644
--- a/src/main/lkt.c
+++ b/src/main/lkt.c
@@ -646,17 +646,38 @@ status__(struct cmd_args *args)
 
     bool play = false, stopped = true, is_updating = false;
     int time_pos = 0, time_duration = 0, song_index = 0, plt_len = 0, ret = EXIT_FAILURE, it = 0;
+    int update_count = 0, update_tick = 0;
     size_t len;
     time_t update_ts = 0;
 
     memset(flags, 0, 24 * sizeof(char));
     FILE *sock = lkt_connect();
 
-#define assign_flag(str, f)  if (STR_NMATCH(buff, str, len) && (atoi(lkt_skip_key(buff)) == 1)) { flags[it++] = f; continue; }
-#define assign_int(str, var) if (STR_NMATCH(buff, str, len)) { var = (strtoll(lkt_skip_key(buff), NULL, 0)); continue; }
-#define check_end()                                     \
-    if (STR_NMATCH(buff, "OK", 2)) break;               \
-    else if (STR_NMATCH(buff, "ACK", 3)) goto error;
+#define assign_flag(str, f) {                                               \
+    if (STR_NMATCH(buff, str, len) && (atoi(lkt_skip_key(buff)) == 1)) {    \
+        flags[it++] = f;                                                    \
+        continue;                                                           \
+    }}
+#define assign_two_int(str, var1, var2) {                   \
+    if (STR_NMATCH(buff, str, len)) {                       \
+        char *endptr = NULL;                                \
+        var1 = (strtoll(lkt_skip_key(buff), &endptr, 0));   \
+        endptr += strspn(endptr, "-/:");                    \
+        if (endptr)                                         \
+            var2 = (strtoll(endptr, NULL, 0));              \
+        continue;                                           \
+    }}
+#define assign_int(str, var) {                          \
+    if (STR_NMATCH(buff, str, len)) {                   \
+        var = (strtoll(lkt_skip_key(buff), NULL, 0));   \
+        continue;                                       \
+    }}
+#define check_end() {                                   \
+    if (STR_NMATCH(buff, "OK", 2)) {                    \
+        break;                                          \
+    } else if (STR_NMATCH(buff, "ACK", 3)) {            \
+        goto error;                                     \
+    }}
 
     /* Get lektor's status */
     write_socket(sock, status_str__);
@@ -695,6 +716,8 @@ status__(struct cmd_args *args)
         assign_int("__is_updating", is_updating)
         assign_int("db_update",     update_ts)
 
+        assign_two_int("__update_progress", update_tick, update_count)
+
         check_end()
     }
 
@@ -703,6 +726,7 @@ status__(struct cmd_args *args)
 
 #undef assign_flag
 #undef assign_int
+#undef assign_two_int
 #undef check_end
 
     struct tm *p_tm = localtime(&update_ts);
@@ -717,13 +741,17 @@ status__(struct cmd_args *args)
     printf("Lektor: %s\n"
            "Queue: #%d/%d\n"
            "Playback: %d:%02d/%d:%02d\n"
-           "Flags: %s\n"
-           "%s: %s\n",
+           "Flags: %s\n",
            play ? "play" : "stopped",
            song_index + 1, plt_len,
            pla_m, pla_s, dur_m, dur_s,
-           flags[0] ? flags : "(none)",
-           is_updating ? "Updating" : "Last update", buff);
+           flags[0] ? flags : "(none)");
+    if (is_updating) {
+        printf("Updating: %d/%d%s\n", update_tick, update_count,
+               update_count >= update_tick ? "" : " (invalid)");
+    } else {
+        printf("Last update: %s\n", buff);
+    }
 
     /* If there is a kara loaded in mpv, get this kara. */
     if (!stopped) {
diff --git a/src/main/server.c b/src/main/server.c
index 7117c8cc..e472659d 100644
--- a/src/main/server.c
+++ b/src/main/server.c
@@ -332,6 +332,7 @@ retry_config:
         database_queue_clear(srv.db);
     }
     lkt_queue_make_available(&srv.queue, lkt_event_prop);
+    lkt_queue_make_available(&srv.queue, lkt_event_update);
 
     {   /* Module initialization */
         char *module = safe_malloc(sizeof(char) * PATH_MAX);
diff --git a/src/module/module_repo.c b/src/module/module_repo.c
index 054bbb19..3969e2ac 100644
--- a/src/module/module_repo.c
+++ b/src/module/module_repo.c
@@ -422,12 +422,14 @@ __handle_got_json_internal_callback(const char *key, const char *val, int comp,
             ++(kara->ignored_count);
             database_update_touch(kara->db, kara->id);
             database_update_set_available(kara->db, kara->id);
+            lkt_queue_send(kara->repo->queue, lkt_event_db_update_tick, NULL);
             LOG_DEBUG("REPO", "Ignore kara %ld", kara->id);
             return;
         }
 
     do_it:
         __handle_got_json_dl(kara, current_id);
+        lkt_queue_send(kara->repo->queue, lkt_event_db_update_tick, NULL);
     }
 
     else
@@ -464,6 +466,7 @@ __handle_got_json(volatile sqlite3 *db, struct module_repo_internal *repo, const
 
     /* Handle the json */
     LOG_INFO("REPO", "Starting to process json for repo %s, total of %ld karas", repo->name, len);
+    lkt_queue_send(repo->queue, lkt_event_db_update_total, (void *) (size_t) len);
     json_parse(json, 2, __handle_got_json_internal_callback, (void *) &kara);
     LOG_INFO("REPO", "Updated %ld karas and ignored %ld karas, total is %ld",
              kara.update_count, kara.ignored_count, len);
diff --git a/src/net/listen.c b/src/net/listen.c
index 284ff2c8..f7383952 100644
--- a/src/net/listen.c
+++ b/src/net/listen.c
@@ -866,14 +866,14 @@ redo:
         free(string);
     })
 
+    __CASE(db_update_total, { srv->update_total += (size_t) evt.attr;                 })
     __CASE(db_updating,     { srv->is_updating = (((uint8_t) (size_t) evt.attr) > 0); })
     __CASE(db_update_tick,  {
         if (srv->update_current != ((size_t) -1))
             srv->update_current++;
-    })
-    __CASE(db_update_total, {
-        srv->update_total   = (size_t) evt.attr;
-        srv->update_current = 0;
+        if (srv->update_current >= srv->update_total)
+            (LOG_WARN("EVENT", "Force updating state because tikcs exceded the update count"),
+             srv->is_updating = 0);
     })
 
     /* The null event / ignored events, just return / continue */
-- 
GitLab