From 1492bb2805f71ea9d9ae2771f4c31fc575522f96 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Sun, 31 Jan 2021 22:45:13 +0100
Subject: [PATCH] WIP: Add events to set the total number of items to update
 and a tick event to make the update progress. Not used for the moment by the
 repo module

---
 CONTRIBUTING.md     |  2 +-
 inc/lektor/net.h    |  4 +++-
 inc/lektor/queue.h  | 24 +++++++++++++-----------
 src/base/commands.c |  5 +++--
 src/net/listen.c    | 14 +++++++++++---
 5 files changed, 31 insertions(+), 18 deletions(-)

diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index efc30f62..5abbfc51 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -184,7 +184,7 @@ For the compatibility column, the possible values are the following:
 | `moveid {from} {to}`                      | `moveid {from} {to}`                  | ~     | `to` is always an absolute position               |
 | `playlist`                                | `playlist`                            | +     |                                                   |
 | `playlistfind {tag} {needle}`             | `playlistfind {uri}`                  | ~     | uses lektord URIs                                 |
-| `playlistid {songid}`                     | `playlistid {id}                      | ~     | the uri is only an id                             |
+| `playlistid {songid}`                     | `playlistid {id}`                     | ~     | the uri is only an id                             |
 | `playlistinfo [[songpos]/[start:end]]`    | `playlistinfo`                        | -     | is an alias to `playlist`                         |
 | `playlistsearch {taf} {needle}`           | `playlistsearch {uri}`                | ~     | uses lektord URIs                                 |
 | `plchanges {version} [start:end]`         |                                       |       | not implemented                                   |
diff --git a/inc/lektor/net.h b/inc/lektor/net.h
index f7607f95..faec5d29 100644
--- a/inc/lektor/net.h
+++ b/inc/lektor/net.h
@@ -54,7 +54,9 @@ struct lkt_state {
     /* The database */
     volatile sqlite3 *db;
     const char *kara_prefix;
-    volatile uint8_t is_updating;
+    uint64_t update_total;
+    uint64_t update_current;
+    uint8_t is_updating;
 
     /* Internal event flags */
     mpd_idle_flag mpd_idle_events;
diff --git a/inc/lektor/queue.h b/inc/lektor/queue.h
index b7f5367a..464e3f8a 100644
--- a/inc/lektor/queue.h
+++ b/inc/lektor/queue.h
@@ -37,17 +37,19 @@ enum {
 #define LKT_DB_UPDATING_FINISHED ((void *) (size_t) __LKT_DB_UPDATING_FINISHED)
 
 enum lkt_event_type {
-    lkt_event_null          = 0,         // NULL
-    lkt_event_play_pos      = (1 <<  1), // size_t
-    lkt_event_play_file     = (1 <<  2), // XXX: UNUSED
-    lkt_event_play_next     = (1 <<  3), // NULL
-    lkt_event_play_prev     = (1 <<  4), // NULL
-    lkt_event_play_toggle   = (1 <<  5), // size_t, `LKT_PLAY_.*`
-    lkt_event_prop_vol      = (1 <<  6), // size_t
-    lkt_event_prop_dur      = (1 <<  7), // size_t
-    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_null              = 0,         // NULL
+    lkt_event_play_pos          = (1 <<  1), // size_t
+    lkt_event_play_file         = (1 <<  2), // XXX: UNUSED
+    lkt_event_play_next         = (1 <<  3), // NULL
+    lkt_event_play_prev         = (1 <<  4), // NULL
+    lkt_event_play_toggle       = (1 <<  5), // size_t, `LKT_PLAY_.*`
+    lkt_event_prop_vol          = (1 <<  6), // size_t
+    lkt_event_prop_dur          = (1 <<  7), // size_t
+    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_tick    = (1 << 12), // NULL, increment the `update_current`
 };
 
 #define lkt_event_play ( lkt_event_play_pos    | lkt_event_play_file    \
diff --git a/src/base/commands.c b/src/base/commands.c
index fea011fc..a2c3b1d1 100644
--- a/src/base/commands.c
+++ b/src/base/commands.c
@@ -155,13 +155,14 @@ 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"
                                   "db_update: %ld\n"
                                   "uptime: %ld\n"
                                   "artists: %d\n"       /* Number of authors */
                                   "albums: %d\n"        /* Number of source names */
                                   "songs: %d\n",        /* Number of songs */
-                                  srv->is_updating, (uint64_t) ts_update,
-                                  (uint64_t) (time(NULL) - srv->start_date),
+                                  srv->is_updating, srv->update_current, srv->update_total,
+                                  (uint64_t) ts_update, (uint64_t) (time(NULL) - srv->start_date),
                                   artists, albums, songs);
     lkt_state_send(srv, c, out);
     return true;
diff --git a/src/net/listen.c b/src/net/listen.c
index f8d2b94f..284ff2c8 100644
--- a/src/net/listen.c
+++ b/src/net/listen.c
@@ -866,10 +866,18 @@ redo:
         free(string);
     })
 
-    __CASE(play_file, {})
-    __CASE(db_updating, { srv->is_updating = (uint8_t) (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;
+    })
 
-    /* The null event, just return */
+    /* The null event / ignored events, just return / continue */
+    __CASE(play_file, {})
     case lkt_event_null:
         return;
 
-- 
GitLab