From 76f69afa15b0e973f01a4c4d50aecaed616862a1 Mon Sep 17 00:00:00 2001 From: "Stanislav (Stas) Katkov" Date: Mon, 23 Dec 2024 01:18:12 +0100 Subject: [PATCH] Add pg dump links or s3 to pages/data list --- app/javascript/controllers/dump_controller.js | 43 ++++++++++--------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/app/javascript/controllers/dump_controller.js b/app/javascript/controllers/dump_controller.js index 7c42fc55582..b1c51366ea2 100644 --- a/app/javascript/controllers/dump_controller.js +++ b/app/javascript/controllers/dump_controller.js @@ -1,44 +1,44 @@ -import { Controller } from "@hotwired/stimulus" +import { Controller } from "@hotwired/stimulus"; export default class extends Controller { - static targets = ["list", "template"] + static targets = ["list", "template"]; connect() { this.getDumpData(); } getDumpData() { - fetch('https://s3-us-west-2.amazonaws.com/rubygems-dumps/?prefix=production/public_postgresql') - .then(response => response.text()) - .then(data => { + fetch("https://s3-us-west-2.amazonaws.com/rubygems-dumps/?prefix=production/public_postgresql") + .then((response) => response.text()) + .then((data) => { const parser = new DOMParser(); const xml = parser.parseFromString(data, "application/xml"); const files = this.parseS3Listing(xml); this.render(files); }) - .catch(error => { + .catch((error) => { console.error(error); }); } parseS3Listing(xml) { - const contents = Array.from(xml.getElementsByTagName('Contents')); - return contents.map(item => { + const contents = Array.from(xml.getElementsByTagName("Contents")); + return contents.map((item) => { return { - Key: item.getElementsByTagName('Key')[0].textContent, - LastModified: item.getElementsByTagName('LastModified')[0].textContent, - Size: item.getElementsByTagName('Size')[0].textContent, - StorageClass: item.getElementsByTagName('StorageClass')[0].textContent + Key: item.getElementsByTagName("Key")[0].textContent, + LastModified: item.getElementsByTagName("LastModified")[0].textContent, + Size: item.getElementsByTagName("Size")[0].textContent, + StorageClass: item.getElementsByTagName("StorageClass")[0].textContent, }; }); } render(files) { files - .filter(item => 'STANDARD' === item.StorageClass) + .filter((item) => "STANDARD" === item.StorageClass) .sort((a, b) => Date.parse(b.LastModified) - Date.parse(a.LastModified)) - .forEach(item => { - let text = `${item.LastModified.replace('.000Z', '')} (${this.bytesToSize(item.Size)})`; + .forEach((item) => { + let text = `${item.LastModified.replace(".000Z", "")} (${this.bytesToSize(item.Size)})`; let uri = `https://s3-us-west-2.amazonaws.com/rubygems-dumps/${item.Key}`; this.appendItem(text, uri); }); @@ -46,16 +46,19 @@ export default class extends Controller { appendItem(text, uri) { const clone = this.templateTarget.content.cloneNode(true); - const a = clone.querySelector('a > span') - a.textContent = text; + const span = clone.querySelector("a > span"); + const a = clone.querySelector("a"); + span.textContent = text; a.href = uri; - this.element.appendChild(clone) + this.element.appendChild(clone); } bytesToSize(bytes) { - if (bytes === 0) { return '0 Bytes' } + if (bytes === 0) { + return "0 Bytes"; + } const k = 1024; - const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; + const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; const i = Math.floor(Math.log(bytes) / Math.log(k)); return (bytes / Math.pow(k, i)).toPrecision(3) + " " + sizes[i]; }