Compare commits
No commits in common. "29054e82428ae7bf307f1692d115b1feba7f53a8" and "eebc3fdae2215590a226ea7e592b6fdc3a311ccd" have entirely different histories.
29054e8242
...
eebc3fdae2
66 changed files with 853 additions and 1999 deletions
|
@ -8,8 +8,9 @@ describe('AlertsComponent', () => {
|
|||
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [AlertsComponent],
|
||||
}).compileComponents();
|
||||
imports: [AlertsComponent]
|
||||
})
|
||||
.compileComponents();
|
||||
|
||||
fixture = TestBed.createComponent(AlertsComponent);
|
||||
component = fixture.componentInstance;
|
||||
|
|
|
@ -5,6 +5,8 @@ import { Component } from '@angular/core';
|
|||
standalone: true,
|
||||
imports: [],
|
||||
templateUrl: './alerts.component.html',
|
||||
styleUrl: './alerts.component.css',
|
||||
styleUrl: './alerts.component.css'
|
||||
})
|
||||
export class AlertsComponent {}
|
||||
export class AlertsComponent {
|
||||
|
||||
}
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
.stillboxLogo {
|
||||
font-family: "Warnes";
|
||||
}
|
|
@ -1,266 +1,63 @@
|
|||
<!-- ========== HEADER ========== -->
|
||||
@if (auth.loggedIn) {
|
||||
<header
|
||||
class="sticky top-0 inset-x-0 flex flex-wrap md:justify-start md:flex-nowrap z-[48] w-full bg-white border-b text-sm py-2.5 lg:ps-[260px] dark:bg-neutral-800 dark:border-neutral-700"
|
||||
>
|
||||
<nav class="px-4 sm:px-6 flex basis-full items-center w-full mx-auto">
|
||||
<div class="me-5 lg:me-0 lg:hidden">
|
||||
<!-- Logo -->
|
||||
<a
|
||||
class="flex-none rounded-md text-xl inline-block stillboxLogo focus:outline-none focus:opacity-80"
|
||||
href="#"
|
||||
aria-label="Stillbox"
|
||||
>
|
||||
Stillbox
|
||||
</a>
|
||||
<!-- End Logo -->
|
||||
</div>
|
||||
<div
|
||||
class="w-full flex items-center justify-end ms-auto md:justify-between gap-x-1 md:gap-x-3"
|
||||
>
|
||||
<div class="hidden md:block">
|
||||
<!-- Search Input -->
|
||||
<div class="relative">
|
||||
<div
|
||||
class="absolute inset-y-0 start-0 flex items-center pointer-events-none z-20 ps-3.5"
|
||||
>
|
||||
<svg
|
||||
class="shrink-0 size-4 text-gray-400 dark:text-white/60"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="24"
|
||||
height="24"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="2"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
>
|
||||
<circle cx="11" cy="11" r="8" />
|
||||
<path d="m21 21-4.3-4.3" />
|
||||
</svg>
|
||||
</div>
|
||||
<input
|
||||
type="text"
|
||||
class="py-2 ps-10 pe-16 block w-full bg-white border-gray-200 rounded-lg text-sm focus:outline-none focus:border-blue-500 focus:ring-blue-500 disabled:opacity-50 disabled:pointer-events-none dark:bg-neutral-800 dark:border-neutral-700 dark:text-neutral-400 dark:placeholder:text-neutral-400 dark:focus:ring-neutral-600"
|
||||
placeholder="Search"
|
||||
/>
|
||||
<div
|
||||
class="hidden absolute inset-y-0 end-0 flex items-center pointer-events-none z-20 pe-1"
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
class="inline-flex shrink-0 justify-center items-center size-6 rounded-full text-gray-500 hover:text-blue-600 focus:outline-none focus:text-blue-600 dark:text-neutral-500 dark:hover:text-blue-500 dark:focus:text-blue-500"
|
||||
aria-label="Close"
|
||||
>
|
||||
<span class="sr-only">Close</span>
|
||||
<svg
|
||||
class="shrink-0 size-4"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="24"
|
||||
height="24"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="2"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
>
|
||||
<circle cx="12" cy="12" r="10" />
|
||||
<path d="m15 9-6 6" />
|
||||
<path d="m9 9 6 6" />
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
<div
|
||||
class="absolute inset-y-0 end-0 flex items-center pointer-events-none z-20 pe-3 text-gray-400"
|
||||
>
|
||||
<span class="mx-1"> </span>
|
||||
<span class="text-xs">/</span>
|
||||
</div>
|
||||
</div>
|
||||
<!-- End Search Input -->
|
||||
</div>
|
||||
<!-- Navbar -->
|
||||
<nav class="navbar justify-between bg-base-300">
|
||||
<!-- Logo -->
|
||||
<a class="btn btn-ghost text-lg">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" d="M15.362 5.214A8.252 8.252 0 0 1 12 21 8.25 8.25 0 0 1 6.038 7.047 8.287 8.287 0 0 0 9 9.601a8.983 8.983 0 0 1 3.361-6.867 8.21 8.21 0 0 0 3 2.48Z" />
|
||||
<path stroke-linecap="round" stroke-linejoin="round" d="M12 18a3.75 3.75 0 0 0 .495-7.468 5.99 5.99 0 0 0-1.925 3.547 5.975 5.975 0 0 1-2.133-1.001A3.75 3.75 0 0 0 12 18Z" />
|
||||
</svg>
|
||||
|
||||
<div class="flex flex-row items-center justify-end gap-1">
|
||||
<button
|
||||
type="button"
|
||||
class="md:hidden size-[38px] relative inline-flex justify-center items-center gap-x-2 text-sm font-semibold rounded-full border border-transparent text-gray-800 hover:bg-gray-100 focus:outline-none focus:bg-gray-100 disabled:opacity-50 disabled:pointer-events-none dark:text-white dark:hover:bg-neutral-700 dark:focus:bg-neutral-700"
|
||||
>
|
||||
<svg
|
||||
class="shrink-0 size-4"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="24"
|
||||
height="24"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="2"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
>
|
||||
<circle cx="11" cy="11" r="8" />
|
||||
<path d="m21 21-4.3-4.3" />
|
||||
</svg>
|
||||
<span class="sr-only">Search</span>
|
||||
</button>
|
||||
</a>
|
||||
|
||||
<a (click)="logout()" class="btn btn-sm btn-primary">Logout</a>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
</header>
|
||||
} @else {
|
||||
<div class="relative flex flex-col h-full max-h-full">
|
||||
<div class="px-6 pt-4">
|
||||
<!-- Logo -->
|
||||
<a
|
||||
class="stillboxLogo flex-none rounded-xl text-xl inline-block font-semibold focus:outline-none focus:opacity-80"
|
||||
href="#"
|
||||
aria-label="Stillbox"
|
||||
>
|
||||
Stillbox
|
||||
</a>
|
||||
<!-- End Logo -->
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
<!-- ========== END HEADER ========== -->
|
||||
|
||||
@if (auth.loggedIn) {
|
||||
<div class="-mt-px">
|
||||
<!-- Breadcrumb -->
|
||||
<div class="sticky top-0 inset-x-0 z-20 bg-white border-y px-4 sm:px-6 lg:px-8 lg:hidden dark:bg-neutral-800 dark:border-neutral-700">
|
||||
<div class="flex items-center py-2">
|
||||
<!-- Navigation Toggle -->
|
||||
<button type="button" class="size-8 flex justify-center items-center gap-x-2 border border-gray-200 text-gray-800 hover:text-gray-500 rounded-lg focus:outline-none focus:text-gray-500 disabled:opacity-50 disabled:pointer-events-none dark:border-neutral-700 dark:text-neutral-200 dark:hover:text-neutral-500 dark:focus:text-neutral-500" aria-haspopup="dialog" aria-expanded="false" aria-controls="hs-application-sidebar" aria-label="Toggle navigation" data-hs-overlay="#hs-application-sidebar">
|
||||
<span class="sr-only">Toggle Navigation</span>
|
||||
<svg class="shrink-0 size-4" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
||||
<rect width="18" height="18" x="3" y="3" rx="2" />
|
||||
<path d="M15 3v18" />
|
||||
<path d="m8 9 3 3-3 3" />
|
||||
</svg>
|
||||
<!-- Menu for mobile -->
|
||||
<div class="dropdown dropdown-end sm:hidden">
|
||||
<button class="btn btn-ghost">
|
||||
<i class="fa-solid fa-bars text-lg"></i>
|
||||
</button>
|
||||
<!-- End Navigation Toggle -->
|
||||
|
||||
<!-- Breadcrumb -->
|
||||
<ol class="ms-3 flex items-center whitespace-nowrap">
|
||||
<li class="flex items-center text-sm text-gray-800 dark:text-neutral-400">
|
||||
Application Layout
|
||||
<svg class="shrink-0 mx-3 overflow-visible size-2.5 text-gray-400 dark:text-neutral-500" width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
|
||||
<path d="M5 1L10.6869 7.16086C10.8637 7.35239 10.8637 7.64761 10.6869 7.83914L5 14" stroke="currentColor" stroke-width="2" stroke-linecap="round" />
|
||||
</svg>
|
||||
</li>
|
||||
<li class="text-sm font-semibold text-gray-800 truncate dark:text-neutral-400" aria-current="page">
|
||||
Dashboard
|
||||
</li>
|
||||
</ol>
|
||||
<!-- End Breadcrumb -->
|
||||
</div>
|
||||
<ul tabindex="0" class="dropdown-content menu z-[1] bg-base-200 p-6 rounded-box shadow w-56 gap-2">
|
||||
<li><a>Item</a></li>
|
||||
<a class="btn btn-sm btn-primary">Do</a>
|
||||
</ul>
|
||||
</div>
|
||||
<!-- End Breadcrumb -->
|
||||
</div>
|
||||
|
||||
<!-- Sidebar -->
|
||||
<div id="hs-application-sidebar" class="hs-overlay [--auto-close:lg]
|
||||
hs-overlay-open:translate-x-0
|
||||
-translate-x-full transition-all duration-300 transform
|
||||
w-[260px] h-full
|
||||
hidden
|
||||
fixed inset-y-0 start-0 z-[60]
|
||||
bg-white border-e border-gray-200
|
||||
lg:block lg:translate-x-0 lg:end-auto lg:bottom-0
|
||||
dark:bg-neutral-800 dark:border-neutral-700" role="dialog" tabindex="-1" aria-label="Sidebar">
|
||||
<div class="relative flex flex-col h-full max-h-full">
|
||||
<div class="px-6 pt-4">
|
||||
<!-- Logo -->
|
||||
<a
|
||||
class="stillboxLogo flex-none rounded-xl text-xl inline-block font-semibold focus:outline-none focus:opacity-80"
|
||||
href="#"
|
||||
aria-label="Stillbox"
|
||||
>
|
||||
Stillbox
|
||||
<!-- Menu for desktop -->
|
||||
<ul class="hidden menu sm:menu-horizontal gap-2">
|
||||
<li><a>Item</a></li>
|
||||
<a class="btn btn-sm btn-primary">Do</a>
|
||||
</ul>
|
||||
</nav>
|
||||
<div class="flex overflow-hidden relative">
|
||||
<!-- Sidebar -->
|
||||
<aside class="h-screen sticky top-0 flex flex-col overflow-y-auto gap-2 py-6 px-2 bg-base-200">
|
||||
<a class="btn btn-square btn-ghost btn-secondary text-xl" title="Home" routerLink="/home" routerLinkActive="btn-active">
|
||||
<ng-icon name="ionHome"></ng-icon>
|
||||
</a>
|
||||
<!-- End Logo -->
|
||||
</div>
|
||||
|
||||
<!-- Content -->
|
||||
<div
|
||||
class="h-full overflow-y-auto [&::-webkit-scrollbar]:w-2 [&::-webkit-scrollbar-thumb]:rounded-full [&::-webkit-scrollbar-track]:bg-gray-100 [&::-webkit-scrollbar-thumb]:bg-gray-300 dark:[&::-webkit-scrollbar-track]:bg-neutral-700 dark:[&::-webkit-scrollbar-thumb]:bg-neutral-500"
|
||||
>
|
||||
<nav
|
||||
class="hs-accordion-group p-3 w-full flex flex-col flex-wrap"
|
||||
data-hs-accordion-always-open
|
||||
>
|
||||
<ul class="flex flex-col space-y-1">
|
||||
<li>
|
||||
<a
|
||||
class="w-full flex items-center gap-x-3.5 py-2 px-2.5 text-sm text-gray-800 rounded-lg hover:bg-gray-100 dark:hover:bg-neutral-700 dark:text-neutral-200 dark:hover:text-neutral-300"
|
||||
routerLink="/home"
|
||||
routerLinkActive="btn-active"
|
||||
>
|
||||
<ng-icon name="ionHome"></ng-icon>Home
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a
|
||||
class="w-full flex items-center gap-x-3.5 py-2 px-2.5 text-sm text-gray-800 rounded-lg hover:bg-gray-100 dark:hover:bg-neutral-700 dark:text-neutral-200 dark:hover:text-neutral-300"
|
||||
routerLink="/talkgroups"
|
||||
routerLinkActive="btn-active"
|
||||
href="#"
|
||||
>
|
||||
<ng-icon name="ionChatbubbles"></ng-icon>Talkgroups
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a
|
||||
class="w-full flex items-center gap-x-3.5 py-2 px-2.5 text-sm text-gray-800 rounded-lg hover:bg-gray-100 dark:hover:bg-neutral-700 dark:text-neutral-200 dark:hover:text-neutral-300"
|
||||
routerLink="/calls"
|
||||
routerLinkActive="btn-active"
|
||||
href="#"
|
||||
>
|
||||
<ng-icon name="ionMegaphoneOutline"></ng-icon>Calls
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a
|
||||
class="w-full flex items-center gap-x-3.5 py-2 px-2.5 text-sm text-gray-800 rounded-lg hover:bg-gray-100 dark:hover:bg-neutral-900 dark:text-neutral-200 dark:hover:text-neutral-300"
|
||||
routerLink="/incidents"
|
||||
routerLinkActive="btn-active"
|
||||
href="#"
|
||||
>
|
||||
<ng-icon name="ionNewspaperOutline"></ng-icon>Incidents
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a
|
||||
class="w-full flex items-center gap-x-3.5 py-2 px-2.5 text-sm text-gray-800 rounded-lg hover:bg-gray-100 dark:hover:bg-neutral-900 dark:text-neutral-200 dark:hover:text-neutral-300"
|
||||
routerLink="/alerts"
|
||||
routerLinkActive="btn-active"
|
||||
>
|
||||
<ng-icon name="ionAlertCircleOutline"></ng-icon>Alerts
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</nav>
|
||||
</div>
|
||||
<!-- End Content -->
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
<!-- End Sidebar -->
|
||||
<a routerLink="/calls" routerLinkActive="btn-active" class="btn btn-ghost btn-secondary text-xl" title="Calls">
|
||||
<ng-icon name="ionMegaphoneOutline"></ng-icon>
|
||||
</a>
|
||||
|
||||
<!-- Content -->
|
||||
@if (auth.loggedIn) {
|
||||
<div class="w-full lg:ps-64">
|
||||
<div class="p-4 sm:p-6 space-y-4 sm:space-y-6">
|
||||
<router-outlet />
|
||||
<a class="btn btn-square btn-ghost text-xl" title="Talkgroups" routerLink="/talkgroups" routerLinkActive="btn-active">
|
||||
<ng-icon name="ionChatbubbles"></ng-icon>
|
||||
</a>
|
||||
|
||||
<div class="divider my-0"></div>
|
||||
|
||||
<a class="btn btn-square btn-ghost text-xl" title="Incidents" routerLink="/incidents" routerLinkActive="btn-active">
|
||||
<ng-icon name="ionNewspaperOutline"></ng-icon>
|
||||
</a>
|
||||
|
||||
<a class="btn btn-square btn-ghost text-xl" title="Alerts" routerLink="/alerts" routerLinkActive="btn-active">
|
||||
<ng-icon name="ionAlertCircleOutline"></ng-icon>
|
||||
</a>
|
||||
<div class="divider my-0"></div>
|
||||
|
||||
<a class="btn btn-circle btn-ghost text-xl" title="Listen">
|
||||
<ng-icon name="ionRadioOutline"></ng-icon>
|
||||
</a>
|
||||
</aside>
|
||||
<div class="container mx-auto px-4">
|
||||
<router-outlet />
|
||||
</div>
|
||||
</div>
|
||||
} @else {
|
||||
<div class="w-full">
|
||||
<div class="p-4 sm:p-6 space-y-4 sm:space-y-6">
|
||||
<router-outlet />
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
|
|
|
@ -3,47 +3,17 @@ import { RouterModule, RouterOutlet, RouterLink } from '@angular/router';
|
|||
import { CommonModule } from '@angular/common';
|
||||
import { AuthService } from './login/auth.service';
|
||||
import { NgIconComponent, provideIcons } from '@ng-icons/core';
|
||||
import {
|
||||
ionMenuOutline,
|
||||
ionChatbubbles,
|
||||
ionNewspaperOutline,
|
||||
ionAlertCircleOutline,
|
||||
ionRadioOutline,
|
||||
ionHome,
|
||||
ionMegaphoneOutline,
|
||||
ionCreateOutline,
|
||||
} from '@ng-icons/ionicons';
|
||||
import { ionMenuOutline, ionChatbubbles, ionNewspaperOutline, ionAlertCircleOutline, ionRadioOutline, ionHome, ionMegaphoneOutline } from '@ng-icons/ionicons';
|
||||
|
||||
@Component({
|
||||
selector: 'app-root',
|
||||
standalone: true,
|
||||
imports: [
|
||||
CommonModule,
|
||||
RouterOutlet,
|
||||
RouterModule,
|
||||
RouterLink,
|
||||
NgIconComponent,
|
||||
],
|
||||
imports: [CommonModule, RouterOutlet, RouterModule, RouterLink, NgIconComponent],
|
||||
templateUrl: './app.component.html',
|
||||
styleUrl: './app.component.css',
|
||||
providers: [
|
||||
provideIcons({
|
||||
ionMenuOutline,
|
||||
ionChatbubbles,
|
||||
ionNewspaperOutline,
|
||||
ionAlertCircleOutline,
|
||||
ionRadioOutline,
|
||||
ionHome,
|
||||
ionMegaphoneOutline,
|
||||
ionCreateOutline,
|
||||
}),
|
||||
],
|
||||
providers: [ provideIcons({ ionMenuOutline, ionChatbubbles, ionNewspaperOutline, ionAlertCircleOutline, ionRadioOutline, ionHome, ionMegaphoneOutline })],
|
||||
})
|
||||
export class AppComponent {
|
||||
auth: AuthService = inject(AuthService);
|
||||
title = 'admin';
|
||||
|
||||
logout() {
|
||||
this.auth.logout();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,8 +7,7 @@ import {
|
|||
withInterceptors,
|
||||
} from '@angular/common/http';
|
||||
import { Observable } from 'rxjs';
|
||||
import { isDevMode, inject } from '@angular/core';
|
||||
import { AuthService } from './login/auth.service';
|
||||
import { isDevMode } from '@angular/core';
|
||||
|
||||
import { routes } from './app.routes';
|
||||
import { provideHttpClient } from '@angular/common/http';
|
||||
|
@ -27,28 +26,10 @@ export function apiBaseInterceptor(
|
|||
return next(apiReq);
|
||||
}
|
||||
|
||||
export function authIntercept(
|
||||
req: HttpRequest<unknown>,
|
||||
next: HttpHandlerFn,
|
||||
): Observable<HttpEvent<unknown>> {
|
||||
let authSvc: AuthService = inject(AuthService);
|
||||
if (authSvc.loggedIn) {
|
||||
req = req.clone({
|
||||
setHeaders: {
|
||||
'Content-Type': 'application/json; charset=utf-8',
|
||||
Accept: 'application/json',
|
||||
Authorization: `Bearer ${authSvc.getToken()}`,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return next(req);
|
||||
}
|
||||
|
||||
export const appConfig: ApplicationConfig = {
|
||||
providers: [
|
||||
provideZoneChangeDetection({ eventCoalescing: true }),
|
||||
provideRouter(routes),
|
||||
provideHttpClient(withInterceptors([apiBaseInterceptor, authIntercept])),
|
||||
provideHttpClient(withInterceptors([apiBaseInterceptor])),
|
||||
],
|
||||
};
|
||||
|
|
|
@ -8,8 +8,9 @@ describe('CallsComponent', () => {
|
|||
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [CallsComponent],
|
||||
}).compileComponents();
|
||||
imports: [CallsComponent]
|
||||
})
|
||||
.compileComponents();
|
||||
|
||||
fixture = TestBed.createComponent(CallsComponent);
|
||||
component = fixture.componentInstance;
|
||||
|
|
|
@ -5,6 +5,8 @@ import { Component } from '@angular/core';
|
|||
standalone: true,
|
||||
imports: [],
|
||||
templateUrl: './calls.component.html',
|
||||
styleUrl: './calls.component.css',
|
||||
styleUrl: './calls.component.css'
|
||||
})
|
||||
export class CallsComponent {}
|
||||
export class CallsComponent {
|
||||
|
||||
}
|
||||
|
|
|
@ -1 +1,3 @@
|
|||
<p>This will be a dashboard someday.</p>
|
||||
<p>
|
||||
This will be a dashboard someday.
|
||||
</p>
|
||||
|
|
|
@ -7,4 +7,5 @@ import { Component } from '@angular/core';
|
|||
templateUrl: './home.component.html',
|
||||
styleUrl: './home.component.css',
|
||||
})
|
||||
export class HomeComponent {}
|
||||
export class HomeComponent {
|
||||
}
|
||||
|
|
|
@ -8,8 +8,9 @@ describe('IncidentsComponent', () => {
|
|||
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [IncidentsComponent],
|
||||
}).compileComponents();
|
||||
imports: [IncidentsComponent]
|
||||
})
|
||||
.compileComponents();
|
||||
|
||||
fixture = TestBed.createComponent(IncidentsComponent);
|
||||
component = fixture.componentInstance;
|
||||
|
|
|
@ -5,6 +5,8 @@ import { Component } from '@angular/core';
|
|||
standalone: true,
|
||||
imports: [],
|
||||
templateUrl: './incidents.component.html',
|
||||
styleUrl: './incidents.component.css',
|
||||
styleUrl: './incidents.component.css'
|
||||
})
|
||||
export class IncidentsComponent {}
|
||||
export class IncidentsComponent {
|
||||
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ export class AuthService {
|
|||
login(username: string, password: string): Observable<HttpResponse<Jwt>> {
|
||||
return this.http
|
||||
.post<Jwt>(
|
||||
'/api/login',
|
||||
'/login',
|
||||
{ username: username, password: password },
|
||||
{ observe: 'response' },
|
||||
)
|
||||
|
@ -41,14 +41,4 @@ export class AuthService {
|
|||
}),
|
||||
);
|
||||
}
|
||||
|
||||
getToken(): string | null {
|
||||
return sessionStorage.getItem('jwt');
|
||||
}
|
||||
|
||||
logout() {
|
||||
sessionStorage.removeItem('jwt');
|
||||
this.loggedIn = false;
|
||||
this._router.navigateByUrl('/login');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,30 +0,0 @@
|
|||
export interface TGID {
|
||||
sys: number;
|
||||
tg: number;
|
||||
}
|
||||
|
||||
export interface AlertRule {
|
||||
times: string[];
|
||||
mult: number;
|
||||
}
|
||||
|
||||
export interface System {
|
||||
id: number;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface Talkgroup {
|
||||
id: number;
|
||||
system_id: number;
|
||||
tgid: number;
|
||||
name: string;
|
||||
alpha_tag: string;
|
||||
tg_group: string;
|
||||
frequency: number;
|
||||
metadata: Object;
|
||||
tags: string[];
|
||||
alert: boolean;
|
||||
alert_config: Map<TGID, AlertRule[]>;
|
||||
system: System;
|
||||
weight: number;
|
||||
}
|
|
@ -1 +0,0 @@
|
|||
<p>TG comp</p>
|
|
@ -1,22 +0,0 @@
|
|||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { TalkgroupRecordComponent } from './talkgroup-record.component';
|
||||
|
||||
describe('TalkgroupRecordComponent', () => {
|
||||
let component: TalkgroupRecordComponent;
|
||||
let fixture: ComponentFixture<TalkgroupRecordComponent>;
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [TalkgroupRecordComponent],
|
||||
}).compileComponents();
|
||||
|
||||
fixture = TestBed.createComponent(TalkgroupRecordComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
|
@ -1,16 +0,0 @@
|
|||
import { Component } from '@angular/core';
|
||||
import { Talkgroup } from '../../talkgroup';
|
||||
|
||||
@Component({
|
||||
selector: 'talkgroup-record',
|
||||
standalone: true,
|
||||
imports: [],
|
||||
templateUrl: './talkgroup-record.component.html',
|
||||
styleUrl: './talkgroup-record.component.css',
|
||||
})
|
||||
export class TalkgroupRecordComponent {
|
||||
tg: Talkgroup;
|
||||
constructor(tg: Talkgroup) {
|
||||
this.tg = tg;
|
||||
}
|
||||
}
|
|
@ -1,26 +1 @@
|
|||
<div class="w-100 justify-center overflow-x-auto">
|
||||
<table class="table">
|
||||
<!-- head -->
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Sys</th>
|
||||
<th>Sys ID</th>
|
||||
<th>Name</th>
|
||||
<th>TG ID</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<!-- row 1 -->
|
||||
@for (tg of tgs; track tg.id) {
|
||||
<tr>
|
||||
<td>{{ tg.system.name }}</td>
|
||||
<td>{{ tg.system.id }}</td>
|
||||
<td>{{ tg.name }}</td>
|
||||
<td>{{ tg.tgid }}</td>
|
||||
<td><ng-icon name="ionCreateOutline"></ng-icon></td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<p>talkgroups works!</p>
|
||||
|
|
|
@ -8,8 +8,9 @@ describe('TalkgroupsComponent', () => {
|
|||
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [TalkgroupsComponent],
|
||||
}).compileComponents();
|
||||
imports: [TalkgroupsComponent]
|
||||
})
|
||||
.compileComponents();
|
||||
|
||||
fixture = TestBed.createComponent(TalkgroupsComponent);
|
||||
component = fixture.componentInstance;
|
||||
|
|
|
@ -1,26 +1,12 @@
|
|||
import { Component, inject } from '@angular/core';
|
||||
import { TalkgroupService } from './talkgroups.service';
|
||||
import { Talkgroup } from '../talkgroup';
|
||||
import { NgIconComponent, provideIcons } from '@ng-icons/core';
|
||||
import { ionCreateOutline } from '@ng-icons/ionicons';
|
||||
import { Component } from '@angular/core';
|
||||
|
||||
@Component({
|
||||
selector: 'talkgroups',
|
||||
selector: 'app-talkgroups',
|
||||
standalone: true,
|
||||
imports: [NgIconComponent],
|
||||
imports: [],
|
||||
templateUrl: './talkgroups.component.html',
|
||||
styleUrl: './talkgroups.component.css',
|
||||
providers: [provideIcons({ ionCreateOutline })],
|
||||
styleUrl: './talkgroups.component.css'
|
||||
})
|
||||
export class TalkgroupsComponent {
|
||||
tgs: Talkgroup[] = [];
|
||||
tgService: TalkgroupService = inject(TalkgroupService);
|
||||
|
||||
ngOnInit() {
|
||||
this.getTalkgroups();
|
||||
}
|
||||
|
||||
getTalkgroups() {
|
||||
this.tgService.getTalkgroups().subscribe((tgs) => (this.tgs = tgs));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { APIService } from './api.service';
|
||||
|
||||
describe('APIService', () => {
|
||||
let service: APIService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(APIService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
|
@ -1,16 +0,0 @@
|
|||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Observable } from 'rxjs';
|
||||
import { Talkgroup } from '../talkgroup';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class TalkgroupService {
|
||||
loggedIn: boolean = false;
|
||||
constructor(private http: HttpClient) {}
|
||||
|
||||
getTalkgroups(): Observable<Talkgroup[]> {
|
||||
return this.http.get<Talkgroup[]>('/api/talkgroup/');
|
||||
}
|
||||
}
|
Binary file not shown.
|
@ -2,8 +2,3 @@
|
|||
@tailwind base;
|
||||
@tailwind components;
|
||||
@tailwind utilities;
|
||||
|
||||
@font-face {
|
||||
font-family: "Warnes";
|
||||
src: url("./assets/Warnes.ttf");
|
||||
}
|
||||
|
|
|
@ -57,7 +57,7 @@ func main() {
|
|||
loginForm.Add("username", *username)
|
||||
loginForm.Add("password", *password)
|
||||
|
||||
loginReq, err := http.NewRequest("POST", "http"+secureSuffix()+"://"+*addr+"/api/login", strings.NewReader(loginForm.Encode()))
|
||||
loginReq, err := http.NewRequest("POST", "http"+secureSuffix()+"://"+*addr+"/login", strings.NewReader(loginForm.Encode()))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
@ -87,7 +87,7 @@ func main() {
|
|||
log.Fatal(err)
|
||||
}
|
||||
|
||||
u := url.URL{Scheme: "ws" + secureSuffix(), Host: *addr, Path: "/api/ws"}
|
||||
u := url.URL{Scheme: "ws" + secureSuffix(), Host: *addr, Path: "/ws"}
|
||||
log.Printf("connecting to %s", u.String())
|
||||
|
||||
dialer := websocket.Dialer{
|
||||
|
|
|
@ -9,8 +9,8 @@ import (
|
|||
|
||||
"dynatron.me/x/stillbox/internal/common"
|
||||
"dynatron.me/x/stillbox/internal/version"
|
||||
"dynatron.me/x/stillbox/pkg/cmd/admin"
|
||||
"dynatron.me/x/stillbox/pkg/cmd/serve"
|
||||
"dynatron.me/x/stillbox/pkg/cmd/admin"
|
||||
"dynatron.me/x/stillbox/pkg/config"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
|
|
@ -33,11 +33,5 @@ alerting:
|
|||
renotify: 30m
|
||||
notify:
|
||||
- provider: slackwebhook
|
||||
# subjectTemplate: "Stillbox Alert ({{ highest . }})"
|
||||
# bodyTemplate: |
|
||||
# {{ range . -}}
|
||||
# {{ .TGName }} is active with a score of {{ f .Score.Score 4 }}! ({{ f .Score.RecentCount 0 }}/{{ .Score.Count }} recent calls)
|
||||
#
|
||||
# {{ end -}}
|
||||
config:
|
||||
webhookURL: "http://somewhere"
|
||||
|
|
1
go.mod
1
go.mod
|
@ -11,7 +11,6 @@ require (
|
|||
github.com/go-chi/httprate v0.9.0
|
||||
github.com/go-chi/jwtauth/v5 v5.3.1
|
||||
github.com/go-chi/render v1.0.3
|
||||
github.com/go-viper/mapstructure/v2 v2.2.1
|
||||
github.com/golang-migrate/migrate/v4 v4.17.1
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/gorilla/websocket v1.5.3
|
||||
|
|
2
go.sum
2
go.sum
|
@ -44,8 +44,6 @@ github.com/go-chi/jwtauth/v5 v5.3.1 h1:1ePWrjVctvp1tyBq5b/2ER8Th/+RbYc7x4qNsc5rh
|
|||
github.com/go-chi/jwtauth/v5 v5.3.1/go.mod h1:6Fl2RRmWXs3tJYE1IQGX81FsPoGqDwq9c15j52R5q80=
|
||||
github.com/go-chi/render v1.0.3 h1:AsXqd2a1/INaIfUSKq3G5uA8weYx20FOsM7uSoCyyt4=
|
||||
github.com/go-chi/render v1.0.3/go.mod h1:/gr3hVkmYR0YlEy3LxCuVRFzEu9Ruok+gFqbIofjao0=
|
||||
github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss=
|
||||
github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
|
||||
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
||||
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
|
@ -44,3 +47,10 @@ func PtrOrNull[T comparable](val T) *T {
|
|||
|
||||
return &val
|
||||
}
|
||||
|
||||
func FmtFloat(v float64, places ...int) string {
|
||||
if len(places) > 0 {
|
||||
return fmt.Sprintf("%."+strconv.Itoa(places[0])+"f", v)
|
||||
}
|
||||
return fmt.Sprintf("%.4f", v)
|
||||
}
|
||||
|
|
|
@ -1,48 +0,0 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"dynatron.me/x/stillbox/internal/jsontime"
|
||||
)
|
||||
|
||||
var (
|
||||
FuncMap = template.FuncMap{
|
||||
"f": fmtFloat,
|
||||
"dict": func(values ...interface{}) (map[string]interface{}, error) {
|
||||
if len(values)%2 != 0 {
|
||||
return nil, errors.New("invalid dict call")
|
||||
}
|
||||
dict := make(map[string]interface{}, len(values)/2)
|
||||
for i := 0; i < len(values); i += 2 {
|
||||
key, ok := values[i].(string)
|
||||
if !ok {
|
||||
return nil, errors.New("dict keys must be strings")
|
||||
}
|
||||
dict[key] = values[i+1]
|
||||
}
|
||||
return dict, nil
|
||||
},
|
||||
"formTime": func(t jsontime.Time) string {
|
||||
return time.Time(t).Format("2006-01-02T15:04")
|
||||
},
|
||||
"ago": func(s string) (string, error) {
|
||||
d, err := time.ParseDuration(s)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return time.Now().Add(-d).Format("2006-01-02T15:04"), nil
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func fmtFloat(v float64, places ...int) string {
|
||||
if len(places) > 0 {
|
||||
return fmt.Sprintf("%."+strconv.Itoa(places[0])+"f", v)
|
||||
}
|
||||
return fmt.Sprintf("%.4f", v)
|
||||
}
|
|
@ -1,81 +0,0 @@
|
|||
package alert
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"dynatron.me/x/stillbox/internal/trending"
|
||||
"dynatron.me/x/stillbox/pkg/database"
|
||||
"dynatron.me/x/stillbox/pkg/talkgroups"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type Alert struct {
|
||||
ID uuid.UUID
|
||||
Timestamp time.Time
|
||||
TGName string
|
||||
Score trending.Score[talkgroups.ID]
|
||||
OrigScore float64
|
||||
Weight float32
|
||||
Suppressed bool
|
||||
}
|
||||
|
||||
func (a *Alert) ToAddAlertParams() database.AddAlertParams {
|
||||
f32score := float32(a.Score.Score)
|
||||
f32origscore := float32(a.OrigScore)
|
||||
|
||||
var origScore *float32
|
||||
if a.Score.Score != a.OrigScore {
|
||||
origScore = &f32origscore
|
||||
}
|
||||
|
||||
return database.AddAlertParams{
|
||||
ID: a.ID,
|
||||
Time: pgtype.Timestamptz{Time: a.Timestamp, Valid: true},
|
||||
PackedTg: a.Score.ID.Pack(),
|
||||
Weight: &a.Weight,
|
||||
Score: &f32score,
|
||||
OrigScore: origScore,
|
||||
Notified: !a.Suppressed,
|
||||
}
|
||||
}
|
||||
|
||||
// makeAlert creates a notification for later rendering by the template.
|
||||
// It takes a talkgroup Score as input.
|
||||
func Make(ctx context.Context, store talkgroups.Store, score trending.Score[talkgroups.ID], origScore float64) (Alert, error) {
|
||||
d := Alert{
|
||||
ID: uuid.New(),
|
||||
Score: score,
|
||||
Timestamp: time.Now(),
|
||||
Weight: 1.0,
|
||||
OrigScore: origScore,
|
||||
}
|
||||
|
||||
tgRecord, err := store.TG(ctx, score.ID)
|
||||
switch err {
|
||||
case nil:
|
||||
d.Weight = tgRecord.Talkgroup.Weight
|
||||
if tgRecord.System.Name == "" {
|
||||
tgRecord.System.Name = strconv.Itoa(int(score.ID.System))
|
||||
}
|
||||
|
||||
if tgRecord.Talkgroup.Name != nil {
|
||||
d.TGName = fmt.Sprintf("%s %s (%d)", tgRecord.System.Name, *tgRecord.Talkgroup.Name, score.ID.Talkgroup)
|
||||
} else {
|
||||
d.TGName = fmt.Sprintf("%s:%d", tgRecord.System.Name, int(score.ID.Talkgroup))
|
||||
}
|
||||
default:
|
||||
system, has := store.SystemName(ctx, int(score.ID.System))
|
||||
if has {
|
||||
d.TGName = fmt.Sprintf("%s:%d", system, int(score.ID.Talkgroup))
|
||||
} else {
|
||||
d.TGName = fmt.Sprintf("%d:%d", int(score.ID.System), int(score.ID.Talkgroup))
|
||||
}
|
||||
}
|
||||
|
||||
return d, nil
|
||||
}
|
|
@ -1,24 +1,27 @@
|
|||
package alerting
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strconv"
|
||||
"sync"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"dynatron.me/x/stillbox/pkg/alerting/alert"
|
||||
"dynatron.me/x/stillbox/pkg/calls"
|
||||
cl "dynatron.me/x/stillbox/pkg/calls"
|
||||
"dynatron.me/x/stillbox/pkg/config"
|
||||
"dynatron.me/x/stillbox/pkg/database"
|
||||
"dynatron.me/x/stillbox/pkg/notify"
|
||||
"dynatron.me/x/stillbox/pkg/sinks"
|
||||
talkgroups "dynatron.me/x/stillbox/pkg/talkgroups"
|
||||
|
||||
"dynatron.me/x/stillbox/internal/timeseries"
|
||||
"dynatron.me/x/stillbox/internal/trending"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
|
@ -43,14 +46,14 @@ type alerter struct {
|
|||
sync.RWMutex
|
||||
clock timeseries.Clock
|
||||
cfg config.Alerting
|
||||
scorer trending.Scorer[talkgroups.ID]
|
||||
scores trending.Scores[talkgroups.ID]
|
||||
scorer trending.Scorer[cl.Talkgroup]
|
||||
scores trending.Scores[cl.Talkgroup]
|
||||
lastScore time.Time
|
||||
sim *Simulation
|
||||
alertCache map[talkgroups.ID]alert.Alert
|
||||
alertCache map[cl.Talkgroup]Alert
|
||||
renotify time.Duration
|
||||
notifier notify.Notifier
|
||||
tgCache talkgroups.Store
|
||||
tgCache cl.TalkgroupCache
|
||||
}
|
||||
|
||||
type offsetClock time.Duration
|
||||
|
@ -85,14 +88,14 @@ func WithNotifier(n notify.Notifier) AlertOption {
|
|||
}
|
||||
|
||||
// New creates a new Alerter using the provided configuration.
|
||||
func New(cfg config.Alerting, tgCache talkgroups.Store, opts ...AlertOption) Alerter {
|
||||
func New(cfg config.Alerting, tgCache cl.TalkgroupCache, opts ...AlertOption) Alerter {
|
||||
if !cfg.Enable {
|
||||
return &noopAlerter{}
|
||||
}
|
||||
|
||||
as := &alerter{
|
||||
cfg: cfg,
|
||||
alertCache: make(map[talkgroups.ID]alert.Alert),
|
||||
alertCache: make(map[cl.Talkgroup]Alert),
|
||||
clock: timeseries.DefaultClock,
|
||||
renotify: DefaultRenotify,
|
||||
tgCache: tgCache,
|
||||
|
@ -108,12 +111,12 @@ func New(cfg config.Alerting, tgCache talkgroups.Store, opts ...AlertOption) Ale
|
|||
|
||||
as.scorer = trending.NewScorer(
|
||||
trending.WithTimeSeries(as.newTimeSeries),
|
||||
trending.WithStorageDuration[talkgroups.ID](time.Hour*24*time.Duration(cfg.LookbackDays)),
|
||||
trending.WithRecentDuration[talkgroups.ID](time.Duration(cfg.Recent)),
|
||||
trending.WithHalfLife[talkgroups.ID](time.Duration(cfg.HalfLife)),
|
||||
trending.WithScoreThreshold[talkgroups.ID](ScoreThreshold),
|
||||
trending.WithCountThreshold[talkgroups.ID](CountThreshold),
|
||||
trending.WithClock[talkgroups.ID](as.clock),
|
||||
trending.WithStorageDuration[cl.Talkgroup](time.Hour*24*time.Duration(cfg.LookbackDays)),
|
||||
trending.WithRecentDuration[cl.Talkgroup](time.Duration(cfg.Recent)),
|
||||
trending.WithHalfLife[cl.Talkgroup](time.Duration(cfg.HalfLife)),
|
||||
trending.WithScoreThreshold[cl.Talkgroup](ScoreThreshold),
|
||||
trending.WithCountThreshold[cl.Talkgroup](CountThreshold),
|
||||
trending.WithClock[cl.Talkgroup](as.clock),
|
||||
)
|
||||
|
||||
return as
|
||||
|
@ -146,29 +149,36 @@ func (as *alerter) Go(ctx context.Context) {
|
|||
|
||||
}
|
||||
|
||||
func (as *alerter) eval(ctx context.Context, now time.Time, testMode bool) ([]alert.Alert, error) {
|
||||
const notificationTemplStr = `{{ range . -}}
|
||||
{{ .TGName }} is active with a score of {{ f .Score.Score 4 }}! ({{ f .Score.RecentCount 0 }}/{{ .Score.Count }} recent calls)
|
||||
|
||||
{{ end -}}`
|
||||
|
||||
var notificationTemplate = template.Must(template.New("notification").Funcs(funcMap).Parse(notificationTemplStr))
|
||||
|
||||
func (as *alerter) eval(ctx context.Context, now time.Time, testMode bool) ([]Alert, error) {
|
||||
err := as.tgCache.Hint(ctx, as.scoredTGs())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("prime TG cache: %w", err)
|
||||
}
|
||||
|
||||
as.Lock()
|
||||
defer as.Unlock()
|
||||
|
||||
db := database.FromCtx(ctx)
|
||||
|
||||
var notifications []alert.Alert
|
||||
var notifications []Alert
|
||||
for _, s := range as.scores {
|
||||
origScore := s.Score
|
||||
tgr, err := as.tgCache.TG(ctx, s.ID)
|
||||
if err == nil && !tgr.Talkgroup.Alert {
|
||||
continue
|
||||
tgr, has := as.tgCache.TG(ctx, s.ID)
|
||||
if has {
|
||||
if !tgr.Alert {
|
||||
continue
|
||||
}
|
||||
s.Score *= float64(tgr.Weight)
|
||||
}
|
||||
|
||||
if s.Score > as.cfg.AlertThreshold || testMode {
|
||||
if old, inCache := as.alertCache[s.ID]; !inCache || now.Sub(old.Timestamp) > as.renotify {
|
||||
s.Score *= as.tgCache.Weight(ctx, s.ID, now)
|
||||
a, err := alert.Make(ctx, as.tgCache, s, origScore)
|
||||
s.Score = as.tgCache.ApplyAlertRules(s, now)
|
||||
a, err := as.makeAlert(ctx, s, origScore)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("makeAlert: %w", err)
|
||||
}
|
||||
|
@ -198,7 +208,9 @@ func (as *alerter) eval(ctx context.Context, now time.Time, testMode bool) ([]al
|
|||
}
|
||||
|
||||
func (as *alerter) testNotifyHandler(w http.ResponseWriter, r *http.Request) {
|
||||
alerts := make([]alert.Alert, 0, len(as.scores))
|
||||
as.RLock()
|
||||
defer as.RUnlock()
|
||||
alerts := make([]Alert, 0, len(as.scores))
|
||||
ctx := r.Context()
|
||||
|
||||
alerts, err := as.eval(ctx, time.Now(), true)
|
||||
|
@ -208,7 +220,7 @@ func (as *alerter) testNotifyHandler(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
err = as.notifier.Send(ctx, alerts)
|
||||
err = as.sendNotification(ctx, alerts)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("test notification send")
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
|
@ -219,8 +231,8 @@ func (as *alerter) testNotifyHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
// scoredTGs gets a list of TGs.
|
||||
func (as *alerter) scoredTGs() []talkgroups.ID {
|
||||
tgs := make([]talkgroups.ID, 0, len(as.scores))
|
||||
func (as *alerter) scoredTGs() []cl.Talkgroup {
|
||||
tgs := make([]cl.Talkgroup, 0, len(as.scores))
|
||||
for _, s := range as.scores {
|
||||
tgs = append(tgs, s.ID)
|
||||
}
|
||||
|
@ -244,18 +256,101 @@ func (as *alerter) notify(ctx context.Context) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
as.Lock()
|
||||
defer as.Unlock()
|
||||
|
||||
notifications, err := as.eval(ctx, time.Now(), false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(notifications) > 0 {
|
||||
return as.notifier.Send(ctx, notifications)
|
||||
return as.sendNotification(ctx, notifications)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type Alert struct {
|
||||
ID uuid.UUID
|
||||
Timestamp time.Time
|
||||
TGName string
|
||||
Score trending.Score[cl.Talkgroup]
|
||||
OrigScore float64
|
||||
Weight float32
|
||||
Suppressed bool
|
||||
}
|
||||
|
||||
func (a *Alert) ToAddAlertParams() database.AddAlertParams {
|
||||
f32score := float32(a.Score.Score)
|
||||
f32origscore := float32(a.OrigScore)
|
||||
|
||||
var origScore *float32
|
||||
if a.Score.Score != a.OrigScore {
|
||||
origScore = &f32origscore
|
||||
}
|
||||
|
||||
return database.AddAlertParams{
|
||||
ID: a.ID,
|
||||
Time: pgtype.Timestamptz{Time: a.Timestamp, Valid: true},
|
||||
PackedTg: a.Score.ID.Pack(),
|
||||
Weight: &a.Weight,
|
||||
Score: &f32score,
|
||||
OrigScore: origScore,
|
||||
Notified: !a.Suppressed,
|
||||
}
|
||||
}
|
||||
|
||||
// sendNotification renders and sends the notification.
|
||||
func (as *alerter) sendNotification(ctx context.Context, n []Alert) error {
|
||||
msgBuffer := new(bytes.Buffer)
|
||||
|
||||
err := notificationTemplate.Execute(msgBuffer, n)
|
||||
if err != nil {
|
||||
return fmt.Errorf("notification template render: %w", err)
|
||||
}
|
||||
|
||||
log.Debug().Str("msg", msgBuffer.String()).Msg("notifying")
|
||||
|
||||
return as.notifier.Send(ctx, NotificationSubject, msgBuffer.String())
|
||||
}
|
||||
|
||||
// makeAlert creates a notification for later rendering by the template.
|
||||
// It takes a talkgroup Score as input.
|
||||
func (as *alerter) makeAlert(ctx context.Context, score trending.Score[cl.Talkgroup], origScore float64) (Alert, error) {
|
||||
d := Alert{
|
||||
ID: uuid.New(),
|
||||
Score: score,
|
||||
Timestamp: time.Now(),
|
||||
Weight: 1.0,
|
||||
OrigScore: origScore,
|
||||
}
|
||||
|
||||
tgRecord, has := as.tgCache.TG(ctx, score.ID)
|
||||
switch has {
|
||||
case true:
|
||||
d.Weight = tgRecord.Weight
|
||||
if tgRecord.SystemName == "" {
|
||||
tgRecord.SystemName = strconv.Itoa(int(score.ID.System))
|
||||
}
|
||||
|
||||
if tgRecord.Name != nil {
|
||||
d.TGName = fmt.Sprintf("%s %s (%d)", tgRecord.SystemName, *tgRecord.Name, score.ID.Talkgroup)
|
||||
} else {
|
||||
d.TGName = fmt.Sprintf("%s:%d", tgRecord.SystemName, int(score.ID.Talkgroup))
|
||||
}
|
||||
case false:
|
||||
system, has := as.tgCache.SystemName(ctx, int(score.ID.System))
|
||||
if has {
|
||||
d.TGName = fmt.Sprintf("%s:%d", system, int(score.ID.Talkgroup))
|
||||
} else {
|
||||
d.TGName = fmt.Sprintf("%d:%d", int(score.ID.System), int(score.ID.Talkgroup))
|
||||
}
|
||||
}
|
||||
|
||||
return d, nil
|
||||
}
|
||||
|
||||
// cleanCache clears the cache of aged-out entries
|
||||
func (as *alerter) cleanCache() {
|
||||
if as.notifier == nil {
|
||||
|
@ -274,7 +369,7 @@ func (as *alerter) cleanCache() {
|
|||
}
|
||||
}
|
||||
|
||||
func (as *alerter) newTimeSeries(id talkgroups.ID) trending.TimeSeries {
|
||||
func (as *alerter) newTimeSeries(id cl.Talkgroup) trending.TimeSeries {
|
||||
ts, _ := timeseries.NewTimeSeries(timeseries.WithGranularities(
|
||||
[]timeseries.Granularity{
|
||||
{Granularity: time.Second, Count: 60},
|
||||
|
@ -322,7 +417,7 @@ func (as *alerter) backfill(ctx context.Context, since time.Time, until time.Tim
|
|||
defer as.Unlock()
|
||||
|
||||
for rows.Next() {
|
||||
var tg talkgroups.ID
|
||||
var tg cl.Talkgroup
|
||||
var callDate time.Time
|
||||
if err := rows.Scan(&tg.System, &tg.Talkgroup, &callDate); err != nil {
|
||||
return count, err
|
||||
|
@ -345,7 +440,7 @@ func (as *alerter) SinkType() string {
|
|||
return "alerting"
|
||||
}
|
||||
|
||||
func (as *alerter) Call(ctx context.Context, call *calls.Call) error {
|
||||
func (as *alerter) Call(ctx context.Context, call *cl.Call) error {
|
||||
as.Lock()
|
||||
defer as.Unlock()
|
||||
as.scorer.AddEvent(call.TalkgroupTuple(), call.DateTime)
|
||||
|
@ -358,7 +453,7 @@ func (*alerter) Enabled() bool { return true }
|
|||
// noopAlerter is used when alerting is disabled.
|
||||
type noopAlerter struct{}
|
||||
|
||||
func (*noopAlerter) SinkType() string { return "noopAlerter" }
|
||||
func (*noopAlerter) Call(_ context.Context, _ *calls.Call) error { return nil }
|
||||
func (*noopAlerter) Go(_ context.Context) {}
|
||||
func (*noopAlerter) Enabled() bool { return false }
|
||||
func (*noopAlerter) SinkType() string { return "noopAlerter" }
|
||||
func (*noopAlerter) Call(_ context.Context, _ *cl.Call) error { return nil }
|
||||
func (*noopAlerter) Go(_ context.Context) {}
|
||||
func (*noopAlerter) Enabled() bool { return false }
|
||||
|
|
|
@ -12,8 +12,8 @@ import (
|
|||
"dynatron.me/x/stillbox/internal/forms"
|
||||
"dynatron.me/x/stillbox/internal/jsontime"
|
||||
"dynatron.me/x/stillbox/internal/trending"
|
||||
cl "dynatron.me/x/stillbox/pkg/calls"
|
||||
"dynatron.me/x/stillbox/pkg/config"
|
||||
"dynatron.me/x/stillbox/pkg/talkgroups"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
@ -58,9 +58,9 @@ func (s *Simulation) stepClock(t time.Time) {
|
|||
}
|
||||
|
||||
// Simulate begins the simulation using the DB handle from ctx. It returns final scores.
|
||||
func (s *Simulation) Simulate(ctx context.Context) (trending.Scores[talkgroups.ID], error) {
|
||||
func (s *Simulation) Simulate(ctx context.Context) (trending.Scores[cl.Talkgroup], error) {
|
||||
now := time.Now()
|
||||
tgc := talkgroups.NewCache()
|
||||
tgc := cl.NewTalkgroupCache()
|
||||
|
||||
s.Enable = true
|
||||
s.alerter = New(s.Alerting, tgc, WithClock(&s.clock)).(*alerter)
|
||||
|
|
|
@ -2,15 +2,17 @@ package alerting
|
|||
|
||||
import (
|
||||
_ "embed"
|
||||
"errors"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"dynatron.me/x/stillbox/pkg/calls"
|
||||
"dynatron.me/x/stillbox/pkg/config"
|
||||
"dynatron.me/x/stillbox/pkg/database"
|
||||
"dynatron.me/x/stillbox/pkg/talkgroups"
|
||||
|
||||
"dynatron.me/x/stillbox/internal/common"
|
||||
"dynatron.me/x/stillbox/internal/jsontime"
|
||||
"dynatron.me/x/stillbox/internal/trending"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
@ -20,14 +22,41 @@ import (
|
|||
//go:embed stats.html
|
||||
var statsTemplateFile string
|
||||
|
||||
var (
|
||||
statTmpl = template.Must(template.New("stats").Funcs(common.FuncMap).Parse(statsTemplateFile))
|
||||
)
|
||||
|
||||
type stats interface {
|
||||
PrivateRoutes(chi.Router)
|
||||
}
|
||||
|
||||
var (
|
||||
funcMap = template.FuncMap{
|
||||
"f": common.FmtFloat,
|
||||
"dict": func(values ...interface{}) (map[string]interface{}, error) {
|
||||
if len(values)%2 != 0 {
|
||||
return nil, errors.New("invalid dict call")
|
||||
}
|
||||
dict := make(map[string]interface{}, len(values)/2)
|
||||
for i := 0; i < len(values); i += 2 {
|
||||
key, ok := values[i].(string)
|
||||
if !ok {
|
||||
return nil, errors.New("dict keys must be strings")
|
||||
}
|
||||
dict[key] = values[i+1]
|
||||
}
|
||||
return dict, nil
|
||||
},
|
||||
"formTime": func(t jsontime.Time) string {
|
||||
return time.Time(t).Format("2006-01-02T15:04")
|
||||
},
|
||||
"ago": func(s string) (string, error) {
|
||||
d, err := time.ParseDuration(s)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return time.Now().Add(-d).Format("2006-01-02T15:04"), nil
|
||||
},
|
||||
}
|
||||
statTmpl = template.Must(template.New("stats").Funcs(funcMap).Parse(statsTemplateFile))
|
||||
)
|
||||
|
||||
func (as *alerter) PrivateRoutes(r chi.Router) {
|
||||
r.Get("/tgstats", as.tgStatsHandler)
|
||||
r.Post("/tgstats", as.simulateHandler)
|
||||
|
@ -40,21 +69,21 @@ func (as *alerter) tgStatsHandler(w http.ResponseWriter, r *http.Request) {
|
|||
ctx := r.Context()
|
||||
db := database.FromCtx(ctx)
|
||||
|
||||
tgs, err := db.GetTalkgroupsWithLearnedByPackedIDs(ctx, as.packedScoredTGs())
|
||||
tgs, err := db.GetTalkgroupsByPackedIDs(ctx, as.packedScoredTGs())
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("stats TG get failed")
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
tgMap := make(map[talkgroups.ID]database.GetTalkgroupsWithLearnedByPackedIDsRow, len(tgs))
|
||||
tgMap := make(map[calls.Talkgroup]database.GetTalkgroupsByPackedIDsRow, len(tgs))
|
||||
for _, t := range tgs {
|
||||
tgMap[talkgroups.ID{System: uint32(t.System.ID), Talkgroup: uint32(t.Talkgroup.Tgid)}] = t
|
||||
tgMap[calls.Talkgroup{System: uint32(t.SystemID), Talkgroup: uint32(t.ID)}] = t
|
||||
}
|
||||
|
||||
renderData := struct {
|
||||
TGs map[talkgroups.ID]database.GetTalkgroupsWithLearnedByPackedIDsRow
|
||||
Scores trending.Scores[talkgroups.ID]
|
||||
TGs map[calls.Talkgroup]database.GetTalkgroupsByPackedIDsRow
|
||||
Scores trending.Scores[calls.Talkgroup]
|
||||
LastScore time.Time
|
||||
Simulation *Simulation
|
||||
Config config.Alerting
|
||||
|
|
|
@ -85,8 +85,8 @@
|
|||
{{ range .Scores }}
|
||||
{{ $tg := (index $.TGs .ID) }}
|
||||
<tr>
|
||||
<td>{{ $tg.System.Name}}</td>
|
||||
<td>{{ $tg.Talkgroup.Name}}</td>
|
||||
<td>{{ $tg.Name_2}}</td>
|
||||
<td>{{ $tg.Name}}</td>
|
||||
<td>{{ .ID.Talkgroup }}</td>
|
||||
<td>{{ f .Count 0 }}</td>
|
||||
<td>{{ f .RecentCount 0 }}</td>
|
||||
|
|
127
pkg/api/api.go
127
pkg/api/api.go
|
@ -1,127 +0,0 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"dynatron.me/x/stillbox/pkg/talkgroups"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-viper/mapstructure/v2"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type API interface {
|
||||
Subrouter() http.Handler
|
||||
}
|
||||
|
||||
type api struct {
|
||||
tgs talkgroups.Store
|
||||
}
|
||||
|
||||
func New(tgs talkgroups.Store) API {
|
||||
s := &api{
|
||||
tgs: tgs,
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func (a *api) Subrouter() http.Handler {
|
||||
r := chi.NewMux()
|
||||
|
||||
r.Get("/talkgroup/{system:\\d+}/{id:\\d+}", a.talkgroup)
|
||||
r.Get("/talkgroup/{system:\\d+}/", a.talkgroup)
|
||||
r.Get("/talkgroup/", a.talkgroup)
|
||||
return r
|
||||
}
|
||||
|
||||
var statusMapping = map[error]int{
|
||||
talkgroups.ErrNotFound: http.StatusNotFound,
|
||||
pgx.ErrNoRows: http.StatusNotFound,
|
||||
}
|
||||
|
||||
func httpCode(err error) int {
|
||||
c, ok := statusMapping[err]
|
||||
if ok {
|
||||
return c
|
||||
}
|
||||
|
||||
for e, c := range statusMapping { // check if err wraps an error we know about
|
||||
if errors.Is(err, e) {
|
||||
return c
|
||||
}
|
||||
}
|
||||
|
||||
return http.StatusInternalServerError
|
||||
}
|
||||
|
||||
func (a *api) writeResponse(w http.ResponseWriter, r *http.Request, data interface{}, err error) {
|
||||
if err != nil {
|
||||
log.Error().Str("path", r.URL.Path).Err(err).Msg("request failed")
|
||||
http.Error(w, err.Error(), httpCode(err))
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
enc := json.NewEncoder(w)
|
||||
err = enc.Encode(data)
|
||||
if err != nil {
|
||||
log.Error().Str("path", r.URL.Path).Err(err).Msg("response marshal failed")
|
||||
http.Error(w, err.Error(), httpCode(err))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func decodeParams(d interface{}, r *http.Request) error {
|
||||
params := chi.RouteContext(r.Context()).URLParams
|
||||
m := make(map[string]string, len(params.Keys))
|
||||
|
||||
for i, k := range params.Keys {
|
||||
m[k] = params.Values[i]
|
||||
}
|
||||
|
||||
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
||||
Metadata: nil,
|
||||
Result: d,
|
||||
TagName: "param",
|
||||
WeaklyTypedInput: true,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return dec.Decode(m)
|
||||
}
|
||||
|
||||
func (a *api) badReq(w http.ResponseWriter, err error) {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
}
|
||||
|
||||
func (a *api) talkgroup(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
p := struct {
|
||||
System *int `param:"system"`
|
||||
ID *int `param:"id"`
|
||||
}{}
|
||||
|
||||
err := decodeParams(&p, r)
|
||||
if err != nil {
|
||||
a.badReq(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
var res interface{}
|
||||
switch {
|
||||
case p.System != nil && p.ID != nil:
|
||||
res, err = a.tgs.TG(ctx, talkgroups.TG(*p.System, *p.ID))
|
||||
case p.System != nil:
|
||||
res, err = a.tgs.SystemTGs(ctx, int32(*p.System))
|
||||
default:
|
||||
res, err = a.tgs.TGs(ctx, nil)
|
||||
}
|
||||
|
||||
a.writeResponse(w, r, res, err)
|
||||
}
|
|
@ -4,10 +4,7 @@ import (
|
|||
"errors"
|
||||
"net/http"
|
||||
|
||||
_ "embed"
|
||||
|
||||
"dynatron.me/x/stillbox/pkg/config"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/jwtauth/v5"
|
||||
)
|
||||
|
||||
|
@ -69,20 +66,3 @@ func ErrorResponse(w http.ResponseWriter, err error) {
|
|||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Auth) PublicRoutes(r chi.Router) {
|
||||
r.Post("/api/login", a.routeAuth)
|
||||
r.Get("/api/login", a.routeLogin)
|
||||
}
|
||||
|
||||
func (a *Auth) PrivateRoutes(r chi.Router) {
|
||||
r.Get("/refresh", a.routeRefresh)
|
||||
}
|
||||
|
||||
//go:embed login.html
|
||||
var loginPage []byte
|
||||
|
||||
func (a *Auth) routeLogin(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Add("Content-Type", "text/html")
|
||||
_, _ = w.Write(loginPage)
|
||||
}
|
||||
|
|
|
@ -110,6 +110,14 @@ func (a *Auth) newToken(uid int32) string {
|
|||
return tokenString
|
||||
}
|
||||
|
||||
func (a *Auth) PublicRoutes(r chi.Router) {
|
||||
r.Post("/login", a.routeAuth)
|
||||
}
|
||||
|
||||
func (a *Auth) PrivateRoutes(r chi.Router) {
|
||||
r.Get("/refresh", a.routeRefresh)
|
||||
}
|
||||
|
||||
func (a *Auth) allowInsecureCookie(r *http.Request) bool {
|
||||
host := strings.Split(r.Host, ":")
|
||||
v, has := a.cfg.AllowInsecure[host[0]]
|
||||
|
|
|
@ -1,17 +0,0 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Login</title>
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
<form action="/login" method="POST">
|
||||
<label for="username">Username: </label>
|
||||
<input type="text" name="username" />
|
||||
<label for="password">Password: </label>
|
||||
<input type="password" name="password" />
|
||||
<input type="submit" value="Login" />
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
|
@ -1,40 +1,21 @@
|
|||
package talkgroups
|
||||
package calls
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"dynatron.me/x/stillbox/internal/ruletime"
|
||||
"dynatron.me/x/stillbox/internal/trending"
|
||||
)
|
||||
|
||||
type AlertConfig struct {
|
||||
sync.RWMutex
|
||||
m map[ID][]AlertRule
|
||||
}
|
||||
type AlertConfig map[Talkgroup][]AlertRule
|
||||
|
||||
type AlertRule struct {
|
||||
Times []ruletime.RuleTime `json:"times"`
|
||||
ScoreMultiplier float32 `json:"mult"`
|
||||
}
|
||||
|
||||
func NewAlertConfig() AlertConfig {
|
||||
return AlertConfig{
|
||||
m: make(map[ID][]AlertRule),
|
||||
}
|
||||
}
|
||||
|
||||
func (ac *AlertConfig) GetRules(tg ID) []AlertRule {
|
||||
ac.RLock()
|
||||
defer ac.RUnlock()
|
||||
|
||||
return ac.m[tg]
|
||||
}
|
||||
|
||||
func (ac *AlertConfig) UnmarshalTGRules(tg ID, confBytes []byte) error {
|
||||
ac.Lock()
|
||||
defer ac.Unlock()
|
||||
|
||||
func (ac AlertConfig) AddAlertConfig(tg Talkgroup, confBytes []byte) error {
|
||||
if len(confBytes) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
@ -45,19 +26,17 @@ func (ac *AlertConfig) UnmarshalTGRules(tg ID, confBytes []byte) error {
|
|||
return err
|
||||
}
|
||||
|
||||
ac.m[tg] = rules
|
||||
ac[tg] = rules
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ac *AlertConfig) ApplyAlertRules(id ID, t time.Time, coversOpts ...ruletime.CoversOption) float64 {
|
||||
ac.RLock()
|
||||
s, has := ac.m[id]
|
||||
ac.RUnlock()
|
||||
func (ac AlertConfig) ApplyAlertRules(score trending.Score[Talkgroup], t time.Time, coversOpts ...ruletime.CoversOption) float64 {
|
||||
s, has := ac[score.ID]
|
||||
if !has {
|
||||
return 1.0
|
||||
return score.Score
|
||||
}
|
||||
|
||||
final := 1.0
|
||||
final := score.Score
|
||||
|
||||
for _, ar := range s {
|
||||
if ar.MatchTime(t, coversOpts...) {
|
||||
|
@ -68,13 +47,6 @@ func (ac *AlertConfig) ApplyAlertRules(id ID, t time.Time, coversOpts ...ruletim
|
|||
return final
|
||||
}
|
||||
|
||||
func (ac *AlertConfig) Invalidate() {
|
||||
ac.Lock()
|
||||
defer ac.Unlock()
|
||||
|
||||
clear(ac.m)
|
||||
}
|
||||
|
||||
func (ar *AlertRule) MatchTime(t time.Time, coversOpts ...ruletime.CoversOption) bool {
|
||||
for _, at := range ar.Times {
|
||||
if at.Covers(t, coversOpts...) {
|
|
@ -1,4 +1,4 @@
|
|||
package talkgroups_test
|
||||
package calls_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
@ -8,26 +8,26 @@ import (
|
|||
|
||||
"dynatron.me/x/stillbox/internal/ruletime"
|
||||
"dynatron.me/x/stillbox/internal/trending"
|
||||
"dynatron.me/x/stillbox/pkg/talkgroups"
|
||||
"dynatron.me/x/stillbox/pkg/calls"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestAlertConfig(t *testing.T) {
|
||||
ac := talkgroups.NewAlertConfig()
|
||||
ac := make(calls.AlertConfig)
|
||||
parseTests := []struct {
|
||||
name string
|
||||
tg talkgroups.ID
|
||||
tg calls.Talkgroup
|
||||
conf string
|
||||
compare []talkgroups.AlertRule
|
||||
compare []calls.AlertRule
|
||||
expectErr error
|
||||
}{
|
||||
{
|
||||
name: "base case",
|
||||
tg: talkgroups.TG(197, 3),
|
||||
tg: calls.TG(197, 3),
|
||||
conf: `[{"times":["7:00+2h","01:00+1h","16:00+1h","19:00+4h"],"mult":0.2},{"times":["11:00+1h","15:00+30m","16:03+20m"],"mult":2.0}]`,
|
||||
compare: []talkgroups.AlertRule{
|
||||
compare: []calls.AlertRule{
|
||||
{
|
||||
Times: []ruletime.RuleTime{
|
||||
ruletime.Must(ruletime.New("7:00+2h")),
|
||||
|
@ -49,7 +49,7 @@ func TestAlertConfig(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "bad spec",
|
||||
tg: talkgroups.TG(197, 3),
|
||||
tg: calls.TG(197, 3),
|
||||
conf: `[{"times":["26:00+2h","01:00+1h","19:00+4h"],"mult":0.2},{"times":["11:00+1h","15:00+30m"],"mult":2.0}]`,
|
||||
expectErr: errors.New("'26:00+2h': invalid hours"),
|
||||
},
|
||||
|
@ -57,12 +57,12 @@ func TestAlertConfig(t *testing.T) {
|
|||
|
||||
for _, tc := range parseTests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := ac.UnmarshalTGRules(tc.tg, []byte(tc.conf))
|
||||
err := ac.AddAlertConfig(tc.tg, []byte(tc.conf))
|
||||
if tc.expectErr != nil {
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), tc.expectErr.Error())
|
||||
} else {
|
||||
assert.Equal(t, tc.compare, ac.GetRules(tc.tg))
|
||||
assert.Equal(t, tc.compare, ac[tc.tg])
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -78,42 +78,42 @@ func TestAlertConfig(t *testing.T) {
|
|||
|
||||
evalTests := []struct {
|
||||
name string
|
||||
tg talkgroups.ID
|
||||
tg calls.Talkgroup
|
||||
t time.Time
|
||||
origScore float64
|
||||
expectScore float64
|
||||
}{
|
||||
{
|
||||
name: "base eval",
|
||||
tg: talkgroups.TG(197, 3),
|
||||
tg: calls.TG(197, 3),
|
||||
t: tMust("1:20"),
|
||||
origScore: 3,
|
||||
expectScore: 0.6,
|
||||
},
|
||||
{
|
||||
name: "base eval",
|
||||
tg: talkgroups.TG(197, 3),
|
||||
tg: calls.TG(197, 3),
|
||||
t: tMust("23:03"),
|
||||
origScore: 3,
|
||||
expectScore: 3,
|
||||
},
|
||||
{
|
||||
name: "base eval",
|
||||
tg: talkgroups.TG(197, 3),
|
||||
tg: calls.TG(197, 3),
|
||||
t: tMust("8:03"),
|
||||
origScore: 1.0,
|
||||
expectScore: 0.2,
|
||||
},
|
||||
{
|
||||
name: "base eval",
|
||||
tg: talkgroups.TG(197, 3),
|
||||
tg: calls.TG(197, 3),
|
||||
t: tMust("15:15"),
|
||||
origScore: 3.0,
|
||||
expectScore: 6.0,
|
||||
},
|
||||
{
|
||||
name: "overlapping eval",
|
||||
tg: talkgroups.TG(197, 3),
|
||||
tg: calls.TG(197, 3),
|
||||
t: tMust("16:10"),
|
||||
origScore: 1.0,
|
||||
expectScore: 0.4,
|
||||
|
@ -122,11 +122,11 @@ func TestAlertConfig(t *testing.T) {
|
|||
|
||||
for _, tc := range evalTests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
cs := trending.Score[talkgroups.ID]{
|
||||
cs := trending.Score[calls.Talkgroup]{
|
||||
ID: tc.tg,
|
||||
Score: tc.origScore,
|
||||
}
|
||||
assert.Equal(t, tc.expectScore, toFixed(cs.Score*ac.ApplyAlertRules(cs.ID, tc.t), 5))
|
||||
assert.Equal(t, tc.expectScore, toFixed(ac.ApplyAlertRules(cs, tc.t), 5))
|
||||
})
|
||||
}
|
||||
}
|
|
@ -7,9 +7,7 @@ import (
|
|||
"dynatron.me/x/stillbox/internal/audio"
|
||||
"dynatron.me/x/stillbox/pkg/auth"
|
||||
"dynatron.me/x/stillbox/pkg/pb"
|
||||
"dynatron.me/x/stillbox/pkg/talkgroups"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
)
|
||||
|
||||
|
@ -19,7 +17,7 @@ func (d CallDuration) Duration() time.Duration {
|
|||
return time.Duration(d)
|
||||
}
|
||||
|
||||
func (d CallDuration) MsInt32Ptr() *int32 {
|
||||
func (d CallDuration) Int32Ptr() *int32 {
|
||||
if time.Duration(d) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
@ -33,7 +31,6 @@ func (d CallDuration) Seconds() int32 {
|
|||
}
|
||||
|
||||
type Call struct {
|
||||
ID uuid.UUID
|
||||
Audio []byte
|
||||
AudioName string
|
||||
AudioType string
|
||||
|
@ -70,7 +67,6 @@ func Make(call *Call, dontStore bool) (*Call, error) {
|
|||
}
|
||||
|
||||
call.shouldStore = dontStore
|
||||
call.ID = uuid.New()
|
||||
|
||||
return call, nil
|
||||
}
|
||||
|
@ -95,7 +91,6 @@ func toInt32Slice(s []int) []int32 {
|
|||
|
||||
func (c *Call) ToPB() *pb.Call {
|
||||
return &pb.Call{
|
||||
Id: c.ID.String(),
|
||||
AudioName: c.AudioName,
|
||||
AudioType: c.AudioType,
|
||||
DateTime: timestamppb.New(c.DateTime),
|
||||
|
@ -106,7 +101,7 @@ func (c *Call) ToPB() *pb.Call {
|
|||
Frequencies: toInt64Slice(c.Frequencies),
|
||||
Patches: toInt32Slice(c.Patches),
|
||||
Sources: toInt32Slice(c.Sources),
|
||||
Duration: c.Duration.MsInt32Ptr(),
|
||||
Duration: c.Duration.Int32Ptr(),
|
||||
Audio: c.Audio,
|
||||
}
|
||||
}
|
||||
|
@ -133,7 +128,3 @@ func (c *Call) computeLength() (err error) {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Call) TalkgroupTuple() talkgroups.ID {
|
||||
return talkgroups.TG(c.System, c.Talkgroup)
|
||||
}
|
||||
|
|
|
@ -5,18 +5,16 @@ import (
|
|||
|
||||
"dynatron.me/x/stillbox/pkg/database"
|
||||
"dynatron.me/x/stillbox/pkg/pb"
|
||||
|
||||
tgs "dynatron.me/x/stillbox/pkg/talkgroups"
|
||||
)
|
||||
|
||||
type TalkgroupFilter struct {
|
||||
Talkgroups []tgs.ID `json:"talkgroups,omitempty"`
|
||||
TalkgroupsNot []tgs.ID `json:"talkgroupsNot,omitempty"`
|
||||
TalkgroupTagsAll []string `json:"talkgroupTagsAll,omitempty"`
|
||||
TalkgroupTagsAny []string `json:"talkgroupTagsAny,omitempty"`
|
||||
TalkgroupTagsNot []string `json:"talkgroupTagsNot,omitempty"`
|
||||
Talkgroups []Talkgroup `json:"talkgroups,omitempty"`
|
||||
TalkgroupsNot []Talkgroup `json:"talkgroupsNot,omitempty"`
|
||||
TalkgroupTagsAll []string `json:"talkgroupTagsAll,omitempty"`
|
||||
TalkgroupTagsAny []string `json:"talkgroupTagsAny,omitempty"`
|
||||
TalkgroupTagsNot []string `json:"talkgroupTagsNot,omitempty"`
|
||||
|
||||
talkgroups map[tgs.ID]bool
|
||||
talkgroups map[Talkgroup]bool
|
||||
}
|
||||
|
||||
func TalkgroupFilterFromPB(ctx context.Context, p *pb.Filter) (*TalkgroupFilter, error) {
|
||||
|
@ -27,9 +25,9 @@ func TalkgroupFilterFromPB(ctx context.Context, p *pb.Filter) (*TalkgroupFilter,
|
|||
}
|
||||
|
||||
if l := len(p.Talkgroups); l > 0 {
|
||||
tgf.Talkgroups = make([]tgs.ID, l)
|
||||
tgf.Talkgroups = make([]Talkgroup, l)
|
||||
for i, t := range p.Talkgroups {
|
||||
tgf.Talkgroups[i] = tgs.ID{
|
||||
tgf.Talkgroups[i] = Talkgroup{
|
||||
System: uint32(t.System),
|
||||
Talkgroup: uint32(t.Talkgroup),
|
||||
}
|
||||
|
@ -37,9 +35,9 @@ func TalkgroupFilterFromPB(ctx context.Context, p *pb.Filter) (*TalkgroupFilter,
|
|||
}
|
||||
|
||||
if l := len(p.TalkgroupsNot); l > 0 {
|
||||
tgf.TalkgroupsNot = make([]tgs.ID, l)
|
||||
tgf.TalkgroupsNot = make([]Talkgroup, l)
|
||||
for i, t := range p.TalkgroupsNot {
|
||||
tgf.TalkgroupsNot[i] = tgs.ID{
|
||||
tgf.TalkgroupsNot[i] = Talkgroup{
|
||||
System: uint32(t.System),
|
||||
Talkgroup: uint32(t.Talkgroup),
|
||||
}
|
||||
|
@ -53,12 +51,12 @@ func (f *TalkgroupFilter) hasTags() bool {
|
|||
return len(f.TalkgroupTagsAny) > 0 || len(f.TalkgroupTagsAll) > 0 || len(f.TalkgroupTagsNot) > 0
|
||||
}
|
||||
|
||||
func (f *TalkgroupFilter) GetFinalTalkgroups() map[tgs.ID]bool {
|
||||
func (f *TalkgroupFilter) GetFinalTalkgroups() map[Talkgroup]bool {
|
||||
return f.talkgroups
|
||||
}
|
||||
|
||||
func (f *TalkgroupFilter) compile(ctx context.Context) error {
|
||||
f.talkgroups = make(map[tgs.ID]bool)
|
||||
f.talkgroups = make(map[Talkgroup]bool)
|
||||
for _, tg := range f.Talkgroups {
|
||||
f.talkgroups[tg] = true
|
||||
}
|
||||
|
@ -71,7 +69,7 @@ func (f *TalkgroupFilter) compile(ctx context.Context) error {
|
|||
}
|
||||
|
||||
for _, tg := range tagTGs {
|
||||
f.talkgroups[tgs.ID{System: uint32(tg.SystemID), Talkgroup: uint32(tg.Tgid)}] = true
|
||||
f.talkgroups[Talkgroup{System: uint32(tg.SystemID), Talkgroup: uint32(tg.Tgid)}] = true
|
||||
}
|
||||
}
|
||||
|
||||
|
|
191
pkg/calls/talkgroups.go
Normal file
191
pkg/calls/talkgroups.go
Normal file
|
@ -0,0 +1,191 @@
|
|||
package calls
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"dynatron.me/x/stillbox/pkg/database"
|
||||
|
||||
"dynatron.me/x/stillbox/internal/ruletime"
|
||||
"dynatron.me/x/stillbox/internal/trending"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type Talkgroup struct {
|
||||
System uint32
|
||||
Talkgroup uint32
|
||||
}
|
||||
|
||||
func (c *Call) TalkgroupTuple() Talkgroup {
|
||||
return Talkgroup{System: uint32(c.System), Talkgroup: uint32(c.Talkgroup)}
|
||||
}
|
||||
|
||||
func TG[T int | uint | int64 | uint64 | int32 | uint32](sys, tgid T) Talkgroup {
|
||||
return Talkgroup{
|
||||
System: uint32(sys),
|
||||
Talkgroup: uint32(tgid),
|
||||
}
|
||||
}
|
||||
|
||||
func (t Talkgroup) Pack() int64 {
|
||||
// P25 system IDs are 12 bits, so we can fit them in a signed 8 byte int (int64, pg INT8)
|
||||
return int64((int64(t.System) << 32) | int64(t.Talkgroup))
|
||||
}
|
||||
|
||||
func (t Talkgroup) String() string {
|
||||
return fmt.Sprintf("%d:%d", t.System, t.Talkgroup)
|
||||
}
|
||||
|
||||
func PackedTGs(tg []Talkgroup) []int64 {
|
||||
s := make([]int64, len(tg))
|
||||
|
||||
for i, v := range tg {
|
||||
s[i] = v.Pack()
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
type tgMap map[Talkgroup]database.GetTalkgroupWithLearnedByPackedIDsRow
|
||||
|
||||
type TalkgroupCache interface {
|
||||
TG(ctx context.Context, tg Talkgroup) (database.GetTalkgroupWithLearnedByPackedIDsRow, bool)
|
||||
SystemName(ctx context.Context, id int) (string, bool)
|
||||
ApplyAlertRules(score trending.Score[Talkgroup], t time.Time, coversOpts ...ruletime.CoversOption) float64
|
||||
Hint(ctx context.Context, tgs []Talkgroup) error
|
||||
Load(ctx context.Context, tgs []int64) error
|
||||
Invalidate()
|
||||
}
|
||||
|
||||
func (t *talkgroupCache) Invalidate() {
|
||||
t.Lock()
|
||||
defer t.Unlock()
|
||||
clear(t.tgs)
|
||||
clear(t.systems)
|
||||
clear(t.AlertConfig)
|
||||
}
|
||||
|
||||
type talkgroupCache struct {
|
||||
sync.RWMutex
|
||||
AlertConfig
|
||||
tgs tgMap
|
||||
systems map[int32]string
|
||||
}
|
||||
|
||||
func NewTalkgroupCache() TalkgroupCache {
|
||||
tgc := &talkgroupCache{
|
||||
tgs: make(tgMap),
|
||||
systems: make(map[int32]string),
|
||||
AlertConfig: make(AlertConfig),
|
||||
}
|
||||
|
||||
return tgc
|
||||
}
|
||||
|
||||
func (t *talkgroupCache) Hint(ctx context.Context, tgs []Talkgroup) error {
|
||||
t.RLock()
|
||||
var toLoad []int64
|
||||
if len(t.tgs) > len(tgs)/2 { // TODO: instrument this
|
||||
for _, tg := range tgs {
|
||||
_, ok := t.tgs[tg]
|
||||
if !ok {
|
||||
toLoad = append(toLoad, tg.Pack())
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
toLoad = make([]int64, 0, len(tgs))
|
||||
for _, g := range tgs {
|
||||
toLoad = append(toLoad, g.Pack())
|
||||
}
|
||||
}
|
||||
|
||||
if len(toLoad) > 0 {
|
||||
t.RUnlock()
|
||||
return t.Load(ctx, toLoad)
|
||||
}
|
||||
|
||||
t.RUnlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *talkgroupCache) add(rec database.GetTalkgroupWithLearnedByPackedIDsRow) error {
|
||||
tg := TG(rec.SystemID, rec.Tgid)
|
||||
t.tgs[tg] = rec
|
||||
t.systems[rec.SystemID] = rec.SystemName
|
||||
|
||||
return t.AlertConfig.AddAlertConfig(tg, rec.AlertConfig)
|
||||
}
|
||||
|
||||
func (t *talkgroupCache) Load(ctx context.Context, tgs []int64) error {
|
||||
tgRecords, err := database.FromCtx(ctx).GetTalkgroupWithLearnedByPackedIDs(ctx, tgs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
t.Lock()
|
||||
defer t.Unlock()
|
||||
|
||||
for _, rec := range tgRecords {
|
||||
err := t.add(rec)
|
||||
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("add alert config fail")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *talkgroupCache) TG(ctx context.Context, tg Talkgroup) (database.GetTalkgroupWithLearnedByPackedIDsRow, bool) {
|
||||
t.RLock()
|
||||
rec, has := t.tgs[tg]
|
||||
t.RUnlock()
|
||||
|
||||
if has {
|
||||
return rec, has
|
||||
}
|
||||
|
||||
recs, err := database.FromCtx(ctx).GetTalkgroupWithLearnedByPackedIDs(ctx, []int64{tg.Pack()})
|
||||
switch err {
|
||||
case nil:
|
||||
case pgx.ErrNoRows:
|
||||
return database.GetTalkgroupWithLearnedByPackedIDsRow{}, false
|
||||
default:
|
||||
log.Error().Err(err).Msg("TG() cache add db get")
|
||||
return database.GetTalkgroupWithLearnedByPackedIDsRow{}, false
|
||||
}
|
||||
|
||||
if len(recs) < 1 {
|
||||
return database.GetTalkgroupWithLearnedByPackedIDsRow{}, false
|
||||
}
|
||||
|
||||
t.Lock()
|
||||
defer t.Unlock()
|
||||
err = t.add(recs[0])
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("TG() cache add")
|
||||
return recs[0], false
|
||||
}
|
||||
|
||||
return recs[0], true
|
||||
}
|
||||
|
||||
func (t *talkgroupCache) SystemName(ctx context.Context, id int) (name string, has bool) {
|
||||
n, has := t.systems[int32(id)]
|
||||
|
||||
if !has {
|
||||
sys, err := database.FromCtx(ctx).GetSystemName(ctx, id)
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
|
||||
return sys, true
|
||||
}
|
||||
|
||||
return n, has
|
||||
}
|
|
@ -65,18 +65,13 @@ type Alerting struct {
|
|||
type Notify []NotifyService
|
||||
|
||||
type NotifyService struct {
|
||||
Provider string `yaml:"provider" json:"provider"`
|
||||
SubjectTemplate *string `yaml:"subjectTemplate" json:"subjectTemplate"`
|
||||
BodyTemplate *string `yaml:"bodyTemplate" json:"bodyTemplate"`
|
||||
Config map[string]interface{} `yaml:"config" json:"config"`
|
||||
Provider string `json:"provider"`
|
||||
Config map[string]interface{} `json:"config"`
|
||||
}
|
||||
|
||||
func (n *NotifyService) GetS(k, defaultVal string) string {
|
||||
if v, has := n.Config[k]; has {
|
||||
if v, isString := v.(string); isString {
|
||||
return v
|
||||
}
|
||||
log.Error().Str("configKey", k).Str("provider", n.Provider).Str("default", defaultVal).Msg("notify config value is not a string! using default")
|
||||
if v, has := n.Config[k].(string); has {
|
||||
return v
|
||||
}
|
||||
|
||||
return defaultVal
|
||||
|
|
|
@ -52,48 +52,30 @@ func (q *Queries) AddAlert(ctx context.Context, arg AddAlertParams) error {
|
|||
return err
|
||||
}
|
||||
|
||||
const addCall = `-- name: AddCall :exec
|
||||
const addCall = `-- name: AddCall :one
|
||||
INSERT INTO calls (
|
||||
id,
|
||||
submitter,
|
||||
system,
|
||||
talkgroup,
|
||||
call_date,
|
||||
audio_name,
|
||||
audio_blob,
|
||||
audio_type,
|
||||
audio_url,
|
||||
duration,
|
||||
frequency,
|
||||
frequencies,
|
||||
patches,
|
||||
tg_label,
|
||||
tg_alpha_tag,
|
||||
tg_group,
|
||||
source
|
||||
) VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
$4,
|
||||
$5,
|
||||
$6,
|
||||
$7,
|
||||
$8,
|
||||
$9,
|
||||
$10,
|
||||
$11,
|
||||
$12,
|
||||
$13,
|
||||
$14,
|
||||
$15,
|
||||
$16,
|
||||
$17
|
||||
)
|
||||
id,
|
||||
submitter,
|
||||
system,
|
||||
talkgroup,
|
||||
call_date,
|
||||
audio_name,
|
||||
audio_blob,
|
||||
audio_type,
|
||||
audio_url,
|
||||
duration,
|
||||
frequency,
|
||||
frequencies,
|
||||
patches,
|
||||
tg_label,
|
||||
tg_alpha_tag,
|
||||
tg_group,
|
||||
source
|
||||
) VALUES (gen_random_uuid(), $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)
|
||||
RETURNING id
|
||||
`
|
||||
|
||||
type AddCallParams struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Submitter *int32 `json:"submitter"`
|
||||
System int `json:"system"`
|
||||
Talkgroup int `json:"talkgroup"`
|
||||
|
@ -112,9 +94,8 @@ type AddCallParams struct {
|
|||
Source int `json:"source"`
|
||||
}
|
||||
|
||||
func (q *Queries) AddCall(ctx context.Context, arg AddCallParams) error {
|
||||
_, err := q.db.Exec(ctx, addCall,
|
||||
arg.ID,
|
||||
func (q *Queries) AddCall(ctx context.Context, arg AddCallParams) (uuid.UUID, error) {
|
||||
row := q.db.QueryRow(ctx, addCall,
|
||||
arg.Submitter,
|
||||
arg.System,
|
||||
arg.Talkgroup,
|
||||
|
@ -132,7 +113,9 @@ func (q *Queries) AddCall(ctx context.Context, arg AddCallParams) error {
|
|||
arg.TgGroup,
|
||||
arg.Source,
|
||||
)
|
||||
return err
|
||||
var id uuid.UUID
|
||||
err := row.Scan(&id)
|
||||
return id, err
|
||||
}
|
||||
|
||||
const getDatabaseSize = `-- name: GetDatabaseSize :one
|
||||
|
|
|
@ -1,11 +0,0 @@
|
|||
package database
|
||||
|
||||
func (d GetTalkgroupsWithLearnedByPackedIDsRow) GetTalkgroup() Talkgroup { return d.Talkgroup }
|
||||
func (d GetTalkgroupsWithLearnedByPackedIDsRow) GetSystem() System { return d.System }
|
||||
func (d GetTalkgroupsWithLearnedByPackedIDsRow) GetLearned() bool { return d.Learned }
|
||||
func (g GetTalkgroupsWithLearnedRow) GetTalkgroup() Talkgroup { return g.Talkgroup }
|
||||
func (g GetTalkgroupsWithLearnedRow) GetSystem() System { return g.System }
|
||||
func (g GetTalkgroupsWithLearnedRow) GetLearned() bool { return g.Learned }
|
||||
func (g GetTalkgroupsWithLearnedBySystemRow) GetTalkgroup() Talkgroup { return g.Talkgroup }
|
||||
func (g GetTalkgroupsWithLearnedBySystemRow) GetSystem() System { return g.System }
|
||||
func (g GetTalkgroupsWithLearnedBySystemRow) GetLearned() bool { return g.Learned }
|
|
@ -13,7 +13,7 @@ import (
|
|||
|
||||
type Querier interface {
|
||||
AddAlert(ctx context.Context, arg AddAlertParams) error
|
||||
AddCall(ctx context.Context, arg AddCallParams) error
|
||||
AddCall(ctx context.Context, arg AddCallParams) (uuid.UUID, error)
|
||||
BulkSetTalkgroupTags(ctx context.Context, iD int64, tags []string) error
|
||||
CreateAPIKey(ctx context.Context, owner int, expires pgtype.Timestamp, disabled *bool) (ApiKey, error)
|
||||
CreateUser(ctx context.Context, arg CreateUserParams) (User, error)
|
||||
|
@ -22,16 +22,14 @@ type Querier interface {
|
|||
GetAPIKey(ctx context.Context, apiKey string) (ApiKey, error)
|
||||
GetDatabaseSize(ctx context.Context) (string, error)
|
||||
GetSystemName(ctx context.Context, systemID int) (string, error)
|
||||
GetTalkgroup(ctx context.Context, systemID int, tgid int) (GetTalkgroupRow, error)
|
||||
GetTalkgroup(ctx context.Context, systemID int, tgid int) (Talkgroup, error)
|
||||
GetTalkgroupIDsByTags(ctx context.Context, anytags []string, alltags []string, nottags []string) ([]GetTalkgroupIDsByTagsRow, error)
|
||||
GetTalkgroupTags(ctx context.Context, sys int, tg int) ([]string, error)
|
||||
GetTalkgroupWithLearned(ctx context.Context, systemID int, tgid int) (GetTalkgroupWithLearnedRow, error)
|
||||
GetTalkgroupWithLearnedByPackedIDs(ctx context.Context, dollar_1 []int64) ([]GetTalkgroupWithLearnedByPackedIDsRow, error)
|
||||
GetTalkgroupsByPackedIDs(ctx context.Context, dollar_1 []int64) ([]GetTalkgroupsByPackedIDsRow, error)
|
||||
GetTalkgroupsWithAllTags(ctx context.Context, tags []string) ([]GetTalkgroupsWithAllTagsRow, error)
|
||||
GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) ([]GetTalkgroupsWithAnyTagsRow, error)
|
||||
GetTalkgroupsWithLearned(ctx context.Context) ([]GetTalkgroupsWithLearnedRow, error)
|
||||
GetTalkgroupsWithLearnedByPackedIDs(ctx context.Context, dollar_1 []int64) ([]GetTalkgroupsWithLearnedByPackedIDsRow, error)
|
||||
GetTalkgroupsWithLearnedBySystem(ctx context.Context, system int32) ([]GetTalkgroupsWithLearnedBySystemRow, error)
|
||||
GetTalkgroupsWithAllTags(ctx context.Context, tags []string) ([]Talkgroup, error)
|
||||
GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) ([]Talkgroup, error)
|
||||
GetUserByID(ctx context.Context, id int32) (User, error)
|
||||
GetUserByUID(ctx context.Context, id int32) (User, error)
|
||||
GetUserByUsername(ctx context.Context, username string) (User, error)
|
||||
|
|
|
@ -31,30 +31,26 @@ func (q *Queries) GetSystemName(ctx context.Context, systemID int) (string, erro
|
|||
}
|
||||
|
||||
const getTalkgroup = `-- name: GetTalkgroup :one
|
||||
SELECT talkgroups.id, talkgroups.system_id, talkgroups.tgid, talkgroups.name, talkgroups.alpha_tag, talkgroups.tg_group, talkgroups.frequency, talkgroups.metadata, talkgroups.tags, talkgroups.alert, talkgroups.alert_config, talkgroups.weight FROM talkgroups
|
||||
SELECT id, system_id, tgid, name, alpha_tag, tg_group, frequency, metadata, tags, alert, alert_config, weight FROM talkgroups
|
||||
WHERE id = systg2id($1, $2)
|
||||
`
|
||||
|
||||
type GetTalkgroupRow struct {
|
||||
Talkgroup Talkgroup `json:"talkgroup"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetTalkgroup(ctx context.Context, systemID int, tgid int) (GetTalkgroupRow, error) {
|
||||
func (q *Queries) GetTalkgroup(ctx context.Context, systemID int, tgid int) (Talkgroup, error) {
|
||||
row := q.db.QueryRow(ctx, getTalkgroup, systemID, tgid)
|
||||
var i GetTalkgroupRow
|
||||
var i Talkgroup
|
||||
err := row.Scan(
|
||||
&i.Talkgroup.ID,
|
||||
&i.Talkgroup.SystemID,
|
||||
&i.Talkgroup.Tgid,
|
||||
&i.Talkgroup.Name,
|
||||
&i.Talkgroup.AlphaTag,
|
||||
&i.Talkgroup.TgGroup,
|
||||
&i.Talkgroup.Frequency,
|
||||
&i.Talkgroup.Metadata,
|
||||
&i.Talkgroup.Tags,
|
||||
&i.Talkgroup.Alert,
|
||||
&i.Talkgroup.AlertConfig,
|
||||
&i.Talkgroup.Weight,
|
||||
&i.ID,
|
||||
&i.SystemID,
|
||||
&i.Tgid,
|
||||
&i.Name,
|
||||
&i.AlphaTag,
|
||||
&i.TgGroup,
|
||||
&i.Frequency,
|
||||
&i.Metadata,
|
||||
&i.Tags,
|
||||
&i.Alert,
|
||||
&i.AlertConfig,
|
||||
&i.Weight,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
@ -105,17 +101,19 @@ func (q *Queries) GetTalkgroupTags(ctx context.Context, sys int, tg int) ([]stri
|
|||
|
||||
const getTalkgroupWithLearned = `-- name: GetTalkgroupWithLearned :one
|
||||
SELECT
|
||||
tg.id, tg.system_id, tg.tgid, tg.name, tg.alpha_tag, tg.tg_group, tg.frequency, tg.metadata, tg.tags, tg.alert, tg.alert_config, tg.weight, sys.id, sys.name,
|
||||
tg.id, tg.system_id, sys.name system_name, tg.tgid, tg.name,
|
||||
tg.tg_group, tg.frequency, tg.metadata, tg.tags, tg.alpha_tag,
|
||||
tg.alert, tg.weight, tg.alert_config,
|
||||
FALSE learned
|
||||
FROM talkgroups tg
|
||||
JOIN systems sys ON tg.system_id = sys.id
|
||||
WHERE tg.id = systg2id($1, $2)
|
||||
UNION
|
||||
SELECT
|
||||
tgl.id::INT8, tgl.system_id::INT4, tgl.tgid::INT4, tgl.name,
|
||||
tgl.alpha_tag, tgl.alpha_tag, NULL::INTEGER, NULL::JSONB,
|
||||
CASE WHEN tgl.alpha_tag IS NULL THEN NULL ELSE ARRAY[tgl.alpha_tag] END,
|
||||
TRUE, NULL::JSONB, 1.0, sys.id, sys.name,
|
||||
tgl.id::INT8, tgl.system_id::INT4, sys.name system_name, tgl.tgid::INT4, tgl.name,
|
||||
tgl.alpha_tag, NULL::INTEGER, NULL::JSONB,
|
||||
CASE WHEN tgl.alpha_tag IS NULL THEN NULL ELSE ARRAY[tgl.alpha_tag] END, tgl.alpha_tag,
|
||||
TRUE, 1.0, NULL::JSONB,
|
||||
TRUE learned
|
||||
FROM talkgroups_learned tgl
|
||||
JOIN systems sys ON tgl.system_id = sys.id
|
||||
|
@ -123,43 +121,138 @@ WHERE tgl.system_id = $1 AND tgl.tgid = $2 AND ignored IS NOT TRUE
|
|||
`
|
||||
|
||||
type GetTalkgroupWithLearnedRow struct {
|
||||
Talkgroup Talkgroup `json:"talkgroup"`
|
||||
System System `json:"system"`
|
||||
Learned bool `json:"learned"`
|
||||
ID int64 `json:"id"`
|
||||
SystemID int32 `json:"system_id"`
|
||||
SystemName string `json:"system_name"`
|
||||
Tgid int32 `json:"tgid"`
|
||||
Name *string `json:"name"`
|
||||
TgGroup *string `json:"tg_group"`
|
||||
Frequency *int32 `json:"frequency"`
|
||||
Metadata []byte `json:"metadata"`
|
||||
Tags []string `json:"tags"`
|
||||
AlphaTag *string `json:"alpha_tag"`
|
||||
Alert bool `json:"alert"`
|
||||
Weight float32 `json:"weight"`
|
||||
AlertConfig []byte `json:"alert_config"`
|
||||
Learned bool `json:"learned"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetTalkgroupWithLearned(ctx context.Context, systemID int, tgid int) (GetTalkgroupWithLearnedRow, error) {
|
||||
row := q.db.QueryRow(ctx, getTalkgroupWithLearned, systemID, tgid)
|
||||
var i GetTalkgroupWithLearnedRow
|
||||
err := row.Scan(
|
||||
&i.Talkgroup.ID,
|
||||
&i.Talkgroup.SystemID,
|
||||
&i.Talkgroup.Tgid,
|
||||
&i.Talkgroup.Name,
|
||||
&i.Talkgroup.AlphaTag,
|
||||
&i.Talkgroup.TgGroup,
|
||||
&i.Talkgroup.Frequency,
|
||||
&i.Talkgroup.Metadata,
|
||||
&i.Talkgroup.Tags,
|
||||
&i.Talkgroup.Alert,
|
||||
&i.Talkgroup.AlertConfig,
|
||||
&i.Talkgroup.Weight,
|
||||
&i.System.ID,
|
||||
&i.System.Name,
|
||||
&i.ID,
|
||||
&i.SystemID,
|
||||
&i.SystemName,
|
||||
&i.Tgid,
|
||||
&i.Name,
|
||||
&i.TgGroup,
|
||||
&i.Frequency,
|
||||
&i.Metadata,
|
||||
&i.Tags,
|
||||
&i.AlphaTag,
|
||||
&i.Alert,
|
||||
&i.Weight,
|
||||
&i.AlertConfig,
|
||||
&i.Learned,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getTalkgroupWithLearnedByPackedIDs = `-- name: GetTalkgroupWithLearnedByPackedIDs :many
|
||||
SELECT
|
||||
tg.id, tg.system_id, sys.name system_name, tg.tgid, tg.name,
|
||||
tg.tg_group, tg.frequency, tg.metadata, tg.tags, tg.alpha_tag,
|
||||
tg.alert, tg.weight, tg.alert_config,
|
||||
FALSE learned
|
||||
FROM talkgroups tg
|
||||
JOIN systems sys ON tg.system_id = sys.id
|
||||
WHERE tg.id = ANY($1::INT8[])
|
||||
UNION
|
||||
SELECT
|
||||
tgl.id::INT8, tgl.system_id::INT4, sys.name system_name, tgl.tgid::INT4, tgl.name,
|
||||
tgl.alpha_tag, NULL::INTEGER, NULL::JSONB,
|
||||
CASE WHEN tgl.alpha_tag IS NULL THEN NULL ELSE ARRAY[tgl.alpha_tag] END, tgl.alpha_tag,
|
||||
TRUE, 1.0, NULL::JSONB,
|
||||
TRUE learned
|
||||
FROM talkgroups_learned tgl
|
||||
JOIN systems sys ON tgl.system_id = sys.id
|
||||
WHERE systg2id(tgl.system_id, tgl.tgid) = ANY($1::INT8[]) AND ignored IS NOT TRUE
|
||||
`
|
||||
|
||||
type GetTalkgroupWithLearnedByPackedIDsRow struct {
|
||||
ID int64 `json:"id"`
|
||||
SystemID int32 `json:"system_id"`
|
||||
SystemName string `json:"system_name"`
|
||||
Tgid int32 `json:"tgid"`
|
||||
Name *string `json:"name"`
|
||||
TgGroup *string `json:"tg_group"`
|
||||
Frequency *int32 `json:"frequency"`
|
||||
Metadata []byte `json:"metadata"`
|
||||
Tags []string `json:"tags"`
|
||||
AlphaTag *string `json:"alpha_tag"`
|
||||
Alert bool `json:"alert"`
|
||||
Weight float32 `json:"weight"`
|
||||
AlertConfig []byte `json:"alert_config"`
|
||||
Learned bool `json:"learned"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetTalkgroupWithLearnedByPackedIDs(ctx context.Context, dollar_1 []int64) ([]GetTalkgroupWithLearnedByPackedIDsRow, error) {
|
||||
rows, err := q.db.Query(ctx, getTalkgroupWithLearnedByPackedIDs, dollar_1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetTalkgroupWithLearnedByPackedIDsRow
|
||||
for rows.Next() {
|
||||
var i GetTalkgroupWithLearnedByPackedIDsRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.SystemID,
|
||||
&i.SystemName,
|
||||
&i.Tgid,
|
||||
&i.Name,
|
||||
&i.TgGroup,
|
||||
&i.Frequency,
|
||||
&i.Metadata,
|
||||
&i.Tags,
|
||||
&i.AlphaTag,
|
||||
&i.Alert,
|
||||
&i.Weight,
|
||||
&i.AlertConfig,
|
||||
&i.Learned,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getTalkgroupsByPackedIDs = `-- name: GetTalkgroupsByPackedIDs :many
|
||||
SELECT tg.id, tg.system_id, tg.tgid, tg.name, tg.alpha_tag, tg.tg_group, tg.frequency, tg.metadata, tg.tags, tg.alert, tg.alert_config, tg.weight, sys.id, sys.name FROM talkgroups tg
|
||||
SELECT tg.id, system_id, tgid, tg.name, alpha_tag, tg_group, frequency, metadata, tags, alert, alert_config, weight, sys.id, sys.name FROM talkgroups tg
|
||||
JOIN systems sys ON tg.system_id = sys.id
|
||||
WHERE tg.id = ANY($1::INT8[])
|
||||
`
|
||||
|
||||
type GetTalkgroupsByPackedIDsRow struct {
|
||||
Talkgroup Talkgroup `json:"talkgroup"`
|
||||
System System `json:"system"`
|
||||
ID int64 `json:"id"`
|
||||
SystemID int32 `json:"system_id"`
|
||||
Tgid int32 `json:"tgid"`
|
||||
Name *string `json:"name"`
|
||||
AlphaTag *string `json:"alpha_tag"`
|
||||
TgGroup *string `json:"tg_group"`
|
||||
Frequency *int32 `json:"frequency"`
|
||||
Metadata []byte `json:"metadata"`
|
||||
Tags []string `json:"tags"`
|
||||
Alert bool `json:"alert"`
|
||||
AlertConfig []byte `json:"alert_config"`
|
||||
Weight float32 `json:"weight"`
|
||||
ID_2 int `json:"id_2"`
|
||||
Name_2 string `json:"name_2"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetTalkgroupsByPackedIDs(ctx context.Context, dollar_1 []int64) ([]GetTalkgroupsByPackedIDsRow, error) {
|
||||
|
@ -172,20 +265,20 @@ func (q *Queries) GetTalkgroupsByPackedIDs(ctx context.Context, dollar_1 []int64
|
|||
for rows.Next() {
|
||||
var i GetTalkgroupsByPackedIDsRow
|
||||
if err := rows.Scan(
|
||||
&i.Talkgroup.ID,
|
||||
&i.Talkgroup.SystemID,
|
||||
&i.Talkgroup.Tgid,
|
||||
&i.Talkgroup.Name,
|
||||
&i.Talkgroup.AlphaTag,
|
||||
&i.Talkgroup.TgGroup,
|
||||
&i.Talkgroup.Frequency,
|
||||
&i.Talkgroup.Metadata,
|
||||
&i.Talkgroup.Tags,
|
||||
&i.Talkgroup.Alert,
|
||||
&i.Talkgroup.AlertConfig,
|
||||
&i.Talkgroup.Weight,
|
||||
&i.System.ID,
|
||||
&i.System.Name,
|
||||
&i.ID,
|
||||
&i.SystemID,
|
||||
&i.Tgid,
|
||||
&i.Name,
|
||||
&i.AlphaTag,
|
||||
&i.TgGroup,
|
||||
&i.Frequency,
|
||||
&i.Metadata,
|
||||
&i.Tags,
|
||||
&i.Alert,
|
||||
&i.AlertConfig,
|
||||
&i.Weight,
|
||||
&i.ID_2,
|
||||
&i.Name_2,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -198,36 +291,32 @@ func (q *Queries) GetTalkgroupsByPackedIDs(ctx context.Context, dollar_1 []int64
|
|||
}
|
||||
|
||||
const getTalkgroupsWithAllTags = `-- name: GetTalkgroupsWithAllTags :many
|
||||
SELECT talkgroups.id, talkgroups.system_id, talkgroups.tgid, talkgroups.name, talkgroups.alpha_tag, talkgroups.tg_group, talkgroups.frequency, talkgroups.metadata, talkgroups.tags, talkgroups.alert, talkgroups.alert_config, talkgroups.weight FROM talkgroups
|
||||
SELECT id, system_id, tgid, name, alpha_tag, tg_group, frequency, metadata, tags, alert, alert_config, weight FROM talkgroups
|
||||
WHERE tags && ARRAY[$1]
|
||||
`
|
||||
|
||||
type GetTalkgroupsWithAllTagsRow struct {
|
||||
Talkgroup Talkgroup `json:"talkgroup"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetTalkgroupsWithAllTags(ctx context.Context, tags []string) ([]GetTalkgroupsWithAllTagsRow, error) {
|
||||
func (q *Queries) GetTalkgroupsWithAllTags(ctx context.Context, tags []string) ([]Talkgroup, error) {
|
||||
rows, err := q.db.Query(ctx, getTalkgroupsWithAllTags, tags)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetTalkgroupsWithAllTagsRow
|
||||
var items []Talkgroup
|
||||
for rows.Next() {
|
||||
var i GetTalkgroupsWithAllTagsRow
|
||||
var i Talkgroup
|
||||
if err := rows.Scan(
|
||||
&i.Talkgroup.ID,
|
||||
&i.Talkgroup.SystemID,
|
||||
&i.Talkgroup.Tgid,
|
||||
&i.Talkgroup.Name,
|
||||
&i.Talkgroup.AlphaTag,
|
||||
&i.Talkgroup.TgGroup,
|
||||
&i.Talkgroup.Frequency,
|
||||
&i.Talkgroup.Metadata,
|
||||
&i.Talkgroup.Tags,
|
||||
&i.Talkgroup.Alert,
|
||||
&i.Talkgroup.AlertConfig,
|
||||
&i.Talkgroup.Weight,
|
||||
&i.ID,
|
||||
&i.SystemID,
|
||||
&i.Tgid,
|
||||
&i.Name,
|
||||
&i.AlphaTag,
|
||||
&i.TgGroup,
|
||||
&i.Frequency,
|
||||
&i.Metadata,
|
||||
&i.Tags,
|
||||
&i.Alert,
|
||||
&i.AlertConfig,
|
||||
&i.Weight,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -240,218 +329,32 @@ func (q *Queries) GetTalkgroupsWithAllTags(ctx context.Context, tags []string) (
|
|||
}
|
||||
|
||||
const getTalkgroupsWithAnyTags = `-- name: GetTalkgroupsWithAnyTags :many
|
||||
SELECT talkgroups.id, talkgroups.system_id, talkgroups.tgid, talkgroups.name, talkgroups.alpha_tag, talkgroups.tg_group, talkgroups.frequency, talkgroups.metadata, talkgroups.tags, talkgroups.alert, talkgroups.alert_config, talkgroups.weight FROM talkgroups
|
||||
SELECT id, system_id, tgid, name, alpha_tag, tg_group, frequency, metadata, tags, alert, alert_config, weight FROM talkgroups
|
||||
WHERE tags @> ARRAY[$1]
|
||||
`
|
||||
|
||||
type GetTalkgroupsWithAnyTagsRow struct {
|
||||
Talkgroup Talkgroup `json:"talkgroup"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) ([]GetTalkgroupsWithAnyTagsRow, error) {
|
||||
func (q *Queries) GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) ([]Talkgroup, error) {
|
||||
rows, err := q.db.Query(ctx, getTalkgroupsWithAnyTags, tags)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetTalkgroupsWithAnyTagsRow
|
||||
var items []Talkgroup
|
||||
for rows.Next() {
|
||||
var i GetTalkgroupsWithAnyTagsRow
|
||||
var i Talkgroup
|
||||
if err := rows.Scan(
|
||||
&i.Talkgroup.ID,
|
||||
&i.Talkgroup.SystemID,
|
||||
&i.Talkgroup.Tgid,
|
||||
&i.Talkgroup.Name,
|
||||
&i.Talkgroup.AlphaTag,
|
||||
&i.Talkgroup.TgGroup,
|
||||
&i.Talkgroup.Frequency,
|
||||
&i.Talkgroup.Metadata,
|
||||
&i.Talkgroup.Tags,
|
||||
&i.Talkgroup.Alert,
|
||||
&i.Talkgroup.AlertConfig,
|
||||
&i.Talkgroup.Weight,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getTalkgroupsWithLearned = `-- name: GetTalkgroupsWithLearned :many
|
||||
SELECT
|
||||
tg.id, tg.system_id, tg.tgid, tg.name, tg.alpha_tag, tg.tg_group, tg.frequency, tg.metadata, tg.tags, tg.alert, tg.alert_config, tg.weight, sys.id, sys.name,
|
||||
FALSE learned
|
||||
FROM talkgroups tg
|
||||
JOIN systems sys ON tg.system_id = sys.id
|
||||
UNION
|
||||
SELECT
|
||||
tgl.id::INT8, tgl.system_id::INT4, tgl.tgid::INT4, tgl.name,
|
||||
tgl.alpha_tag, tgl.alpha_tag, NULL::INTEGER, NULL::JSONB,
|
||||
CASE WHEN tgl.alpha_tag IS NULL THEN NULL ELSE ARRAY[tgl.alpha_tag] END,
|
||||
TRUE, NULL::JSONB, 1.0, sys.id, sys.name,
|
||||
TRUE learned
|
||||
FROM talkgroups_learned tgl
|
||||
JOIN systems sys ON tgl.system_id = sys.id
|
||||
WHERE ignored IS NOT TRUE
|
||||
`
|
||||
|
||||
type GetTalkgroupsWithLearnedRow struct {
|
||||
Talkgroup Talkgroup `json:"talkgroup"`
|
||||
System System `json:"system"`
|
||||
Learned bool `json:"learned"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetTalkgroupsWithLearned(ctx context.Context) ([]GetTalkgroupsWithLearnedRow, error) {
|
||||
rows, err := q.db.Query(ctx, getTalkgroupsWithLearned)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetTalkgroupsWithLearnedRow
|
||||
for rows.Next() {
|
||||
var i GetTalkgroupsWithLearnedRow
|
||||
if err := rows.Scan(
|
||||
&i.Talkgroup.ID,
|
||||
&i.Talkgroup.SystemID,
|
||||
&i.Talkgroup.Tgid,
|
||||
&i.Talkgroup.Name,
|
||||
&i.Talkgroup.AlphaTag,
|
||||
&i.Talkgroup.TgGroup,
|
||||
&i.Talkgroup.Frequency,
|
||||
&i.Talkgroup.Metadata,
|
||||
&i.Talkgroup.Tags,
|
||||
&i.Talkgroup.Alert,
|
||||
&i.Talkgroup.AlertConfig,
|
||||
&i.Talkgroup.Weight,
|
||||
&i.System.ID,
|
||||
&i.System.Name,
|
||||
&i.Learned,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getTalkgroupsWithLearnedByPackedIDs = `-- name: GetTalkgroupsWithLearnedByPackedIDs :many
|
||||
SELECT
|
||||
tg.id, tg.system_id, tg.tgid, tg.name, tg.alpha_tag, tg.tg_group, tg.frequency, tg.metadata, tg.tags, tg.alert, tg.alert_config, tg.weight, sys.id, sys.name,
|
||||
FALSE learned
|
||||
FROM talkgroups tg
|
||||
JOIN systems sys ON tg.system_id = sys.id
|
||||
WHERE tg.id = ANY($1::INT8[])
|
||||
UNION
|
||||
SELECT
|
||||
tgl.id::INT8, tgl.system_id::INT4, tgl.tgid::INT4, tgl.name,
|
||||
tgl.alpha_tag, tgl.alpha_tag, NULL::INTEGER, NULL::JSONB,
|
||||
CASE WHEN tgl.alpha_tag IS NULL THEN NULL ELSE ARRAY[tgl.alpha_tag] END,
|
||||
TRUE, NULL::JSONB, 1.0, sys.id, sys.name,
|
||||
TRUE learned
|
||||
FROM talkgroups_learned tgl
|
||||
JOIN systems sys ON tgl.system_id = sys.id
|
||||
WHERE systg2id(tgl.system_id, tgl.tgid) = ANY($1::INT8[]) AND ignored IS NOT TRUE
|
||||
`
|
||||
|
||||
type GetTalkgroupsWithLearnedByPackedIDsRow struct {
|
||||
Talkgroup Talkgroup `json:"talkgroup"`
|
||||
System System `json:"system"`
|
||||
Learned bool `json:"learned"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetTalkgroupsWithLearnedByPackedIDs(ctx context.Context, dollar_1 []int64) ([]GetTalkgroupsWithLearnedByPackedIDsRow, error) {
|
||||
rows, err := q.db.Query(ctx, getTalkgroupsWithLearnedByPackedIDs, dollar_1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetTalkgroupsWithLearnedByPackedIDsRow
|
||||
for rows.Next() {
|
||||
var i GetTalkgroupsWithLearnedByPackedIDsRow
|
||||
if err := rows.Scan(
|
||||
&i.Talkgroup.ID,
|
||||
&i.Talkgroup.SystemID,
|
||||
&i.Talkgroup.Tgid,
|
||||
&i.Talkgroup.Name,
|
||||
&i.Talkgroup.AlphaTag,
|
||||
&i.Talkgroup.TgGroup,
|
||||
&i.Talkgroup.Frequency,
|
||||
&i.Talkgroup.Metadata,
|
||||
&i.Talkgroup.Tags,
|
||||
&i.Talkgroup.Alert,
|
||||
&i.Talkgroup.AlertConfig,
|
||||
&i.Talkgroup.Weight,
|
||||
&i.System.ID,
|
||||
&i.System.Name,
|
||||
&i.Learned,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getTalkgroupsWithLearnedBySystem = `-- name: GetTalkgroupsWithLearnedBySystem :many
|
||||
SELECT
|
||||
tg.id, tg.system_id, tg.tgid, tg.name, tg.alpha_tag, tg.tg_group, tg.frequency, tg.metadata, tg.tags, tg.alert, tg.alert_config, tg.weight, sys.id, sys.name,
|
||||
FALSE learned
|
||||
FROM talkgroups tg
|
||||
JOIN systems sys ON tg.system_id = sys.id
|
||||
WHERE tg.system_id = $1
|
||||
UNION
|
||||
SELECT
|
||||
tgl.id::INT8, tgl.system_id::INT4, tgl.tgid::INT4, tgl.name,
|
||||
tgl.alpha_tag, tgl.alpha_tag, NULL::INTEGER, NULL::JSONB,
|
||||
CASE WHEN tgl.alpha_tag IS NULL THEN NULL ELSE ARRAY[tgl.alpha_tag] END,
|
||||
TRUE, NULL::JSONB, 1.0, sys.id, sys.name,
|
||||
TRUE learned
|
||||
FROM talkgroups_learned tgl
|
||||
JOIN systems sys ON tgl.system_id = sys.id
|
||||
WHERE tgl.system_id = $1 AND ignored IS NOT TRUE
|
||||
`
|
||||
|
||||
type GetTalkgroupsWithLearnedBySystemRow struct {
|
||||
Talkgroup Talkgroup `json:"talkgroup"`
|
||||
System System `json:"system"`
|
||||
Learned bool `json:"learned"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetTalkgroupsWithLearnedBySystem(ctx context.Context, system int32) ([]GetTalkgroupsWithLearnedBySystemRow, error) {
|
||||
rows, err := q.db.Query(ctx, getTalkgroupsWithLearnedBySystem, system)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetTalkgroupsWithLearnedBySystemRow
|
||||
for rows.Next() {
|
||||
var i GetTalkgroupsWithLearnedBySystemRow
|
||||
if err := rows.Scan(
|
||||
&i.Talkgroup.ID,
|
||||
&i.Talkgroup.SystemID,
|
||||
&i.Talkgroup.Tgid,
|
||||
&i.Talkgroup.Name,
|
||||
&i.Talkgroup.AlphaTag,
|
||||
&i.Talkgroup.TgGroup,
|
||||
&i.Talkgroup.Frequency,
|
||||
&i.Talkgroup.Metadata,
|
||||
&i.Talkgroup.Tags,
|
||||
&i.Talkgroup.Alert,
|
||||
&i.Talkgroup.AlertConfig,
|
||||
&i.Talkgroup.Weight,
|
||||
&i.System.ID,
|
||||
&i.System.Name,
|
||||
&i.Learned,
|
||||
&i.ID,
|
||||
&i.SystemID,
|
||||
&i.Tgid,
|
||||
&i.Name,
|
||||
&i.AlphaTag,
|
||||
&i.TgGroup,
|
||||
&i.Frequency,
|
||||
&i.Metadata,
|
||||
&i.Tags,
|
||||
&i.Alert,
|
||||
&i.AlertConfig,
|
||||
&i.Weight,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -1,88 +0,0 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
const getTalkgroupWithLearnedByPackedIDsTest = `-- name: GetTalkgroupWithLearnedByPackedIDs :many
|
||||
SELECT
|
||||
tg.id, tg.system_id, tg.tgid, tg.name, tg.alpha_tag, tg.tg_group, tg.frequency, tg.metadata, tg.tags, tg.alert, tg.alert_config, tg.weight, sys.id, sys.name,
|
||||
FALSE learned
|
||||
FROM talkgroups tg
|
||||
JOIN systems sys ON tg.system_id = sys.id
|
||||
WHERE tg.id = ANY($1::INT8[])
|
||||
UNION
|
||||
SELECT
|
||||
tgl.id::INT8, tgl.system_id::INT4, tgl.tgid::INT4, tgl.name,
|
||||
tgl.alpha_tag, tgl.alpha_tag, NULL::INTEGER, NULL::JSONB,
|
||||
CASE WHEN tgl.alpha_tag IS NULL THEN NULL ELSE ARRAY[tgl.alpha_tag] END,
|
||||
TRUE, NULL::JSONB, 1.0, sys.id, sys.name,
|
||||
TRUE learned
|
||||
FROM talkgroups_learned tgl
|
||||
JOIN systems sys ON tgl.system_id = sys.id
|
||||
WHERE systg2id(tgl.system_id, tgl.tgid) = ANY($1::INT8[]) AND ignored IS NOT TRUE
|
||||
`
|
||||
const getTalkgroupWithLearnedTest = `-- name: GetTalkgroupWithLearned :one
|
||||
SELECT
|
||||
tg.id, tg.system_id, tg.tgid, tg.name, tg.alpha_tag, tg.tg_group, tg.frequency, tg.metadata, tg.tags, tg.alert, tg.alert_config, tg.weight, sys.id, sys.name,
|
||||
FALSE learned
|
||||
FROM talkgroups tg
|
||||
JOIN systems sys ON tg.system_id = sys.id
|
||||
WHERE tg.id = systg2id($1, $2)
|
||||
UNION
|
||||
SELECT
|
||||
tgl.id::INT8, tgl.system_id::INT4, tgl.tgid::INT4, tgl.name,
|
||||
tgl.alpha_tag, tgl.alpha_tag, NULL::INTEGER, NULL::JSONB,
|
||||
CASE WHEN tgl.alpha_tag IS NULL THEN NULL ELSE ARRAY[tgl.alpha_tag] END,
|
||||
TRUE, NULL::JSONB, 1.0, sys.id, sys.name,
|
||||
TRUE learned
|
||||
FROM talkgroups_learned tgl
|
||||
JOIN systems sys ON tgl.system_id = sys.id
|
||||
WHERE tgl.system_id = $1 AND tgl.tgid = $2 AND ignored IS NOT TRUE
|
||||
`
|
||||
|
||||
const getTalkgroupsWithLearnedBySystemTest = `-- name: GetTalkgroupsWithLearnedBySystem :many
|
||||
SELECT
|
||||
tg.id, tg.system_id, tg.tgid, tg.name, tg.alpha_tag, tg.tg_group, tg.frequency, tg.metadata, tg.tags, tg.alert, tg.alert_config, tg.weight, sys.id, sys.name,
|
||||
FALSE learned
|
||||
FROM talkgroups tg
|
||||
JOIN systems sys ON tg.system_id = sys.id
|
||||
WHERE tg.system_id = $1
|
||||
UNION
|
||||
SELECT
|
||||
tgl.id::INT8, tgl.system_id::INT4, tgl.tgid::INT4, tgl.name,
|
||||
tgl.alpha_tag, tgl.alpha_tag, NULL::INTEGER, NULL::JSONB,
|
||||
CASE WHEN tgl.alpha_tag IS NULL THEN NULL ELSE ARRAY[tgl.alpha_tag] END,
|
||||
TRUE, NULL::JSONB, 1.0, sys.id, sys.name,
|
||||
TRUE learned
|
||||
FROM talkgroups_learned tgl
|
||||
JOIN systems sys ON tgl.system_id = sys.id
|
||||
WHERE tg.system_id = $1 AND ignored IS NOT TRUE
|
||||
`
|
||||
|
||||
const getTalkgroupsWithLearnedTest = `-- name: GetTalkgroupsWithLearned :many
|
||||
SELECT
|
||||
tg.id, tg.system_id, tg.tgid, tg.name, tg.alpha_tag, tg.tg_group, tg.frequency, tg.metadata, tg.tags, tg.alert, tg.alert_config, tg.weight, sys.id, sys.name,
|
||||
FALSE learned
|
||||
FROM talkgroups tg
|
||||
JOIN systems sys ON tg.system_id = sys.id
|
||||
UNION
|
||||
SELECT
|
||||
tgl.id::INT8, tgl.system_id::INT4, tgl.tgid::INT4, tgl.name,
|
||||
tgl.alpha_tag, tgl.alpha_tag, NULL::INTEGER, NULL::JSONB,
|
||||
CASE WHEN tgl.alpha_tag IS NULL THEN NULL ELSE ARRAY[tgl.alpha_tag] END,
|
||||
TRUE, NULL::JSONB, 1.0, sys.id, sys.name,
|
||||
TRUE learned
|
||||
FROM talkgroups_learned tgl
|
||||
JOIN systems sys ON tgl.system_id = sys.id
|
||||
WHERE ignored IS NOT TRUE
|
||||
`
|
||||
|
||||
func TestQueryColumnsMatch(t *testing.T) {
|
||||
require.Equal(t, getTalkgroupsWithLearnedByPackedIDsTest, getTalkgroupWithLearnedByPackedIDs)
|
||||
require.Equal(t, getTalkgroupWithLearnedTest, getTalkgroupWithLearned)
|
||||
require.Equal(t, getTalkgroupsWithLearnedBySystemTest, getTalkgroupsWithLearnedBySystem)
|
||||
require.Equal(t, getTalkgroupsWithLearnedTest, getTalkgroupsWithLearned)
|
||||
}
|
|
@ -5,9 +5,10 @@ import (
|
|||
"encoding/json"
|
||||
|
||||
"dynatron.me/x/stillbox/pkg/calls"
|
||||
"dynatron.me/x/stillbox/pkg/database"
|
||||
"dynatron.me/x/stillbox/pkg/pb"
|
||||
"dynatron.me/x/stillbox/pkg/talkgroups"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/rs/zerolog/log"
|
||||
"google.golang.org/protobuf/types/known/structpb"
|
||||
)
|
||||
|
@ -60,18 +61,19 @@ func (c *client) SendError(cmd *pb.Command, err error) {
|
|||
}
|
||||
|
||||
func (c *client) Talkgroup(ctx context.Context, tg *pb.Talkgroup) error {
|
||||
tgi, err := talkgroups.StoreFrom(ctx).TG(ctx, talkgroups.TG(tg.System, tg.Talkgroup))
|
||||
db := database.FromCtx(ctx)
|
||||
tgi, err := db.GetTalkgroupWithLearned(ctx, int(tg.System), int(tg.Talkgroup))
|
||||
if err != nil {
|
||||
if err != talkgroups.ErrNotFound {
|
||||
if err != pgx.ErrNoRows {
|
||||
log.Error().Err(err).Int32("sys", tg.System).Int32("tg", tg.Talkgroup).Msg("get talkgroup fail")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
var md *structpb.Struct
|
||||
if len(tgi.Talkgroup.Metadata) > 0 {
|
||||
if len(tgi.Metadata) > 0 {
|
||||
m := make(map[string]interface{})
|
||||
err := json.Unmarshal(tgi.Talkgroup.Metadata, &m)
|
||||
err := json.Unmarshal(tgi.Metadata, &m)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Int32("sys", tg.System).Int32("tg", tg.Talkgroup).Msg("unmarshal tg metadata")
|
||||
}
|
||||
|
@ -83,14 +85,14 @@ func (c *client) Talkgroup(ctx context.Context, tg *pb.Talkgroup) error {
|
|||
|
||||
resp := &pb.TalkgroupInfo{
|
||||
Tg: tg,
|
||||
Name: tgi.Talkgroup.Name,
|
||||
Group: tgi.Talkgroup.TgGroup,
|
||||
Frequency: tgi.Talkgroup.Frequency,
|
||||
Name: tgi.Name,
|
||||
Group: tgi.TgGroup,
|
||||
Frequency: tgi.Frequency,
|
||||
Metadata: md,
|
||||
Tags: tgi.Talkgroup.Tags,
|
||||
Tags: tgi.Tags,
|
||||
Learned: tgi.Learned,
|
||||
AlphaTag: tgi.Talkgroup.AlphaTag,
|
||||
SystemName: tgi.System.Name,
|
||||
AlphaTag: tgi.AlphaTag,
|
||||
SystemName: tgi.SystemName,
|
||||
}
|
||||
|
||||
_ = c.Send(&pb.Message{
|
||||
|
|
|
@ -183,5 +183,5 @@ func (conn *wsConn) writeToClient(w io.WriteCloser, msg ToClient) {
|
|||
}
|
||||
|
||||
func (n *wsManager) PrivateRoutes(r chi.Router) {
|
||||
r.HandleFunc("/api/ws", n.serveWS)
|
||||
r.HandleFunc("/ws", n.serveWS)
|
||||
}
|
||||
|
|
|
@ -1,87 +1,28 @@
|
|||
package notify
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
stdhttp "net/http"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"dynatron.me/x/stillbox/internal/common"
|
||||
"dynatron.me/x/stillbox/pkg/alerting/alert"
|
||||
"dynatron.me/x/stillbox/pkg/config"
|
||||
|
||||
"github.com/go-viper/mapstructure/v2"
|
||||
"github.com/nikoksr/notify"
|
||||
"github.com/nikoksr/notify/service/http"
|
||||
)
|
||||
|
||||
type Notifier interface {
|
||||
Send(ctx context.Context, alerts []alert.Alert) error
|
||||
}
|
||||
|
||||
type backend struct {
|
||||
*notify.Notify
|
||||
subject *template.Template
|
||||
body *template.Template
|
||||
notify.Notifier
|
||||
}
|
||||
|
||||
type notifier struct {
|
||||
backends []backend
|
||||
*notify.Notify
|
||||
}
|
||||
|
||||
func highest(a []alert.Alert) string {
|
||||
if len(a) < 1 {
|
||||
return "none"
|
||||
}
|
||||
func (n *notifier) buildSlackWebhookPayload(cfg config.NotifyService) func(string, string) any {
|
||||
icon := cfg.GetS("icon", "🚨")
|
||||
url := cfg.GetS("messageURL", "")
|
||||
|
||||
top := a[0]
|
||||
for _, a := range a {
|
||||
if a.Score.Score > top.Score.Score {
|
||||
top = a
|
||||
}
|
||||
}
|
||||
|
||||
return top.TGName
|
||||
}
|
||||
|
||||
var alertFm = template.FuncMap{
|
||||
"highest": highest,
|
||||
}
|
||||
|
||||
const defaultBodyTemplStr = `{{ range . -}}
|
||||
{{ .TGName }} is active with a score of {{ f .Score.Score 4 }}! ({{ f .Score.RecentCount 0 }}/{{ .Score.Count }} recent calls)
|
||||
|
||||
{{ end -}}`
|
||||
|
||||
var defaultBodyTemplate = template.Must(template.New("body").Funcs(common.FuncMap).Funcs(alertFm).Parse(defaultBodyTemplStr))
|
||||
|
||||
var defaultSubjectTemplStr = `Stillbox Alert ({{ highest . }}`
|
||||
var defaultSubjectTemplate = template.Must(template.New("subject").Funcs(common.FuncMap).Funcs(alertFm).Parse(defaultSubjectTemplStr))
|
||||
|
||||
// Send renders and sends the Alerts.
|
||||
func (b *backend) Send(ctx context.Context, alerts []alert.Alert) (err error) {
|
||||
var subject, body bytes.Buffer
|
||||
err = b.subject.ExecuteTemplate(&subject, "subject", alerts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = b.body.ExecuteTemplate(&body, "body", alerts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = b.Notify.Send(ctx, subject.String(), body.String())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func buildSlackWebhookPayload(cfg *slackWebhookConfig) func(string, string) any {
|
||||
type Attachment struct {
|
||||
Title string `json:"title"`
|
||||
Text string `json:"text"`
|
||||
|
@ -101,90 +42,40 @@ func buildSlackWebhookPayload(cfg *slackWebhookConfig) func(string, string) any
|
|||
{
|
||||
Title: subject,
|
||||
Text: message,
|
||||
TitleLink: cfg.MessageURL,
|
||||
TitleLink: url,
|
||||
Timestamp: time.Now().Unix(),
|
||||
},
|
||||
},
|
||||
IconEmoji: cfg.Icon,
|
||||
IconEmoji: icon,
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
}
|
||||
|
||||
type slackWebhookConfig struct {
|
||||
WebhookURL string `mapstructure:"webhookURL"`
|
||||
Icon string `mapstructure:"icon"`
|
||||
MessageURL string `mapstructure:"messageURL"`
|
||||
SubjectTemplate string `mapstructure:"subjectTemplate"`
|
||||
BodyTemplate string `mapstructure:"bodyTemplate"`
|
||||
}
|
||||
|
||||
func (n *notifier) addService(cfg config.NotifyService) (err error) {
|
||||
be := backend{}
|
||||
|
||||
switch cfg.SubjectTemplate {
|
||||
case nil:
|
||||
be.subject = defaultSubjectTemplate
|
||||
default:
|
||||
be.subject, err = template.New("subject").Funcs(common.FuncMap).Funcs(alertFm).Parse(*cfg.SubjectTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
switch cfg.BodyTemplate {
|
||||
case nil:
|
||||
be.body = defaultBodyTemplate
|
||||
default:
|
||||
be.body, err = template.New("body").Funcs(common.FuncMap).Funcs(alertFm).Parse(*cfg.BodyTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
be.Notify = notify.New()
|
||||
|
||||
func (n *notifier) addService(cfg config.NotifyService) error {
|
||||
switch cfg.Provider {
|
||||
case "slackwebhook":
|
||||
swc := &slackWebhookConfig{
|
||||
Icon: "🚨",
|
||||
}
|
||||
err := mapstructure.Decode(cfg.Config, &swc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
hs := http.New()
|
||||
hs.AddReceivers(&http.Webhook{
|
||||
ContentType: "application/json",
|
||||
Header: make(stdhttp.Header),
|
||||
Method: stdhttp.MethodPost,
|
||||
URL: swc.WebhookURL,
|
||||
BuildPayload: buildSlackWebhookPayload(swc),
|
||||
URL: cfg.GetS("webhookURL", ""),
|
||||
BuildPayload: n.buildSlackWebhookPayload(cfg),
|
||||
})
|
||||
be.UseServices(hs)
|
||||
n.UseServices(hs)
|
||||
default:
|
||||
return fmt.Errorf("unknown provider '%s'", cfg.Provider)
|
||||
}
|
||||
|
||||
n.backends = append(n.backends, be)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *notifier) Send(ctx context.Context, alerts []alert.Alert) error {
|
||||
for _, be := range n.backends {
|
||||
err := be.Send(ctx, alerts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func New(cfg config.Notify) (Notifier, error) {
|
||||
n := new(notifier)
|
||||
n := ¬ifier{
|
||||
Notify: notify.NewWithServices(),
|
||||
}
|
||||
|
||||
for _, s := range cfg {
|
||||
err := n.addService(s)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.33.0
|
||||
// protoc v5.28.3
|
||||
// protoc v5.28.2
|
||||
// source: stillbox.proto
|
||||
|
||||
package pb
|
||||
|
@ -288,19 +288,18 @@ type Call struct {
|
|||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
||||
AudioName string `protobuf:"bytes,2,opt,name=audioName,proto3" json:"audioName,omitempty"`
|
||||
AudioType string `protobuf:"bytes,3,opt,name=audioType,proto3" json:"audioType,omitempty"`
|
||||
DateTime *timestamppb.Timestamp `protobuf:"bytes,4,opt,name=date_time,json=dateTime,proto3" json:"date_time,omitempty"`
|
||||
System int32 `protobuf:"varint,5,opt,name=system,proto3" json:"system,omitempty"`
|
||||
Talkgroup int32 `protobuf:"varint,6,opt,name=talkgroup,proto3" json:"talkgroup,omitempty"`
|
||||
Source int32 `protobuf:"varint,7,opt,name=source,proto3" json:"source,omitempty"`
|
||||
Frequency int64 `protobuf:"varint,8,opt,name=frequency,proto3" json:"frequency,omitempty"`
|
||||
Frequencies []int64 `protobuf:"varint,9,rep,packed,name=frequencies,proto3" json:"frequencies,omitempty"`
|
||||
Patches []int32 `protobuf:"varint,10,rep,packed,name=patches,proto3" json:"patches,omitempty"`
|
||||
Sources []int32 `protobuf:"varint,11,rep,packed,name=sources,proto3" json:"sources,omitempty"`
|
||||
Duration *int32 `protobuf:"varint,12,opt,name=duration,proto3,oneof" json:"duration,omitempty"`
|
||||
Audio []byte `protobuf:"bytes,13,opt,name=audio,proto3" json:"audio,omitempty"`
|
||||
AudioName string `protobuf:"bytes,1,opt,name=audioName,proto3" json:"audioName,omitempty"`
|
||||
AudioType string `protobuf:"bytes,2,opt,name=audioType,proto3" json:"audioType,omitempty"`
|
||||
DateTime *timestamppb.Timestamp `protobuf:"bytes,3,opt,name=date_time,json=dateTime,proto3" json:"date_time,omitempty"`
|
||||
System int32 `protobuf:"varint,4,opt,name=system,proto3" json:"system,omitempty"`
|
||||
Talkgroup int32 `protobuf:"varint,5,opt,name=talkgroup,proto3" json:"talkgroup,omitempty"`
|
||||
Source int32 `protobuf:"varint,6,opt,name=source,proto3" json:"source,omitempty"`
|
||||
Frequency int64 `protobuf:"varint,7,opt,name=frequency,proto3" json:"frequency,omitempty"`
|
||||
Frequencies []int64 `protobuf:"varint,8,rep,packed,name=frequencies,proto3" json:"frequencies,omitempty"`
|
||||
Patches []int32 `protobuf:"varint,9,rep,packed,name=patches,proto3" json:"patches,omitempty"`
|
||||
Sources []int32 `protobuf:"varint,10,rep,packed,name=sources,proto3" json:"sources,omitempty"`
|
||||
Duration *int32 `protobuf:"varint,11,opt,name=duration,proto3,oneof" json:"duration,omitempty"`
|
||||
Audio []byte `protobuf:"bytes,12,opt,name=audio,proto3" json:"audio,omitempty"`
|
||||
}
|
||||
|
||||
func (x *Call) Reset() {
|
||||
|
@ -335,13 +334,6 @@ func (*Call) Descriptor() ([]byte, []int) {
|
|||
return file_stillbox_proto_rawDescGZIP(), []int{2}
|
||||
}
|
||||
|
||||
func (x *Call) GetId() string {
|
||||
if x != nil {
|
||||
return x.Id
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *Call) GetAudioName() string {
|
||||
if x != nil {
|
||||
return x.AudioName
|
||||
|
@ -1195,30 +1187,29 @@ var file_stillbox_proto_rawDesc = []byte{
|
|||
0x6f, 0x75, 0x70, 0x49, 0x6e, 0x66, 0x6f, 0x48, 0x00, 0x52, 0x06, 0x74, 0x67, 0x49, 0x6e, 0x66,
|
||||
0x6f, 0x42, 0x12, 0x0a, 0x10, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x5f, 0x72, 0x65, 0x73,
|
||||
0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x0d, 0x0a, 0x0b, 0x5f, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e,
|
||||
0x64, 0x5f, 0x69, 0x64, 0x22, 0x91, 0x03, 0x0a, 0x04, 0x43, 0x61, 0x6c, 0x6c, 0x12, 0x0e, 0x0a,
|
||||
0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x1c, 0x0a,
|
||||
0x09, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09,
|
||||
0x64, 0x5f, 0x69, 0x64, 0x22, 0x81, 0x03, 0x0a, 0x04, 0x43, 0x61, 0x6c, 0x6c, 0x12, 0x1c, 0x0a,
|
||||
0x09, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09,
|
||||
0x52, 0x09, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x61,
|
||||
0x75, 0x64, 0x69, 0x6f, 0x54, 0x79, 0x70, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09,
|
||||
0x75, 0x64, 0x69, 0x6f, 0x54, 0x79, 0x70, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09,
|
||||
0x61, 0x75, 0x64, 0x69, 0x6f, 0x54, 0x79, 0x70, 0x65, 0x12, 0x37, 0x0a, 0x09, 0x64, 0x61, 0x74,
|
||||
0x65, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1a, 0x2e, 0x67,
|
||||
0x65, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1a, 0x2e, 0x67,
|
||||
0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x54,
|
||||
0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x52, 0x08, 0x64, 0x61, 0x74, 0x65, 0x54, 0x69,
|
||||
0x6d, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x18, 0x05, 0x20, 0x01,
|
||||
0x6d, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x18, 0x04, 0x20, 0x01,
|
||||
0x28, 0x05, 0x52, 0x06, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x12, 0x1c, 0x0a, 0x09, 0x74, 0x61,
|
||||
0x6c, 0x6b, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x18, 0x06, 0x20, 0x01, 0x28, 0x05, 0x52, 0x09, 0x74,
|
||||
0x6c, 0x6b, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x18, 0x05, 0x20, 0x01, 0x28, 0x05, 0x52, 0x09, 0x74,
|
||||
0x61, 0x6c, 0x6b, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x6f, 0x75, 0x72,
|
||||
0x63, 0x65, 0x18, 0x07, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65,
|
||||
0x12, 0x1c, 0x0a, 0x09, 0x66, 0x72, 0x65, 0x71, 0x75, 0x65, 0x6e, 0x63, 0x79, 0x18, 0x08, 0x20,
|
||||
0x63, 0x65, 0x18, 0x06, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65,
|
||||
0x12, 0x1c, 0x0a, 0x09, 0x66, 0x72, 0x65, 0x71, 0x75, 0x65, 0x6e, 0x63, 0x79, 0x18, 0x07, 0x20,
|
||||
0x01, 0x28, 0x03, 0x52, 0x09, 0x66, 0x72, 0x65, 0x71, 0x75, 0x65, 0x6e, 0x63, 0x79, 0x12, 0x20,
|
||||
0x0a, 0x0b, 0x66, 0x72, 0x65, 0x71, 0x75, 0x65, 0x6e, 0x63, 0x69, 0x65, 0x73, 0x18, 0x09, 0x20,
|
||||
0x0a, 0x0b, 0x66, 0x72, 0x65, 0x71, 0x75, 0x65, 0x6e, 0x63, 0x69, 0x65, 0x73, 0x18, 0x08, 0x20,
|
||||
0x03, 0x28, 0x03, 0x52, 0x0b, 0x66, 0x72, 0x65, 0x71, 0x75, 0x65, 0x6e, 0x63, 0x69, 0x65, 0x73,
|
||||
0x12, 0x18, 0x0a, 0x07, 0x70, 0x61, 0x74, 0x63, 0x68, 0x65, 0x73, 0x18, 0x0a, 0x20, 0x03, 0x28,
|
||||
0x12, 0x18, 0x0a, 0x07, 0x70, 0x61, 0x74, 0x63, 0x68, 0x65, 0x73, 0x18, 0x09, 0x20, 0x03, 0x28,
|
||||
0x05, 0x52, 0x07, 0x70, 0x61, 0x74, 0x63, 0x68, 0x65, 0x73, 0x12, 0x18, 0x0a, 0x07, 0x73, 0x6f,
|
||||
0x75, 0x72, 0x63, 0x65, 0x73, 0x18, 0x0b, 0x20, 0x03, 0x28, 0x05, 0x52, 0x07, 0x73, 0x6f, 0x75,
|
||||
0x75, 0x72, 0x63, 0x65, 0x73, 0x18, 0x0a, 0x20, 0x03, 0x28, 0x05, 0x52, 0x07, 0x73, 0x6f, 0x75,
|
||||
0x72, 0x63, 0x65, 0x73, 0x12, 0x1f, 0x0a, 0x08, 0x64, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e,
|
||||
0x18, 0x0c, 0x20, 0x01, 0x28, 0x05, 0x48, 0x00, 0x52, 0x08, 0x64, 0x75, 0x72, 0x61, 0x74, 0x69,
|
||||
0x6f, 0x6e, 0x88, 0x01, 0x01, 0x12, 0x14, 0x0a, 0x05, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x18, 0x0d,
|
||||
0x18, 0x0b, 0x20, 0x01, 0x28, 0x05, 0x48, 0x00, 0x52, 0x08, 0x64, 0x75, 0x72, 0x61, 0x74, 0x69,
|
||||
0x6f, 0x6e, 0x88, 0x01, 0x01, 0x12, 0x14, 0x0a, 0x05, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x18, 0x0c,
|
||||
0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x42, 0x0b, 0x0a, 0x09, 0x5f,
|
||||
0x64, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x22, 0x3e, 0x0a, 0x05, 0x48, 0x65, 0x6c, 0x6c,
|
||||
0x6f, 0x12, 0x35, 0x0a, 0x0b, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x5f, 0x69, 0x6e, 0x66, 0x6f,
|
||||
|
|
|
@ -24,19 +24,18 @@ message CommandResponse {
|
|||
}
|
||||
|
||||
message Call {
|
||||
string id = 1;
|
||||
string audioName = 2;
|
||||
string audioType = 3;
|
||||
google.protobuf.Timestamp date_time = 4;
|
||||
int32 system = 5;
|
||||
int32 talkgroup = 6;
|
||||
int32 source = 7;
|
||||
int64 frequency = 8;
|
||||
repeated int64 frequencies = 9;
|
||||
repeated int32 patches = 10;
|
||||
repeated int32 sources = 11;
|
||||
optional int32 duration = 12;
|
||||
bytes audio = 13;
|
||||
string audioName = 1;
|
||||
string audioType = 2;
|
||||
google.protobuf.Timestamp date_time = 3;
|
||||
int32 system = 4;
|
||||
int32 talkgroup = 5;
|
||||
int32 source = 6;
|
||||
int64 frequency = 7;
|
||||
repeated int64 frequencies = 8;
|
||||
repeated int32 patches = 9;
|
||||
repeated int32 sources = 10;
|
||||
optional int32 duration = 11;
|
||||
bytes audio = 12;
|
||||
}
|
||||
|
||||
message Hello {
|
||||
|
|
|
@ -36,7 +36,6 @@ func (s *Server) setupRoutes() {
|
|||
s.nex.PrivateRoutes(r)
|
||||
s.auth.PrivateRoutes(r)
|
||||
s.alerter.PrivateRoutes(r)
|
||||
r.Mount("/api", s.api.Subrouter())
|
||||
})
|
||||
|
||||
r.Group(func(r chi.Router) {
|
||||
|
|
|
@ -6,8 +6,8 @@ import (
|
|||
"os"
|
||||
"time"
|
||||
|
||||
"dynatron.me/x/stillbox/pkg/calls"
|
||||
"dynatron.me/x/stillbox/pkg/alerting"
|
||||
"dynatron.me/x/stillbox/pkg/api"
|
||||
"dynatron.me/x/stillbox/pkg/auth"
|
||||
"dynatron.me/x/stillbox/pkg/config"
|
||||
"dynatron.me/x/stillbox/pkg/database"
|
||||
|
@ -15,7 +15,6 @@ import (
|
|||
"dynatron.me/x/stillbox/pkg/notify"
|
||||
"dynatron.me/x/stillbox/pkg/sinks"
|
||||
"dynatron.me/x/stillbox/pkg/sources"
|
||||
"dynatron.me/x/stillbox/pkg/talkgroups"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
"github.com/go-chi/cors"
|
||||
|
@ -36,8 +35,7 @@ type Server struct {
|
|||
alerter alerting.Alerter
|
||||
notifier notify.Notifier
|
||||
hup chan os.Signal
|
||||
tgs talkgroups.Store
|
||||
api api.API
|
||||
tgCache calls.TalkgroupCache
|
||||
}
|
||||
|
||||
func New(ctx context.Context, cfg *config.Config) (*Server, error) {
|
||||
|
@ -60,8 +58,7 @@ func New(ctx context.Context, cfg *config.Config) (*Server, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
tgCache := talkgroups.NewCache()
|
||||
api := api.New(tgCache)
|
||||
tgCache := calls.NewTalkgroupCache()
|
||||
|
||||
srv := &Server{
|
||||
auth: authenticator,
|
||||
|
@ -72,8 +69,7 @@ func New(ctx context.Context, cfg *config.Config) (*Server, error) {
|
|||
logger: logger,
|
||||
alerter: alerting.New(cfg.Alerting, tgCache, alerting.WithNotifier(notifier)),
|
||||
notifier: notifier,
|
||||
tgs: tgCache,
|
||||
api: api,
|
||||
tgCache: tgCache,
|
||||
}
|
||||
|
||||
srv.sinks.Register("database", sinks.NewDatabaseSink(srv.db), true)
|
||||
|
@ -108,7 +104,6 @@ func (s *Server) Go(ctx context.Context) error {
|
|||
s.installHupHandler()
|
||||
|
||||
ctx = database.CtxWithDB(ctx, s.db)
|
||||
ctx = talkgroups.CtxWithStore(ctx, s.tgs)
|
||||
|
||||
httpSrv := &http.Server{
|
||||
Addr: s.conf.Listen,
|
||||
|
|
|
@ -17,7 +17,6 @@ func (s *Server) huppers() []hupper {
|
|||
return []hupper{
|
||||
s.logger,
|
||||
s.auth,
|
||||
s.tgs,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -26,12 +26,12 @@ func (s *DatabaseSink) Call(ctx context.Context, call *calls.Call) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
err := s.db.AddCall(ctx, s.toAddCallParams(call))
|
||||
dbCall, err := s.db.AddCall(ctx, s.toAddCallParams(call))
|
||||
if err != nil {
|
||||
return fmt.Errorf("add call: %w", err)
|
||||
}
|
||||
|
||||
log.Debug().Str("id", call.ID.String()).Int("system", call.System).Int("tgid", call.Talkgroup).Msg("stored")
|
||||
log.Debug().Str("id", dbCall.String()).Int("system", call.System).Int("tgid", call.Talkgroup).Msg("stored")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -42,7 +42,6 @@ func (s *DatabaseSink) SinkType() string {
|
|||
|
||||
func (s *DatabaseSink) toAddCallParams(call *calls.Call) database.AddCallParams {
|
||||
return database.AddCallParams{
|
||||
ID: call.ID,
|
||||
Submitter: call.Submitter.Int32Ptr(),
|
||||
System: call.System,
|
||||
Talkgroup: call.Talkgroup,
|
||||
|
@ -50,7 +49,7 @@ func (s *DatabaseSink) toAddCallParams(call *calls.Call) database.AddCallParams
|
|||
AudioName: common.PtrOrNull(call.AudioName),
|
||||
AudioBlob: call.Audio,
|
||||
AudioType: common.PtrOrNull(call.AudioType),
|
||||
Duration: call.Duration.MsInt32Ptr(),
|
||||
Duration: call.Duration.Int32Ptr(),
|
||||
Frequency: call.Frequency,
|
||||
Frequencies: call.Frequencies,
|
||||
Patches: call.Patches,
|
||||
|
|
|
@ -7,8 +7,8 @@ import (
|
|||
|
||||
"dynatron.me/x/stillbox/internal/common"
|
||||
"dynatron.me/x/stillbox/internal/forms"
|
||||
"dynatron.me/x/stillbox/pkg/auth"
|
||||
"dynatron.me/x/stillbox/pkg/calls"
|
||||
"dynatron.me/x/stillbox/pkg/auth"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
|
|
@ -1,292 +0,0 @@
|
|||
package talkgroups
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"dynatron.me/x/stillbox/internal/ruletime"
|
||||
|
||||
"dynatron.me/x/stillbox/pkg/config"
|
||||
"dynatron.me/x/stillbox/pkg/database"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type tgMap map[ID]*Talkgroup
|
||||
|
||||
type Store interface {
|
||||
// TG retrieves a Talkgroup from the Store.
|
||||
TG(ctx context.Context, tg ID) (*Talkgroup, error)
|
||||
|
||||
// TGs retrieves many talkgroups from the Store.
|
||||
TGs(ctx context.Context, tgs IDs) ([]*Talkgroup, error)
|
||||
|
||||
// SystemTGs retrieves all Talkgroups associated with a System.
|
||||
SystemTGs(ctx context.Context, systemID int32) ([]*Talkgroup, error)
|
||||
|
||||
// SystemName retrieves a system name from the store. It returns the record and whether one was found.
|
||||
SystemName(ctx context.Context, id int) (string, bool)
|
||||
|
||||
// ApplyAlertRules applies the score's talkgroup alert rules to the call occurring at t and returns the weighted score.
|
||||
ApplyAlertRules(id ID, t time.Time, coversOpts ...ruletime.CoversOption) float64
|
||||
|
||||
// Hint hints the Store that the provided talkgroups will be asked for.
|
||||
Hint(ctx context.Context, tgs []ID) error
|
||||
|
||||
// Load loads the provided packed talkgroup IDs into the Store.
|
||||
Load(ctx context.Context, tgs []int64) error
|
||||
|
||||
// Invalidate invalidates any caching in the Store.
|
||||
Invalidate()
|
||||
|
||||
// Weight returns the final weight of this talkgroup, including its static and rules-derived weight.
|
||||
Weight(ctx context.Context, id ID, t time.Time) float64
|
||||
|
||||
// Hupper
|
||||
HUP(*config.Config)
|
||||
}
|
||||
|
||||
type CtxStoreKeyT string
|
||||
|
||||
const CtxStoreKey CtxStoreKeyT = "store"
|
||||
|
||||
func CtxWithStore(ctx context.Context, s Store) context.Context {
|
||||
return context.WithValue(ctx, CtxStoreKey, s)
|
||||
}
|
||||
|
||||
func StoreFrom(ctx context.Context) Store {
|
||||
s, ok := ctx.Value(CtxStoreKey).(Store)
|
||||
if !ok {
|
||||
return NewCache()
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func (t *cache) HUP(_ *config.Config) {
|
||||
t.Invalidate()
|
||||
}
|
||||
|
||||
func (t *cache) Invalidate() {
|
||||
t.Lock()
|
||||
defer t.Unlock()
|
||||
clear(t.tgs)
|
||||
clear(t.systems)
|
||||
t.AlertConfig.Invalidate()
|
||||
}
|
||||
|
||||
type cache struct {
|
||||
sync.RWMutex
|
||||
AlertConfig
|
||||
tgs tgMap
|
||||
systems map[int32]string
|
||||
}
|
||||
|
||||
// NewCache returns a new cache Store.
|
||||
func NewCache() Store {
|
||||
tgc := &cache{
|
||||
tgs: make(tgMap),
|
||||
systems: make(map[int32]string),
|
||||
AlertConfig: NewAlertConfig(),
|
||||
}
|
||||
|
||||
return tgc
|
||||
}
|
||||
|
||||
func (t *cache) Hint(ctx context.Context, tgs []ID) error {
|
||||
t.RLock()
|
||||
var toLoad []int64
|
||||
if len(t.tgs) > len(tgs)/2 { // TODO: instrument this
|
||||
for _, tg := range tgs {
|
||||
_, ok := t.tgs[tg]
|
||||
if !ok {
|
||||
toLoad = append(toLoad, tg.Pack())
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
toLoad = make([]int64, 0, len(tgs))
|
||||
for _, g := range tgs {
|
||||
toLoad = append(toLoad, g.Pack())
|
||||
}
|
||||
}
|
||||
|
||||
if len(toLoad) > 0 {
|
||||
t.RUnlock()
|
||||
return t.Load(ctx, toLoad)
|
||||
}
|
||||
|
||||
t.RUnlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *cache) add(rec *Talkgroup) error {
|
||||
t.Lock()
|
||||
defer t.Unlock()
|
||||
|
||||
tg := TG(rec.System.ID, rec.Talkgroup.Tgid)
|
||||
t.tgs[tg] = rec
|
||||
t.systems[int32(rec.System.ID)] = rec.System.Name
|
||||
|
||||
return t.AlertConfig.UnmarshalTGRules(tg, rec.Talkgroup.AlertConfig)
|
||||
}
|
||||
|
||||
type row interface {
|
||||
database.GetTalkgroupsWithLearnedByPackedIDsRow | database.GetTalkgroupsWithLearnedRow |
|
||||
database.GetTalkgroupsWithLearnedBySystemRow
|
||||
GetTalkgroup() database.Talkgroup
|
||||
GetSystem() database.System
|
||||
GetLearned() bool
|
||||
}
|
||||
|
||||
func rowToTalkgroup[T row](r T) *Talkgroup {
|
||||
return &Talkgroup{
|
||||
Talkgroup: r.GetTalkgroup(),
|
||||
System: r.GetSystem(),
|
||||
Learned: r.GetLearned(),
|
||||
}
|
||||
}
|
||||
|
||||
func addToRowList[T row](t *cache, r []*Talkgroup, tgRecords []T) ([]*Talkgroup, error) {
|
||||
for _, rec := range tgRecords {
|
||||
tg := rowToTalkgroup(rec)
|
||||
err := t.add(tg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
r = append(r, tg)
|
||||
}
|
||||
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func (t *cache) TGs(ctx context.Context, tgs IDs) ([]*Talkgroup, error) {
|
||||
r := make([]*Talkgroup, 0, len(tgs))
|
||||
var err error
|
||||
if tgs != nil {
|
||||
toGet := make(IDs, 0, len(tgs))
|
||||
t.RLock()
|
||||
for _, id := range tgs {
|
||||
rec, has := t.tgs[id]
|
||||
if has {
|
||||
r = append(r, rec)
|
||||
} else {
|
||||
toGet = append(toGet, id)
|
||||
}
|
||||
}
|
||||
t.RUnlock()
|
||||
|
||||
tgRecords, err := database.FromCtx(ctx).GetTalkgroupsWithLearnedByPackedIDs(ctx, toGet.Packed())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return addToRowList(t, r, tgRecords)
|
||||
}
|
||||
|
||||
// get all talkgroups
|
||||
|
||||
tgRecords, err := database.FromCtx(ctx).GetTalkgroupsWithLearned(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return addToRowList(t, r, tgRecords)
|
||||
}
|
||||
|
||||
func (t *cache) Load(ctx context.Context, tgs []int64) error {
|
||||
tgRecords, err := database.FromCtx(ctx).GetTalkgroupsWithLearnedByPackedIDs(ctx, tgs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, rec := range tgRecords {
|
||||
err := t.add(rowToTalkgroup(rec))
|
||||
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("add alert config fail")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var ErrNotFound = errors.New("talkgroup not found")
|
||||
|
||||
func (t *cache) Weight(ctx context.Context, id ID, tm time.Time) float64 {
|
||||
tg, err := t.TG(ctx, id)
|
||||
if err != nil {
|
||||
return 1.0
|
||||
}
|
||||
|
||||
m := float64(tg.Weight)
|
||||
|
||||
m *= t.AlertConfig.ApplyAlertRules(id, tm)
|
||||
|
||||
return float64(m)
|
||||
}
|
||||
|
||||
func (t *cache) SystemTGs(ctx context.Context, systemID int32) ([]*Talkgroup, error) {
|
||||
recs, err := database.FromCtx(ctx).GetTalkgroupsWithLearnedBySystem(ctx, systemID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
r := make([]*Talkgroup, 0, len(recs))
|
||||
return addToRowList(t, r, recs)
|
||||
}
|
||||
|
||||
func (t *cache) TG(ctx context.Context, tg ID) (*Talkgroup, error) {
|
||||
t.RLock()
|
||||
rec, has := t.tgs[tg]
|
||||
t.RUnlock()
|
||||
|
||||
if has {
|
||||
return rec, nil
|
||||
}
|
||||
|
||||
recs, err := database.FromCtx(ctx).GetTalkgroupsWithLearnedByPackedIDs(ctx, []int64{tg.Pack()})
|
||||
switch err {
|
||||
case nil:
|
||||
case pgx.ErrNoRows:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
log.Error().Err(err).Msg("TG() cache add db get")
|
||||
return nil, errors.Join(ErrNotFound, err)
|
||||
}
|
||||
|
||||
if len(recs) < 1 {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
|
||||
err = t.add(rowToTalkgroup(recs[0]))
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("TG() cache add")
|
||||
return rowToTalkgroup(recs[0]), errors.Join(ErrNotFound, err)
|
||||
}
|
||||
|
||||
return rowToTalkgroup(recs[0]), nil
|
||||
}
|
||||
|
||||
func (t *cache) SystemName(ctx context.Context, id int) (name string, has bool) {
|
||||
t.RLock()
|
||||
n, has := t.systems[int32(id)]
|
||||
t.RUnlock()
|
||||
|
||||
if !has {
|
||||
sys, err := database.FromCtx(ctx).GetSystemName(ctx, id)
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
|
||||
t.Lock()
|
||||
t.systems[int32(id)] = sys
|
||||
t.Unlock()
|
||||
|
||||
return sys, true
|
||||
}
|
||||
|
||||
return n, has
|
||||
}
|
|
@ -1,55 +0,0 @@
|
|||
package talkgroups
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"dynatron.me/x/stillbox/pkg/database"
|
||||
)
|
||||
|
||||
type Talkgroup struct {
|
||||
database.Talkgroup
|
||||
System database.System `json:"system"`
|
||||
Learned bool `json:"learned"`
|
||||
}
|
||||
|
||||
type Names struct {
|
||||
System string
|
||||
Talkgroup string
|
||||
}
|
||||
|
||||
type ID struct {
|
||||
System uint32 `json:"sys"`
|
||||
Talkgroup uint32 `json:"tg"`
|
||||
}
|
||||
|
||||
type IDs []ID
|
||||
|
||||
func (ids *IDs) Packed() []int64 {
|
||||
r := make([]int64, len(*ids))
|
||||
for i := range *ids {
|
||||
r[i] = (*ids)[i].Pack()
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
type intId interface {
|
||||
int | uint | int64 | uint64 | int32 | uint32
|
||||
}
|
||||
|
||||
func TG[T intId, U intId](sys T, tgid U) ID {
|
||||
return ID{
|
||||
System: uint32(sys),
|
||||
Talkgroup: uint32(tgid),
|
||||
}
|
||||
}
|
||||
|
||||
func (t ID) Pack() int64 {
|
||||
// P25 system IDs are 12 bits, so we can fit them in a signed 8 byte int (int64, pg INT8)
|
||||
return int64((int64(t.System) << 32) | int64(t.Talkgroup))
|
||||
}
|
||||
|
||||
func (t ID) String() string {
|
||||
return fmt.Sprintf("%d:%d", t.System, t.Talkgroup)
|
||||
|
||||
}
|
|
@ -1,41 +1,24 @@
|
|||
-- name: AddCall :exec
|
||||
-- name: AddCall :one
|
||||
INSERT INTO calls (
|
||||
id,
|
||||
submitter,
|
||||
system,
|
||||
talkgroup,
|
||||
call_date,
|
||||
audio_name,
|
||||
audio_blob,
|
||||
audio_type,
|
||||
audio_url,
|
||||
duration,
|
||||
frequency,
|
||||
frequencies,
|
||||
patches,
|
||||
tg_label,
|
||||
tg_alpha_tag,
|
||||
tg_group,
|
||||
source
|
||||
) VALUES (
|
||||
@id,
|
||||
@submitter,
|
||||
@system,
|
||||
@talkgroup,
|
||||
@call_date,
|
||||
@audio_name,
|
||||
@audio_blob,
|
||||
@audio_type,
|
||||
@audio_url,
|
||||
@duration,
|
||||
@frequency,
|
||||
@frequencies,
|
||||
@patches,
|
||||
@tg_label,
|
||||
@tg_alpha_tag,
|
||||
@tg_group,
|
||||
@source
|
||||
);
|
||||
id,
|
||||
submitter,
|
||||
system,
|
||||
talkgroup,
|
||||
call_date,
|
||||
audio_name,
|
||||
audio_blob,
|
||||
audio_type,
|
||||
audio_url,
|
||||
duration,
|
||||
frequency,
|
||||
frequencies,
|
||||
patches,
|
||||
tg_label,
|
||||
tg_alpha_tag,
|
||||
tg_group,
|
||||
source
|
||||
) VALUES (gen_random_uuid(), $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)
|
||||
RETURNING id;
|
||||
|
||||
-- name: SetCallTranscript :exec
|
||||
UPDATE calls SET transcript = $2 WHERE id = $1;
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
-- name: GetTalkgroupsWithAnyTags :many
|
||||
SELECT sqlc.embed(talkgroups) FROM talkgroups
|
||||
SELECT * FROM talkgroups
|
||||
WHERE tags @> ARRAY[$1];
|
||||
|
||||
-- name: GetTalkgroupsWithAllTags :many
|
||||
SELECT sqlc.embed(talkgroups) FROM talkgroups
|
||||
SELECT * FROM talkgroups
|
||||
WHERE tags && ARRAY[$1];
|
||||
|
||||
-- name: GetTalkgroupIDsByTags :many
|
||||
|
@ -25,85 +25,53 @@ UPDATE talkgroups SET tags = $2
|
|||
WHERE id = ANY($1);
|
||||
|
||||
-- name: GetTalkgroup :one
|
||||
SELECT sqlc.embed(talkgroups) FROM talkgroups
|
||||
SELECT * FROM talkgroups
|
||||
WHERE id = systg2id(sqlc.arg(system_id), sqlc.arg(tgid));
|
||||
|
||||
-- name: GetTalkgroupsByPackedIDs :many
|
||||
SELECT sqlc.embed(tg), sqlc.embed(sys) FROM talkgroups tg
|
||||
SELECT * FROM talkgroups tg
|
||||
JOIN systems sys ON tg.system_id = sys.id
|
||||
WHERE tg.id = ANY($1::INT8[]);
|
||||
|
||||
-- name: GetTalkgroupWithLearned :one
|
||||
SELECT
|
||||
sqlc.embed(tg), sqlc.embed(sys),
|
||||
tg.id, tg.system_id, sys.name system_name, tg.tgid, tg.name,
|
||||
tg.tg_group, tg.frequency, tg.metadata, tg.tags, tg.alpha_tag,
|
||||
tg.alert, tg.weight, tg.alert_config,
|
||||
FALSE learned
|
||||
FROM talkgroups tg
|
||||
JOIN systems sys ON tg.system_id = sys.id
|
||||
WHERE tg.id = systg2id(sqlc.arg(system_id), sqlc.arg(tgid))
|
||||
UNION
|
||||
SELECT
|
||||
tgl.id::INT8, tgl.system_id::INT4, tgl.tgid::INT4, tgl.name,
|
||||
tgl.alpha_tag, tgl.alpha_tag, NULL::INTEGER, NULL::JSONB,
|
||||
CASE WHEN tgl.alpha_tag IS NULL THEN NULL ELSE ARRAY[tgl.alpha_tag] END,
|
||||
TRUE, NULL::JSONB, 1.0, sys.id, sys.name,
|
||||
tgl.id::INT8, tgl.system_id::INT4, sys.name system_name, tgl.tgid::INT4, tgl.name,
|
||||
tgl.alpha_tag, NULL::INTEGER, NULL::JSONB,
|
||||
CASE WHEN tgl.alpha_tag IS NULL THEN NULL ELSE ARRAY[tgl.alpha_tag] END, tgl.alpha_tag,
|
||||
TRUE, 1.0, NULL::JSONB,
|
||||
TRUE learned
|
||||
FROM talkgroups_learned tgl
|
||||
JOIN systems sys ON tgl.system_id = sys.id
|
||||
WHERE tgl.system_id = sqlc.arg(system_id) AND tgl.tgid = sqlc.arg(tgid) AND ignored IS NOT TRUE;
|
||||
|
||||
-- name: GetTalkgroupsWithLearnedByPackedIDs :many
|
||||
-- name: GetTalkgroupWithLearnedByPackedIDs :many
|
||||
SELECT
|
||||
sqlc.embed(tg), sqlc.embed(sys),
|
||||
tg.id, tg.system_id, sys.name system_name, tg.tgid, tg.name,
|
||||
tg.tg_group, tg.frequency, tg.metadata, tg.tags, tg.alpha_tag,
|
||||
tg.alert, tg.weight, tg.alert_config,
|
||||
FALSE learned
|
||||
FROM talkgroups tg
|
||||
JOIN systems sys ON tg.system_id = sys.id
|
||||
WHERE tg.id = ANY($1::INT8[])
|
||||
UNION
|
||||
SELECT
|
||||
tgl.id::INT8, tgl.system_id::INT4, tgl.tgid::INT4, tgl.name,
|
||||
tgl.alpha_tag, tgl.alpha_tag, NULL::INTEGER, NULL::JSONB,
|
||||
CASE WHEN tgl.alpha_tag IS NULL THEN NULL ELSE ARRAY[tgl.alpha_tag] END,
|
||||
TRUE, NULL::JSONB, 1.0, sys.id, sys.name,
|
||||
tgl.id::INT8, tgl.system_id::INT4, sys.name system_name, tgl.tgid::INT4, tgl.name,
|
||||
tgl.alpha_tag, NULL::INTEGER, NULL::JSONB,
|
||||
CASE WHEN tgl.alpha_tag IS NULL THEN NULL ELSE ARRAY[tgl.alpha_tag] END, tgl.alpha_tag,
|
||||
TRUE, 1.0, NULL::JSONB,
|
||||
TRUE learned
|
||||
FROM talkgroups_learned tgl
|
||||
JOIN systems sys ON tgl.system_id = sys.id
|
||||
WHERE systg2id(tgl.system_id, tgl.tgid) = ANY($1::INT8[]) AND ignored IS NOT TRUE;
|
||||
|
||||
-- name: GetTalkgroupsWithLearnedBySystem :many
|
||||
SELECT
|
||||
sqlc.embed(tg), sqlc.embed(sys),
|
||||
FALSE learned
|
||||
FROM talkgroups tg
|
||||
JOIN systems sys ON tg.system_id = sys.id
|
||||
WHERE tg.system_id = @system
|
||||
UNION
|
||||
SELECT
|
||||
tgl.id::INT8, tgl.system_id::INT4, tgl.tgid::INT4, tgl.name,
|
||||
tgl.alpha_tag, tgl.alpha_tag, NULL::INTEGER, NULL::JSONB,
|
||||
CASE WHEN tgl.alpha_tag IS NULL THEN NULL ELSE ARRAY[tgl.alpha_tag] END,
|
||||
TRUE, NULL::JSONB, 1.0, sys.id, sys.name,
|
||||
TRUE learned
|
||||
FROM talkgroups_learned tgl
|
||||
JOIN systems sys ON tgl.system_id = sys.id
|
||||
WHERE tgl.system_id = @system AND ignored IS NOT TRUE;
|
||||
|
||||
-- name: GetTalkgroupsWithLearned :many
|
||||
SELECT
|
||||
sqlc.embed(tg), sqlc.embed(sys),
|
||||
FALSE learned
|
||||
FROM talkgroups tg
|
||||
JOIN systems sys ON tg.system_id = sys.id
|
||||
UNION
|
||||
SELECT
|
||||
tgl.id::INT8, tgl.system_id::INT4, tgl.tgid::INT4, tgl.name,
|
||||
tgl.alpha_tag, tgl.alpha_tag, NULL::INTEGER, NULL::JSONB,
|
||||
CASE WHEN tgl.alpha_tag IS NULL THEN NULL ELSE ARRAY[tgl.alpha_tag] END,
|
||||
TRUE, NULL::JSONB, 1.0, sys.id, sys.name,
|
||||
TRUE learned
|
||||
FROM talkgroups_learned tgl
|
||||
JOIN systems sys ON tgl.system_id = sys.id
|
||||
WHERE ignored IS NOT TRUE;
|
||||
|
||||
|
||||
-- name: GetSystemName :one
|
||||
SELECT name FROM systems WHERE id = sqlc.arg(system_id);
|
||||
|
|
Loading…
Reference in a new issue