Compare commits

...

13 commits

Author SHA1 Message Date
3573ebcc08 wip 2024-11-02 14:44:14 -04:00
c2789f1e0e Prettier 2024-11-02 14:44:14 -04:00
f075e49d1e Login works 2024-11-02 14:44:14 -04:00
dc9f7eb7fb Initial admin UI 2024-11-02 14:44:14 -04:00
2922db0db9 Rename to ApplyAlertRules 2024-11-02 14:43:47 -04:00
36bc28899b Merge pull request 'alertconfig' (#21) from alertconfig into trunk
Reviewed-on: #21
2024-11-02 14:28:36 -04:00
ecd7a39db0 Store suppressed alerts 2024-11-02 14:27:07 -04:00
a0cc7eedfd Only set origscore when it is different 2024-11-02 12:45:26 -04:00
64c73fab99 Alert config 2024-11-02 11:39:02 -04:00
83b6b0e4f4 Initial 2024-11-02 09:41:48 -04:00
29acaf017a ruletime 2024-11-01 15:13:12 -04:00
f54d0d250a Add alerts to table 2024-11-01 09:15:48 -04:00
489e72b17a Remove message 2024-11-01 08:27:24 -04:00
53 changed files with 16319 additions and 165 deletions

View file

@ -1,6 +1,6 @@
VPKG=dynatron.me/x/stillbox/internal/version
VER!=git describe --tags --always --dirty
BUILDDATE!=date '+%Y-%m-%e'
BUILDDATE!=date '+%Y%m%d'
LDFLAGS=-ldflags="-X '${VPKG}.Version=${VER}' -X '${VPKG}.Built=${BUILDDATE}'"
all: checkcalls

View file

@ -0,0 +1,17 @@
# Editor configuration, see https://editorconfig.org
root = true
[*]
charset = utf-8
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true
[*.ts]
quote_type = single
ij_typescript_use_double_quotes = false
[*.md]
max_line_length = off
trim_trailing_whitespace = false

42
client/admin/.gitignore vendored Normal file
View file

@ -0,0 +1,42 @@
# See https://docs.github.com/get-started/getting-started-with-git/ignoring-files for more about ignoring files.
# Compiled output
/dist
/tmp
/out-tsc
/bazel-out
# Node
/node_modules
npm-debug.log
yarn-error.log
# IDEs and editors
.idea/
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# Visual Studio Code
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
.history/*
# Miscellaneous
/.angular/cache
.sass-cache/
/connect.lock
/coverage
/libpeerconnection.log
testem.log
/typings
# System files
.DS_Store
Thumbs.db

View file

@ -0,0 +1,3 @@
# Ignore artifacts:
build
coverage

1
client/admin/.prettierrc Normal file
View file

@ -0,0 +1 @@
{}

4
client/admin/.vscode/extensions.json vendored Normal file
View file

@ -0,0 +1,4 @@
{
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=827846
"recommendations": ["angular.ng-template"]
}

20
client/admin/.vscode/launch.json vendored Normal file
View file

@ -0,0 +1,20 @@
{
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "ng serve",
"type": "chrome",
"request": "launch",
"preLaunchTask": "npm: start",
"url": "http://localhost:4200/"
},
{
"name": "ng test",
"type": "chrome",
"request": "launch",
"preLaunchTask": "npm: test",
"url": "http://localhost:9876/debug.html"
}
]
}

42
client/admin/.vscode/tasks.json vendored Normal file
View file

@ -0,0 +1,42 @@
{
// For more information, visit: https://go.microsoft.com/fwlink/?LinkId=733558
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "start",
"isBackground": true,
"problemMatcher": {
"owner": "typescript",
"pattern": "$tsc",
"background": {
"activeOnStart": true,
"beginsPattern": {
"regexp": "(.*?)"
},
"endsPattern": {
"regexp": "bundle generation complete"
}
}
}
},
{
"type": "npm",
"script": "test",
"isBackground": true,
"problemMatcher": {
"owner": "typescript",
"pattern": "$tsc",
"background": {
"activeOnStart": true,
"beginsPattern": {
"regexp": "(.*?)"
},
"endsPattern": {
"regexp": "bundle generation complete"
}
}
}
}
]
}

27
client/admin/README.md Normal file
View file

@ -0,0 +1,27 @@
# Admin
This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 18.2.10.
## Development server
Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The application will automatically reload if you change any of the source files.
## Code scaffolding
Run `ng generate component component-name` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module`.
## Build
Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory.
## Running unit tests
Run `ng test` to execute the unit tests via [Karma](https://karma-runner.github.io).
## Running end-to-end tests
Run `ng e2e` to execute the end-to-end tests via a platform of your choice. To use this command, you need to first add a package that implements end-to-end testing capabilities.
## Further help
To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI Overview and Command Reference](https://angular.dev/tools/cli) page.

87
client/admin/angular.json Normal file
View file

@ -0,0 +1,87 @@
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"admin": {
"projectType": "application",
"schematics": {},
"root": "",
"sourceRoot": "src",
"prefix": "app",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:application",
"options": {
"outputPath": "dist/admin",
"index": "src/index.html",
"browser": "src/main.ts",
"polyfills": ["zone.js"],
"tsConfig": "tsconfig.app.json",
"assets": [
{
"glob": "**/*",
"input": "public"
}
],
"styles": ["src/styles.css"],
"scripts": []
},
"configurations": {
"production": {
"budgets": [
{
"type": "initial",
"maximumWarning": "500kB",
"maximumError": "1MB"
},
{
"type": "anyComponentStyle",
"maximumWarning": "2kB",
"maximumError": "4kB"
}
],
"outputHashing": "all"
},
"development": {
"optimization": false,
"extractLicenses": false,
"sourceMap": true
}
},
"defaultConfiguration": "production"
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"configurations": {
"production": {
"buildTarget": "admin:build:production"
},
"development": {
"buildTarget": "admin:build:development"
}
},
"defaultConfiguration": "development"
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n"
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"polyfills": ["zone.js", "zone.js/testing"],
"tsConfig": "tsconfig.spec.json",
"assets": [
{
"glob": "**/*",
"input": "public"
}
],
"styles": ["src/styles.css"],
"scripts": []
}
}
}
}
}
}

14612
client/admin/package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

41
client/admin/package.json Normal file
View file

@ -0,0 +1,41 @@
{
"name": "admin",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"watch": "ng build --watch --configuration development",
"test": "ng test"
},
"private": true,
"dependencies": {
"@angular/animations": "^18.2.0",
"@angular/common": "^18.2.0",
"@angular/compiler": "^18.2.0",
"@angular/core": "^18.2.0",
"@angular/forms": "^18.2.0",
"@angular/platform-browser": "^18.2.0",
"@angular/platform-browser-dynamic": "^18.2.0",
"@angular/router": "^18.2.0",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.14.10"
},
"devDependencies": {
"@angular-devkit/build-angular": "^18.2.10",
"@angular/cli": "^18.2.10",
"@angular/compiler-cli": "^18.2.0",
"@types/jasmine": "~5.1.0",
"daisyui": "^4.12.13",
"jasmine-core": "~5.2.0",
"karma": "~6.4.0",
"karma-chrome-launcher": "~3.2.0",
"karma-coverage": "~2.2.0",
"karma-jasmine": "~5.1.0",
"karma-jasmine-html-reporter": "~2.1.0",
"prettier": "3.3.3",
"tailwindcss": "^3.4.14",
"typescript": "~5.5.2"
}
}

View file

@ -0,0 +1,16 @@
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();
});
});

View file

@ -0,0 +1,36 @@
import { Injectable } from '@angular/core';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { Router } from '@angular/router';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
export class Jwt {
constructor(public jwt: string) {}
}
@Injectable({
providedIn: 'root',
})
export class APIService {
constructor(
private http: HttpClient,
private _router: Router,
) {}
login(username: string, password: string): Observable<HttpResponse<Jwt>> {
return this.http
.post<Jwt>(
'/login',
{ username: username, password: password },
{ observe: 'response' },
)
.pipe(
tap((event) => {
if (event.status == 200) {
sessionStorage.setItem('jwt', event.body?.jwt.toString() ?? '');
this._router.navigateByUrl('/home');
}
}),
);
}
}

View file

View file

@ -0,0 +1,26 @@
<div class="navbar bg-base-100">
<a class="btn btn-ghost text-xl">Stillbox</a>
</div>
<div class="drawer lg:drawer-open">
<input id="my-drawer-2" type="checkbox" class="drawer-toggle" />
<div class="drawer-content flex flex-col items-center justify-center">
<!-- Page content here -->
<div class="container mx-auto px-4">
<router-outlet />
</div>
<label for="my-drawer-2" class="btn btn-primary drawer-button lg:hidden">
Open drawer
</label>
</div>
<div class="drawer-side">
<label
for="my-drawer-2"
aria-label="close sidebar"
class="drawer-overlay"
></label>
<ul class="menu bg-base-200 text-base-content min-h-full w-80 p-4">
<li><a>Home</a></li>
<li><a>Talkgroups</a></li>
</ul>
</div>
</div>

View file

@ -0,0 +1,29 @@
import { TestBed } from '@angular/core/testing';
import { AppComponent } from './app.component';
describe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [AppComponent],
}).compileComponents();
});
it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app).toBeTruthy();
});
it(`should have the 'admin' title`, () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app.title).toEqual('admin');
});
it('should render title', () => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const compiled = fixture.nativeElement as HTMLElement;
expect(compiled.querySelector('h1')?.textContent).toContain('Hello, admin');
});
});

View file

@ -0,0 +1,13 @@
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet],
templateUrl: './app.component.html',
styleUrl: './app.component.css',
})
export class AppComponent {
title = 'admin';
}

View file

@ -0,0 +1,35 @@
import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
import { provideRouter } from '@angular/router';
import {
HttpRequest,
HttpHandlerFn,
HttpEvent,
withInterceptors,
} from '@angular/common/http';
import { Observable } from 'rxjs';
import { isDevMode } from '@angular/core';
import { routes } from './app.routes';
import { provideHttpClient } from '@angular/common/http';
export function apiBaseInterceptor(
req: HttpRequest<unknown>,
next: HttpHandlerFn,
): Observable<HttpEvent<unknown>> {
let baseUrl: string;
if (isDevMode()) {
baseUrl = 'http://xenon:3050';
} else {
baseUrl = '';
}
const apiReq = req.clone({ url: `${baseUrl}${req.url}` });
return next(apiReq);
}
export const appConfig: ApplicationConfig = {
providers: [
provideZoneChangeDetection({ eventCoalescing: true }),
provideRouter(routes),
provideHttpClient(withInterceptors([apiBaseInterceptor])),
],
};

View file

@ -0,0 +1,11 @@
import { Routes } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { LoginComponent } from './login/login.component';
import { AuthGuard } from './auth.guard';
export const routes: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent, canActivate: [AuthGuard] },
{ path: 'login', component: LoginComponent },
];

View file

@ -0,0 +1,17 @@
import { TestBed } from '@angular/core/testing';
import { CanActivateFn } from '@angular/router';
import { authGuard } from './auth.guard';
describe('authGuard', () => {
const executeGuard: CanActivateFn = (...guardParameters) =>
TestBed.runInInjectionContext(() => authGuard(...guardParameters));
beforeEach(() => {
TestBed.configureTestingModule({});
});
it('should be created', () => {
expect(executeGuard).toBeTruthy();
});
});

View file

@ -0,0 +1,9 @@
import { CanActivateFn } from '@angular/router';
export const AuthGuard: CanActivateFn = (route, state) => {
if (sessionStorage.getItem('jwt') == null) {
return false;
} else {
return true;
}
};

View file

@ -0,0 +1 @@
<p>Hi</p>

View file

@ -0,0 +1,22 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { HomeComponent } from './home.component';
describe('HomeComponent', () => {
let component: HomeComponent;
let fixture: ComponentFixture<HomeComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [HomeComponent],
}).compileComponents();
fixture = TestBed.createComponent(HomeComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View file

@ -0,0 +1,10 @@
import { Component } from '@angular/core';
@Component({
selector: 'app-home',
standalone: true,
imports: [],
templateUrl: './home.component.html',
styleUrl: './home.component.css',
})
export class HomeComponent {}

View file

@ -0,0 +1,34 @@
<div class="flex justify-center">
<div class="card w-96 bg-base-100 shadow-xl mt-20 mb-20">
<div class="card-body">
<div class="items-center mt-2">
<label class="input input-bordered flex items-center gap-2 mb-2">
<input
type="text"
[(ngModel)]="username"
class="grow"
placeholder="login"
/>
</label>
<label class="input input-bordered flex items-center gap-2 mb-2">
<input
type="password"
[(ngModel)]="password"
class="grow"
placeholder="password"
/>
</label>
</div>
<div class="card-actions justify-end">
<button (click)="onSubmit()" class="btn btn-primary w-full">
Login
</button>
</div>
</div>
@if (failed) {
<div role="alert" class="alert alert-error">
<span>Login Failed!</span>
</div>
}
</div>
</div>

View file

@ -0,0 +1,22 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { LoginComponent } from './login.component';
describe('LoginComponent', () => {
let component: LoginComponent;
let fixture: ComponentFixture<LoginComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [LoginComponent],
}).compileComponents();
fixture = TestBed.createComponent(LoginComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View file

@ -0,0 +1,39 @@
import { Component, inject } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { APIService } from '../api.service';
import { catchError, of } from 'rxjs';
import { Router } from '@angular/router';
@Component({
selector: 'app-login',
standalone: true,
imports: [FormsModule],
templateUrl: './login.component.html',
styleUrl: './login.component.css',
})
export class LoginComponent {
apiService: APIService = inject(APIService);
router: Router = inject(Router);
username: string = '';
password: string = '';
failed: boolean = false;
onSubmit() {
this.failed = false;
this.apiService
.login(this.username, this.password)
.pipe(
catchError(() => {
this.failed = true;
return of(null);
}),
)
.subscribe((event) => {
if (event?.status == 200) {
this.router.navigateByUrl('/home');
} else {
this.failed = true;
}
});
}
}

View file

@ -0,0 +1,13 @@
<!doctype html>
<html lang="en" data-theme="dark">
<head>
<meta charset="utf-8" />
<title>Admin</title>
<base href="/" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" type="image/x-icon" href="favicon.ico" />
</head>
<body>
<app-root></app-root>
</body>
</html>

7
client/admin/src/main.ts Normal file
View file

@ -0,0 +1,7 @@
import { bootstrapApplication } from '@angular/platform-browser';
import { appConfig } from './app/app.config';
import { AppComponent } from './app/app.component';
bootstrapApplication(AppComponent, appConfig).catch((err) =>
console.error(err),
);

View file

@ -0,0 +1,4 @@
/* You can add global styles to this file, and also import other style files */
@tailwind base;
@tailwind components;
@tailwind utilities;

View file

@ -0,0 +1,18 @@
/** @type {import('tailwindcss').Config} */
module.exports = {
content: ["./src/**/*.{html,ts}"],
theme: {
extend: {},
},
plugins: [require("daisyui")],
daisyui: {
themes: true,
styled: true,
themes: true,
base: true,
utils: true,
logs: true,
rtl: false,
},
};

View file

@ -0,0 +1,11 @@
/* To learn more about Typescript configuration file: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html. */
/* To learn more about Angular compiler options: https://angular.dev/reference/configs/angular-compiler-options. */
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/app",
"types": []
},
"files": ["src/main.ts"],
"include": ["src/**/*.d.ts"]
}

View file

@ -0,0 +1,30 @@
/* To learn more about Typescript configuration file: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html. */
/* To learn more about Angular compiler options: https://angular.dev/reference/configs/angular-compiler-options. */
{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"skipLibCheck": true,
"isolatedModules": true,
"esModuleInterop": true,
"sourceMap": true,
"declaration": false,
"experimentalDecorators": true,
"moduleResolution": "bundler",
"importHelpers": true,
"target": "ES2022",
"module": "ES2022",
"lib": ["ES2022", "dom"]
},
"angularCompilerOptions": {
"enableI18nLegacyMessageIdFormat": false,
"strictInjectionParameters": true,
"strictInputAccessModifiers": true,
"strictTemplates": true
}
}

View file

@ -0,0 +1,10 @@
/* To learn more about Typescript configuration file: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html. */
/* To learn more about Angular compiler options: https://angular.dev/reference/configs/angular-compiler-options. */
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/spec",
"types": ["jasmine"]
},
"include": ["src/**/*.spec.ts", "src/**/*.d.ts"]
}

View file

@ -0,0 +1,160 @@
package ruletime
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
)
// RuleTime specifies a start and end of a time period.
type RuleTime struct {
Hour uint8
Minute uint8
Second uint8
Duration time.Duration
}
type coversOptions struct {
loc *time.Location
}
type CoversOption func(*coversOptions)
// Must is for testing.
func Must(rt RuleTime, err error) RuleTime {
if err != nil {
panic(err)
}
return rt
}
// WithLocation makes Covers use the provided *time.Location
func WithLocation(loc *time.Location) CoversOption {
return func(o *coversOptions) {
o.loc = loc
}
}
// Covers returns whether the RuleTime covers the provided time.Time.
func (rt *RuleTime) Covers(t time.Time, opts ...CoversOption) bool {
o := coversOptions{}
for _, opt := range opts {
opt(&o)
}
if o.loc == nil {
o.loc = time.Local
}
start := time.Date(t.Year(), t.Month(), t.Day(), int(rt.Hour), int(rt.Minute), int(rt.Second), 0, o.loc)
end := start.Add(rt.Duration)
// wrap things around the clock
if end.Day() > start.Day() || end.Month() > start.Month() || end.Year() > start.Year() {
start = start.Add(-24 * time.Hour)
end = start.Add(rt.Duration)
if t.Sub(start) > 24*time.Hour {
t = t.Add(-24 * time.Hour)
}
}
return t.After(start) && t.Before(end)
}
// CoversNow returns whether the RuleTime covers this instant.
func (rt *RuleTime) CoversNow(opts ...CoversOption) bool {
return rt.Covers(time.Now(), opts...)
}
func (rt *RuleTime) MarshalJSON() ([]byte, error) {
return json.Marshal(rt.String())
}
func (rt *RuleTime) UnmarshalJSON(b []byte) error {
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
return rt.Parse(s)
}
func (rt *RuleTime) String() string {
return fmt.Sprintf("%d:%d:%d+%s", rt.Hour, rt.Minute, rt.Second, rt.Duration.String())
}
// ParseRuleTime emits a RuleTime from a string in the format of `[h]h:[m]m[:[s]s]+duration`
// such as: `08:09:00+1h2m`. Time must be in 24 hour format.
func (rt *RuleTime) Parse(s string) error {
perr := func(e string) error {
return fmt.Errorf("parse rule time '%s': %s", s, e)
}
werr := func(w error) error {
return fmt.Errorf("parse rule time '%s': %w", s, w)
}
timeDurationParts := strings.Split(s, "+")
if len(timeDurationParts) != 2 {
return perr("needs time and duration part")
}
timeSpec := strings.Split(timeDurationParts[0], ":")
if len(timeSpec) != 2 && len(timeSpec) != 3 {
return perr("invalid time part")
}
for i, v := range timeSpec {
nv, err := strconv.Atoi(v)
if err != nil {
return werr(err)
}
switch i {
case 0:
if nv > 23 || nv < 0 {
return perr("invalid hours")
}
rt.Hour = uint8(nv)
case 1:
if nv > 59 || nv < 0 {
return perr("invalid minutes")
}
rt.Minute = uint8(nv)
case 2:
if nv > 59 || nv < 0 {
return perr("invalid seconds")
}
rt.Second = uint8(nv)
}
}
dur, err := time.ParseDuration(timeDurationParts[1])
if err != nil {
return werr(err)
}
if dur < 0 {
return perr("duration must be positive")
}
if dur > 24*time.Hour {
return perr("duration too long")
}
rt.Duration = dur
return nil
}
// New creates a new RuleTime with the provided value.
func New(s string) (RuleTime, error) {
rt := RuleTime{}
return rt, rt.Parse(s)
}

View file

@ -0,0 +1,227 @@
package ruletime_test
import (
"errors"
"testing"
"time"
"dynatron.me/x/stillbox/internal/ruletime"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestParse(t *testing.T) {
tests := []struct {
name string
ruleTime string
compare ruletime.RuleTime
expectErr error
}{
{
name: "base case",
ruleTime: "23:45:01+2h1m",
compare: ruletime.RuleTime{
23, 45, 1, (2 * time.Hour) + time.Minute,
},
},
{
name: "no seconds",
ruleTime: "23:45+2h1m",
compare: ruletime.RuleTime{
23, 45, 0, (2 * time.Hour) + time.Minute,
},
},
{
name: "bad hour",
ruleTime: "29:45+2h1m",
expectErr: errors.New("invalid hours"),
},
{
name: "bad minute",
ruleTime: "22:70+2h1m",
expectErr: errors.New("invalid minutes"),
},
{
name: "bad minute 2",
ruleTime: "22:-70+2h1m",
expectErr: errors.New("invalid minutes"),
},
{
name: "bad hour 2",
ruleTime: "-20:34+2h1m",
expectErr: errors.New("invalid hours"),
},
{
name: "bad seconds",
ruleTime: "20:34:94+2h1m",
expectErr: errors.New("invalid seconds"),
},
{
name: "negative duration",
ruleTime: "20:34:33+-2h1m",
expectErr: errors.New("duration must be positive"),
},
{
name: "bad duration",
ruleTime: "20:34:04+2j",
expectErr: errors.New("in duration"),
},
{
name: "duration too long",
ruleTime: "20:34:04+25h",
expectErr: errors.New("duration too long"),
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
rt, err := ruletime.New(tc.ruleTime)
if tc.expectErr != nil {
require.Error(t, err)
assert.Contains(t, err.Error(), tc.expectErr.Error())
} else {
require.NoError(t, err)
assert.Equal(t, tc.compare, rt)
}
})
}
}
func TestCovers(t *testing.T) {
tM := func(s string) func(*time.Location) time.Time {
return func(loc *time.Location) time.Time {
tm, err := time.ParseInLocation("2006-01-02 15:04:05", "2024-11-01 "+s, loc)
if err != nil {
panic(err)
}
return tm
}
}
tz := func(s string) *time.Location {
l, err := time.LoadLocation(s)
if err != nil {
panic(err)
}
return l
}
tests := []struct {
name string
timespec string
t func(*time.Location) time.Time
opts []ruletime.CoversOption
loc *time.Location
covers bool
}{
{
name: "base",
timespec: "8:45+1h",
t: tM("9:00:00"),
covers: true,
},
{
name: "base false",
timespec: "8:45+1h",
t: tM("9:47:00"),
covers: false,
},
{
name: "wrong tz",
timespec: "8:45+1h",
t: tM("9:37:00"),
loc: tz("America/New_York"),
covers: false,
opts: []ruletime.CoversOption{ruletime.WithLocation(time.UTC)},
},
{
name: "wrong tz 2",
timespec: "8:45+1h",
t: tM("9:07:00"),
loc: tz("America/Chicago"),
covers: false,
opts: []ruletime.CoversOption{ruletime.WithLocation(tz("America/New_York"))},
},
{
name: "right tz",
timespec: "8:45+1h",
t: tM("9:17:00"),
loc: tz("America/New_York"),
covers: true,
opts: []ruletime.CoversOption{ruletime.WithLocation(tz("America/New_York"))},
},
{
name: "past midnight",
timespec: "23:45+1h",
t: tM("0:07:00"),
loc: tz("America/Chicago"),
covers: true,
opts: []ruletime.CoversOption{ruletime.WithLocation(tz("America/Chicago"))},
},
{
name: "not past midnight",
timespec: "15:00+10h",
t: tM("3:07:00"),
loc: tz("America/Chicago"),
covers: false,
opts: []ruletime.CoversOption{ruletime.WithLocation(tz("America/Chicago"))},
},
{
name: "not past midnight 2",
timespec: "15:00+10h",
t: tM("14:07:00"),
loc: tz("America/Chicago"),
covers: false,
opts: []ruletime.CoversOption{ruletime.WithLocation(tz("America/Chicago"))},
},
{
name: "not past midnight 3",
timespec: "15:00+10h",
t: tM("15:07:00"),
loc: tz("America/Chicago"),
covers: true,
opts: []ruletime.CoversOption{ruletime.WithLocation(tz("America/Chicago"))},
},
{
name: "normal",
timespec: "1:00+5h",
t: tM("17:07:00"),
covers: false,
},
{
name: "normal",
timespec: "1:00+5h",
t: tM("3:07:00"),
covers: true,
},
{
name: "24h duration",
timespec: "15:00+24h",
t: tM("3:07:00"),
loc: tz("America/Chicago"),
covers: true,
opts: []ruletime.CoversOption{ruletime.WithLocation(tz("America/Chicago"))},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
rt, err := ruletime.New(tc.timespec)
require.NoError(t, err)
loc := time.Local
if tc.loc != nil {
loc = tc.loc
}
c := rt.Covers(tc.t(loc), tc.opts...)
if tc.covers {
assert.True(t, c)
} else {
assert.False(t, c)
}
})
}
}

58
pkg/calls/alertconfig.go Normal file
View file

@ -0,0 +1,58 @@
package calls
import (
"encoding/json"
"time"
"dynatron.me/x/stillbox/internal/ruletime"
"dynatron.me/x/stillbox/internal/trending"
)
type AlertConfig map[Talkgroup][]AlertRule
type AlertRule struct {
Times []ruletime.RuleTime `json:"times"`
ScoreMultiplier float32 `json:"mult"`
}
func (ac AlertConfig) AddAlertConfig(tg Talkgroup, confBytes []byte) error {
if len(confBytes) == 0 {
return nil
}
var rules []AlertRule
err := json.Unmarshal(confBytes, &rules)
if err != nil {
return err
}
ac[tg] = rules
return nil
}
func (ac AlertConfig) ApplyAlertRules(score trending.Score[Talkgroup], t time.Time, coversOpts ...ruletime.CoversOption) float64 {
s, has := ac[score.ID]
if !has {
return score.Score
}
final := score.Score
for _, ar := range s {
if ar.MatchTime(t, coversOpts...) {
final *= float64(ar.ScoreMultiplier)
}
}
return final
}
func (ar *AlertRule) MatchTime(t time.Time, coversOpts ...ruletime.CoversOption) bool {
for _, at := range ar.Times {
if at.Covers(t, coversOpts...) {
return true
}
}
return false
}

View file

@ -0,0 +1,141 @@
package calls_test
import (
"errors"
"math"
"testing"
"time"
"dynatron.me/x/stillbox/internal/ruletime"
"dynatron.me/x/stillbox/internal/trending"
"dynatron.me/x/stillbox/pkg/calls"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAlertConfig(t *testing.T) {
ac := make(calls.AlertConfig)
parseTests := []struct {
name string
tg calls.Talkgroup
conf string
compare []calls.AlertRule
expectErr error
}{
{
name: "base case",
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: []calls.AlertRule{
{
Times: []ruletime.RuleTime{
ruletime.Must(ruletime.New("7:00+2h")),
ruletime.Must(ruletime.New("1:00+1h")),
ruletime.Must(ruletime.New("16:00+1h")),
ruletime.Must(ruletime.New("19:00+4h")),
},
ScoreMultiplier: 0.2,
},
{
Times: []ruletime.RuleTime{
ruletime.Must(ruletime.New("11:00+1h")),
ruletime.Must(ruletime.New("15:00+30m")),
ruletime.Must(ruletime.New("16:03+20m")),
},
ScoreMultiplier: 2.0,
},
},
},
{
name: "bad spec",
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"),
},
}
for _, tc := range parseTests {
t.Run(tc.name, func(t *testing.T) {
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[tc.tg])
}
})
}
tMust := func(s string) time.Time {
t, err := time.ParseInLocation("2006-01-02 15:04", "2024-11-02 "+s, time.Local)
if err != nil {
panic(err)
}
return t
}
evalTests := []struct {
name string
tg calls.Talkgroup
t time.Time
origScore float64
expectScore float64
}{
{
name: "base eval",
tg: calls.TG(197, 3),
t: tMust("1:20"),
origScore: 3,
expectScore: 0.6,
},
{
name: "base eval",
tg: calls.TG(197, 3),
t: tMust("23:03"),
origScore: 3,
expectScore: 3,
},
{
name: "base eval",
tg: calls.TG(197, 3),
t: tMust("8:03"),
origScore: 1.0,
expectScore: 0.2,
},
{
name: "base eval",
tg: calls.TG(197, 3),
t: tMust("15:15"),
origScore: 3.0,
expectScore: 6.0,
},
{
name: "overlapping eval",
tg: calls.TG(197, 3),
t: tMust("16:10"),
origScore: 1.0,
expectScore: 0.4,
},
}
for _, tc := range evalTests {
t.Run(tc.name, func(t *testing.T) {
cs := trending.Score[calls.Talkgroup]{
ID: tc.tg,
Score: tc.origScore,
}
assert.Equal(t, tc.expectScore, toFixed(ac.ApplyAlertRules(cs, tc.t), 5))
})
}
}
func round(num float64) int {
return int(num + math.Copysign(0.5, num))
}
func toFixed(num float64, precision int) float64 {
output := math.Pow(10, float64(precision))
return float64(round(num*output)) / output
}

View file

@ -2,8 +2,17 @@ package calls
import (
"context"
"fmt"
"sync"
"time"
"dynatron.me/x/stillbox/pkg/gordio/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 {
@ -27,6 +36,10 @@ func (t Talkgroup) Pack() int64 {
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))
@ -38,41 +51,141 @@ func PackedTGs(tg []Talkgroup) []int64 {
}
type tgMap map[Talkgroup]database.GetTalkgroupWithLearnedByPackedIDsRow
type TalkgroupCache struct {
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(ctx context.Context, packedTgs []int64) (*TalkgroupCache, error) {
tgc := &TalkgroupCache{
tgs: make(tgMap),
systems: make(map[int32]string),
func NewTalkgroupCache() TalkgroupCache {
tgc := &talkgroupCache{
tgs: make(tgMap),
systems: make(map[int32]string),
AlertConfig: make(AlertConfig),
}
return tgc, tgc.LoadTGs(ctx, packedTgs)
return tgc
}
func (t *TalkgroupCache) LoadTGs(ctx context.Context, packedTgs []int64) error {
tgRecords, err := database.FromCtx(ctx).GetTalkgroupWithLearnedByPackedIDs(ctx, packedTgs)
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 {
t.tgs[TG(rec.SystemID, rec.Tgid)] = rec
t.systems[rec.SystemID] = rec.SystemName
err := t.add(rec)
if err != nil {
log.Error().Err(err).Msg("add alert config fail")
}
}
return nil
}
func (t *TalkgroupCache) TG(tg Talkgroup) (database.GetTalkgroupWithLearnedByPackedIDsRow, bool) {
func (t *talkgroupCache) TG(ctx context.Context, tg Talkgroup) (database.GetTalkgroupWithLearnedByPackedIDsRow, bool) {
t.RLock()
rec, has := t.tgs[tg]
t.RUnlock()
return rec, has
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(id int) (string, bool) {
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
}

View file

@ -20,6 +20,8 @@ import (
"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"
)
@ -42,15 +44,16 @@ type Alerter interface {
type alerter struct {
sync.RWMutex
clock timeseries.Clock
cfg config.Alerting
scorer trending.Scorer[cl.Talkgroup]
scores trending.Scores[cl.Talkgroup]
lastScore time.Time
sim *Simulation
notifyCache map[cl.Talkgroup]time.Time
renotify time.Duration
notifier notify.Notifier
clock timeseries.Clock
cfg config.Alerting
scorer trending.Scorer[cl.Talkgroup]
scores trending.Scores[cl.Talkgroup]
lastScore time.Time
sim *Simulation
alertCache map[cl.Talkgroup]Alert
renotify time.Duration
notifier notify.Notifier
tgCache cl.TalkgroupCache
}
type offsetClock time.Duration
@ -85,16 +88,17 @@ func WithNotifier(n notify.Notifier) AlertOption {
}
// New creates a new Alerter using the provided configuration.
func New(cfg config.Alerting, opts ...AlertOption) Alerter {
func New(cfg config.Alerting, tgCache cl.TalkgroupCache, opts ...AlertOption) Alerter {
if !cfg.Enable {
return &noopAlerter{}
}
as := &alerter{
cfg: cfg,
notifyCache: make(map[cl.Talkgroup]time.Time),
clock: timeseries.DefaultClock,
renotify: DefaultRenotify,
cfg: cfg,
alertCache: make(map[cl.Talkgroup]Alert),
clock: timeseries.DefaultClock,
renotify: DefaultRenotify,
tgCache: tgCache,
}
if cfg.Renotify != nil {
@ -105,7 +109,7 @@ func New(cfg config.Alerting, opts ...AlertOption) Alerter {
opt(as)
}
as.scorer = trending.NewScorer[cl.Talkgroup](
as.scorer = trending.NewScorer(
trending.WithTimeSeries(as.newTimeSeries),
trending.WithStorageDuration[cl.Talkgroup](time.Hour*24*time.Duration(cfg.LookbackDays)),
trending.WithRecentDuration[cl.Talkgroup](time.Duration(cfg.Recent)),
@ -152,30 +156,71 @@ const notificationTemplStr = `{{ range . -}}
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)
}
db := database.FromCtx(ctx)
var notifications []Alert
for _, s := range as.scores {
origScore := s.Score
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.ApplyAlertRules(s, now)
a, err := as.makeAlert(ctx, s, origScore)
if err != nil {
return nil, fmt.Errorf("makeAlert: %w", err)
}
if s.Score < as.cfg.AlertThreshold {
a.Suppressed = true
}
as.alertCache[s.ID] = a
if !testMode {
err = db.AddAlert(ctx, a.ToAddAlertParams())
if err != nil {
return nil, fmt.Errorf("addAlert: %w", err)
}
}
if !a.Suppressed {
notifications = append(notifications, a)
}
}
}
}
return notifications, nil
}
func (as *alerter) testNotifyHandler(w http.ResponseWriter, r *http.Request) {
as.RLock()
defer as.RUnlock()
ns := make([]notification, 0, len(as.scores))
alerts := make([]Alert, 0, len(as.scores))
ctx := r.Context()
tgc, err := cl.NewTalkgroupCache(ctx, as.packedScoredTGs())
alerts, err := as.eval(ctx, time.Now(), true)
if err != nil {
log.Error().Err(err).Msg("test notificaiton tg cache")
log.Error().Err(err).Msg("test notification eval")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
for _, s := range as.scores {
n, err := makeNotification(tgc, s)
if err != nil {
log.Error().Err(err).Msg("test notificaiton")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ns = append(ns, n)
}
err = as.sendNotification(ctx, ns)
err = as.sendNotification(ctx, alerts)
if err != nil {
log.Error().Err(err).Msg("test notification send")
http.Error(w, err.Error(), http.StatusInternalServerError)
@ -185,14 +230,24 @@ func (as *alerter) testNotifyHandler(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("Sent"))
}
// packedScoredTGs gets a packed list of TG IDs for DB use.
func (as *alerter) packedScoredTGs() []int64 {
packedTGs := make([]int64, 0, len(as.scores))
// scoredTGs gets a list of TGs.
func (as *alerter) scoredTGs() []cl.Talkgroup {
tgs := make([]cl.Talkgroup, 0, len(as.scores))
for _, s := range as.scores {
packedTGs = append(packedTGs, s.ID.Pack())
tgs = append(tgs, s.ID)
}
return packedTGs
return tgs
}
// packedScoredTGs gets a list of packed TGIDs.
func (as *alerter) packedScoredTGs() []int64 {
tgs := make([]int64, 0, len(as.scores))
for _, s := range as.scores {
tgs = append(tgs, s.ID.Pack())
}
return tgs
}
// notify iterates the scores and sends out any necessary notifications
@ -201,38 +256,14 @@ func (as *alerter) notify(ctx context.Context) error {
return nil
}
now := time.Now()
as.Lock()
defer as.Unlock()
tgc, err := cl.NewTalkgroupCache(ctx, as.packedScoredTGs())
notifications, err := as.eval(ctx, time.Now(), false)
if err != nil {
return err
}
var notifications []notification
for _, s := range as.scores {
tgr, has := tgc.TG(s.ID)
if has {
if !tgr.Notify {
continue
}
s.Score *= float64(tgr.Weight)
}
if s.Score > as.cfg.AlertThreshold {
if t, inCache := as.notifyCache[s.ID]; !inCache || now.Sub(t) > as.renotify {
as.notifyCache[s.ID] = time.Now()
n, err := makeNotification(tgc, s)
if err != nil {
return err
}
notifications = append(notifications, n)
}
}
}
if len(notifications) > 0 {
return as.sendNotification(ctx, notifications)
}
@ -240,13 +271,38 @@ func (as *alerter) notify(ctx context.Context) error {
return nil
}
type notification struct {
TGName string
Score trending.Score[cl.Talkgroup]
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 []notification) error {
func (as *alerter) sendNotification(ctx context.Context, n []Alert) error {
msgBuffer := new(bytes.Buffer)
err := notificationTemplate.Execute(msgBuffer, n)
@ -259,16 +315,21 @@ func (as *alerter) sendNotification(ctx context.Context, n []notification) error
return as.notifier.Send(ctx, NotificationSubject, msgBuffer.String())
}
// makeNotification creates a notification for later rendering by the template.
// makeAlert creates a notification for later rendering by the template.
// It takes a talkgroup Score as input.
func makeNotification(tgs *cl.TalkgroupCache, score trending.Score[cl.Talkgroup]) (notification, error) {
d := notification{
Score: score,
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 := tgs.TG(score.ID)
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))
}
@ -279,7 +340,7 @@ func makeNotification(tgs *cl.TalkgroupCache, score trending.Score[cl.Talkgroup]
d.TGName = fmt.Sprintf("%s:%d", tgRecord.SystemName, int(score.ID.Talkgroup))
}
case false:
system, has := tgs.SystemName(int(score.ID.System))
system, has := as.tgCache.SystemName(ctx, int(score.ID.System))
if has {
d.TGName = fmt.Sprintf("%s:%d", system, int(score.ID.Talkgroup))
} else {
@ -301,9 +362,9 @@ func (as *alerter) cleanCache() {
as.Lock()
defer as.Unlock()
for k, t := range as.notifyCache {
if now.Sub(t) > as.renotify {
delete(as.notifyCache, k)
for k, a := range as.alertCache {
if now.Sub(a.Timestamp) > as.renotify {
delete(as.alertCache, k)
}
}
}

View file

@ -58,10 +58,12 @@ 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[cl.Talkgroup] {
func (s *Simulation) Simulate(ctx context.Context) (trending.Scores[cl.Talkgroup], error) {
now := time.Now()
tgc := cl.NewTalkgroupCache()
s.Enable = true
s.alerter = New(s.Alerting, WithClock(&s.clock)).(*alerter)
s.alerter = New(s.Alerting, tgc, WithClock(&s.clock)).(*alerter)
if time.Time(s.ScoreEnd).IsZero() {
s.ScoreEnd = jsontime.Time(now)
}
@ -79,7 +81,7 @@ func (s *Simulation) Simulate(ctx context.Context) trending.Scores[cl.Talkgroup]
// backfill from lookback start until score start
_, err := s.backfill(ctx, sinceLookback, time.Time(s.ScoreStart))
if err != nil {
log.Error().Err(err).Msg("simulate backfill")
return nil, fmt.Errorf("simulate backfill: %w", err)
}
// initial score
@ -99,13 +101,13 @@ func (s *Simulation) Simulate(ctx context.Context) trending.Scores[cl.Talkgroup]
// backfill from scorestart until now. sim is enabled, so scoring will be done by stepClock()
_, err = s.backfill(ctx, time.Time(s.ScoreStart), scoreEnd)
if err != nil {
log.Error().Err(err).Msg("simulate backfill final")
return nil, fmt.Errorf("simulate backfill final: %w", err)
}
s.lastScore = scoreEnd
sort.Sort(s.scores)
return s.scores
return s.scores, nil
}
// simulateHandler is the POST endpoint handler.
@ -136,6 +138,11 @@ func (as *alerter) simulateHandler(w http.ResponseWriter, r *http.Request) {
return
}
s.Simulate(ctx)
_, err = s.Simulate(ctx)
if err != nil {
err = fmt.Errorf("simulate: %w", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
s.tgStatsHandler(w, r)
}

View file

@ -151,7 +151,6 @@ func (a *Auth) routeRefresh(w http.ResponseWriter, r *http.Request) {
if a.allowInsecureCookie(r) {
cookie.Secure = false
cookie.SameSite = http.SameSiteLaxMode
log.Debug().Msg("same site none")
}
if cookie.Secure {

View file

@ -12,6 +12,46 @@ import (
"github.com/jackc/pgx/v5/pgtype"
)
const addAlert = `-- name: AddAlert :exec
INSERT INTO alerts (id, time, talkgroup, weight, score, orig_score, notified, metadata)
VALUES
(
$1,
$2,
$3,
$4,
$5,
$6,
$7,
$8
)
`
type AddAlertParams struct {
ID uuid.UUID `json:"id"`
Time pgtype.Timestamptz `json:"time"`
PackedTg int64 `json:"packed_tg"`
Weight *float32 `json:"weight"`
Score *float32 `json:"score"`
OrigScore *float32 `json:"orig_score"`
Notified bool `json:"notified"`
Metadata []byte `json:"metadata"`
}
func (q *Queries) AddAlert(ctx context.Context, arg AddAlertParams) error {
_, err := q.db.Exec(ctx, addAlert,
arg.ID,
arg.Time,
arg.PackedTg,
arg.Weight,
arg.Score,
arg.OrigScore,
arg.Notified,
arg.Metadata,
)
return err
}
const addCall = `-- name: AddCall :one
INSERT INTO calls (
id,

View file

@ -12,13 +12,15 @@ import (
)
type Alert struct {
ID int32 `json:"id"`
ID uuid.UUID `json:"id"`
Time pgtype.Timestamptz `json:"time"`
Talkgroup int64 `json:"talkgroup"`
SystemID int32 `json:"system_id"`
Tgid int32 `json:"tgid"`
Weight *float32 `json:"weight"`
Score *float32 `json:"score"`
OrigScore *float32 `json:"orig_score"`
Notified bool `json:"notified"`
Metadata []byte `json:"metadata"`
}
@ -80,17 +82,18 @@ type System struct {
}
type Talkgroup struct {
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"`
Notify bool `json:"notify"`
Weight float32 `json:"weight"`
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"`
}
type TalkgroupsLearned struct {

View file

@ -12,6 +12,7 @@ import (
)
type Querier interface {
AddAlert(ctx context.Context, arg AddAlertParams) 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)

View file

@ -31,7 +31,7 @@ func (q *Queries) GetSystemName(ctx context.Context, systemID int) (string, erro
}
const getTalkgroup = `-- name: GetTalkgroup :one
SELECT id, system_id, tgid, name, alpha_tag, tg_group, frequency, metadata, tags, notify, 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)
`
@ -48,7 +48,8 @@ func (q *Queries) GetTalkgroup(ctx context.Context, systemID int, tgid int) (Tal
&i.Frequency,
&i.Metadata,
&i.Tags,
&i.Notify,
&i.Alert,
&i.AlertConfig,
&i.Weight,
)
return i, err
@ -102,7 +103,7 @@ const getTalkgroupWithLearned = `-- name: GetTalkgroupWithLearned :one
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.notify, tg.weight,
tg.alert, tg.weight, tg.alert_config,
FALSE learned
FROM talkgroups tg
JOIN systems sys ON tg.system_id = sys.id
@ -111,7 +112,8 @@ 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,
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
@ -119,19 +121,20 @@ WHERE tgl.system_id = $1 AND tgl.tgid = $2 AND ignored IS NOT TRUE
`
type GetTalkgroupWithLearnedRow 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"`
Notify bool `json:"notify"`
Weight float32 `json:"weight"`
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) {
@ -148,8 +151,9 @@ func (q *Queries) GetTalkgroupWithLearned(ctx context.Context, systemID int, tgi
&i.Metadata,
&i.Tags,
&i.AlphaTag,
&i.Notify,
&i.Alert,
&i.Weight,
&i.AlertConfig,
&i.Learned,
)
return i, err
@ -159,7 +163,7 @@ const getTalkgroupWithLearnedByPackedIDs = `-- name: GetTalkgroupWithLearnedByPa
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.notify, tg.weight,
tg.alert, tg.weight, tg.alert_config,
FALSE learned
FROM talkgroups tg
JOIN systems sys ON tg.system_id = sys.id
@ -168,7 +172,8 @@ 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,
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
@ -176,19 +181,20 @@ WHERE systg2id(tgl.system_id, tgl.tgid) = ANY($1::INT8[]) AND ignored IS NOT TRU
`
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"`
Notify bool `json:"notify"`
Weight float32 `json:"weight"`
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) GetTalkgroupWithLearnedByPackedIDs(ctx context.Context, dollar_1 []int64) ([]GetTalkgroupWithLearnedByPackedIDsRow, error) {
@ -211,8 +217,9 @@ func (q *Queries) GetTalkgroupWithLearnedByPackedIDs(ctx context.Context, dollar
&i.Metadata,
&i.Tags,
&i.AlphaTag,
&i.Notify,
&i.Alert,
&i.Weight,
&i.AlertConfig,
&i.Learned,
); err != nil {
return nil, err
@ -226,25 +233,26 @@ func (q *Queries) GetTalkgroupWithLearnedByPackedIDs(ctx context.Context, dollar
}
const getTalkgroupsByPackedIDs = `-- name: GetTalkgroupsByPackedIDs :many
SELECT tg.id, system_id, tgid, tg.name, alpha_tag, tg_group, frequency, metadata, tags, notify, 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 {
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"`
Notify bool `json:"notify"`
Weight float32 `json:"weight"`
ID_2 int `json:"id_2"`
Name_2 string `json:"name_2"`
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) {
@ -266,7 +274,8 @@ func (q *Queries) GetTalkgroupsByPackedIDs(ctx context.Context, dollar_1 []int64
&i.Frequency,
&i.Metadata,
&i.Tags,
&i.Notify,
&i.Alert,
&i.AlertConfig,
&i.Weight,
&i.ID_2,
&i.Name_2,
@ -282,7 +291,7 @@ func (q *Queries) GetTalkgroupsByPackedIDs(ctx context.Context, dollar_1 []int64
}
const getTalkgroupsWithAllTags = `-- name: GetTalkgroupsWithAllTags :many
SELECT id, system_id, tgid, name, alpha_tag, tg_group, frequency, metadata, tags, notify, 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]
`
@ -305,7 +314,8 @@ func (q *Queries) GetTalkgroupsWithAllTags(ctx context.Context, tags []string) (
&i.Frequency,
&i.Metadata,
&i.Tags,
&i.Notify,
&i.Alert,
&i.AlertConfig,
&i.Weight,
); err != nil {
return nil, err
@ -319,7 +329,7 @@ func (q *Queries) GetTalkgroupsWithAllTags(ctx context.Context, tags []string) (
}
const getTalkgroupsWithAnyTags = `-- name: GetTalkgroupsWithAnyTags :many
SELECT id, system_id, tgid, name, alpha_tag, tg_group, frequency, metadata, tags, notify, 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]
`
@ -342,7 +352,8 @@ func (q *Queries) GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) (
&i.Frequency,
&i.Metadata,
&i.Tags,
&i.Notify,
&i.Alert,
&i.AlertConfig,
&i.Weight,
); err != nil {
return nil, err

View file

@ -6,6 +6,7 @@ import (
"os"
"time"
"dynatron.me/x/stillbox/pkg/calls"
"dynatron.me/x/stillbox/pkg/gordio/alerting"
"dynatron.me/x/stillbox/pkg/gordio/auth"
"dynatron.me/x/stillbox/pkg/gordio/config"
@ -34,6 +35,7 @@ type Server struct {
alerter alerting.Alerter
notifier notify.Notifier
hup chan os.Signal
tgCache calls.TalkgroupCache
}
func New(ctx context.Context, cfg *config.Config) (*Server, error) {
@ -56,6 +58,8 @@ func New(ctx context.Context, cfg *config.Config) (*Server, error) {
return nil, err
}
tgCache := calls.NewTalkgroupCache()
srv := &Server{
auth: authenticator,
conf: cfg,
@ -63,8 +67,9 @@ func New(ctx context.Context, cfg *config.Config) (*Server, error) {
r: r,
nex: nexus.New(),
logger: logger,
alerter: alerting.New(cfg.Alerting, alerting.WithNotifier(notifier)),
alerter: alerting.New(cfg.Alerting, tgCache, alerting.WithNotifier(notifier)),
notifier: notifier,
tgCache: tgCache,
}
srv.sinks.Register("database", sinks.NewDatabaseSink(srv.db), true)

View file

@ -54,7 +54,8 @@ CREATE TABLE IF NOT EXISTS talkgroups(
frequency INTEGER,
metadata JSONB,
tags TEXT[] NOT NULL DEFAULT '{}',
notify BOOLEAN NOT NULL DEFAULT 'true',
alert BOOLEAN NOT NULL DEFAULT 'true',
alert_config JSONB,
weight REAL NOT NULL DEFAULT 1.0
);
@ -71,13 +72,15 @@ CREATE TABLE IF NOT EXISTS talkgroups_learned(
);
CREATE TABLE IF NOT EXISTS alerts(
id SERIAL PRIMARY KEY,
id UUID PRIMARY KEY,
time TIMESTAMPTZ NOT NULL,
talkgroup INT8 REFERENCES talkgroups(id) NOT NULL,
system_id INT4 REFERENCES systems(id) NOT NULL GENERATED ALWAYS AS (talkgroup >> 32) STORED,
tgid INT4 NOT NULL GENERATED ALWAYS AS (talkgroup & x'ffffffff'::BIGINT) STORED,
weight REAL,
score REAL,
orig_score REAL,
notified BOOLEAN NOT NULL DEFAULT 'false',
metadata JSONB
);

View file

@ -23,5 +23,19 @@ RETURNING id;
-- name: SetCallTranscript :exec
UPDATE calls SET transcript = $2 WHERE id = $1;
-- name: AddAlert :exec
INSERT INTO alerts (id, time, talkgroup, weight, score, orig_score, notified, metadata)
VALUES
(
sqlc.arg(id),
sqlc.arg(time),
sqlc.arg(packed_tg),
sqlc.arg(weight),
sqlc.arg(score),
sqlc.arg(orig_score),
sqlc.arg(notified),
sqlc.arg(metadata)
);
-- name: GetDatabaseSize :one
SELECT pg_size_pretty(pg_database_size(current_database()));

View file

@ -37,7 +37,7 @@ WHERE tg.id = ANY($1::INT8[]);
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.notify, tg.weight,
tg.alert, tg.weight, tg.alert_config,
FALSE learned
FROM talkgroups tg
JOIN systems sys ON tg.system_id = sys.id
@ -46,7 +46,8 @@ 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,
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
@ -56,7 +57,7 @@ WHERE tgl.system_id = sqlc.arg(system_id) AND tgl.tgid = sqlc.arg(tgid) AND igno
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.notify, tg.weight,
tg.alert, tg.weight, tg.alert_config,
FALSE learned
FROM talkgroups tg
JOIN systems sys ON tg.system_id = sys.id
@ -65,7 +66,8 @@ 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,
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