use db wifi_location as metric source

This commit is contained in:
Hendrik Schutter 2025-01-14 21:03:44 +01:00
parent e8154d1e13
commit 8a4eadefcb
4 changed files with 27 additions and 38 deletions

View File

@ -1,12 +1,10 @@
import express, { Request, Response } from "express";
import { container } from "tsyringe";
import { Counter, Gauge, collectDefaultMetrics, register } from "prom-client";
import { MetricsService } from "../services/metricsService";
import { LocationService } from "../services/locationService";
import { WifiLocationService } from "../services/wifiLocationService";
const router = express.Router();
const metricsService = container.resolve(MetricsService);
const locationService = container.resolve(LocationService);
const wifiLocationService = container.resolve(WifiLocationService);
@ -24,21 +22,28 @@ const requestCounter = new Counter({
const locationsTotal = new Gauge({
name: `${prefix}locations_total`,
help: "Total number of location entries in database",
labelNames: ["database"],
labelNames: ["database"],
});
const wifiLocationTotal = new Gauge({
name: `${prefix}wifi_locations_total`,
help: "Total number of wifi location entries in database",
labelNames: ["database"],
labelNames: ["database"],
});
const wigleApiRequest = new Gauge({
name: `${prefix}wigle_api_requests_total`,
help: "Total number of wifi location entries in database",
labelNames: ["wigle"],
const wifiLocationNotResolvable = new Gauge({
name: `${prefix}wifi_locations_not_resolvable`,
help: "Unresolved number of wifi location entries in database",
labelNames: ["database"],
});
const wifiLocationRequestLimitExceeded = new Gauge({
name: `${prefix}wifi_locations_request_limit_exceeded`,
help: "Unresolved number of wifi location because request limit exceeded entries in database",
labelNames: ["database"],
});
// Define the metrics endpoint
router.get("/", async (req: Request, res: Response) => {
try {
@ -46,7 +51,8 @@ router.get("/", async (req: Request, res: Response) => {
locationsTotal.set((await locationService.getAllLocations()).length);
wifiLocationTotal.set((await wifiLocationService.getAllWifiLocations()).length);
wigleApiRequest.set(metricsService.getWigleApiNumberOfRequests());
wifiLocationNotResolvable.set((await wifiLocationService.getAllWifiLocationsByNotResolvable).length);
wifiLocationRequestLimitExceeded.set((await wifiLocationService.getAllWifiLocationsByRequestLimitExceeded).length);
// Increment the counter with labels
requestCounter.inc({ method: req.method, route: req.route.path, status: 200 });

View File

@ -1,8 +1,3 @@
import { container } from "tsyringe";
import { MetricsService } from "../services/metricsService";
const metricsService = container.resolve(MetricsService);
interface WigleApiResonse {
response?: WifiLocationResponse,
status_code: number,
@ -52,7 +47,6 @@ interface Result {
export const getLocationForWifi = async (
mac: string
): Promise<WigleApiResonse | undefined> => {
metricsService.incWigleApiNumberOfRequests();
try {
const url = `${process.env.WIGLE_BASE_URL!}${process.env
.WIGLE_NETWORK_SEARCH!}?netid=${encodeURIComponent(mac)}`;

View File

@ -1,23 +0,0 @@
import { inject, injectable } from "tsyringe";
@injectable()
export class MetricsService {
private wigleApiNumberOfRequests: number;
constructor() {
this.wigleApiNumberOfRequests = 0; // Initialize the variable
}
// Method to increment the request count
public incWigleApiNumberOfRequests() {
console.log("inc")
this.wigleApiNumberOfRequests += 1; // Increment the number of requests
console.log(this.wigleApiNumberOfRequests)
}
// Method to get the current request count
public getWigleApiNumberOfRequests() {
console.log("get: " + this.wigleApiNumberOfRequests)
return this.wigleApiNumberOfRequests;
}
}

View File

@ -29,6 +29,18 @@ export class WifiLocationService {
});
}
public async getAllWifiLocationsByNotResolvable() {
return this.repository.findAll({
where: { location_not_resolvable: true },
});
}
public async getAllWifiLocationsByRequestLimitExceeded() {
return this.repository.findAll({
where: { request_limit_exceeded: true },
});
}
public async getWifiLocationByMac(mac: string) {
let wifiLocation = await this.repository.findById(mac);