Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add href attribute with a link to pg dump for each list element on /pages/dump #5358

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions app/javascript/controllers/dump_controller.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,64 @@
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);
});
}

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];
}
Expand Down