Module:Indicator datasets table
Jump to navigation
Jump to search
Documentation for this module may be created at Module:Indicator datasets table/doc
local p = {}
function p.getDatasets(frame)
local articleids = frame.args.articleids
if not articleids or articleids == '' then
return {}
end
-- Entferne alle Kommas und Leerzeichen am Ende von articleids
articleids = mw.ustring.gsub(articleids, '[,%s]*$', '')
local ids = mw.text.split(articleids, ",")
local datasets = {}
local titleobject
for _, id in ipairs(ids) do
titleobject = mw.title.new(tonumber(id))
local existspage = titleobject.exists
if existspage then
local content = titleobject:getContent()
local dataset = mw.text.jsonDecode(content)
-- Überprüfe, ob belongs to der aktuellen PageID entspricht
if tonumber(dataset.schemas.Dataset['belongs to']) == mw.title.getCurrentTitle().id then
table.insert(datasets, dataset)
end
else
return "Error: Page not found for ID " .. id
end
end
return datasets
end
-- Funktion zur Formatierung des Datums gemäß den Nutzereinstellungen
function formatDate(date)
return mw.language.getContentLanguage():formatDate(date)
end
function round(number, decimalPlaces)
local shift = 10 ^ decimalPlaces
return math.floor(number * shift + 0.5) / shift
end
function p.generateTable(frame)
local datasets = p.getDatasets(frame)
if not datasets or #datasets == 0 then
return "No data" -- Keine Datensätze gefunden, gib leeren String zurück
end
-- Number or percent
local entity = frame.args.entity
-- local entity = mw.slots.slotContent('jsondata', 'Tasks/Test_Task').schemas['Task']['title']
local suffix = ''
if entity == 'percent' then
suffix = '%'
end
-- Sortiere die Datensätze nach dem Datum
table.sort(datasets, function(a, b) return a.schemas.Dataset['date'] < b.schemas.Dataset['date'] end)
local html = mw.html.create()
local tableNode = html:tag('table'):addClass('wikitable sortable')
-- Check if the dataset contains sub-classes and sub-sub-classes
local dataset = datasets[1] -- Check first dataset
local hasSubClasses = dataset.schemas.Dataset.classes[1]['sub-classes'][1]['sub-class name'] and dataset.schemas.Dataset.classes[1]['sub-classes'][1]['sub-class name'] ~= ''
local hasSubSubClasses = dataset.schemas.Dataset.classes[1]['sub-classes'][1]['sub-sub-classes'][1]['sub-sub-class name'] and dataset.schemas.Dataset.classes[1]['sub-classes'][1]['sub-sub-classes'][1]['sub-sub-class name'] ~= ''
-- Erstelle die Tabelle mit Legenden aus dem ersten Datensatz
local headerRow = tableNode:tag('tr')
headerRow:tag('th'):wikitext('Class')
if hasSubClasses then headerRow:tag('th'):wikitext(dataset.schemas.Dataset.classes[1]['sub-classes'][1]['sub-class name']) end
if hasSubSubClasses then headerRow:tag('th'):wikitext(dataset.schemas.Dataset.classes[1]['sub-classes'][1]['sub-sub-classes'][1]['sub-sub-class name']) end
for _, dataset in ipairs(datasets) do
local formattedDate = formatDate(dataset.schemas.Dataset['date'])
headerRow:tag('th'):wikitext(formattedDate):tag('div'):tag('small'):wikitext(mw.message.new('webmo-' .. dataset.schemas.Dataset['dataset type']):plain()):tag('div'):tag('small')
end
local columnSums = {}
local columnCounts = {}
-- Füge die Datenzeilen hinzu
for _, classData in ipairs(datasets[1].schemas.Dataset.classes) do
local class = classData['class name']
for _, subClassData in ipairs(classData['sub-classes']) do
local subClass = subClassData['sub-class name']
for _, subSubClassData in ipairs(subClassData['sub-sub-classes']) do
local subSubClass = subSubClassData['sub-sub-class name']
local row = tableNode:tag('tr')
row:tag('td'):wikitext(class)
if hasSubClasses then row:tag('td'):wikitext(subClass) end
if hasSubSubClasses then row:tag('td'):wikitext(subSubClass) end
for i, dataset in ipairs(datasets) do
local value = {}
for _, currentClassData in ipairs(dataset.schemas.Dataset.classes) do
if currentClassData['class name'] == class then
for _, currentSubClassData in ipairs(currentClassData['sub-classes']) do
if currentSubClassData['sub-class name'] == subClass then
for _, currentSubSubClassData in ipairs(currentSubClassData['sub-sub-classes']) do
if currentSubSubClassData['sub-sub-class name'] == subSubClass then
local val = tonumber(currentSubSubClassData.value) or 0
table.insert(value, val)
-- Berechne Summen und Anzahlen für jede Spalte
columnSums[i] = columnSums[i] or 0
columnCounts[i] = columnCounts[i] or 0
columnSums[i] = columnSums[i] + val
columnCounts[i] = columnCounts[i] + 1
end
end
end
end
end
end
row:tag('td'):wikitext(table.concat(value, ", ") .. suffix)
end
end
end
end
-- Füge die Totalzeile hinzu
local totalRow = tableNode:tag('tr'):addClass('sortbottom')
totalRow:tag('th'):wikitext('Total')
if hasSubClasses then totalRow:tag('th'):wikitext('') end
if hasSubSubClasses then totalRow:tag('th'):wikitext('') end
for i, dataset in ipairs(datasets) do
local totalValue
if columnCounts[i] and columnCounts[i] > 0 then
if entity == 'percent' then
totalValue = columnSums[i] / columnCounts[i] -- Berechne Durchschnitt, falls Werte vorhanden sind
else
totalValue = columnSums[i]
end
else
totalValue = 'N/A'
end
totalRow:tag('th'):wikitext(round(totalValue, 2) .. suffix)
end
return tostring(html)
end
return p