Commit c37b5c19 authored by Sven Franck's avatar Sven Franck

erp5_officejs_afs_directory: switch to clean converter and publisher storage

parent 6d623b82
/*jslint indent: 2, nomen: true, maxlen: 80*/
/*global window, jIO, RSVP, DOMParser, Object */
(function (window, jIO, RSVP, DOMParser, Object) {
"use strict";
// XXX Bad proxy!
var LOC = 'https://softinst56769.host.vifib.net/erp5/web_site_module/afs/',
ID_LOC = "https://raw.githubusercontent.com/",
ID_PATH = "Nexedi/awesome-free-software/master/",
FETCH = "Software_getAnalysis?url=",
PARSER = new DOMParser();
function garble(str) {
return window.encodeURIComponent(str.replace(/ /g, "-"));
}
function isArray(value) {
return Object.prototype.toString.call(value) === '[object Array]';
}
function isValidCase(success_case) {
return (
success_case !== "N/A" &&
success_case.title !== "" &&
success_case.title !== "N/A"
);
}
// only called on repair, convert publisher storage content into records
// of publishers, software and success cases
function convertDataSet(storage, result_list) {
var response = [];
if (result_list.length === 0) {
return response;
}
return new RSVP.Queue()
.push(function () {
return RSVP.all(result_list.map(function (item) {
return storage.get(item.id);
}));
})
.push(function (publisher_list) {
var publisher,
software_list,
software,
success_case_list,
success_case,
i,
j,
k;
for (i = 0; i < publisher_list.length; i += 1) {
publisher = publisher_list[i];
// XXX parallel promises? title is too unreliable as id
publisher.item_id = result_list[i].id.split("/").pop().split(".")[0];
// add publisher id
response.push({
"id": garble(publisher.item_id),
"value": {}
});
software_list = publisher.free_software_list;
if (isArray(software_list)) {
for (j = 0; j < software_list.length; j += 1) {
software = software_list[j];
// add software id
response.push({
id: garble(publisher.item_id) + "/" + garble(software.title),
value: {}
});
success_case_list = software.success_case_list;
if (isArray(success_case_list)) {
for (k = 0; k < success_case_list.length; k += 1) {
success_case = success_case_list[k];
if (isValidCase(success_case)) {
// add case id
response.push({
id: garble(publisher.item_id) + "/" +
garble(software.title) + "/" +
garble(success_case.title),
value: {}
});
}
}
}
}
}
}
return response;
});
}
function retrieveOpenHubAnalysisTotalLines(software) {
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax({
type: "GET",
url: LOC + FETCH + software.source_code_profile,
dataType: "text"
});
})
.push(function (data) {
var response = data.target.response || data.target.responseText;
return PARSER.parseFromString(response, "text/xml")
.getElementsByTagName("total_code_lines")[0].childNodes[0].nodeValue;
});
}
function postFixProfileUrl(url) {
if (url.indexOf("languages_summary") === -1) {
return url + "/analyses/latest/languages_summary";
}
return url;
}
function isValidProfileUrl(url) {
return url && url !== "" && url.indexOf("hub") > -1;
}
function isInt(value) {
return !isNaN(value) && (function (x) {
if ((x | 0) === x) {
return x;
}
return 0;
})(parseFloat(value));
}
function totalLines(arr) {
return arr.reduce(function (running_total, line) {
return running_total += isInt(line);
}, 0);
}
function retrieveTotalLinesFromPublisher(publisher) {
/*
var unique_software_list = [];
return new RSVP.Queue()
.push(function () {
return RSVP.all(publisher.free_software_list.map(function (software) {
var profile_url;
if (isValidProfileUrl(software.source_code_profile)) {
profile_url = postFixProfileUrl(software.source_code_profile);
if (unique_software_list.indexOf(profile_url) === -1) {
unique_software_list.push(profile_url);
return retrieveOpenHubAnalysisTotalLines(software);
}
}
}).filter(Boolean));
})
.push(function (line_list) {
return totalLines(line_list);
});
*/
return 0;
}
function retrieveSuccessCaseFromSoftware(publisher, path_list) {
return publisher.free_software_list.reduce(function (response, software) {
var software_title = garble(software.title);
if (software_title === path_list[1]) {
if (isArray(software.success_case_list)) {
return software.success_case_list.reduce(function (response, item) {
var case_title = garble(item.title);
if (case_title === path_list[2]) {
item.portal_type = "success_case";
item.software = software.title;
item.software_website = software.website;
item.publisher = software.publisher;
item.publisher_website = software.website;
item.category_list = software.category_list;
item.uid = case_title;
return item;
}
return response;
});
}
}
return response;
}, undefined);
}
function retrieveSoftwareFromPublisher(publisher, path_list) {
return publisher.free_software_list.reduce(function (response, software) {
var software_title = garble(software.title);
if (software_title === path_list[1]) {
software.portal_type = "software";
software.publisher = publisher.title;
software.publisher_website = publisher.website;
software.uid = garble(publisher.title) + "/" + software_title;
return software;
}
return response;
}, undefined);
}
// find and assemble single object from publisher entries
function convertDataItem(storage, id) {
var path_list = id.split("/"),
path_len = path_list.length,
publisher;
return new RSVP.Queue()
.push(function () {
return new RSVP.Queue()
.push(function () {
return storage.get(ID_LOC + ID_PATH + path_list[0] + ".json");
})
.push(function (result) {
if (path_len === 1) {
return RSVP.all([
result,
retrieveTotalLinesFromPublisher(result)
]);
}
return [result];
});
})
.push(function (result_list) {
var response;
publisher = result_list[0];
publisher.portal_type = "publisher";
publisher.uid = garble(publisher.title);
publisher.total_lines = result_list[1] || 0;
if (path_len === 1) {
response = publisher;
} else if (path_len === 2) {
response = retrieveSoftwareFromPublisher(publisher, path_list);
} else if (path_len === 3) {
response = retrieveSuccessCaseFromSoftware(publisher, path_list);
}
return response;
})
.push(null, function (error) {
console.log(error);
throw error;
});
}
function ConverterStorage(spec) {
this._sub_storage = jIO.createJIO(spec.sub_storage);
}
ConverterStorage.prototype.get = function (id) {
return convertDataItem(this._sub_storage, id);
};
ConverterStorage.prototype.repair = function () {
return this._sub_storage.repair.apply(this._sub_storage, arguments);
};
ConverterStorage.prototype.hasCapacity = function (name) {
return (name === "list");
};
ConverterStorage.prototype.buildQuery = function () {
var storage = this._sub_storage,
argument_list = arguments;
return new RSVP.Queue()
.push(function () {
return storage.buildQuery.apply(storage, argument_list);
})
.push(function (result) {
return convertDataSet(storage, result);
});
};
jIO.addStorage('converter_storage', ConverterStorage);
}(window, jIO, RSVP, DOMParser, Object));
......@@ -9,9 +9,9 @@
<script src="renderjs.js"></script>
<script src="jiodev.js"></script>
<script src="gadget_erp5_afs_storage.js"></script>
<!--script src="gadget_erp5_afs_publisher_storage.js"></script-->
<!--script src="gadget_erp5_afs_converter_storage.js"></script-->
<!--script src="gadget_erp5_afs_storage.js"></script-->
<script src="gadget_erp5_afs_publisher_storage.js"></script>
<script src="gadget_erp5_afs_converter_storage.js"></script>
<script src="gadget_erp5_afs_jio.js"></script>
</head>
<body>
......
......@@ -79,7 +79,7 @@
</item>
<item>
<key> <string>description</string> </key>
<value> <string>Custom AFS storage gadget</string> </value>
<value> <string>Custom AFS Jio gadget</string> </value>
</item>
<item>
<key> <string>id</string> </key>
......@@ -101,7 +101,7 @@
</item>
<item>
<key> <string>title</string> </key>
<value> <string>Gadget AFS Storage</string> </value>
<value> <string>Gadget AFS Jio</string> </value>
</item>
<item>
<key> <string>version</string> </key>
......@@ -232,7 +232,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>961.9243.54613.9181</string> </value>
<value> <string>961.10708.46372.11741</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -250,7 +250,7 @@
</tuple>
<state>
<tuple>
<float>1501689797.72</float>
<float>1501772511.21</float>
<string>UTC</string>
</tuple>
</state>
......
......@@ -75,7 +75,7 @@
</item>
<item>
<key> <string>description</string> </key>
<value> <string>Custom AFS storage gadget</string> </value>
<value> <string>Custom AFS Jio gadget JS</string> </value>
</item>
<item>
<key> <string>id</string> </key>
......@@ -97,7 +97,7 @@
</item>
<item>
<key> <string>title</string> </key>
<value> <string>Gadget AFS Storage JS</string> </value>
<value> <string>Gadget AFS Jio JS</string> </value>
</item>
<item>
<key> <string>version</string> </key>
......@@ -228,7 +228,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>959.45138.58632.4420</string> </value>
<value> <string>961.10725.27982.54749</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -246,7 +246,7 @@
</tuple>
<state>
<tuple>
<float>1496066128.63</float>
<float>1501772490.89</float>
<string>UTC</string>
</tuple>
</state>
......
/*jslint indent: 2, nomen: true, maxlen: 80*/
/*global jIO, RSVP, JSON */
(function (jIO, RSVP, JSON) {
"use strict";
var HREF = 'https://api.github.com/repos/',
USER = 'Nexedi',
REPO = 'awesome-free-software',
BRANCH = '/contents/?ref=master';
function fetchUrl(option_dict) {
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax(option_dict);
})
.push(function (data) {
return JSON.parse(data.target.response || data.target.responseText);
});
}
function PublisherStorage(spec) {
this._href = spec.href || HREF;
this._user = spec.user || USER;
this._repo = spec.repo || REPO;
this._branch = spec.branch || BRANCH;
this._url = this._href + this._user + '/' + this._repo + this._branch;
}
PublisherStorage.prototype.get = function (id) {
return new RSVP.Queue()
.push(function () {
return fetchUrl({type: "GET", url: id, dataType: "text"});
})
.push(undefined, function (error) {
if ((error.target !== undefined) && (error.target.status === 404)) {
throw new jIO.util.jIOError("Cannot find document", 404);
}
throw error;
});
};
PublisherStorage.prototype.hasCapacity = function (name) {
return (name === "list");
};
PublisherStorage.prototype.buildQuery = function () {
var url = this._url;
return new RSVP.Queue()
.push(function () {
return fetchUrl({"type": "GET", "url": url});
})
.push(function (data_list) {
var result_list = [],
len = data_list.length,
data_entry,
i;
for (i = 0; i < len; i += 1) {
data_entry = data_list[i];
if (data_entry.path.indexOf(".json") > -1) {
result_list.push({
id: data_entry.download_url,
value: {}
});
}
}
return result_list;
});
};
jIO.addStorage('publisher_storage', PublisherStorage);
}(jIO, RSVP, JSON));
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment