Skip to content

Commit fdc15c7

Browse files
committed
[log-selection] add add-to-favorites funcionality
1 parent d52a265 commit fdc15c7

10 files changed

Lines changed: 128 additions & 4 deletions

File tree

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
88
## [0.2.2] PENDING
99

1010
### Added
11+
- **[Packages]:** addsdb package
12+
- **[log-selection]:** add add-to-favorites funcionality
1113

1214
### Changed
1315
- **[app-window]:** moves app window to the center of the screen

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,11 @@
8080
"node": ">=10.9.0"
8181
},
8282
"dependencies": {
83+
"@types/pouchdb": "^6.4.0",
8384
"electron-log": "^4.2.2",
8485
"electron-updater": "^4.1.2",
8586
"ng-zorro-antd": "^8.3.1",
86-
"node-powershell": "^4.0.0"
87+
"node-powershell": "^4.0.0",
88+
"pouchdb": "^7.2.2"
8789
}
8890
}

src/app/components/event-logs-management-dialog/log-selection/log-selection.component.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@
2323
[nzLoading]="loading"
2424
>
2525
<ng-template #itemRef let-item>
26-
<li nz-list-item [nzActions]="[deleteAction, selectAction]" [nzContent]="item.log + ' ('+ item.entries +')'" nzNoFlex></li>
26+
<li nz-list-item [nzActions]="[favorite, deleteAction, selectAction]" [nzContent]="item.log + ' ('+ item.entries +')'" nzNoFlex></li>
27+
<ng-template #favorite>
28+
<img
29+
(click)="UiOnFavoriteClicked(item)"
30+
[src]="item.isFavorite ? './assets/icons/star-filled.png': './assets/icons/star-empty.png'"
31+
style="width: 20px; height: 20px;"/>
32+
</ng-template>
2733
<ng-template #deleteAction><a (click)="UiOnRemoveItemClicked(item)" style="color: #E89999;">remove</a></ng-template>
2834
<ng-template #selectAction><a (click)="UiOnItemSelected(item)">select</a></ng-template>
2935
</ng-template>

src/app/components/event-logs-management-dialog/log-selection/log-selection.component.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { map, debounceTime } from 'rxjs/operators';
77
import { PowershellCommands } from '../../../services/powershell/powershell-commands';
88
import { GlobalUtils } from '../../../services/global-utils';
99
import { AppLogger } from '../../../services/AppLogger';
10+
import { AppDatabase } from '../../../services/AppDatabase';
11+
import { DbEventLog } from '../../../types/database/DbEventLog';
1012
//#endregion imports
1113

1214
@Component({
@@ -32,6 +34,7 @@ export class LogSelectionComponent implements OnInit, OnDestroy {
3234

3335
public loading: boolean= true;
3436
private _logers: EventLog[] = [];
37+
private _dbLoggers: DbEventLog[] = [];
3538
public logers: EventLog[] = [];
3639
private _searchValue: string = '';
3740
public remoteComputer: string = '';
@@ -40,6 +43,7 @@ export class LogSelectionComponent implements OnInit, OnDestroy {
4043

4144
//#region Component Methods
4245
ngOnInit(): void {
46+
AppDatabase.getInstance().getAllLoggs().then(items => this._dbLoggers = items);
4347
this._search();
4448
this._setupSearch();
4549
}
@@ -76,7 +80,9 @@ export class LogSelectionComponent implements OnInit, OnDestroy {
7680
this.loading = true;
7781
PowershellCommands.getEventLogs(this.remoteComputer)
7882
.then((evs: EventLog[]) => {
79-
this._logers = evs;
83+
this._logers = evs
84+
.map(logger => this._dbLoggers.find(dbLogger => dbLogger.IsSame(logger)) || logger)
85+
.sort((it1, it2) => it1.isFavorite == it2.isFavorite ? 0 : (it1.isFavorite ? -1 : 1));
8086
this.loading = false;
8187
this._applyFiltering();
8288
})
@@ -145,6 +151,25 @@ export class LogSelectionComponent implements OnInit, OnDestroy {
145151
});
146152
});
147153
}
148-
//#endregion UiCallbacks
154+
155+
public UiOnFavoriteClicked(item: EventLog): void {
156+
const dbItem = new DbEventLog(item);
157+
dbItem.isFavorite = !item.isFavorite;
158+
159+
AppDatabase
160+
.getInstance()
161+
.upsertItem(dbItem)
162+
.then((res :boolean) => {
163+
if (!res) return;
164+
165+
item.isFavorite = !item.isFavorite;
166+
if (this._dbLoggers.find(dbLogger => dbLogger.IsSame(dbItem)) == null) {
167+
this._dbLoggers.push(dbItem);
168+
}
169+
else {
170+
this._dbLoggers = this._dbLoggers.map(logger => logger.IsSame(dbItem) ? dbItem : logger);
171+
}
172+
});
173+
}
149174

150175
}

src/app/services/AppDatabase.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { AppLogger } from "./AppLogger";
2+
import { DbEventLog } from "../types/database/DbEventLog";
3+
4+
//import PouchDB from 'pouchdb';
5+
//const PouchDB = require('pouchdb').default;
6+
import PouchDB from 'pouchdb';
7+
8+
export class AppDatabase {
9+
//#region Singleton
10+
private static _instance;
11+
public static getInstance(): AppDatabase {
12+
if (this._instance == null) this._instance = new AppDatabase();
13+
return this._instance;
14+
}
15+
16+
private constructor() {
17+
this._db = new PouchDB('DbEventLog');
18+
}
19+
//#endregion Singleton
20+
21+
22+
//#region Properties
23+
private _db: PouchDB.Database;
24+
//#endregion Properties
25+
26+
27+
//#region Public Api
28+
public upsertItem(item: any) : Promise<boolean> {
29+
return this._db
30+
.get(item._id)
31+
.catch(e => {
32+
if (e.status == 404) return null
33+
throw e;
34+
})
35+
.then(existingItem => {
36+
if (existingItem) {
37+
item._rev = existingItem._rev;
38+
}
39+
return this._db.put(item);
40+
})
41+
.then(res => true)
42+
.catch(e => {
43+
AppLogger.getDebug().logErrorMessage(`Error while executing upsertItem. ItemData:\n${JSON.stringify(item)}`);
44+
AppLogger.getDebug().logError(e);
45+
return false;
46+
});
47+
}
48+
49+
50+
public getAllLoggs(): Promise<DbEventLog[]> {
51+
return this._db
52+
.allDocs({include_docs: true})
53+
.then(res => res.rows.map(row => new DbEventLog(row.doc)))
54+
.catch(e => {
55+
AppLogger.getDebug().logErrorMessage(`Error while executing getAllLoggs`);
56+
AppLogger.getDebug().logError(e);
57+
return [];
58+
});
59+
}
60+
//#endregion Public Api
61+
}

src/app/types/EventLog.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ export class EventLog {
33

44
public log: string;
55
public entries: number;
6+
public friendlyName: string;
7+
public isFavorite: boolean;
8+
9+
constructor(instance? :EventLog) {
10+
if (instance != null) {
11+
this.computerName = instance.computerName;
12+
this.log = instance.log;
13+
this.entries = instance.entries;
14+
this.friendlyName = instance.friendlyName;
15+
this.isFavorite = instance.isFavorite;
16+
}
17+
}
618

719
//#region Ui Methods
820
public UiTabLabel(): string {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { EventLog } from "../EventLog";
2+
3+
export class DbEventLog extends EventLog {
4+
public _id: string;
5+
6+
constructor(instance? :any) {
7+
super(instance);
8+
this.setId();
9+
}
10+
11+
12+
public setId(): void {
13+
this._id = `${this.log}#${this.computerName || ''}`;
14+
}
15+
}

src/assets/icons/star-empty.png

2.33 KB
Loading

src/assets/icons/star-filled.png

4.64 KB
Loading

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"emitDecoratorMetadata": true,
99
"experimentalDecorators": true,
1010
"target": "es5",
11+
"allowSyntheticDefaultImports": true,
1112
"typeRoots": [
1213
"node_modules/@types"
1314
],

0 commit comments

Comments
 (0)