Skip to content

Commit

Permalink
Fixed JS code for cache info, table info, and reports modal
Browse files Browse the repository at this point in the history
  • Loading branch information
Atulin committed Sep 14, 2024
1 parent f7dab96 commit bf09479
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 51 deletions.
6 changes: 5 additions & 1 deletion Ogma3/Areas/Admin/Api/V1/Telemetry/GetImportantItemsCount.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Immediate.Apis.Shared;
using Immediate.Handlers.Shared;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.EntityFrameworkCore;
Expand Down Expand Up @@ -32,9 +33,12 @@ CancellationToken cancellationToken
""";

var counts = await context.Database
.SqlQueryRaw<(string Name, int Count)>(sql)
.SqlQueryRaw<TableInfo>(sql)
.ToDictionaryAsync(i => i.Name, i => i.Count, cancellationToken);

return TypedResults.Ok(counts);
}

[UsedImplicitly]
private sealed record TableInfo(string Name, int Count);
}
5 changes: 4 additions & 1 deletion Ogma3/Areas/Admin/Api/V1/Telemetry/GetTableInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Immediate.Apis.Shared;
using Immediate.Handlers.Shared;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.EntityFrameworkCore;
Expand Down Expand Up @@ -31,10 +32,12 @@ FROM information_schema.tables
ORDER BY size DESC
""";
var info = await context.Database
.SqlQueryRaw<(string Name, ulong Size)>(sql)
.SqlQueryRaw<TableInfo>(sql)
.ToDictionaryAsync(t => t.Name, t => t.Size, cancellationToken);

return TypedResults.Ok(info);
}

[UsedImplicitly]
private sealed record TableInfo(string Name, ulong Size);
}
6 changes: 5 additions & 1 deletion Ogma3/Areas/Admin/Pages/Index.cshtml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
@page
@using Microsoft.AspNetCore.Antiforgery
@using Microsoft.AspNetCore.Mvc.TagHelpers
@model Ogma3.Areas.Admin.Pages.Index
@inject IAntiforgery Antiforgery

@{
ViewData["Title"] = "Dashboard";
ViewData["ActivePage"] = NavPages.Index;
var csrf = Antiforgery.GetAndStoreTokens(HttpContext).RequestToken;
}

<div class="counters">
Expand All @@ -18,7 +22,7 @@

<br>

<cache-info></cache-info>
<cache-info csrf="@csrf"></cache-info>

<br>

Expand Down
2 changes: 1 addition & 1 deletion Ogma3/wwwroot/js/src-webcomponents/report-modal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { html, LitElement } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { Reports_PostReports as postReport } from "../generated/paths-public";
import { PostApiReports as postReport } from "../generated/paths-public";
import { EReportableContentTypes } from "../generated/types-public";

@customElement("report-modal")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { LitElement, css, html } from "lit";
import { css, html, LitElement } from "lit";
import { DeleteAdminApiCache, GetAdminApiCache } from "../../../generated/paths-internal";
import { customElement, property, state } from "lit/decorators.js";

@customElement("cache-info")
export class CacheInfo extends LitElement {
constructor() {
super();
this.sortBy = "size";
this.sortOrder = "asc";
}

@state()
private cacheCount: number;

@property()
private csrf: string;

static get styles() {
return css`
Expand All @@ -25,12 +32,6 @@ export class CacheInfo extends LitElement {
`;
}

static get properties() {
return {
cacheCount: { type: Array, attribute: false },
};
}

async connectedCallback() {
super.connectedCallback();
this.classList.add("wc-loaded");
Expand All @@ -51,19 +52,17 @@ export class CacheInfo extends LitElement {
`;
}

async _load() {
const res = await fetch("/admin/api/cache");
private async _load() {
const res = await GetAdminApiCache();
if (!res.ok) return;
this.cacheCount = await res.json();
}

async _purge() {
private async _purge() {
if (confirm("Are you sure?")) {
const res = await fetch("/admin/api/cache", {
method: "DELETE",
});
const res = await DeleteAdminApiCache({ RequestVerificationToken: this.csrf });
if (!res.ok) return;
await this._load();
}
}
}

window.customElements.define("cache-info", CacheInfo);
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { LitElement, css, html } from "lit";
import { css, html, LitElement } from "lit";
import { GetAdminApiTelemetryGetTableInfo } from "../../../generated/paths-internal";
import { customElement, state } from "lit/decorators.js";

@customElement("table-info")
export class TableInfo extends LitElement {
constructor() {
super();
this.sortBy = "size";
this.sortOrder = "asc";
}

@state() private sortBy: "size" | "name" = "size";
@state() private sortOrder: "asc" | "desc" = "asc";
@state() private tableInfo: { name: string; size: number }[] = [];

static get styles() {
return css`
table {
Expand All @@ -27,14 +32,6 @@ export class TableInfo extends LitElement {
`;
}

static get properties() {
return {
tableInfo: { type: Array, attribute: false },
sortOrder: { type: String, attribute: false },
sortBy: { type: String, attribute: false },
};
}

async connectedCallback() {
super.connectedCallback();
this.classList.add("wc-loaded");
Expand All @@ -55,43 +52,41 @@ export class TableInfo extends LitElement {
Size ${this.sortBy !== "size" ? "⯁" : this.sortOrder === "asc" ? "⯆" : "⯅"}
</th>
</tr>
${
this.tableInfo
? this.tableInfo.map(
(i) => html`
${this.tableInfo
? this.tableInfo.map(
(i) => html`
<tr>
<td>${i.name}</td>
<td>${i.size} B</td>
<td>${this._formatBytes(i.size)}</td>
</tr>
`,
)
: html` <tr>
)
: html` <tr>
<td colspan="3">Loading...</td>
</tr>`
}
</tr>`}
</table>
`;
}

async _load() {
const res = await fetch("/admin/api/telemetry/gettableinfo");
this.tableInfo = await res.json();
private async _load() {
const res = await GetAdminApiTelemetryGetTableInfo();
if (!res.ok) return;
const data = await res.json();
this.tableInfo = Object.entries(data).map(([k, v]) => ({ name: k, size: Number.parseInt(v) }));
}

_sort(by) {
private _sort(by: "size" | "name") {
this.sortBy = by;
this.sortOrder = this.sortOrder === "desc" ? "asc" : "desc";
if (by === "size") {
this.tableInfo.sort((a, b) => (this.sortOrder === "desc" ? a.size - b.size : b.size - a.size));
} else {
this.tableInfo.sort((a, b) =>
this.sortOrder === "desc" ? a.name.localeCompare(b.name) : b.name.localeCompare(a.name),
);
this.tableInfo.sort((a, b) => (this.sortOrder === "desc" ? a.name.localeCompare(b.name) : b.name.localeCompare(a.name)));
}
}

_formatBytes(bytes, decimals = 2) {
private _formatBytes(bytes: number, decimals = 2) {
if (bytes === 0) return "0 B";

const k = 1024;
Expand All @@ -104,5 +99,3 @@ export class TableInfo extends LitElement {
return `${value} ${sizes[i]}`;
}
}

window.customElements.define("table-info", TableInfo);
2 changes: 1 addition & 1 deletion Ogma3/wwwroot/manifest.js.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"GeneratedAt":"2024-09-12T07:50:37.9720781Z","Files":{"/js/bundle/components.js":"xwF0zCR9YEAVCxE8tT_p9wXPPLzwtJqneVjJ7myAvyw","/js/dist/account/blacklists.js":"7Fy2n3RB96PoQEx-2xuZagkzb4uTpqxBwdFB7rBOJXg","/js/dist/account/invite-codes.js":"r43CD3sFTBGXLRcVO_uAs8ixbtTOW-UX72P3O7FJdNA","/js/dist/account/register.js":"lTI6nc18_Dd3jf4E_VZ_r3wZMh86pPb7A-ti_PGeJ88","/js/dist/admin/categories.js":"mVS9cqjyeOYSTzjnW4MQRsUNCEq4dg6sAAo6z7i0ZVs","/js/dist/admin/components/manage-infraction-component.js":"-l311L0zq--8kw0QQss036JC-LIHqtTmcdOdXgWhs3k","/js/dist/admin/faqs.js":"Lp9gamolphA4lpXd10U1byaUyVs2r76l8jRhk2_qeA0","/js/dist/admin/invite-codes.js":"b0mv3DkNNAK4r4AKpqPRUWJsMAJss8Tf8RH5Jp8qYTo","/js/dist/admin/lit-components/cache-info.js":"fuyr5TkO8dMPlEekVgFSC_bwkQ1HdKJf0HFWZXFg1U8","/js/dist/admin/lit-components/table-info.js":"lac0nIbFyX1riftMCZ_1FH9udcPDeeEsjv97LMgUvvU","/js/dist/admin/namespaces.js":"4q9LaBguuoIvQDnnZNMch_D-oyCXLYt3HlNrd_lsqg4","/js/dist/admin/quotes.js":"ngFaQWL_nOvTWELio0ExUMH0ID13CSuMZzJlJOAaz40","/js/dist/admin/ratings.js":"xWn9qdW3OB9uzG-deBJXJS1YJVCB6o_81KCpMRO5p1w","/js/dist/admin/roles.js":"UhE1Tp10SEPQ2cpHbviaczAvnnNQjTRtMmxvIAN5X_g","/js/dist/admin/settings.js":"R6edHou-OmbP2tpX2SP8_X7ZkgGeOqwrYwvXVr840lc","/js/dist/admin/tags.js":"tSLMQMrCzW1VP0aC7UlRf-GjslCAxarWy6Sfsx2THg4","/js/dist/admin/users.js":"8VWxBH8P8iEo6uoRlK8tqoFc6Ee4yigUkUc97lDNPk8","/js/dist/alerts.js":"0xrTKxc16Ry05brwq-WgMdd8RpBzGEOjnRGRNWS-dr0","/js/dist/chapter-reads.js":"kYU0fF1GaDD9ZDPUuoMpuDSO_HrbEm2eGIHn7qY8HFI","/js/dist/comments.js":"n689rfK15mWGax7W8NhDO7qHcKpJI_fofq6SGOGzA2k","/js/dist/components/comment-component.js":"InUPsRJeA4AZrhEqrhlFRa0vv-GUwtcT61eRSyVXjLE","/js/dist/components/input-blog-tags-component.js":"bAiz9BfjVdFPwbpDDoA8dNFYkFpoczzi3B4VrOGf_AE","/js/dist/components/input-counter-component.js":"V-fTwyBNoday6ixZuNgE_m8p2eA_Ehd37Mut048yRQU","/js/dist/components/input-file-component.js":"jIzG9K_xz6IOHTpuQ3ciSvaZKiE47RnoqksrRZwaKU8","/js/dist/components/rating-component.js":"ne4DrPCyAAgGpKf--0zpU7nmxbp8gA01rBC41K7sT9o","/js/dist/components/report-modal.js":"lvNxvmcEljYu1VFN18DEfnbJZMexTm9LydKE6gGR3i8","/js/dist/components/status-select-component.js":"mlf7A_h4_A2T6DB8JxyWWXtDonbpJVMyepsciSyrHLs","/js/dist/components/tag-search-select-component.js":"mZJSgwxMXqDOL-MfDMU4vGscR0NZ-N6KmENLrmdLCsI","/js/dist/components/textarea-counter-component.js":"FuiHKM4Iz5_OhF04nWFmfXdbTl_ntgOvXe8M8zNBwc4","/js/dist/components/toggle-component.js":"A0OAE1B2-Odfr2fQAJLjCO_zk_ExIA54R7qA9Fouz18","/js/dist/cookie-consent.js":"t_dYerrAhrXLqZqcDmsmhRL4WdizfrBfXVeHv27TkjA","/js/dist/editors/markdown-toolbar.js":"ei3tIeLC6Yi67-1-sZv5Mm8r5Vob6q8Ctda5IhrTnEY","/js/dist/input-progressbars.js":"s0wWsRpD6xhZ75t7q4Bqa_F9YTnpwS73TT86RSl0nlI","/js/dist/local-settings.js":"30r0VCZ28IODtwlY4Wb1wWrtds1BOViq2GnQ8kRnse8","/js/dist/login.js":"XjLIAOYijC6fPaRoHXvpZXS1qjcAxShrOA3uKQ4zJfM","/js/dist/navbar.js":"4pz9qAGgdWruboXc4Um1qOf8hpf1ZhYVDA1KqkD_kUo","/js/dist/notifications.js":"mbewwB-0EUUi7IZ36zqnkEn9ge_TXVrhZDLnxTKQ1k4","/js/dist/passwords.js":"dusNRJRGZzgkRoGYmqj4VbzRFIOhnY8Vee_L7QmfeAM","/js/dist/search/blog.js":"IZCZXHNyA1NAlOAqszkhPM5PuXyCus9oqNPXswHIGfM","/js/dist/search/story.js":"mCwWSkJ83yxJxfX9Uh7seu-ql1bhnDpFzizDlcH2Q7w","/js/dist/shelves.js":"wcSHgdkpXu-EZvrHiUYKNxWUQWQsna50FjV7GVIuFfE","/js/dist/site.js":"SvA4PuzeCQwyKpa_s7We3S_9HlMDqNcl69YJTuMI_Mw","/js/dist/sw/cache-service-worker.js":"Z6tA3zM5vuBVjyBklXdMmNubQu1qv_iZntioGXoawfo","/js/dist/themeswap.js":"I7yGQuK797X7HfTZxiPRtX5dV4zb4QxI3UgKIlM4Z9o","/js/dist/tts.js":"n8EcAC1dbOEJf8ZBUDK0sojtPT6NBjDUX_cVuUC-7p0","/js/dist/users.js":"uTkBcCFTcTZei0p6o7MxGmq0lF-Fs5wEo6SAEfTZnKk","/js/dist/vue-directives/closeable.js":"8b1NCfYqzeWlRHch9mhkdc7M45PivuWOQr-7sT453OU","/js/dist/watch-chapter-read.js":"xBtM6_ma6WLNaWcEMHLw9iRBwHvYctwGJaRdUHKWlaY"}}
{"GeneratedAt":"2024-09-14T22:05:45.2766035Z","Files":{"/js/bundle/components.js":"mejZGbnLIDf7PP6P2wRfKPs-YLJGTvrT3ZWcB-h4B7g","/js/dist/account/blacklists.js":"7Fy2n3RB96PoQEx-2xuZagkzb4uTpqxBwdFB7rBOJXg","/js/dist/account/invite-codes.js":"UvQ8QpZLIOhkigxtTKf5kVOwsdGDsRQP64GLwKrFNEo","/js/dist/account/register.js":"lTI6nc18_Dd3jf4E_VZ_r3wZMh86pPb7A-ti_PGeJ88","/js/dist/admin/categories.js":"mVS9cqjyeOYSTzjnW4MQRsUNCEq4dg6sAAo6z7i0ZVs","/js/dist/admin/components/manage-infraction-component.js":"F1uLamLkXKKN7WAXMJRY9gqcKRQvWyRWDQ9afqITOXE","/js/dist/admin/faqs.js":"ptocODw7hNUGkxOud5p4keQUVD3j5Gcnr5XbsXtSyXE","/js/dist/admin/invite-codes.js":"G4YXrzNlV0YwDLExzJtwUiS3cQWL-UeBW6bFfNIhiFI","/js/dist/admin/lit-components/cache-info.js":"fuyr5TkO8dMPlEekVgFSC_bwkQ1HdKJf0HFWZXFg1U8","/js/dist/admin/lit-components/table-info.js":"lac0nIbFyX1riftMCZ_1FH9udcPDeeEsjv97LMgUvvU","/js/dist/admin/namespaces.js":"4q9LaBguuoIvQDnnZNMch_D-oyCXLYt3HlNrd_lsqg4","/js/dist/admin/quotes.js":"eb0xfMapoy0JJ7W3UlDy_pj_jEzhzjyyygS_nbhe1D0","/js/dist/admin/ratings.js":"P75x-6Ao9AP8TG4hpyNzArhuPaQ9UJgREgjL_kzzRC0","/js/dist/admin/roles.js":"emd0CeOn7NHMhUuY36ECxKSaOg_qawQISnUUx1QHaIg","/js/dist/admin/settings.js":"R6edHou-OmbP2tpX2SP8_X7ZkgGeOqwrYwvXVr840lc","/js/dist/admin/tags.js":"R6_FY7u1zpyAGRJi2uDYJIhoEXONM6jL3jJK5yA-loM","/js/dist/admin/users.js":"0PwnqFbPZ7w4XtdMsuOLFDn9I5k17uV8DloeioSMrSY","/js/dist/alerts.js":"0xrTKxc16Ry05brwq-WgMdd8RpBzGEOjnRGRNWS-dr0","/js/dist/chapter-reads.js":"ORS0Z9RqyVHNrK7GM8HA38PX9V24GeOoS9Y9IkxeCjE","/js/dist/comments.js":"XdCnym4f2UusaDVf6vL8qZ76ax5XP1fnRe4IPSi0Vtg","/js/dist/components/comment-component.js":"GC0XqIx2bpbS4FX5wjwmJ_qK3yQERmkBgv01uoyOh64","/js/dist/components/input-blog-tags-component.js":"bAiz9BfjVdFPwbpDDoA8dNFYkFpoczzi3B4VrOGf_AE","/js/dist/components/input-counter-component.js":"V-fTwyBNoday6ixZuNgE_m8p2eA_Ehd37Mut048yRQU","/js/dist/components/input-file-component.js":"jIzG9K_xz6IOHTpuQ3ciSvaZKiE47RnoqksrRZwaKU8","/js/dist/components/rating-component.js":"ne4DrPCyAAgGpKf--0zpU7nmxbp8gA01rBC41K7sT9o","/js/dist/components/report-modal.js":"lvNxvmcEljYu1VFN18DEfnbJZMexTm9LydKE6gGR3i8","/js/dist/components/status-select-component.js":"mlf7A_h4_A2T6DB8JxyWWXtDonbpJVMyepsciSyrHLs","/js/dist/components/tag-search-select-component.js":"mZJSgwxMXqDOL-MfDMU4vGscR0NZ-N6KmENLrmdLCsI","/js/dist/components/textarea-counter-component.js":"FuiHKM4Iz5_OhF04nWFmfXdbTl_ntgOvXe8M8zNBwc4","/js/dist/components/toggle-component.js":"A0OAE1B2-Odfr2fQAJLjCO_zk_ExIA54R7qA9Fouz18","/js/dist/cookie-consent.js":"t_dYerrAhrXLqZqcDmsmhRL4WdizfrBfXVeHv27TkjA","/js/dist/editors/markdown-toolbar.js":"ei3tIeLC6Yi67-1-sZv5Mm8r5Vob6q8Ctda5IhrTnEY","/js/dist/input-progressbars.js":"s0wWsRpD6xhZ75t7q4Bqa_F9YTnpwS73TT86RSl0nlI","/js/dist/local-settings.js":"30r0VCZ28IODtwlY4Wb1wWrtds1BOViq2GnQ8kRnse8","/js/dist/login.js":"dZwpcjJd6TOgy7-gQR4cs_b-J1fO23BszTP1qEvsb_M","/js/dist/navbar.js":"4pz9qAGgdWruboXc4Um1qOf8hpf1ZhYVDA1KqkD_kUo","/js/dist/notifications.js":"sGSkxJuIlkQS-Y5dV25-bFcgP8SCFRNyzokIzuLRa-A","/js/dist/passwords.js":"dusNRJRGZzgkRoGYmqj4VbzRFIOhnY8Vee_L7QmfeAM","/js/dist/search/blog.js":"IZCZXHNyA1NAlOAqszkhPM5PuXyCus9oqNPXswHIGfM","/js/dist/search/story.js":"mCwWSkJ83yxJxfX9Uh7seu-ql1bhnDpFzizDlcH2Q7w","/js/dist/shelves.js":"fV3IcISK8pTTSpf7UL6hGd4CV2ALTPSZ0ebJbkTztAY","/js/dist/site.js":"SvA4PuzeCQwyKpa_s7We3S_9HlMDqNcl69YJTuMI_Mw","/js/dist/sw/cache-service-worker.js":"Z6tA3zM5vuBVjyBklXdMmNubQu1qv_iZntioGXoawfo","/js/dist/themeswap.js":"I7yGQuK797X7HfTZxiPRtX5dV4zb4QxI3UgKIlM4Z9o","/js/dist/tts.js":"n8EcAC1dbOEJf8ZBUDK0sojtPT6NBjDUX_cVuUC-7p0","/js/dist/users.js":"uTkBcCFTcTZei0p6o7MxGmq0lF-Fs5wEo6SAEfTZnKk","/js/dist/vue-directives/closeable.js":"8b1NCfYqzeWlRHch9mhkdc7M45PivuWOQr-7sT453OU","/js/dist/watch-chapter-read.js":"xBtM6_ma6WLNaWcEMHLw9iRBwHvYctwGJaRdUHKWlaY"}}

0 comments on commit bf09479

Please sign in to comment.