[chronojump] Chronojump importer imports forceSession. also old files at DB <1.68



commit 27d4860da2e664a89ccdfcd09dfd50b6204de838
Author: Xavier de Blas <xaviblas gmail com>
Date:   Sun Oct 6 18:19:13 2019 +0200

    Chronojump importer imports forceSession. also old files at DB <1.68

 src/chronojump-importer/chronojump_importer.py | 91 ++++++++++++++++++++++++++
 src/chronojump.cs                              |  3 +
 src/chronojumpImporter.cs                      |  7 +-
 src/forceSensor.cs                             | 12 ++++
 src/gui/chronojump.cs                          |  5 ++
 src/sqlite/forceSensor.cs                      | 32 +++++++--
 src/sqlite/main.cs                             |  4 +-
 src/sqlite/runEncoder.cs                       |  6 +-
 8 files changed, 148 insertions(+), 12 deletions(-)
---
diff --git a/src/chronojump-importer/chronojump_importer.py b/src/chronojump-importer/chronojump_importer.py
index 0e9b5631..1373124b 100755
--- a/src/chronojump-importer/chronojump_importer.py
+++ b/src/chronojump-importer/chronojump_importer.py
@@ -417,6 +417,7 @@ class ImportSession:
         self._import_runs()
         self._import_pulse()
         self._import_encoder()
+        self._import_force_sensor()
 
     def _import_session(self):
         """
@@ -655,6 +656,47 @@ class ImportSession:
                                   avoids_duplicate_column=None,
                                   matches_columns=None)
 
+    def _import_force_sensor(self):
+        # Imports ForceSensorExercise
+        # based on encoder exercise code because rest of the code exercises and tests are linked by names
+        # but on encoder and forceSensor is linked by ex.uniqueID
+
+        if(DEBUGTOFILE):
+            debugFile.write(" start _import_force_sensor ")
+
+        forceSensor_exercise_from_forceSensor = self.source_db.read(table_name="ForceSensorExercise",
+                where_condition="ForceSensor.uniqueID={}".format(self.source_session),
+                join_clause="LEFT JOIN ForceSensor ON ForceSensor.exerciseID=ForceSensorExercise.uniqueID",
+                group_by_clause="ForceSensorExercise.uniqueID")
+
+        forceSensor_exercise = Table("forceSensorExercise")
+        forceSensor_exercise.concatenate_table(forceSensor_exercise_from_forceSensor)
+        forceSensor_exercise.remove_duplicates()
+
+
+        self.destination_db.write(table=forceSensor_exercise,
+                                  matches_columns=self.destination_db.column_names("ForceSensorExercise", 
["uniqueID"]))
+
+
+        # Imports ForceSensor
+        forceSensor = self.source_db.read(table_name="ForceSensor",
+                                      where_condition="ForceSensor.sessionID={}".format(self.source_session))
+        forceSensor.update_ids("personID", self.persons77, "uniqueID", "new_uniqueID")
+        forceSensor.update_ids("exerciseID", forceSensor_exercise, "uniqueID", "new_uniqueID")
+        forceSensor.update_session_ids(self.new_session_id)
+
+
+        self._import_forceSensor_files(forceSensor)
+
+        self.destination_db.write(table=forceSensor,
+                                  matches_columns=self.destination_db.column_names("forceSensor", 
skip_columns=["uniqueID", "personID", "sessionID", "exerciseID"]))
+
+        if(DEBUGTOFILE):
+            debugFile.write(" end _import_force_sensor ")
+            debugFile.close()
+
+
+
     @staticmethod
     def _encoder_filename(person_id, original_filename):
         """ original_filename is like 1-Carmelo-89-2014-12-03_12-48-54.txt. It only replaces the person_id 
(1 in this case)"""
@@ -666,6 +708,17 @@ class ImportSession:
     def _encoder_url(session_id, signal_or_curve):
         return os.path.join("encoder", str(session_id), "data", signal_or_curve)
 
+    @staticmethod
+    def _forceSensor_filename(person_id, original_filename):
+        """ original_filename is like 1-Carmelo-89-2014-12-03_12-48-54.csv. It only replaces the person_id 
(1 in this case)"""
+        filename=original_filename.split("-", 1)
+        filename[0] = str(person_id)
+        return "-".join(filename)
+
+    @staticmethod
+    def _forceSensor_url(session_id):
+        return os.path.join("forceSensor", str(session_id))
+
     @staticmethod
     def _normalize_path(path):
         """
@@ -724,6 +777,44 @@ class ImportSession:
             if not os.path.isdir(destination_directory):
                 os.makedirs(destination_directory)
 
+    def _import_forceSensor_files(self, forceSensor_table):
+        if self.source_base_directory is None:
+            # We are skipping to copy the Encoding files. This is used in unit tests.
+            return
+
+        if(DEBUGTOFILE):
+            debugFile.write(" at import_forceSensor_files")
+
+        for row in forceSensor_table:
+            #if(DEBUGTOFILE):
+            #    debugFile.write(" row: ")
+            #    debugFile.write(row.get("url"))
+
+            # Gets information from row
+            person_id = row.get("personID")
+            original_filename = row.get("filename")
+            original_url = self._normalize_path(row.get("url"))
+            session_id = row.get("sessionID")
+
+            # Prepares the new filename and destination_url
+            filename=self._forceSensor_filename(person_id, original_filename)
+            destination_url = self._forceSensor_url(session_id)
+
+            # Sets it to the row
+            row.set("filename", filename)
+            row.set("url", destination_url)
+
+            # Copies the files to the new place
+            destination_directory = os.path.join(self.destination_path, "..", "..", destination_url)
+            destination_directory = os.path.abspath(destination_directory)  # os.makedirs() can't handle 
directories with ".."
+
+            destination_filename = os.path.join(destination_directory, filename)
+            source_file = os.path.join(self.source_base_directory, original_url, original_filename)
+
+            if not os.path.isdir(destination_directory):
+                os.makedirs(destination_directory)
+
+            shutil.copy(source_file, destination_filename)
 
 
 def json_information(database_path):
diff --git a/src/chronojump.cs b/src/chronojump.cs
index 029c0395..78e1c96b 100644
--- a/src/chronojump.cs
+++ b/src/chronojump.cs
@@ -373,6 +373,9 @@ public class ChronoJump
                                messageToShowOnBoot += Catalog.GetString("All DJ jumps have been renamed as 
'DJna' (Drop Jumps with No Arms).") + "\n\n"+ 
                                        Catalog.GetString("If your Drop Jumps were executed using the arms, 
please rename them manually as 'DJa'.") + "\n";
 
+                       SqliteForceSensor.DirToImport = Util.GetForceSensorDir();
+                       SqliteRunEncoder.DirToImport = Util.GetRunEncoderDir();
+
                        bool softwareIsNew = Sqlite.ConvertToLastChronojumpDBVersion();
                        updatingDB = false;
                        
diff --git a/src/chronojumpImporter.cs b/src/chronojumpImporter.cs
index d8e8ec16..9219eb51 100644
--- a/src/chronojumpImporter.cs
+++ b/src/chronojumpImporter.cs
@@ -152,7 +152,7 @@ class ChronojumpImporter
                                "Please, update the running Chronojump."));
                } else if (destinationDatabaseVersionNum > sourceDatabaseVersionNum) {
                        LogB.Debug ("chronojump-importer version before update: ", 
sourceDatabaseVersion.output);
-                       updateDatabase (temporarySourceFile);
+                       updateDatabase (temporarySourceFile, Path.GetDirectoryName(sourceFile));
                        string versionAfterUpdate = getDatabaseVersionFromFile (temporarySourceFile).output;
                        LogB.Debug ("chronojump-importer version after update: ", versionAfterUpdate);
                }
@@ -188,7 +188,7 @@ class ChronojumpImporter
                return result;
        }
 
-       private static void updateDatabase(string databaseFile)
+       private static void updateDatabase(string databaseFile, string sourceDir)
        {
                StaticClassState classOriginalState = new StaticClassState (typeof (Sqlite));
 
@@ -200,6 +200,9 @@ class ChronojumpImporter
                Sqlite.setSqlFilePath (databaseFile);
                Sqlite.Connect ();
 
+               SqliteForceSensor.DirToImport = Path.Combine(sourceDir, "..", "forceSensor");
+               SqliteRunEncoder.DirToImport = Path.Combine(sourceDir, "..", "raceAnalyzer");
+
                Sqlite.ConvertToLastChronojumpDBVersion ();
 
                classOriginalState.writeAttributes (classOriginalState);
diff --git a/src/forceSensor.cs b/src/forceSensor.cs
index a562678a..8e0e200f 100644
--- a/src/forceSensor.cs
+++ b/src/forceSensor.cs
@@ -320,6 +320,18 @@ public class ForceSensorExercise
                        Util.BoolToInt(elastic).ToString();
        }
 
+       public string ToSQLInsertString_DB_1_68()
+       {
+               string uniqueIDStr = "NULL";
+               if(uniqueID != -1)
+                       uniqueIDStr = uniqueID.ToString();
+
+               return
+                       uniqueIDStr + ", \"" + name + "\", " + percentBodyWeight + ", \"" +
+                       resistance + "\", " + angleDefault + ", \"" + description + "\", " +
+                       Util.BoolToInt(tareBeforeCapture).ToString();
+       }
+
        public bool Changed(ForceSensorExercise newEx)
        {
                if(
diff --git a/src/gui/chronojump.cs b/src/gui/chronojump.cs
index 8dee32de..580ffded 100644
--- a/src/gui/chronojump.cs
+++ b/src/gui/chronojump.cs
@@ -2502,6 +2502,11 @@ public partial class ChronoJumpWindow
                        combo_select_runs_interval.Active = 0;
                        combo_result_runs_interval.Active = 0;
 
+                       // TODO: we need this on encoder or is already done at reloadSession???
+                       //createEncoderCombos();
+
+                       // forceSensor
+                       fillForceSensorExerciseCombo("");
 
                        //update stats combos
                        updateComboStats ();
diff --git a/src/sqlite/forceSensor.cs b/src/sqlite/forceSensor.cs
index f626faa2..5c00d76e 100644
--- a/src/sqlite/forceSensor.cs
+++ b/src/sqlite/forceSensor.cs
@@ -224,13 +224,14 @@ class SqliteForceSensor : Sqlite
                return array;
        }
 
+       public static string DirToImport;
+
        protected internal static void import_from_1_68_to_1_69() //database is opened
        {
                LogB.PrintAllThreads = true; //TODO: remove this
                LogB.Information("at import_from_1_68_to_1_69()");
-               //LogB.Information("Sqlite isOpened: " + Sqlite.IsOpened.ToString());
 
-               string forceSensorDir = Util.GetForceSensorDir();
+               string forceSensorDir = DirToImport;
 
                int unknownExerciseID = Sqlite.ExistsAndGetUniqueID(true, Constants.ForceSensorExerciseTable, 
Catalog.GetString("Unknown"));
 
@@ -243,7 +244,6 @@ class SqliteForceSensor : Sqlite
                                string fileWithoutExtension = 
Util.RemoveExtension(Util.GetLastPartOfPath(file.Name));
                                ForceSensorLoadTryToAssignPersonAndMore fslt =
                                        new ForceSensorLoadTryToAssignPersonAndMore(true, 
fileWithoutExtension, Convert.ToInt32(session.Name));
-                               //TODO: no se si session.ToString() és la manera de saber el nom del 
DirectoryInfo
 
                                Person p = fslt.GetPerson();
                                if(p.UniqueID == -1)
@@ -265,8 +265,8 @@ class SqliteForceSensor : Sqlite
                                        if(unknownExerciseID == -1)
                                        {
                                                ForceSensorExercise fse = new ForceSensorExercise (-1, 
Catalog.GetString("Unknown"), 0, "", 0, "", false, false, false);
-                                               unknownExerciseID = SqliteForceSensorExercise.Insert(true, 
fse);
-                                               //note this import already goes to 1.73, then that import 
will produce a catch
+                                               //note we are on 1_68 so we need this import method
+                                               unknownExerciseID = 
SqliteForceSensorExercise.InsertAtDB_1_68(true, fse);
                                        }
 
                                        exerciseID = unknownExerciseID;
@@ -365,6 +365,28 @@ class SqliteForceSensorExercise : Sqlite
                return myLast;
        }
 
+       public static int InsertAtDB_1_68 (bool dbconOpened, ForceSensorExercise ex)
+       {
+               if(! dbconOpened)
+                       Sqlite.Open();
+
+               dbcmd.CommandText = "INSERT INTO " + table +
+                               " (uniqueID, name, percentBodyWeight, resistance, angleDefault, " +
+                               " description, tareBeforeCapture)" +
+                               " VALUES (" + ex.ToSQLInsertString_DB_1_68() + ")";
+               LogB.SQL(dbcmd.CommandText.ToString());
+               dbcmd.ExecuteNonQuery();
+
+               string myString = @"select last_insert_rowid()";
+               dbcmd.CommandText = myString;
+               int myLast = Convert.ToInt32(dbcmd.ExecuteScalar()); // Need to type-cast since 
`ExecuteScalar` returns an object.
+
+               if(! dbconOpened)
+                       Sqlite.Close();
+
+               return myLast;
+       }
+
        public static void Update (bool dbconOpened, ForceSensorExercise ex)
        {
                if(! dbconOpened)
diff --git a/src/sqlite/main.cs b/src/sqlite/main.cs
index ac07e9e3..612c5906 100644
--- a/src/sqlite/main.cs
+++ b/src/sqlite/main.cs
@@ -303,9 +303,7 @@ class Sqlite
 
        public static string CurrentVersion
        {
-               set {
-                       currentVersion = value;
-               }
+               set { currentVersion = value; }
        }
 
        public static void setSqlFilePath(string filePath)
diff --git a/src/sqlite/runEncoder.cs b/src/sqlite/runEncoder.cs
index faf1da66..a723f083 100644
--- a/src/sqlite/runEncoder.cs
+++ b/src/sqlite/runEncoder.cs
@@ -221,14 +221,16 @@ class SqliteRunEncoder : Sqlite
                return array;
        }
 
+
+       public static string DirToImport;
+
        protected internal static void import_from_1_70_to_1_71() //database is opened
        {
                LogB.PrintAllThreads = true; //TODO: remove this
                LogB.Information("at import_from_1_70_to_1_71()");
-               //LogB.Information("Sqlite isOpened: " + Sqlite.IsOpened.ToString());
 
                bool importedSomething = false;
-               string raceAnalyzerDir = Util.GetRunEncoderDir();
+               string raceAnalyzerDir = DirToImport;
                DirectoryInfo [] sessions = new DirectoryInfo(raceAnalyzerDir).GetDirectories();
                foreach (DirectoryInfo session in sessions) //session.Name will be the UniqueID
                {


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