Moduuli:Json-stats
Ulkoasu
Tämän moduulin ohjeistuksen voi tehdä sivulle Moduuli:Json-stats/ohje
local p = {}
-- Remove extra quotes from json
function queryjsoncleanup(str)
if string.gmatch(str, ":\"%[") then
str=string.gsub(str, "\"%[", "[")
str=string.gsub(str, "%]\"", "]")
end
return str;
end
function printinfo(frame, jsonstat)
local ret="\n"
local skipnames={'id', 'class', 'value', 'version', 'status', 'size'}
for k,v in pairs( jsonstat) do
local skip=0
for _, s in ipairs(skipnames) do
if k==s then
skip = 1
break
end
end
if skip==0 then
if type(v) == "table" then
content="<pre>" .. mw.text.jsonEncode(v,mw.text.JSON_PRETTY) .."</pre>"
else
content=v
end
ret=ret .."==" .. k .."==\n"
ret=ret .. content .. "\n";
end
end
return ret;
end
function charttemplate(frame, jsonstat, data, axis, templateparams)
local params= {
width=400,
height=100,
type="line",
xAxisTitle="X",
yAxisTitle="Y",
legend="",
-- xAxisFormat="",
-- yAxisFormat="",
-- xAxisMin=0,
-- xAxisMax=100,
-- yAxisMin=0,
-- yAxisMax=100,
-- y1Title="",
-- y2Title="",
-- x="1,2,3,4,5,6,7,8",
-- y="10,12,6,14,2,10,7,9"
}
local x={}
local y={}
local dim = jsonstat.dimension;
local xkeynames={}
local ykeynames={}
local xkeys={}
local ykeys={}
params.xAxisTitle=dim[axis[1]].label
params.yAxisTitle=dim[axis[2]].label
for xk,xv in pairs(dim[axis[1]].category.index) do
xv=xv + 1 -- It is customary in Lua to start arrays with index 1 and not 0 so we fix the offset
xkeynames[xv]=xk;
table.insert(xkeys, xv)
end
for yk,yv in pairs(dim[axis[2]].category.index) do
yv=yv + 1 -- It is customary in Lua to start arrays with index 1 and not 0 so we fix the offset
ykeynames[yv]=yk
table.insert(ykeys, yv)
end
table.sort(xkeys);
table.sort(ykeys);
for _, xk in ipairs(xkeys) do
xkeyname=xkeynames[xk];
if data[xkeyname] then
if not params["x"] then
params["x"]=xkeyname
else
params["x"]=params["x"] .. "," .. xkeyname
end
yindex=0;
for _, yk in ipairs(ykeys) do
ykeyname=ykeynames[yk]
yvalue=data[xkeyname][ykeyname]
if yvalue then
yindex=yindex+1
ykey="y" .. yindex;
ylabelkey="y" .. yindex .. "Title";
params[ylabelkey]=dim[axis[2]].category.label[ykeyname]
if not params[ykey] then
params[ykey]=yvalue
else
params[ykey]=params[ykey] .. "," .. yvalue
end
end
end
end
end
for k,v in pairs( templateparams ) do
params[k]=v
end
-- return params
ret= frame:expandTemplate{ title = 'kaavio', args = params }
ret= ret .. "\n* " .. jsonstat.label .. "\n* " .. jsonstat.source
-- ret = ret .."<pre>" .. mw.text.jsonEncode(ykeys,mw.text.JSON_PRETTY) .. "</pre>"
-- ret = ret .."<pre>" .. mw.text.jsonEncode(params,mw.text.JSON_PRETTY) .. "</pre>"
return ret
end
function getValueIndex(jsonstat, indices)
local size = jsonstat.size or jsonstat.dimension.size
num=1
mult=1
for i=1, #size do
if i == 1 then
mult=1
else
mult=mult*size[#size-i+2]
end
num=num+(mult*indices[#size-i+1])
end
return num
end
local function getAxis(jsonstat)
local ret={};
local ids = jsonstat.id or jsonstat.dimension.id
for i=1, #ids do
ret[i]=ids[i] ;
end
return ret;
end
local function getDimIndices(jsonstat, query)
local ret={}
local dim=jsonstat.dimension;
local ids = jsonstat.id or jsonstat.dimension.id
for i=1, #ids do
ret[i]=getDimIndex(dim , ids[i] , query[ids[i]])
end
return ret
end
function getDimIndex( dim , name , values )
--In single category dimensions, "index" is optional
local ret={};
if not dim[name].category.index then
return 0
end
local ndx=dim[name].category.index
if (type(values) ~= "table") then
values={values}
end
for i = 1, #values do
local value=values[i]
-- Return all items if selector is "*"
if value == "*" then
ret={}
for k,v in pairs(ndx) do
ret[#ret + 1] = v
end
return ret
end
-- return all selected items
if type(ndx) == "table" then
for k,v in pairs(ndx) do
if value == k then
ret[#ret + 1] = v
end
end
-- if only one item then return it
else
ret[#ret + 1] = value
end
end
return ret
end
function getAxisId(axis_ids, axisname)
for k,v in pairs(axis_ids) do
if axisname == v then
return k
end
end
end
function getDimId(dim, name, value)
if dim[name].category.index then
for k,v in pairs( dim[name].category.index) do
if v==value then
return k
end
end
end
return value
end
function getValue(jsonstat,query,axis)
local ret={}
local dim = jsonstat.dimension;
local axis_ids=getAxis(jsonstat);
local indices=getDimIndices( jsonstat , query );
local x_axis_id=getAxisId(axis_ids, axis[1])
local y_axis_id=getAxisId(axis_ids, axis[2])
-- Convert x and y axis to table
for k,v in pairs(indices) do
if k == x_axis_id or k == y_axis_id then
if type(v) ~= "table" then
indices[k]={ v }
end
else
if type(v) == "table" then
indices[k]=indices[k][1]
end
end
end
if 1 then
-- return indices
end
local cursorindice={}
for k,v in pairs(indices) do
cursorindice[k]=v
end
for xk,xv in pairs(indices[x_axis_id]) do
local x_value_id=getDimId(dim, axis[1], xv)
if not ret[ x_value_id] then
ret[ x_value_id]={}
end
for yk,yv in pairs(indices[y_axis_id]) do
-- set cursor
cursorindice[x_axis_id]=xv
cursorindice[y_axis_id]=yv
local y_value_id=getDimId(dim, axis[2], yv)
local index=getValueIndex(jsonstat, cursorindice)
ret[x_value_id][y_value_id]=jsonstat.value[index]
-- ret[x_value_id][y_value_id]=index
-- ret[xv][yv]=jsonstat.value[index]
-- ret[ x_value_id][yv]=jsonstat.value[index]
end
end
return ret;
end
-- Return prettyprinted json
function p.printjson(frame)
local datajson = frame.args[1] or ""
local jsonstat=mw.text.jsonDecode(datajson)
return "<pre>" .. mw.text.jsonEncode(jsonstat,mw.text.JSON_PRETTY) .."</pre>"
end
function p.table(frame)
local datajson = frame.args["url"] or ""
local datajson = frame.args["json-data"] or ""
local xaxis = frame.args["xaxis"] or ""
local yaxis = frame.args["yaxis"] or ""
local queryjson = frame.args["query"] or ""
local helptext = frame.args["helptext"] or ""
local templateparams={}
local charttemplatekeys={
'width',
'height',
'type',
'interpolate',
'colors',
'xAxisTitle',
'yAxisTitle',
'xAxisMin',
'xAxisMax',
'yAxisMin',
'yAxisMax',
'xAxisFormat',
'yAxisFormat',
'xType',
'yType',
'x',
'y1', 'y2', 'y3', 'y4', 'y5', 'y6', 'y7', 'y8', 'y9','y10',
'legend',
'y1title', 'y2title', 'y3title', 'y4title', 'y5title', 'y6title', 'y7title', 'y8title', 'y9title','y10title',
'linewidth',
'showValues',
'format',
'fontcolor',
'fontsize',
'offset',
'angle',
'innerRadius'
}
-- Haetaan kutsuvalla sivulla määritellyt parametrit
local parent=frame:getParent()
if parent then
for _, k in ipairs(charttemplatekeys) do
if parent.args[k] and parent.args[k] ~= "" and parent.args[k] ~= "-" then
templateparams[k]=parent.args[k]
end
end
end
-- Haetaan mallinesivun parametrit
for _, k in ipairs(charttemplatekeys) do
if frame.args[k] and frame.args[k] ~= "" and frame.args[k] ~= "-" then
templateparams[k]=frame.args[k]
end
end
if datajson == "" then
if url~="" then
local uploadurl="https://tools.wmflabs.org/fiwiki-tools/update-stats-json.php?"
local uploadurl=uploadurl .. "page=" .. mw.uri.encode(mw.title.getCurrentTitle) .."&url=" .. mw.uri.encode(uploadurl)
return "[" .. url .. " Linkitettyä tilastoa] ei ole tallennettu wikiin json-stats -muodossa. [" .. uploadurl .." Tallenna tiedot klikkaamalla tätä linkkiä]."
end
return ""
end
local jsonstat=mw.text.jsonDecode(datajson)
queryjson=queryjsoncleanup(queryjson)
local query=mw.text.jsonDecode(queryjson)
if not jsonstat.dimension and jsonstat.dataset then
jsonstat = jsonstat.dataset
end
local axis={xaxis,yaxis}
r=getValue(jsonstat, query, axis)
-- r=query
-- chart=""
local chart= charttemplate(frame, jsonstat, r, axis, templateparams)
if helptext == "true" then
query = "\n==xaxis==\n" .. xaxis .. "\n==yaxis==\n" .. yaxis .. "\n\n==Query==\n" .. "<pre>" .. mw.text.jsonEncode(query,mw.text.JSON_PRETTY) .."</pre>"
meta= printinfo(frame, jsonstat);
return chart .. query .. meta
else
return chart
end
end
return p;