admin: add authorisations metric (#3811)
add authorizations metric Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org> Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
		@ -1,47 +0,0 @@
 | 
			
		||||
import { DEFAULT_CONFIG } from "@goauthentik/common/api/config";
 | 
			
		||||
import { AKChart } from "@goauthentik/elements/charts/Chart";
 | 
			
		||||
import { ChartData } from "chart.js";
 | 
			
		||||
 | 
			
		||||
import { t } from "@lingui/macro";
 | 
			
		||||
 | 
			
		||||
import { customElement } from "lit/decorators.js";
 | 
			
		||||
 | 
			
		||||
import { AdminApi, LoginMetrics } from "@goauthentik/api";
 | 
			
		||||
 | 
			
		||||
@customElement("ak-charts-admin-login")
 | 
			
		||||
export class AdminLoginsChart extends AKChart<LoginMetrics> {
 | 
			
		||||
    apiRequest(): Promise<LoginMetrics> {
 | 
			
		||||
        return new AdminApi(DEFAULT_CONFIG).adminMetricsRetrieve();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    getChartData(data: LoginMetrics): ChartData {
 | 
			
		||||
        return {
 | 
			
		||||
            datasets: [
 | 
			
		||||
                {
 | 
			
		||||
                    label: t`Failed Logins`,
 | 
			
		||||
                    backgroundColor: "rgba(201, 25, 11, .5)",
 | 
			
		||||
                    spanGaps: true,
 | 
			
		||||
                    data:
 | 
			
		||||
                        data.loginsFailedPer1h?.map((cord) => {
 | 
			
		||||
                            return {
 | 
			
		||||
                                x: cord.xCord || 0,
 | 
			
		||||
                                y: cord.yCord || 0,
 | 
			
		||||
                            };
 | 
			
		||||
                        }) || [],
 | 
			
		||||
                },
 | 
			
		||||
                {
 | 
			
		||||
                    label: t`Successful Logins`,
 | 
			
		||||
                    backgroundColor: "rgba(189, 229, 184, .5)",
 | 
			
		||||
                    spanGaps: true,
 | 
			
		||||
                    data:
 | 
			
		||||
                        data.loginsPer1h?.map((cord) => {
 | 
			
		||||
                            return {
 | 
			
		||||
                                x: cord.xCord || 0,
 | 
			
		||||
                                y: cord.yCord || 0,
 | 
			
		||||
                            };
 | 
			
		||||
                        }) || [],
 | 
			
		||||
                },
 | 
			
		||||
            ],
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -1,6 +1,16 @@
 | 
			
		||||
import { EVENT_REFRESH } from "@goauthentik/common/constants";
 | 
			
		||||
import { AKElement } from "@goauthentik/elements/Base";
 | 
			
		||||
import { Chart, ChartConfiguration, ChartData, ChartOptions, Plugin, Tick } from "chart.js";
 | 
			
		||||
import {
 | 
			
		||||
    Chart,
 | 
			
		||||
    ChartConfiguration,
 | 
			
		||||
    ChartData,
 | 
			
		||||
    ChartOptions,
 | 
			
		||||
    Filler,
 | 
			
		||||
    LineElement,
 | 
			
		||||
    Plugin,
 | 
			
		||||
    PointElement,
 | 
			
		||||
    Tick,
 | 
			
		||||
} from "chart.js";
 | 
			
		||||
import { Legend, Tooltip } from "chart.js";
 | 
			
		||||
import { BarController, DoughnutController, LineController } from "chart.js";
 | 
			
		||||
import { ArcElement, BarElement } from "chart.js";
 | 
			
		||||
@ -14,12 +24,33 @@ import { property } from "lit/decorators.js";
 | 
			
		||||
 | 
			
		||||
Chart.register(Legend, Tooltip);
 | 
			
		||||
Chart.register(LineController, BarController, DoughnutController);
 | 
			
		||||
Chart.register(ArcElement, BarElement);
 | 
			
		||||
Chart.register(TimeScale, LinearScale);
 | 
			
		||||
Chart.register(ArcElement, BarElement, PointElement, LineElement);
 | 
			
		||||
Chart.register(TimeScale, LinearScale, Filler);
 | 
			
		||||
 | 
			
		||||
export const FONT_COLOUR_DARK_MODE = "#fafafa";
 | 
			
		||||
export const FONT_COLOUR_LIGHT_MODE = "#151515";
 | 
			
		||||
 | 
			
		||||
export class RGBAColor {
 | 
			
		||||
    constructor(public r: number, public g: number, public b: number, public a: number = 1) {}
 | 
			
		||||
    toString(): string {
 | 
			
		||||
        return `rgba(${this.r}, ${this.g}, ${this.b}, ${this.a})`;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function getColorFromString(stringInput: string): RGBAColor {
 | 
			
		||||
    let hash = 0;
 | 
			
		||||
    for (let i = 0; i < stringInput.length; i++) {
 | 
			
		||||
        hash = stringInput.charCodeAt(i) + ((hash << 5) - hash);
 | 
			
		||||
        hash = hash & hash;
 | 
			
		||||
    }
 | 
			
		||||
    const rgb = [0, 0, 0];
 | 
			
		||||
    for (let i = 0; i < 3; i++) {
 | 
			
		||||
        const value = (hash >> (i * 8)) & 255;
 | 
			
		||||
        rgb[i] = value;
 | 
			
		||||
    }
 | 
			
		||||
    return new RGBAColor(rgb[0], rgb[1], rgb[2]);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export abstract class AKChart<T> extends AKElement {
 | 
			
		||||
    abstract apiRequest(): Promise<T>;
 | 
			
		||||
    abstract getChartData(data: T): ChartData;
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user