metric-service (#130)

* metric-service

* Removes the interesting field and uses the "Map" directly

* Increase metric for profile visits

* Fix the sorting by returning 0 if undefined

---------

Co-authored-by: SondreB <sondre@outlook.com>
This commit is contained in:
Lu 2023-05-28 21:57:49 +03:00 committed by GitHub
parent 6ba07dd615
commit 17296d30c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 70 additions and 4 deletions

View File

@ -40,6 +40,8 @@
<mat-option value="followed-desc">{{ 'People.FollowedOldest' | translate }}</mat-option>
<mat-option value="created-asc">{{ 'People.UpdatedNewest' | translate }}</mat-option>
<mat-option value="created-desc">{{ 'People.UpdatedOldest' | translate }}</mat-option>
<mat-option value="interesting-asc">{{ 'People.MostInteresting' | translate }}</mat-option>
<mat-option value="interesting-desc">{{ 'People.LessInteresting' | translate }}</mat-option>
</mat-select>
</mat-form-field>

View File

@ -17,6 +17,7 @@ import { ImportFollowDialog, ImportFollowDialogData } from './import-follow-dial
import { DataService } from '../services/data';
import { CircleService } from '../services/circle';
import { OptionsService } from '../services/options';
import { MetricService } from '../services/metric-service';
@Component({
selector: 'app-people',
@ -73,7 +74,8 @@ export class PeopleComponent {
public utilities: Utilities,
private router: Router,
private snackBar: MatSnackBar,
public optionsService: OptionsService
public optionsService: OptionsService,
private metricService: MetricService
) {}
// async clearBlocked() {
@ -110,6 +112,14 @@ export class PeopleComponent {
this.sortedItems = this.items.sort((a, b) => {
return a.created_at! < b.created_at! ? 1 : -1;
});
} else if (sorting === 'interesting-asc') {
this.sortedItems = this.items.sort((a, b) => {
return this.metricService.get(a.pubkey) < this.metricService.get(b.pubkey) ? 1 : -1;
});
} else if (sorting === 'interesting-desc') {
this.sortedItems = this.items.sort((a, b) => {
return this.metricService.get(a.pubkey) > this.metricService.get(b.pubkey) ? 1 : -1;
});
}
}

View File

@ -0,0 +1,40 @@
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root',
})
export class MetricService {
users: {
[pubKey: string]: number;
} = {};
increase(value: number, pubKey: string) {
let existingMetric = this.users[pubKey];
if (!existingMetric) {
existingMetric = 0;
}
this.users[pubKey] = existingMetric + value;
}
decrease(value: number, pubKey: string) {
let existingMetric = this.users[pubKey];
if (!existingMetric) {
existingMetric = 0;
}
this.users[pubKey] = existingMetric - value;
}
get(pubKey: string) {
let value = this.users[pubKey];
if (!value) {
return 0;
}
return value;
}
}

View File

@ -10,6 +10,7 @@ import { QueueService } from './queue.service';
import { UIService } from './ui';
import { DataService } from './data';
import { Event, Kind } from 'nostr-tools';
import { MetricService } from './metric-service';
@Injectable({
providedIn: 'root',
@ -181,7 +182,7 @@ export class ProfileService {
this.#profilesChangedSubject.next(undefined);
}
constructor(private db: StorageService, private ui: UIService, private queueService: QueueService, private appState: ApplicationState, private utilities: Utilities) {
constructor(private db: StorageService, private ui: UIService, private queueService: QueueService, private appState: ApplicationState, private utilities: Utilities, private metricService: MetricService) {
// this.ui.profile$.subscribe((profile) => {
// });
// this.ui.pubkey$.subscribe(async (pubkey) => {
@ -576,7 +577,7 @@ export class ProfileService {
website: '',
created: Math.floor(Date.now() / 1000),
verifications: [],
pubkey: pubkey,
pubkey: pubkey
};
}

View File

@ -17,6 +17,7 @@ import { NotesService } from '../services/notes';
import { QueueService } from '../services/queue.service';
import { UIService } from '../services/ui';
import { StorageService } from '../services/storage';
import { MetricService } from '../services/metric-service';
@Component({
selector: 'app-user',
@ -132,7 +133,8 @@ export class UserComponent {
private utilities: Utilities,
public notesService: NotesService,
private router: Router,
private ngZone: NgZone
private ngZone: NgZone,
private metricService: MetricService
) {
this.subscriptions.push(
this.ui.profile$.subscribe(async (profile) => {
@ -157,6 +159,11 @@ export class UserComponent {
this.isFollowing = this.profiles.isFollowing(profile.pubkey);
// Only store metrics for profiles we're following.
if (this.isFollowing) {
this.metricService.increase(1, this.ui.profile!.pubkey);
}
// TODO: Increase this, made low during development.
const timeAgo = moment().subtract(5, 'minutes').unix();

View File

@ -166,6 +166,8 @@
"FollowedOldest": "Followed (Oldest)",
"UpdatedNewest": "Updated (Newest)",
"UpdatedOldest": "Updated (Oldest)",
"MostInteresting": "Most interesting",
"LessInteresting": "Less interesting",
"LargeIcons": "Large icons",
"MediumIcons": "Medium icons",
"SmallIcons": "Small icons",

View File

@ -165,6 +165,8 @@
"FollowedOldest": "Followed (Oldest)",
"UpdatedNewest": "Updated (Newest)",
"UpdatedOldest": "Updated (Oldest)",
"MostInteresting": "Most interesting",
"LessInteresting": "Less interesting",
"LargeIcons": "Large icons",
"MediumIcons": "Medium icons",
"SmallIcons": "Small icons",

View File

@ -166,6 +166,8 @@
"FollowedOldest": "Followed (Oldest)",
"UpdatedNewest": "Updated (Newest)",
"UpdatedOldest": "Updated (Oldest)",
"MostInteresting": "Most interesting",
"LessInteresting": "Less interesting",
"LargeIcons": "Large icons",
"MediumIcons": "Medium icons",
"SmallIcons": "Small icons",