Skip to content

Commit

Permalink
Merge pull request #313 from zaanposni/zaanposni/adminendpoints
Browse files Browse the repository at this point in the history
zaanposni/adminendpoints
  • Loading branch information
zaanposni authored Jun 22, 2021
2 parents a52772b + a1fb3b5 commit 3f3d30f
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 26 deletions.
16 changes: 16 additions & 0 deletions backend/masz/Controllers/api/v1/AdminStatsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,21 @@ public async Task<IActionResult> Status() {
cachedDataFromDiscord = cache.Keys
});
}

[HttpPost("cache")]
public async Task<IActionResult> TriggerCache() {
logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | Incoming request.");
if (! await this.IsSiteAdmin()) {
logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | 401 Unauthorized.");
return Unauthorized();
}

Task task = new Task(() => {
this.cacher.CacheAll();
});
task.Start();

return Ok();
}
}
}
2 changes: 1 addition & 1 deletion backend/masz/Services/Implementations/PunishmentHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void StartTimer()
Thread.Sleep(1000 * 60);
}
});
task.Start();
task.Start();
logger.LogWarning("Finished action loop.");
}

Expand Down
112 changes: 94 additions & 18 deletions docs/discord-masz.postman_collection.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"info": {
"_postman_id": "b4f361ca-1687-49f0-a9d9-672384917577",
"_postman_id": "2b26a0ad-43ae-4279-9f65-7298cf76f29c",
"name": "discord-masz",
"description": "# Introduction\nWhat does your API do?\n\n# Overview\nThings that the developers should know about\n\n# Authentication\nWhat is the preferred way of using the API?\n\n# Error Codes\nWhat errors and status codes can a user expect?\n\n# Rate limit\nIs there a limit to the number of requests an user can send?",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
Expand All @@ -25,6 +25,12 @@
{
"key": "Cookie",
"value": "masz_access_token={{masz_access_token}}",
"type": "text",
"disabled": true
},
{
"key": "Authorization",
"value": "Bearer {{masz_api_token}}",
"type": "text"
}
],
Expand Down Expand Up @@ -674,8 +680,7 @@
"variable": [
{
"key": "caseid",
"value": "1",
"type": "string"
"value": "1"
}
]
}
Expand Down Expand Up @@ -788,8 +793,7 @@
},
{
"key": "dataname",
"value": "680e67b0d3528da5c1c10bc5b6db33520f4a2db5_7d2801f2_bobtreffen.png",
"type": "string"
"value": "680e67b0d3528da5c1c10bc5b6db33520f4a2db5_7d2801f2_bobtreffen.png"
}
]
}
Expand Down Expand Up @@ -899,8 +903,7 @@
"variable": [
{
"key": "dataname",
"value": "680e67b0d3528da5c1c10bc5b6db33520f4a2db5_7d2801f2_bobtreffen.png",
"type": "string"
"value": "680e67b0d3528da5c1c10bc5b6db33520f4a2db5_7d2801f2_bobtreffen.png"
}
]
}
Expand All @@ -921,6 +924,11 @@
"key": "Cookie",
"value": "masz_access_token={{masz_access_token}}",
"type": "text"
},
{
"key": "Authorization",
"value": "Bearer {{masz_api_token}}",
"type": "text"
}
],
"url": {
Expand All @@ -947,6 +955,12 @@
{
"key": "Cookie",
"value": "masz_access_token={{masz_access_token}}",
"type": "text",
"disabled": true
},
{
"key": "Authorization",
"value": "Bearer {{masz_api_token}}",
"type": "text"
}
],
Expand All @@ -965,8 +979,7 @@
"variable": [
{
"key": "userid",
"value": "749886323380453438",
"type": "string"
"value": "272655001329991681"
}
]
}
Expand Down Expand Up @@ -1338,8 +1351,7 @@
"variable": [
{
"key": "type",
"value": "0",
"type": "string"
"value": "0"
}
]
}
Expand Down Expand Up @@ -1373,8 +1385,7 @@
"variable": [
{
"key": "type",
"value": "0",
"type": "string"
"value": "0"
}
]
}
Expand Down Expand Up @@ -1453,8 +1464,7 @@
"variable": [
{
"key": "id",
"value": "9",
"type": "string"
"value": "9"
}
]
}
Expand Down Expand Up @@ -1486,8 +1496,7 @@
"variable": [
{
"key": "id",
"value": "1",
"type": "string"
"value": "1"
}
]
}
Expand Down Expand Up @@ -1909,7 +1918,7 @@
"variable": [
{
"key": "userid",
"value": "12"
"value": "272655001329991681"
}
]
}
Expand Down Expand Up @@ -2304,6 +2313,73 @@
"response": []
}
]
},
{
"name": "admin",
"item": [
{
"name": "adminstats",
"request": {
"method": "GET",
"header": [
{
"key": "Accept",
"value": "application/json",
"type": "text"
},
{
"key": "Cookie",
"value": "masz_access_token={{masz_access_token}}",
"type": "text"
}
],
"url": {
"raw": "{{url}}/api/v1/meta/adminstats",
"host": [
"{{url}}"
],
"path": [
"api",
"v1",
"meta",
"adminstats"
]
}
},
"response": []
},
{
"name": "trigger recache",
"request": {
"method": "POST",
"header": [
{
"key": "Accept",
"value": "application/json",
"type": "text"
},
{
"key": "Cookie",
"value": "masz_access_token={{masz_access_token}}",
"type": "text"
}
],
"url": {
"raw": "{{url}}/api/v1/meta/cache",
"host": [
"{{url}}"
],
"path": [
"api",
"v1",
"meta",
"cache"
]
}
},
"response": []
}
]
}
],
"description": "api v1\r\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
</ng-container>
<mat-list-item *ngIf="!loading && (showList == undefined || showList?.length == 0)">
<mat-icon mat-list-icon>error_outline</mat-icon>
<div mat-line>No logins found.</div>
<div mat-line>{{ errorMessage }}</div>
</mat-list-item>
</mat-list>
<button mat-raised-button color="accent"
*ngIf="!this.showAll && showList?.slice(5)?.length != 0 && !loading"
*ngIf="!this.showAll && showList?.slice(5)?.length != 0 && !loading && showList"
(click)="this.showAll = true">
Show more
</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class AdminlistComponent implements OnInit {
@Input() public showList: string[] | undefined = []
@Input() public loading: boolean = true;
@Input() public emote: string = "person";
@Input() public errorMessage: string = "No logins found.";
public showAll: boolean = false;
constructor() { }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<div style="height: 100px;"></div>
<div class="d-flex flex-column justify-content-center align-items-center">
<div class="d-flex flex-column justify-content-center align-items-left cw-50-75">
<div class="mat-display-2 font-weight-bold">Insights</div>
<div class="mat-display-2 font-weight-bold mb-0">Insights</div>
<div class="d-flex flex-row justify-content-end m-2">
<button mat-raised-button color="primary" aria-label="create token" class="flex-shrink-1" (click)="triggerCache()"
[disabled]="stats.loading">
Clear Cache
</button>
</div>
<app-adminlist class="flex-grow-1"
[title]="'Currently logged in'"
[showList]="stats.content?.loginsInLast15Minutes"
Expand All @@ -23,7 +29,8 @@
[title]="'Discord cache'"
[showList]="stats.content?.cachedDataFromDiscord"
[loading]="stats.loading"
[emote]="'save'">
[emote]="'save'"
[errorMessage]="'No cached data found.'">
</app-adminlist>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Component, OnInit } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { ToastrService } from 'ngx-toastr';
import { interval } from 'rxjs';
import { Adminstats } from 'src/app/models/Adminstats';
import { ContentLoading } from 'src/app/models/ContentLoading';
import { ApiService } from 'src/app/services/api.service';
import { ConfirmationDialogComponent } from '../../dialogs/confirmation-dialog/confirmation-dialog.component';

@Component({
selector: 'app-adminstats',
Expand All @@ -18,7 +20,7 @@ export class AdminstatsComponent implements OnInit {
public minutesToNewCache?: string = '--';
public stats: ContentLoading<Adminstats> = { loading: true, content: undefined };

constructor(private api: ApiService, private toastr: ToastrService) { }
constructor(private api: ApiService, private toastr: ToastrService, private dialog: MatDialog) { }

ngOnInit(): void {
this.reload();
Expand Down Expand Up @@ -52,5 +54,23 @@ export class AdminstatsComponent implements OnInit {

ngOnDestroy() {
this.subscription?.unsubscribe();
}
}

public triggerCache() {
const confirmDialogRef = this.dialog.open(ConfirmationDialogComponent);
confirmDialogRef.afterClosed().subscribe(confirmed => {
if (confirmed) {
this.api.postSimpleData(`/meta/cache`, {}).subscribe((data) => {
this.stats = { loading: true, content: undefined };
this.toastr.success("Cache cleared.");
setTimeout(() => {
this.reload();
}, 2000);
}, (error) => {
this.toastr.error("Something went wrong.");
console.error(error);
});
}
});
}
}
8 changes: 8 additions & 0 deletions nginx/static/patchnotes.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
[
{
"title": "Version 1.11.6",
"version": "v1.11.6",
"changes": [
"Added a clear cache button in the insights dashboard.",
"Adjusted some error messages to be clearer."
]
},
{
"title": "Version 1.11.5",
"version": "v1.11.5",
Expand Down
2 changes: 1 addition & 1 deletion nginx/static/version.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "v1.11.5",
"version": "v1.11.6",
"pre_release": false
}

0 comments on commit 3f3d30f

Please sign in to comment.