Browse Source

UDC: copy user id from previous SD version

pull/2/head
Daniel Grunwald 15 years ago
parent
commit
7d4d262b9b
  1. 36
      src/AddIns/Misc/UsageDataCollector/UsageDataCollector.AddIn/AnalyticsMonitor.cs
  2. 55
      src/AddIns/Misc/UsageDataCollector/UsageDataCollector/UsageDataSessionWriter.cs

36
src/AddIns/Misc/UsageDataCollector/UsageDataCollector.AddIn/AnalyticsMonitor.cs

@ -71,6 +71,35 @@ namespace ICSharpCode.UsageDataCollector @@ -71,6 +71,35 @@ namespace ICSharpCode.UsageDataCollector
SharpDevelop.Gui.WorkbenchSingleton.WorkbenchUnloaded += delegate { CloseSession(); };
}
static Guid FindUserId()
{
// Ensure we assign only 1 ID to each user; even when he has multiple UDC databases because there
// are multiple SharpDevelop versions installed. We do this by reading out the userID GUID from
// the existing databases in any neighbor config directory.
string[] otherSharpDevelopVersions;
try {
otherSharpDevelopVersions = Directory.GetDirectories(Path.Combine(PropertyService.ConfigDirectory, ".."));
} catch (IOException) {
otherSharpDevelopVersions = new string[0];
} catch (UnauthorizedAccessException) {
otherSharpDevelopVersions = new string[0];
}
LoggingService.Debug("Looking for existing UDC database in " + otherSharpDevelopVersions.Length + " directories");
foreach (string path in otherSharpDevelopVersions) {
string dbFileName = Path.Combine(path, "usageData.dat");
if (File.Exists(dbFileName)) {
LoggingService.Info("Found existing UDC database: " + dbFileName);
Guid? guid = UsageDataSessionWriter.RetrieveUserId(dbFileName);
if (guid.HasValue) {
LoggingService.Info("Found GUID in existing UDC database: " + guid.Value);
return guid.Value;
}
}
}
LoggingService.Info("Did not find existing UDC database; creating new GUID.");
return Guid.NewGuid();
}
/// <summary>
/// Opens the database connection, updates the database if required.
/// Will start an upload to the server, if required.
@ -82,16 +111,17 @@ namespace ICSharpCode.UsageDataCollector @@ -82,16 +111,17 @@ namespace ICSharpCode.UsageDataCollector
lock (lockObj) {
if (session == null) {
try {
session = new UsageDataSessionWriter(dbFileName);
session = new UsageDataSessionWriter(dbFileName, FindUserId);
} catch (IncompatibleDatabaseException ex) {
if (ex.ActualVersion < ex.ExpectedVersion) {
LoggingService.Info("AnalyticsMonitor: " + ex.Message + ", removing old database");
Guid? oldUserId = UsageDataSessionWriter.RetrieveUserId(dbFileName);
// upgrade database by deleting the old one
TryDeleteDatabase();
try {
session = new UsageDataSessionWriter(dbFileName);
session = new UsageDataSessionWriter(dbFileName, () => (oldUserId ?? FindUserId()));
} catch (IncompatibleDatabaseException ex2) {
LoggingService.Warn("AnalyticsMonitor: Could upgrade database: " + ex2.Message);
LoggingService.Warn("AnalyticsMonitor: Could not upgrade database: " + ex2.Message);
}
} else {
LoggingService.Warn("AnalyticsMonitor: " + ex.Message);

55
src/AddIns/Misc/UsageDataCollector/UsageDataCollector/UsageDataSessionWriter.cs

@ -39,15 +39,15 @@ namespace ICSharpCode.UsageDataCollector @@ -39,15 +39,15 @@ namespace ICSharpCode.UsageDataCollector
/// </summary>
/// <exception cref="IncompatibleDatabaseException">The database version is not compatible with this
/// version of the AnalyticsSessionWriter.</exception>
public UsageDataSessionWriter(string databaseFileName)
public UsageDataSessionWriter(string databaseFileName, Func<Guid> guidProvider = null)
{
SQLiteConnectionStringBuilder conn = new SQLiteConnectionStringBuilder();
conn.Add("Data Source", databaseFileName);
conn.DataSource = databaseFileName;
connection = new SQLiteConnection(conn.ConnectionString);
connection.Open();
try {
InitializeTables();
InitializeTables(guidProvider ?? Guid.NewGuid);
StartSession();
} catch {
@ -92,12 +92,48 @@ namespace ICSharpCode.UsageDataCollector @@ -92,12 +92,48 @@ namespace ICSharpCode.UsageDataCollector
}
}
/// <summary>
/// Retrieves the user ID from the specified database.
/// </summary>
public static Guid? RetrieveUserId(string databaseFileName)
{
SQLiteConnectionStringBuilder conn = new SQLiteConnectionStringBuilder();
conn.DataSource = databaseFileName;
conn.FailIfMissing = true;
try {
using (var connection = new SQLiteConnection(conn.ConnectionString)) {
connection.Open();
return RetrieveUserId(connection);
}
} catch (SQLiteException) {
return null;
}
}
static Guid? RetrieveUserId(SQLiteConnection connection)
{
using (SQLiteCommand cmd = connection.CreateCommand()) {
cmd.CommandText = "SELECT value FROM Properties WHERE name = 'userID';";
string userID = (string)cmd.ExecuteScalar();
if (userID != null) {
try {
return new Guid(userID);
} catch (FormatException) {
} catch (OverflowException) {
// ignore incorrect GUIDs
}
}
return null;
}
}
static readonly Version expectedDBVersion = new Version(1, 0, 1);
/// <summary>
/// Creates or upgrades the database
/// </summary>
void InitializeTables()
void InitializeTables(Func<Guid> userIdProvider)
{
using (SQLiteTransaction transaction = this.connection.BeginTransaction()) {
using (SQLiteCommand cmd = this.connection.CreateCommand()) {
@ -120,10 +156,15 @@ namespace ICSharpCode.UsageDataCollector @@ -120,10 +156,15 @@ namespace ICSharpCode.UsageDataCollector
throw new IncompatibleDatabaseException(expectedDBVersion, actualDBVersion);
}
}
if (RetrieveUserId(this.connection) == null) {
using (SQLiteCommand cmd = this.connection.CreateCommand()) {
cmd.CommandText = @"INSERT OR IGNORE INTO Properties (name, value) VALUES ('userID', ?);";
cmd.Parameters.Add(new SQLiteParameter { Value = userIdProvider().ToString() });
cmd.ExecuteNonQuery();
}
}
using (SQLiteCommand cmd = this.connection.CreateCommand()) {
cmd.CommandText = @"
INSERT OR IGNORE INTO Properties (name, value) VALUES ('userID', '" + Guid.NewGuid().ToString() + @"');
CREATE TABLE IF NOT EXISTS Sessions (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
startTime TEXT NOT NULL,
@ -465,7 +506,7 @@ namespace ICSharpCode.UsageDataCollector @@ -465,7 +506,7 @@ namespace ICSharpCode.UsageDataCollector
}
}
}
/// <summary>
/// Represents a feature use that is currently being written.
/// </summary>

Loading…
Cancel
Save