API: Modularize data source loaders

feature/node-rewrite
Sven Slootweg 6 years ago
parent 385d526b0f
commit 1df8aa5981

@ -0,0 +1,52 @@
"use strict";
const Promise = require("bluebird");
const memoizee = require("memoizee");
const lsblk = require("../../wrappers/lsblk");
const All = require("../../graphql/symbols/all");
function linearizeDevices(devices) {
let linearizedDevices = [];
function add(list) {
for (let device of list) {
linearizedDevices.push(device);
if (device.children != null) {
add(device.children);
}
}
}
add(devices);
return linearizedDevices;
}
module.exports = function () {
let lsblkOnce = memoizee(() => {
return Promise.try(() => {
return lsblk();
}).then((devices) => {
return {
tree: devices,
list: linearizeDevices(devices)
};
});
});
return function (names) {
return Promise.try(() => {
return lsblkOnce();
}).then(({tree, list}) => {
return names.map((name) => {
if (name === All) {
return tree;
} else {
return list.find((device) => device.name === name);
}
});
});
};
};

@ -0,0 +1,25 @@
"use strict";
const Promise = require("bluebird");
const memoizee = require("memoizee");
const lvm = require("../../../wrappers/lvm");
const All = require("../../../graphql/symbols/all");
module.exports = function () {
let getPhysicalVolumesOnce = memoizee(lvm.getPhysicalVolumes);
return function (paths) {
return Promise.try(() => {
return getPhysicalVolumesOnce();
}).then((volumes) => {
return paths.map((path) => {
if (path === All) {
return volumes;
} else {
return volumes.find((device) => device.path === path);
}
});
});
};
};

@ -0,0 +1,12 @@
"use strict";
const Promise = require("bluebird");
const smartctl = require("../../../wrappers/smartctl");
module.exports = function () {
return function (paths) {
return Promise.map(paths, (path) => {
return smartctl.attributes({ devicePath: path });
});
};
};

@ -0,0 +1,12 @@
"use strict";
const Promise = require("bluebird");
const smartctl = require("../../../wrappers/smartctl");
module.exports = function () {
return function (paths) {
return Promise.map(paths, (path) => {
return smartctl.info({ devicePath: path });
});
};
};

@ -0,0 +1,25 @@
"use strict";
const Promise = require("bluebird");
const memoizee = require("memoizee");
const smartctl = require("../../../wrappers/smartctl");
const All = require("../../../graphql/symbols/all");
module.exports = function () {
let scanOnce = memoizee(smartctl.scan);
return function (paths) {
return Promise.try(() => {
return scanOnce();
}).then((devices) => {
return paths.map((path) => {
if (path === All) {
return devices;
} else {
return devices.find((device) => device.path === path);
}
});
});
};
};

@ -1,98 +1,21 @@
"use strict"; "use strict";
const Promise = require("bluebird");
const memoizee = require("memoizee");
const DataLoader = require("dataloader"); const DataLoader = require("dataloader");
const mapObj = require("map-obj");
const lvm = require("../wrappers/lvm");
const smartctl = require("../wrappers/smartctl"); let dataSourceFactories = {
const lsblk = require("../wrappers/lsblk"); lsblk: require("./data-sources/lsblk"),
const findmnt = require("../wrappers/findmnt"); smartctlInfo: require("./data-sources/smartctl/info"),
const All = require("../graphql/symbols/all"); smartctlScan: require("./data-sources/smartctl/scan"),
smartctlAttributes: require("./data-sources/smartctl/attributes"),
function linearizeDevices(devices) { lvmPhysicalVolumes: require("./data-sources/lvm/physical-volumes"),
let linearizedDevices = []; };
function add(list) {
for (let device of list) {
linearizedDevices.push(device);
if (device.children != null) {
add(device.children);
}
}
}
add(devices);
return linearizedDevices;
}
module.exports = function createLoaders() { module.exports = function createLoaders() {
/* The below is to ensure that commands that produce a full list of all possible items, only ever get called and processed *once* per query, no matter what data is requested. */ return mapObj(dataSourceFactories, (name, factory) => {
let smartctlScanOnce = memoizee(smartctl.scan); return [
let lvmGetPhysicalVolumesOnce = memoizee(lvm.getPhysicalVolumes); name,
new DataLoader(factory())
let lsblkOnce = memoizee(() => { ];
return Promise.try(() => {
return lsblk();
}).then((devices) => {
return {
tree: devices,
list: linearizeDevices(devices)
};
});
});
return {
lsblk: new DataLoader((names) => {
return Promise.try(() => {
return lsblkOnce();
}).then(({tree, list}) => {
return names.map((name) => {
if (name === All) {
return tree;
} else {
return list.find((device) => device.name === name);
}
});
});
}),
smartctlScan: new DataLoader((paths) => {
return Promise.try(() => {
return smartctlScanOnce();
}).then((devices) => {
return paths.map((path) => {
if (path === All) {
return devices;
} else {
return devices.find((device) => device.path === path);
}
});
});
}),
smartctlInfo: new DataLoader((paths) => {
return Promise.map(paths, (path) => {
return smartctl.info({ devicePath: path });
});
}),
smartctlAttributes: new DataLoader((paths) => {
return Promise.map(paths, (path) => {
return smartctl.attributes({ devicePath: path });
});
}),
lvmPhysicalVolumes: new DataLoader((paths) => {
return Promise.try(() => {
return lvmGetPhysicalVolumesOnce();
}).then((volumes) => {
return paths.map((path) => {
if (path === All) {
return volumes;
} else {
return volumes.find((device) => device.path === path);
}
});
}); });
}),
};
}; };
Loading…
Cancel
Save