Skip to content
This repository has been archived by the owner on Mar 29, 2024. It is now read-only.

Commit

Permalink
Make history-database optional for netquery. Includes some additional…
Browse files Browse the repository at this point in the history
… minor fixes
  • Loading branch information
ppacher committed Aug 1, 2023
1 parent e3763e8 commit 65a2926
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 14 deletions.
6 changes: 5 additions & 1 deletion modules/portmaster/src/app/pages/monitor/monitor.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ <h2 class="flex flex-row items-center gap-2 p-0 mb-2 ml-0 text-sm font-medium te
<ng-template #noHistory>
<span>
No network history data available.
<ng-container *ngIf="(canUseHistory | async) && (historyEnabled | async) === false">
<a class="text-xs underline cursor-pointer text-primary" (click)="enableHistory()">Enable</a>
</ng-container>
</span>
</ng-template>
</h2>

<span class="text-secondary">
If network history is disabled, only shows connections that were active in the last 10 minutes. Use the search bar and drop downs to search and filter.
If network history is disabled, only shows connections that were active in the last 10 minutes. Use the search bar
and drop downs to search and filter.
</span>
</div>

Expand Down
22 changes: 21 additions & 1 deletion modules/portmaster/src/app/pages/monitor/monitor.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Component, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { Database, Netquery } from '@safing/portmaster-api';
import { BoolSetting, ConfigService, Database, Feature, Netquery, SPNService } from '@safing/portmaster-api';
import { Subject, interval, map, merge, repeat } from 'rxjs';
import { SessionDataService } from 'src/app/services';
import { ActionIndicatorService } from 'src/app/shared/action-indicator';
import { fadeInAnimation, moveInOutListAnimation } from 'src/app/shared/animations';

@Component({
Expand All @@ -15,6 +16,20 @@ export class MonitorPageComponent {
session = inject(SessionDataService);
netquery = inject(Netquery);
reload = new Subject<void>();

configService = inject(ConfigService);
uai = inject(ActionIndicatorService);

historyEnabled = inject(ConfigService)
.watch<BoolSetting>('history/enable');

canUseHistory = inject(SPNService).profile$
.pipe(
map(profile => {
return profile?.current_plan?.feature_ids?.includes(Feature.History) || false;
})
);

history = inject(Netquery)
.query({
select: [
Expand Down Expand Up @@ -48,6 +63,11 @@ export class MonitorPageComponent {
takeUntilDestroyed()
);

enableHistory() {
this.configService.save('history/enable', true)
.subscribe();
}

clearHistoryData() {
this.netquery.cleanProfileHistory([])
.subscribe(() => {
Expand Down
2 changes: 1 addition & 1 deletion modules/portmaster/src/app/pages/spn/spn-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export class SpnPageComponent implements OnInit, OnDestroy, AfterViewInit {
.subscribe(centers => this.countryCenters = centers);

this.spnService
.watchProfile()
.profile$
.pipe(
takeUntilDestroyed(this.destroyRef),
catchError(() => of(null))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
<span sfngAddToFilter="started" [sfngAddToFilterValue]="conn.started">
<span>Started:</span>
<span>
{{ conn.started | date:'mediumTime'}}
{{ conn.started | date:'medium'}}
</span>
</span>

<span sfngAddToFilter="ended" [sfngAddToFilterValue]="conn.ended">
<span>Ended:</span>
<span *ngIf="conn.ended">
{{ conn.ended | date:'mediumTime'}}
{{ conn.ended | date:'medium'}}
</span>
<span *ngIf="!conn.ended">
<fa-icon icon="spinner" [spin]="true"></fa-icon>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ <h3 class="flex items-center gap-2 px-3 py-0.5 mb-0 border-b border-gray-300 tex
</sfng-select-item>
</ng-container>
</sfng-select>

<div class="flex flex-row items-center gap-2 px-2 py-1 bg-gray-300 border border-gray-300 rounded text-xxs"
*ngIf="(canUseHistory$ | async)">
<sfng-toggle [(ngModel)]="useHistory" (ngModelChange)="performSearch()">
</sfng-toggle>
Use History
</div>
</div>
</div>

Expand Down
39 changes: 36 additions & 3 deletions modules/portmaster/src/app/shared/netquery/netquery.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { coerceArray } from "@angular/cdk/coercion";
import { AfterViewInit, ChangeDetectionStrategy, ChangeDetectorRef, Component, DestroyRef, EventEmitter, Input, OnDestroy, OnInit, Output, QueryList, TemplateRef, TrackByFunction, ViewChildren, inject } from "@angular/core";
import { takeUntilDestroyed } from "@angular/core/rxjs-interop";
import { ActivatedRoute, Router } from "@angular/router";
import { ChartResult, Condition, IPScope, Netquery, NetqueryConnection, OrderBy, PossilbeValue, Query, QueryResult, Select, Verdict } from "@safing/portmaster-api";
import { ChartResult, Condition, Database, Feature, IPScope, Netquery, NetqueryConnection, OrderBy, PossilbeValue, Query, QueryResult, SPNService, Select, Verdict } from "@safing/portmaster-api";
import { Datasource, DynamicItemsPaginator, SelectOption } from "@safing/ui";
import { BehaviorSubject, Observable, Subject, combineLatest, forkJoin, interval, of } from "rxjs";
import { catchError, debounceTime, filter, map, share, skip, switchMap, take, takeUntil } from "rxjs/operators";
Expand Down Expand Up @@ -161,6 +161,29 @@ export class SfngNetqueryViewer implements OnInit, OnDestroy, AfterViewInit {
share()
)

// whether or not the history database should be queried as well.
useHistory = false;

private get databases(): Database[] {
if (!this.useHistory) {
return [Database.Live];
}

return [Database.Live, Database.History];
}

// whether or not the current use has the history feature available.
canUseHistory$ = inject(SPNService).profile$
.pipe(
map(profile => {
if (!profile) {
return false;
}

return profile.current_plan?.feature_ids?.includes(Feature.History) || false;
})
);

constructor(
private netquery: Netquery,
private helper: NetqueryHelper,
Expand Down Expand Up @@ -369,7 +392,13 @@ export class SfngNetqueryViewer implements OnInit, OnDestroy, AfterViewInit {
})
),

totalConnCount: this.netquery.query({ select: { $count: { field: '*', as: 'totalConnCount' } }, query: this.mergeFilter || {} })
totalConnCount: this.netquery.query({
select: {
$count: { field: '*', as: 'totalConnCount' }
},
query: this.mergeFilter || {},
databases: this.databases,
})
})
}),
)
Expand Down Expand Up @@ -584,6 +613,7 @@ export class SfngNetqueryViewer implements OnInit, OnDestroy, AfterViewInit {
{ field: 'started', desc: true },
{ field: 'ended', desc: true }
],
databases: this.databases,
})
.subscribe(result => {
const paginator = new DynamicItemsPaginator<NetqueryConnection>({
Expand All @@ -596,6 +626,7 @@ export class SfngNetqueryViewer implements OnInit, OnDestroy, AfterViewInit {
],
page: pageNumber - 1,
pageSize: pageSize,
databases: this.databases,
}) as Observable<NetqueryConnection[]>;
}
}, 25)
Expand Down Expand Up @@ -638,7 +669,8 @@ export class SfngNetqueryViewer implements OnInit, OnDestroy, AfterViewInit {
groupBy: [
field,
],
orderBy: [{ field: "count", desc: true }, { field, desc: true }]
orderBy: [{ field: "count", desc: true }, { field, desc: true }],
databases: this.databases,
})
.pipe(this.helper.encodeToPossibleValues(field))
.subscribe(result => {
Expand Down Expand Up @@ -893,6 +925,7 @@ export class SfngNetqueryViewer implements OnInit, OnDestroy, AfterViewInit {
groupBy: this.groupByKeys,
orderBy: orderBy,
textSearch,
databases: this.databases,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CommonModule } from "@angular/common";
import { NgModule } from "@angular/core";
import { FormsModule } from "@angular/forms";
import { FontAwesomeModule } from "@fortawesome/angular-fontawesome";
import { SfngAccordionModule, SfngDropDownModule, SfngPaginationModule, SfngSelectModule, SfngTipUpModule, SfngTooltipModule } from "@safing/ui";
import { SfngAccordionModule, SfngDropDownModule, SfngPaginationModule, SfngSelectModule, SfngTipUpModule, SfngToggleSwitchModule, SfngTooltipModule } from "@safing/ui";
import { SfngAppIconModule } from "../app-icon";
import { CountIndicatorModule } from "../count-indicator";
import { CountryFlagModule } from "../country-flag";
Expand Down Expand Up @@ -39,6 +39,7 @@ import { SfngNetqueryTagbarComponent } from "./tag-bar";
SfngFocusModule,
SfngAppIconModule,
SfngTipUpModule,
SfngToggleSwitchModule,
A11yModule,
ExpertiseModule,
OverlayModule,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, DestroyRef, OnInit, TrackByFunction, inject } from "@angular/core";
import { takeUntilDestroyed } from "@angular/core/rxjs-interop";
import { BoolSetting, Condition, ConfigService, ExpertiseLevel, IProfileStats, Netquery, Pin, SPNService } from "@safing/portmaster-api";
import { Subject, combineLatest, debounceTime, forkJoin, interval, startWith, switchMap, takeUntil } from "rxjs";
import { Subject, combineLatest, debounceTime, forkJoin, interval, retry, startWith, switchMap, takeUntil } from "rxjs";
import { fadeInListAnimation } from "../animations";
import { ExpertiseService } from './../expertise/expertise.service';

Expand Down Expand Up @@ -152,7 +152,8 @@ export class NetworkScoutComponent implements OnInit {
return forkJoin({
stats: this.netquery.getProfileStats(query),
})
})
}),
retry({ delay: 5000 })
),

this.spn.watchPins()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class SPNAccountDetailsComponent implements OnInit {
}

ngOnInit(): void {
this.spnService.watchProfile()
this.spnService.profile$
.pipe(
takeUntilDestroyed(this.destroyRef),
catchError(err => of(null)),
Expand Down
2 changes: 1 addition & 1 deletion modules/portmaster/src/app/shared/spn-login/spn-login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class SPNLoginComponent implements OnInit {
}

ngOnInit(): void {
this.spnService.watchProfile()
this.spnService.profile$
.pipe(
takeUntilDestroyed(this.destroyRef),
catchError(() => of(null))
Expand Down
2 changes: 1 addition & 1 deletion modules/portmaster/src/app/shared/spn-status/spn-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class SPNStatusComponent implements OnInit {

ngOnInit(): void {
this.spnService
.watchProfile()
.profile$
.pipe(
takeUntilDestroyed(this.destroyRef),
catchError(() => of(null))
Expand Down

0 comments on commit 65a2926

Please sign in to comment.