-
-
Notifications
You must be signed in to change notification settings - Fork 341
Add EvidenceStore for tracking activity evidence (#496) #501
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| import { YamlService } from '../service/yaml-loader/yaml-loader.service'; | ||
| import { Uuid } from './types'; | ||
|
|
||
| export interface EvidenceAttachment { | ||
| type: string; // e.g. 'document', 'image', 'link' | ||
| externalLink: string; // URL | ||
| } | ||
|
|
||
| export interface EvidenceEntry { | ||
| id: string; // stable UUID for this entry | ||
| teams: string[]; | ||
| title: string; | ||
| evidenceRecorded: string; // ISO date string | ||
| reviewer?: string; | ||
| description: string; | ||
| attachment?: EvidenceAttachment[]; | ||
| } | ||
|
|
||
| export type EvidenceData = Record<Uuid, EvidenceEntry[]>; | ||
|
|
||
| const LOCALSTORAGE_KEY: string = 'evidence'; | ||
|
|
||
| export class EvidenceStore { | ||
| private yamlService: YamlService = new YamlService(); | ||
| private _evidence: EvidenceData = {}; | ||
|
|
||
| // ─── Lifecycle ──────────────────────────────────────────── | ||
|
|
||
| public initFromLocalStorage(): void { | ||
| const stored = this.retrieveStoredEvidence(); | ||
| if (stored) { | ||
| this.addEvidenceData(stored); | ||
| } | ||
| } | ||
|
|
||
| // ─── Accessors ──────────────────────────────────────────── | ||
|
|
||
| public getEvidenceData(): EvidenceData { | ||
| return this._evidence; | ||
| } | ||
|
|
||
| public getEvidence(activityUuid: Uuid): EvidenceEntry[] { | ||
| return this._evidence[activityUuid] || []; | ||
| } | ||
|
|
||
| public hasEvidence(activityUuid: Uuid): boolean { | ||
| return (this._evidence[activityUuid]?.length || 0) > 0; | ||
| } | ||
|
|
||
| public getEvidenceCount(activityUuid: Uuid): number { | ||
| return this._evidence[activityUuid]?.length ?? 0; | ||
| } | ||
|
|
||
| public getTotalEvidenceCount(): number { | ||
| let count = 0; | ||
| for (const uuid in this._evidence) { | ||
| count += this._evidence[uuid].length; | ||
| } | ||
| return count; | ||
| } | ||
|
|
||
| public getActivityUuidsWithEvidence(): Uuid[] { | ||
| return Object.keys(this._evidence).filter(uuid => this._evidence[uuid].length > 0); | ||
| } | ||
|
|
||
| // ─── Mutators ──────────────────────────────────────────── | ||
|
|
||
| public addEvidenceData(newEvidence: EvidenceData): void { | ||
| if (!newEvidence) return; | ||
|
|
||
| for (const activityUuid in newEvidence) { | ||
| if (!this._evidence[activityUuid]) { | ||
| this._evidence[activityUuid] = []; | ||
| } | ||
|
|
||
| const newEntries = newEvidence[activityUuid]; | ||
| if (Array.isArray(newEntries)) { | ||
| for (const entry of newEntries) { | ||
| const existingIndex = this._evidence[activityUuid].findIndex(e => e.id === entry.id); | ||
| if (existingIndex !== -1) { | ||
| this._evidence[activityUuid][existingIndex] = entry; | ||
| } else { | ||
| this._evidence[activityUuid].push(entry); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public replaceEvidenceData(data: EvidenceData): void { | ||
| this._evidence = data; | ||
| this.saveToLocalStorage(); | ||
| } | ||
|
|
||
| public addEvidence(activityUuid: Uuid, entry: EvidenceEntry): void { | ||
| if (!this._evidence[activityUuid]) { | ||
| this._evidence[activityUuid] = []; | ||
| } | ||
| this._evidence[activityUuid].push(entry); | ||
| this.saveToLocalStorage(); | ||
| } | ||
|
|
||
| public updateEvidence( | ||
| activityUuid: Uuid, | ||
| entryId: string, | ||
| updatedEntry: Partial<EvidenceEntry> | ||
| ): void { | ||
| const entries = this._evidence[activityUuid]; | ||
| if (!entries) { | ||
| console.warn(`No evidence found for activity ${activityUuid}`); | ||
| return; | ||
| } | ||
| const index = entries.findIndex(e => e.id === entryId); | ||
| if (index === -1) { | ||
| console.warn(`Cannot find evidence with id ${entryId} for activity ${activityUuid}`); | ||
| return; | ||
| } | ||
| // Immutable update for Angular change detection | ||
| entries[index] = { ...entries[index], ...updatedEntry }; | ||
| this.saveToLocalStorage(); | ||
| } | ||
|
|
||
| public deleteEvidence(activityUuid: Uuid, entryId: string): void { | ||
| const entries = this._evidence[activityUuid]; | ||
| if (!entries) { | ||
| console.warn(`No evidence found for activity ${activityUuid}`); | ||
| return; | ||
| } | ||
| const index = entries.findIndex(e => e.id === entryId); | ||
| if (index === -1) { | ||
| console.warn(`Cannot find evidence with id ${entryId} for activity ${activityUuid}`); | ||
| return; | ||
| } | ||
| entries.splice(index, 1); | ||
|
|
||
| if (entries.length === 0) { | ||
| delete this._evidence[activityUuid]; | ||
| } | ||
| this.saveToLocalStorage(); | ||
| } | ||
|
|
||
| public renameTeam(oldName: string, newName: string): void { | ||
| console.log(`Renaming team '${oldName}' to '${newName}' in evidence store`); | ||
| for (const uuid in this._evidence) { | ||
| this._evidence[uuid].forEach(entry => { | ||
| entry.teams = entry.teams.map(t => (t === oldName ? newName : t)); | ||
| }); | ||
| } | ||
| this.saveToLocalStorage(); | ||
| } | ||
|
|
||
| // ─── Serialization ────────────────────────────────────── | ||
|
|
||
| public asYamlString(): string { | ||
| return this.yamlService.stringify({ evidence: this._evidence }); | ||
| } | ||
|
|
||
| public saveToLocalStorage(): void { | ||
| const yamlStr = this.asYamlString(); | ||
| localStorage.setItem(LOCALSTORAGE_KEY, yamlStr); | ||
| } | ||
|
|
||
| public deleteBrowserStoredEvidence(): void { | ||
| console.log('Deleting evidence from browser storage'); | ||
| localStorage.removeItem(LOCALSTORAGE_KEY); | ||
| } | ||
|
|
||
| public retrieveStoredEvidenceYaml(): string | null { | ||
| return localStorage.getItem(LOCALSTORAGE_KEY); | ||
| } | ||
|
|
||
| public retrieveStoredEvidence(): EvidenceData | null { | ||
| const yamlStr = this.retrieveStoredEvidenceYaml(); | ||
| if (!yamlStr) return null; | ||
|
|
||
| const parsed = this.yamlService.parse(yamlStr); | ||
| return parsed?.evidence ?? null; | ||
| } | ||
|
|
||
| // ─── Helpers ───────────────────────────────────────────── | ||
|
|
||
| private isDuplicateEntry(activityUuid: Uuid, entry: EvidenceEntry): boolean { | ||
| const existing = this._evidence[activityUuid]; | ||
| if (!existing) return false; | ||
| return existing.some(e => e.id === entry.id); | ||
| } | ||
|
|
||
| public static todayDateString(): string { | ||
| const now = new Date(); | ||
| return now.toISOString().substring(0, 10); | ||
| } | ||
|
|
||
| // to be used when creating new evidence entries to ensure they have a stable UUID | ||
| public static generateId(): string { | ||
| return crypto.randomUUID?.() ?? `${Date.now()}-${Math.random().toString(36).substring(2, 9)}`; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -84,6 +84,14 @@ export class LoaderService { | |||||
| this.dataStore.addProgressData(browserProgress?.progress); | ||||||
| } | ||||||
|
|
||||||
| // Load evidence data | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current setup will prioritize YAML fil over loaclStorage. I think locally changed evidence should take priority over server side evidence. Otherwise you will not be able to see your own changes if you reload the page. |
||||||
| const evidenceData = await this.loadEvidence(this.dataStore.meta); | ||||||
| this.dataStore.addEvidenceData(evidenceData.evidence); | ||||||
| this.dataStore.evidenceStore?.initFromLocalStorage(); | ||||||
|
|
||||||
| // DEBUG ONLY | ||||||
| console.log('Merged EvidenceStore:', this.dataStore.evidenceStore?.getEvidenceData()); | ||||||
|
|
||||||
| console.log(`${perfNow()}: YAML: All YAML files loaded`); | ||||||
|
|
||||||
| return this.dataStore; | ||||||
|
|
@@ -134,6 +142,10 @@ export class LoaderService { | |||||
| meta.activityFiles = meta.activityFiles.map(file => | ||||||
| this.yamlService.makeFullPath(file, this.META_FILE) | ||||||
| ); | ||||||
| if (!meta.teamProgressFile) { | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| throw Error("The meta.yaml has no 'teamEvidenceFile' to be loaded"); | ||||||
| } | ||||||
| meta.teamEvidenceFile = this.yamlService.makeFullPath(meta.teamEvidenceFile, this.META_FILE); | ||||||
|
|
||||||
| if (this.debug) console.log(`${perfNow()} s: meta loaded`); | ||||||
| console.log(`${perfNow()} s: Loaded teams: ${meta.teams.join(', ')}`); | ||||||
|
|
@@ -145,6 +157,11 @@ export class LoaderService { | |||||
| return this.yamlService.loadYaml(meta.teamProgressFile); | ||||||
| } | ||||||
|
|
||||||
| private async loadEvidence(meta: MetaStore): Promise<{ evidence: any }> { | ||||||
| if (this.debug) console.log(`${perfNow()}s: Loading Team Evidence: ${meta.teamEvidenceFile}`); | ||||||
| return this.yamlService.loadYaml(meta.teamEvidenceFile); | ||||||
| } | ||||||
|
|
||||||
| private async loadActivities(meta: MetaStore): Promise<ActivityStore> { | ||||||
| const activityStore = new ActivityStore(); | ||||||
| const errors: string[] = []; | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # Export team evidence from the browser, and replace this file | ||
| evidence: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah. I was not clear in my example.
The UUID on top level under the root node
evidence:I was thinking would be the activity-uuid.So unless we want to create an UUID as an ID for the evidence entry itself, I don't think this will be needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. I did understand in the issue example that the top level UUID was the activity-uuid.
It was my mistake that I did not point this extra design choice of the data-structure in the PR description.
Let me calrify this here.
Since we are not going to tie evidence with progress (as specifically mentioned in the issue's comments). For the same team and the same activity we could have more than one EvidenceEntry (say entries for starting and partly implementing but not fully implementing, 2 here).
For this I went for a unique id for each EvidenceEntry to identify them separately.
I have updated the Format in the original PR description which showcases this. I just copy-pasted the Format form the issue earlier. my bad.!
If this design choice seems unnecessary, please suggest another way to uniquely identify multiple EvidenceEntry entries for the same team and activity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay. So the top-level uuid is an
activityId, and theidproperty is anevidenceId.That makes sense. And the second ID is important to handle evidence from yaml and localStorage. Thumbs up.
[] However, maybe we should create a
type ActivityId = Uuidas well astype EvidenceId = Uuidto more easily distinguish them. (TheUuidtype has only one purpose today.)