Jump to content

Module:Medical cases data

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mxn (talk | contribs) at 08:44, 1 June 2020 (Created page with 'local p = {} local lang = mw.getContentLanguage() local tabularData = require("Module:Tabular data") local wd = require("Module:wd") local function round(x) re...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

local p = {}
local lang = mw.getContentLanguage()
local tabularData = require("Module:Tabular data")
local wd = require("Module:wd")

local function round(x)
	return (math.modf(x + (x < 0 and -0.5 or 0.5)))
end

function p._regionData(outbreakItem, columnsByRegion, populationDate)
	local outbreakEntityStatements = mw.wikibase.getBestStatements(outbreakItem, "P527")
	
	local regions = {}
	for i, statement in ipairs(outbreakEntityStatements) do
		local outbreakEntity = statement.mainsnak.datavalue.value.id
		local locationEntity = mw.wikibase.getBestStatements(outbreakEntity, "P276")[1].mainsnak.datavalue.value.id
		local dataTableName = mw.wikibase.getBestStatements(outbreakEntity, "P8204")[1].mainsnak.datavalue.value
		local dataTable = mw.ext.data.get((dataTableName:gsub("^Data:", "")))
		
		local columns = columnsByRegion[outbreakEntity] or columnsByRegion[locationEntity]
		local region = {
			name = mw.wikibase.getLabel(locationEntity),
			link = mw.wikibase.getSitelink(locationEntity),
			population = tonumber(wd._property({
				"raw",
				locationEntity,
				"P1082",
				P585 = populationDate,
			})),
			dataTableName = dataTableName,
			cases = tabularData._cell({
				data = dataTable,
				output_row = -1,
				output_column = columns and columns.cases or "totalConfirmedCases",
			}) + (columns and columns.cases2 and tabularData._cell({
				data = dataTable,
				output_row = -1,
				output_column = columns.cases2,
			}) or 0),
			deaths = tabularData._cell({
				data = dataTable,
				output_row = -1,
				output_column = columns and columns.deaths or "deaths",
			}) or tabularData._lookup({
				data = dataTable,
				search_pattern = "%d",
				search_column = columns and columns.deaths or "deaths",
				occurrence = -1,
				output_column = columns and columns.deaths or "deaths",
			}),
			recoveries = columns and columns.recoveries and tabularData._cell({
				data = dataTable,
				output_row = -1,
				output_column = columns.recoveries,
			}),
		}
		table.insert(regions, region)
	end
	return regions
end

-- Usage: =p._caseTable({config="San Francisco Bay Area"})
function p._caseTable(args)
	local config = args.config and mw.loadData("Module:User:Mxn/COVID-19 pandemic data/" .. args.config)
	local populationDate = config and config.populationDate or args.populationDate
	local regions = p._regionData(
		config and config.outbreakItem or args.outbreakItem,
		config and config.columnsByRegion,
		populationDate)
	table.sort(regions, function (left, right)
		return left.cases > right.cases
	end)
	
	local htmlTable = mw.html.create("table")
		:addClass("wikitable")
		:addClass("sortable")
	local headerRow = htmlTable
		:tag("tr")
	headerRow
		:tag("th")
		:attr("scope", "col")
		:attr("data-sort-type", "string")
		:wikitext(args.regionTerm or "Regions")
	headerRow
		:tag("th")
		:attr("scope", "col")
		:attr("data-sort-type", "number")
		:wikitext("Cases")
	headerRow
		:tag("th")
		:attr("scope", "col")
		:attr("data-sort-type", "number")
		:wikitext("Deaths")
	headerRow
		:tag("th")
		:attr("scope", "col")
		:attr("data-sort-type", "number")
		:wikitext("Recoveries")
	headerRow
		:tag("th")
		:attr("scope", "col")
		:attr("data-sort-type", "number")
		:wikitext(populationDate and mw.ustring.format("Population (%d)", lang:formatDate("Y", populationDate)) or "Population")
	headerRow
		:tag("th")
		:attr("scope", "col")
		:attr("data-sort-type", "number")
			:tag("abbr")
			:attr("title", "Cases per 10,000 inhabitants")
			:wikitext("Cases/10k")
	headerRow
		:tag("th")
		:attr("scope", "col")
		:addClass("unsortable")
			:tag("abbr")
			:attr("title", "Reference")
			:wikitext("Ref.")
	
	for i, region in ipairs(regions) do
		local row = htmlTable:tag("tr")
		row
			:tag("th")
			:attr("scope", "row")
			:wikitext(mw.ustring.format("[[%s|%s]]", region.link, region.name))
		row
			:tag("td")
			:wikitext(lang:formatNum(region.cases))
		row
			:tag("td")
			:wikitext(lang:formatNum(region.deaths))
		if region.recoveries then
			row
				:tag("td")
				:wikitext(lang:formatNum(region.recoveries))
		else
			row
				:tag("td")
				:addClass("unknown")
				:addClass("table-unknown")
				:wikitext("?")
		end
		row
			:tag("td")
			:wikitext(lang:formatNum(region.population))
		row
			:tag("td")
			:wikitext(lang:formatNum(round(region.cases / region.population * 10000 * 100) / 100))
		row
			:tag("td")
			:wikitext(mw.ustring.format("[[c:%s|view]]", region.dataTableName))
	end
	
	return htmlTable
end

function p.caseTable(frame)
	return p._caseTable(frame.args)
end

return p