index.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <script setup lang="ts">
  2. import { defineAsyncComponent, ref, onMounted } from "vue";
  3. import { useRoute } from "vue-router";
  4. import Toast from "toasters";
  5. import { useWebsocketsStore } from "@/stores/websockets";
  6. import { useModalsStore } from "@/stores/modals";
  7. import { TableColumn, TableFilter, TableEvents } from "@/types/advancedTable";
  8. const AdvancedTable = defineAsyncComponent(
  9. () => import("@/components/AdvancedTable.vue")
  10. );
  11. const RunJobDropdown = defineAsyncComponent(
  12. () => import("@/components/RunJobDropdown.vue")
  13. );
  14. const LineChart = defineAsyncComponent(
  15. () => import("@/components/LineChart.vue")
  16. );
  17. const QuickConfirm = defineAsyncComponent(
  18. () => import("@/components/QuickConfirm.vue")
  19. );
  20. const route = useRoute();
  21. const { socket } = useWebsocketsStore();
  22. const quotaStatus = ref<
  23. Record<
  24. string,
  25. {
  26. title: string;
  27. quotaUsed: number;
  28. limit: number;
  29. quotaExceeded: boolean;
  30. }
  31. >
  32. >({});
  33. const fromDate = ref();
  34. const columnDefault = ref<TableColumn>({
  35. sortable: true,
  36. hidable: true,
  37. defaultVisibility: "shown",
  38. draggable: true,
  39. resizable: true,
  40. minWidth: 150,
  41. maxWidth: 600
  42. });
  43. const columns = ref<TableColumn[]>([
  44. {
  45. name: "options",
  46. displayName: "Options",
  47. properties: ["_id"],
  48. sortable: false,
  49. hidable: false,
  50. resizable: false,
  51. minWidth: 85,
  52. defaultWidth: 85
  53. },
  54. {
  55. name: "quotaCost",
  56. displayName: "Quota Cost",
  57. properties: ["quotaCost"],
  58. sortProperty: "quotaCost",
  59. minWidth: 150,
  60. defaultWidth: 150
  61. },
  62. {
  63. name: "timestamp",
  64. displayName: "Timestamp",
  65. properties: ["date"],
  66. sortProperty: "date",
  67. minWidth: 150,
  68. defaultWidth: 150
  69. },
  70. {
  71. name: "url",
  72. displayName: "URL",
  73. properties: ["url"],
  74. sortProperty: "url"
  75. },
  76. {
  77. name: "_id",
  78. displayName: "Request ID",
  79. properties: ["_id"],
  80. sortProperty: "_id",
  81. minWidth: 230,
  82. defaultWidth: 230
  83. }
  84. ]);
  85. const filters = ref<TableFilter[]>([
  86. {
  87. name: "_id",
  88. displayName: "Request ID",
  89. property: "_id",
  90. filterTypes: ["exact"],
  91. defaultFilterType: "exact"
  92. },
  93. {
  94. name: "quotaCost",
  95. displayName: "Quota Cost",
  96. property: "quotaCost",
  97. filterTypes: [
  98. "numberLesserEqual",
  99. "numberLesser",
  100. "numberGreater",
  101. "numberGreaterEqual",
  102. "numberEquals"
  103. ],
  104. defaultFilterType: "numberLesser"
  105. },
  106. {
  107. name: "timestamp",
  108. displayName: "Timestamp",
  109. property: "date",
  110. filterTypes: ["datetimeBefore", "datetimeAfter"],
  111. defaultFilterType: "datetimeBefore"
  112. },
  113. {
  114. name: "url",
  115. displayName: "URL",
  116. property: "url",
  117. filterTypes: ["contains", "exact", "regex"],
  118. defaultFilterType: "contains"
  119. }
  120. ]);
  121. const events = ref<TableEvents>({
  122. adminRoom: "youtube",
  123. removed: {
  124. event: "admin.youtubeApiRequest.removed",
  125. id: "requestId"
  126. }
  127. });
  128. const charts = ref({
  129. quotaUsage: {},
  130. apiRequests: {}
  131. });
  132. const jobs = ref([
  133. {
  134. name: "Reset stored API requests",
  135. socket: "youtube.resetStoredApiRequests"
  136. }
  137. ]);
  138. const { openModal } = useModalsStore();
  139. const getDateFormatted = createdAt => {
  140. const date = new Date(createdAt);
  141. const year = date.getFullYear();
  142. const month = `${date.getMonth() + 1}`.padStart(2, "0");
  143. const day = `${date.getDate()}`.padStart(2, "0");
  144. const hour = `${date.getHours()}`.padStart(2, "0");
  145. const minute = `${date.getMinutes()}`.padStart(2, "0");
  146. return `${year}-${month}-${day} ${hour}:${minute}`;
  147. };
  148. const removeApiRequest = requestId => {
  149. socket.dispatch(
  150. "youtube.removeStoredApiRequest",
  151. requestId,
  152. res => new Toast(res.message)
  153. );
  154. };
  155. onMounted(() => {
  156. socket.onConnect(() => {
  157. if (route.query.fromDate) fromDate.value = route.query.fromDate;
  158. socket.dispatch("youtube.getQuotaStatus", fromDate.value, res => {
  159. if (res.status === "success") quotaStatus.value = res.data.status;
  160. });
  161. socket.dispatch(
  162. "youtube.getQuotaChartData",
  163. "days",
  164. new Date().setDate(new Date().getDate() - 6),
  165. new Date().setDate(new Date().getDate() + 1),
  166. "usage",
  167. res => {
  168. if (res.status === "success")
  169. charts.value.quotaUsage = res.data;
  170. }
  171. );
  172. socket.dispatch(
  173. "youtube.getQuotaChartData",
  174. "days",
  175. new Date().setDate(new Date().getDate() - 6),
  176. new Date().setDate(new Date().getDate() + 1),
  177. "count",
  178. res => {
  179. if (res.status === "success")
  180. charts.value.apiRequests = res.data;
  181. }
  182. );
  183. });
  184. });
  185. </script>
  186. <template>
  187. <div class="admin-tab container">
  188. <page-metadata title="Admin | YouTube" />
  189. <div class="card tab-info">
  190. <div class="info-row">
  191. <h1>YouTube API</h1>
  192. <p>
  193. Analyze YouTube quota usage and API requests made on this
  194. instance
  195. </p>
  196. </div>
  197. <div class="button-row">
  198. <run-job-dropdown :jobs="jobs" />
  199. </div>
  200. </div>
  201. <div v-if="charts" class="card charts">
  202. <div class="chart">
  203. <h4 class="has-text-centered">Quota Usage</h4>
  204. <line-chart
  205. v-if="charts.quotaUsage"
  206. chart-id="youtube-quota-usage"
  207. :data="charts.quotaUsage"
  208. />
  209. </div>
  210. <div class="chart">
  211. <h4 class="has-text-centered">API Requests</h4>
  212. <line-chart
  213. v-if="charts.apiRequests"
  214. chart-id="youtube-api-requests"
  215. :data="charts.apiRequests"
  216. />
  217. </div>
  218. </div>
  219. <div class="card">
  220. <h4>Quota Stats</h4>
  221. <hr class="section-horizontal-rule" />
  222. <div class="quotas">
  223. <div
  224. v-for="[quotaName, quotaObject] in Object.entries(
  225. quotaStatus
  226. )"
  227. :key="quotaName"
  228. class="card quota"
  229. >
  230. <h5>{{ quotaObject.title }}</h5>
  231. <p>
  232. <strong>Quota used:</strong> {{ quotaObject.quotaUsed }}
  233. </p>
  234. <p><strong>Limit:</strong> {{ quotaObject.limit }}</p>
  235. <p>
  236. <strong>Quota exceeded:</strong>
  237. {{ quotaObject.quotaExceeded }}
  238. </p>
  239. </div>
  240. </div>
  241. </div>
  242. <div class="card">
  243. <h4>API Requests</h4>
  244. <hr class="section-horizontal-rule" />
  245. <advanced-table
  246. :column-default="columnDefault"
  247. :columns="columns"
  248. :filters="filters"
  249. :events="events"
  250. data-action="youtube.getApiRequests"
  251. name="admin-youtube-api-requests"
  252. :max-width="1140"
  253. >
  254. <template #column-options="slotProps">
  255. <div class="row-options">
  256. <button
  257. class="button is-primary icon-with-button material-icons"
  258. @click="
  259. openModal({
  260. modal: 'viewApiRequest',
  261. data: {
  262. requestId: slotProps.item._id,
  263. removeAction:
  264. 'youtube.removeStoredApiRequest'
  265. }
  266. })
  267. "
  268. :disabled="slotProps.item.removed"
  269. content="View API Request"
  270. v-tippy
  271. >
  272. open_in_full
  273. </button>
  274. <quick-confirm
  275. @confirm="removeApiRequest(slotProps.item._id)"
  276. :disabled="slotProps.item.removed"
  277. >
  278. <button
  279. class="button is-danger icon-with-button material-icons"
  280. content="Remove API Request"
  281. v-tippy
  282. >
  283. delete_forever
  284. </button>
  285. </quick-confirm>
  286. </div>
  287. </template>
  288. <template #column-_id="slotProps">
  289. <span :title="slotProps.item._id">{{
  290. slotProps.item._id
  291. }}</span>
  292. </template>
  293. <template #column-quotaCost="slotProps">
  294. <span :title="slotProps.item.quotaCost">{{
  295. slotProps.item.quotaCost
  296. }}</span>
  297. </template>
  298. <template #column-timestamp="slotProps">
  299. <span :title="new Date(slotProps.item.date).toString()">{{
  300. getDateFormatted(slotProps.item.date)
  301. }}</span>
  302. </template>
  303. <template #column-url="slotProps">
  304. <span :title="slotProps.item.url">{{
  305. slotProps.item.url
  306. }}</span>
  307. </template>
  308. </advanced-table>
  309. </div>
  310. </div>
  311. </template>
  312. <style lang="less" scoped>
  313. .night-mode .admin-tab {
  314. .table {
  315. color: var(--light-grey-2);
  316. background-color: var(--dark-grey-3);
  317. thead tr {
  318. background: var(--dark-grey-3);
  319. td {
  320. color: var(--white);
  321. }
  322. }
  323. tbody tr:hover {
  324. background-color: var(--dark-grey-4) !important;
  325. }
  326. tbody tr:nth-child(even) {
  327. background-color: var(--dark-grey-2) !important;
  328. }
  329. strong {
  330. color: var(--light-grey-2);
  331. }
  332. }
  333. .card .quotas .card.quota {
  334. background-color: var(--dark-grey-2) !important;
  335. }
  336. }
  337. .admin-tab {
  338. td {
  339. vertical-align: middle;
  340. }
  341. .is-primary:focus {
  342. background-color: var(--primary-color) !important;
  343. }
  344. .card {
  345. &.charts {
  346. flex-direction: row !important;
  347. .chart {
  348. width: 50%;
  349. }
  350. @media screen and (max-width: 1100px) {
  351. flex-direction: column !important;
  352. .chart {
  353. width: unset;
  354. &:not(:first-child) {
  355. margin-top: 10px;
  356. }
  357. }
  358. }
  359. }
  360. .quotas {
  361. display: flex;
  362. flex-direction: row !important;
  363. row-gap: 10px;
  364. column-gap: 10px;
  365. .card.quota {
  366. background-color: var(--light-grey) !important;
  367. padding: 10px !important;
  368. flex-basis: 33.33%;
  369. &:not(:last-child) {
  370. margin-right: 10px;
  371. }
  372. h5 {
  373. margin-bottom: 5px !important;
  374. }
  375. }
  376. @media screen and (max-width: 1100px) {
  377. flex-direction: column !important;
  378. .card.quota {
  379. flex-basis: unset;
  380. }
  381. }
  382. }
  383. }
  384. }
  385. </style>