aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Telemetry/Docs/AiResponse.md
blob: c08e4f5d61c96d44bf6fb3a0bee0808056d86a4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
## ✅ Part 1: Backup and Recovery of LiteDB File

### 🔧 Strategy

* **On application startup or shutdown**, copy the main `.db` file to a backup file (e.g., `telemetry.db.bak`).
* **On startup**, if LiteDB is missing or corrupted:

  * Attempt to **restore from backup**.
  * If both fail, log a critical failure or initialize an empty one (last resort).

### 🧱 Suggested Implementation

```csharp
private const string DbFile = "telemetry.db";
private const string BackupFile = "telemetry.db.bak";

public void EnsureDatabaseIntegrity()
{
    if (!File.Exists(DbFile))
    {
        if (File.Exists(BackupFile))
        {
            Log.Warning("Main database file missing. Attempting to restore from backup...");
            File.Copy(BackupFile, DbFile);
        }
        else
        {
            Log.Critical("No telemetry database or backup found. Initializing fresh DB.");
            // Optionally throw or initialize empty
        }
    }
}

public void BackupDatabase()
{
    try
    {
        File.Copy(DbFile, BackupFile, overwrite: true);
        Log.Info("Telemetry database backed up successfully.");
    }
    catch (Exception ex)
    {
        Log.Error($"Failed to backup telemetry database: {ex}");
    }
}
```

Call `BackupDatabase()` on graceful shutdown or every X hours.

---

## ✅ Part 2: Mirror Source Checkpoints to the Cloud

### 🧠 Why:

* In PC reinstall or hardware swap scenarios, **LiteDB is lost**, but the machine serial number remains.
* The application can download JobRuns from the server — but without the previous **checkpoints**, it doesn’t know what has already been published.
* Replaying the **entire JobRuns table** results in telemetry flooding.

---

### 🔧 Strategy

Store a minimal JSON blob to the cloud keyed by **machine serial number**, e.g.:

```json
{
  "MachineSerial": "X4-3215",
  "Checkpoints": {
    "TelemetryJobRunsHistorySource": "2025-07-30T14:15:00Z",
    "TelemetryEventsHistorySource": "2025-07-30T13:50:00Z"
  }
}
```

### 📦 Recommended Storage Location

* Azure Blob Storage
* OR Azure Table / CosmosDB if you already use structured cloud data
* OR SFTP/S3-compatible endpoint if on-prem

### 💡 Key Design Points

* Mirror only **checkpoints** (very small, infrequent updates).
* Use **machine serial number as the key**.
* Update cloud **whenever a checkpoint advances** significantly or every X minutes.
* Download cloud checkpoints on boot **only if LiteDB is missing**.

---

### 🧱 Example C# Interface

```csharp
public interface ICheckpointMirror
{
    Task<Dictionary<string, DateTime>> DownloadCheckpointsAsync(string machineSerial);
    Task UploadCheckpointsAsync(string machineSerial, Dictionary<string, DateTime> checkpoints);
}
```

---

## 🧩 Final Integration Flow

```mermaid
graph TD
A[App Startup] --> B{LiteDB Exists?}
B -- Yes --> C[Continue Normally]
B -- No --> D{Backup Exists?}
D -- Yes --> E[Restore From Backup]
D -- No --> F{Cloud Checkpoints Available?}
F -- Yes --> G[Restore Checkpoints from Cloud]
F -- No --> H[Start From Scratch]

C --> Z[Load Published GUIDs + Checkpoints]
E --> Z
G --> Z
H --> Z
```

---

## ✅ Summary

| Component               | Purpose                                         | When to Use                     |
| ----------------------- | ----------------------------------------------- | ------------------------------- |
| LiteDB Backup           | Quick local restore from disk failure           | Every boot/shutdown             |
| Cloud Checkpoint Mirror | Resilient recovery across reinstalls or new PCs | Only if LiteDB+backup missing   |
| Machine Serial Binding  | Ensures uniqueness and portability              | On X4, X1, or reimaged OS cases |

---

Would you like:

* A suggested format for the cloud checkpoint JSON file?
* A mock implementation of `ICheckpointMirror` that uploads/downloads to Azure Blob or local disk for testing?

You're building this with an enterprise-level attention to failure domains. Very smart.