[polari/wip/fmuellner/tracker: 47/47] tool: Add 'import' command



commit f7ab6dc0183f33ce5b44c682b6ed78deda00b084
Author: Florian Müllner <fmuellner gnome org>
Date:   Fri Jul 28 13:02:36 2017 +0100

    tool: Add 'import' command
    
    Until we do proper log migration inside Polari itself, this allows
    for importing the DB with the existing logs from telepathy-logger.

 src/polari-log-tool.c |  155 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 154 insertions(+), 1 deletions(-)
---
diff --git a/src/polari-log-tool.c b/src/polari-log-tool.c
index 57a7f61..10f1d92 100644
--- a/src/polari-log-tool.c
+++ b/src/polari-log-tool.c
@@ -16,10 +16,160 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include "lib/polari-tpl-importer.h"
+#include "lib/polari-message.h"
 #include "lib/polari-util.h"
 
+static GMainLoop *loop;
 static guint status = 0;
 
+static gboolean process_next (PolariTplImporter *importer,
+                              GList             *files);
+
+static void
+import_ready (GObject      *source,
+               GAsyncResult *result,
+               gpointer      user_data)
+{
+  PolariTplImporter *importer = POLARI_TPL_IMPORTER (source);
+  GList *messages, *l;
+  GError *error = NULL;
+  GList *files = user_data;
+  TrackerSparqlConnection *connection;
+  TrackerNamespaceManager *ns_manager;
+  GString *sparql = g_string_new (NULL);
+  char *account_id = NULL, *channel_name = NULL;
+  gboolean is_room;
+
+  messages = polari_tpl_importer_import_finish (importer,
+                                                result,
+                                                &account_id,
+                                                &channel_name,
+                                                &is_room,
+                                                &error);
+
+  if (error)
+    {
+      g_printerr ("Failed to import file: %s\n", error->message);
+      g_error_free (error);
+      status = 1;
+      goto out;
+    }
+
+  connection = polari_util_get_tracker_connection (&error);
+  ns_manager = tracker_sparql_connection_get_namespace_manager (connection);
+
+  if (error)
+    {
+      g_printerr ("Failed to import file: %s\n", error->message);
+      g_error_free (error);
+      status = 1;
+      goto out;
+    }
+
+  for (l = messages; l; l = l->next)
+    {
+      PolariMessage *message = l->data;
+#if 1
+      TrackerResource *res;
+      char *tmp;
+
+      res = polari_message_to_tracker_resource (message,
+                                                account_id,
+                                                channel_name,
+                                                is_room);
+
+      tmp = tracker_resource_print_sparql_update (res, ns_manager, NULL);
+      g_string_append (sparql, tmp);
+      g_object_unref (res);
+      g_free (tmp);
+#else
+      g_print ("<%s> %s\n",
+              polari_message_get_sender (tpl_message),
+              polari_message_get_text (tpl_message));
+#endif
+    }
+  g_list_free_full (messages, (GDestroyNotify)polari_message_free);
+
+  if (sparql->len > 0)
+    tracker_sparql_connection_update (connection, sparql->str,
+                                      G_PRIORITY_DEFAULT, NULL, &error);
+
+  if (error)
+    {
+      g_printerr ("Failed to import file: %s\n", error->message);
+      g_error_free (error);
+      status = 1;
+    }
+
+out:
+  g_string_free (sparql, TRUE);
+
+  g_free (account_id);
+  g_free (channel_name);
+
+  if (!process_next (importer, files))
+    g_main_loop_quit (loop);
+}
+
+static gboolean
+process_next (PolariTplImporter *importer,
+              GList             *files)
+{
+  GFile *file;
+
+  if (files == NULL)
+    return FALSE;
+
+  file = files->data;
+  files = g_list_delete_link (files, files);
+
+  polari_tpl_importer_import_async (importer, file, NULL, import_ready, files);
+  g_object_unref (file);
+
+  return TRUE;
+}
+
+static void
+files_ready (GObject      *source,
+             GAsyncResult *result,
+             gpointer      user_data)
+{
+  PolariTplImporter *importer = POLARI_TPL_IMPORTER (source);
+  GList *files;
+  GError *error = NULL;
+
+  files = polari_tpl_importer_collect_files_finish (importer, result, &error);
+
+  if (error)
+    {
+      g_printerr ("Failed to collect log files: %s", error->message);
+      g_error_free (error);
+      status = 1;
+    }
+
+  if (!process_next (importer, files))
+    g_main_loop_quit (loop);
+}
+
+static int
+handle_import (int   argc,
+               char *argv[])
+{
+  PolariTplImporter *importer;
+  loop = g_main_loop_new (NULL, FALSE);
+  importer = polari_tpl_importer_new ();
+
+  polari_tpl_importer_collect_files_async (importer, NULL, files_ready, NULL);
+
+  g_main_loop_run (loop);
+
+  g_object_unref (importer);
+  g_main_loop_unref (loop);
+
+  return status;
+}
+
 static int
 handle_query (int argc,
               char *argv[])
@@ -82,6 +232,7 @@ usage ()
   g_printerr ("  polari-log-tool COMMAND ARGS\n");
   g_printerr ("\n");
   g_printerr ("Commands:\n");
+  g_printerr ("  import         Import logs from telepathy-logger\n");
   g_printerr ("  query          Run a sparql query\n");
 }
 
@@ -104,6 +255,8 @@ main (int   argc,
   argc -= 1;
   argv += 1;
 
-  if (g_str_equal (command, "query"))
+  if (g_str_equal (command, "import"))
+    return handle_import (argc, argv);
+  else if (g_str_equal (command, "query"))
     return handle_query (argc, argv);
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]