Mô đun:Dữ liệu đại dịch COVID-19 tại Việt Nam

local viData = mw.title.new('Bản mẫu:Dữ liệu đại dịch COVID-19 tại Việt Nam/dữ liệu')
local jsonRawData = viData:getContent()
local data = mw.text.jsonDecode(jsonRawData)
local p = {}
local vaccineColumns = { vaccine_per_100 = true, dose1Percent = true, dose2Percent = true, dose3Percent = true, dose4Percent = true, dose5Percent = true, dose6Percent = true, dose7Percent = true, dose8Percent = true, dose9Percent = true, dose10Percent = true }

function format_num(number)
if not number then
return
end
local _, _, minus, int, dec = tostring(number):find('([-]?)(%d+)([.]?%d)')
int = int:reverse():gsub("(%d%d%d)", "%1.")
int = int:reverse():gsub("^%.", "")
int = minus .. int .. dec
return int
end

function p.main()
local out =
local data_s = {}
-- Khởi tạo bảng mới
local isArchive = mw.getCurrentFrame().args["yearArchive"]
if isArchive and isArchive:match("%d+") then
viData = mw.title.new('Bản mẫu:Dữ liệu đại dịch COVID-19 tại Việt Nam/dữ liệu ' .. isArchive)
jsonRawData = viData:getContent()
if jsonRawData ~= nil then
data = mw.text.jsonDecode(jsonRawData)
end
end
local locations = {}
if mw.getCurrentFrame().args['locations'] then
for location in string.gmatch(
mw.getCurrentFrame().args['locations'],
'([^,]+)'
) do
locations[location] = true
end
else
locations = nil
end
for key, value in pairs(data) do
local index
if mw.getCurrentFrame().args['sort'] then
if value[mw.getCurrentFrame().args['sort']] then
index = value[mw.getCurrentFrame().args['sort']]
else
index = 0
end
else
if value.cases then
index = value.cases
elseif value.deaths then
index = value.deaths
elseif value.newCase then
index = value.newCase
elseif value.totalInjected then
index = value.totalInjected
elseif value.dose1 then
index = value.dose1
elseif value.dose2 then
index = value.dose2
elseif value.dose3 then
index = value.dose3
elseif value.dose4 then
index = value.dose4
elseif value.dose5 then
index = value.dose5
elseif value.dose6 then
index = value.dose6
elseif value.dose7 then
index = value.dose7
elseif value.dose8 then
index = value.dose8
elseif value.dose9 then
index = value.dose9
elseif value.dose10 then
index = value.dose10
elseif value.allocatedReality then
index = value.allocatedReality
elseif value.population then
index = value.population
elseif value.vaccine_per_100 then
index = value.vaccine_per_100
elseif value.dose1Percent then
index = value.dose1Percent
elseif value.dose2Percent then
index = value.dose2Percent
elseif value.dose3Percent then
index = value.dose3Percent
elseif value.dose4Percent then
index = value.dose4Percent
elseif value.dose5Percent then
index = value.dose5Percent
elseif value.dose6Percent then
index = value.dose6Percent
elseif value.dose7Percent then
index = value.dose7Percent
elseif value.dose8Percent then
index = value.dose8Percent
elseif value.dose9Percent then
index = value.dose9Percent
elseif value.dose10Percent then
index = value.dose10Percent
else index = 0
end
end
if (not locations) or locations[key] then
data_s[data_s+1] = value
data_s[data_s]['_index'] = index
data_s[data_s]['_code'] = key
end
end
-- Sắp xếp bảng
table.sort(
data_s,
function(x, y)
if x._code 'VN' then
return true
elseif y._code 'VN' then
return false
else
return (x._index > y._index)
end
end
)
-- Khởi tạo cột
local columns = {
cases = false,
deaths = false,
newCase = false,
totalInjected = false,
dose1 = false,
dose2 = false,
dose3 = false,
dose4 = false,
dose5 = false,
dose6 = false,
dose7 = false,
dose8 = false,
dose9 = false,
dose10 = false,
allocatedReality = false,
population = false,
vaccine_per_100 = false,
dose1Percent = false,
dose2Percent = false,
dose3Percent = false,
dose4Percent = false,
dose5Percent = false,
dose6Percent = false,
dose7Percent = false,
dose8Percent = false,
dose9Percent = false,
dose10Percent = false
}
local columns_index = {}
if mw.getCurrentFrame().args['columns'] then
for column in string.gmatch(
mw.getCurrentFrame().args['columns'],
'([^,]+)'
) do
columns[column] = true
columns_index[columns_index+1] = column
end
else
columns = {
cases = true,
deaths = true,
newCase = true,
totalInjected = false,
dose1 = false,
dose2 = false,
dose3 = false,
dose4 = false,
dose5 = false,
dose6 = false,
dose7 = false,
dose8 = false,
dose9 = false,
dose10 = false,
allocatedReality = false,
population = false,
vaccine_per_100 = false,
dose1Percent = false,
dose2Percent = false,
dose3Percent = false,
dose4Percent = false,
dose5Percent = false,
dose6Percent = false,
dose7Percent = false,
dose8Percent = false,
dose9Percent = false,
dose10Percent = false
}
columns_index = {
'cases',
'deaths',
'newCase',
'totalInjected',
'dose1',
'dose2',
'dose3',
'dose4',
'dose5',
'dose6',
'dose7',
'dose8',
'dose9',
'dose10',
'allocatedReality',
'population',
'vaccine_per_100',
'dose1Percent',
'dose2Percent',
'dose3Percent',
'dose4Percent',
'dose5Percent',
'dose6Percent',
'dose7Percent',
'dose8Percent',
'dose9Percent',
'dose10Percent'
}
end
-- Tạo nội dung wikitext
for _, row in ipairs(data_s) do
-- Kiểm tra các hàng rỗng
local has_data = false
for _, column in pairs(columns_index) do
if columns[column] and row[column] then
has_data = true
end
end
if has_data then -- Chỉ thêm hàng nếu có dữ liệu
out = out .. '\n|-'
if row._code == 'VN' then
out = out .. 'class="sorttop static-row-header"' .. '\n|| Cả nước'
elseif row._code == 'HM' then
-- Thêm tên
out = out .. '\n|| ' .. row.name ..
else
-- Thêm tên
out = out .. '\n|| ' .. .. row.name ..
end
-- Thêm ghi chú
if row.note then
out = out ..
mw.getCurrentFrame():expandTemplate{
title = 'Efn',
args = {row.note}
}
end
-- Nạp các cột
for _, column in ipairs(columns_index) do
if columns[column] then
if row[column] then
if nil ~= vaccineColumns[column] then
out = out .. '||' .. string.format("%.1f", row[column]):gsub("%.", ",") .. '%'
else
out = out .. '|| data-sort-value=' ..
tostring(row[column]) ..
'|' .. format_num(row[column])
end
else
out = out ..
'|| style="background: cccccc;"' ..
' data-sort-value=0 | —'
end
end
end
end
end
out = out .. '\n|- class="sortbottom static-row-header" ' ..
'style="text-align: left;"\n' ..
'| colspan=8 style="width: 0;" |' ..
mw.getCurrentFrame():expandTemplate{
title = 'Danh sách ghi chú'
}
return out
end

function p.text()
local location = mw.getCurrentFrame().args['location']
local column = mw.getCurrentFrame().args['column']
return data[location][column] or "Không xác định"
end

return p