You are browsing a read-only backup copy of Wikitech. The live site can be found at wikitech.wikimedia.org

User:Greg Grossmeier/deployment-module-ugh

From Wikitech-static
Jump to navigation Jump to search

local p = {}

local timezones = { -- Add timezone offsets here! Z = 0, UTC = 0, PST = -8, PDT = -7, SF = 'America/Los_Angeles', DE = 'Europe/Berlin', }

function getSanFranciscoTimezone( time ) -- mw.getContentLanguage():formatDate() will handle input with tzdata -- timezones, outputting the corresponding time in UTC. So we pass it input -- that is manually offset for PST and PDT and see which one matches UTC. local utc = os.date( '!%FT%TZ', time ) local pst = mw.getContentLanguage():formatDate( 'Y-m-d\\TH:i:s\\Z', os.date( '!%FT%T America/Los_Angeles', time + timezones.PST * 3600 ) ) local pdt = mw.getContentLanguage():formatDate( 'Y-m-d\\TH:i:s\\Z', os.date( '!%FT%T America/Los_Angeles', time + timezones.PDT * 3600 ) )

if utc == pst then return 'PST' elseif utc == pdt then return 'PDT' else mw.logObject( { UTC = utc, PST = pst, PDT = pdt }, 'Cannot determine SF time' ) return 'PST' end end

function trim( str ) -- Trims a string because wikitech apparently doesn't have this library... str = string.gsub( str, "%s+$", "" ) str = string.gsub( str, "^%s+", "" ) return str end

function p.row( frame ) -- Produces a raw row to be reconsumed by this script for sorting -- Expects: --- when, string, like YYYY-MM-DD HH:mm XXX where XXX is a valid timezone listed in timezones --- length, number, of hours the deploy will take --- window, string, what deployment window this is (e.g. SWAT, parsoid, MediaWiki) --- who, string, who is performing the deploy --- what, string, what will be deployed -- -- Outputs: UTC Timestamp,Length,Who,What --- commas in strings are replaced with ,

local when = frame.args['when'] local length = ( tonumber( frame.args['length'] ) or 2 ) * 60 * 60 local window = frame.args['window'] or "N/A" local who = frame.args['who'] or "Anonymous Cowards (must provide 'who=')" local what = frame.args['what'] or "Nothing? Then why is this an entry!? (must provide 'what=')"

who = string.gsub( who, ",", "," ) what = string.gsub( what, ",", "," )

if when == nil then utc_time = 0 what = "No time given (must provide 'when' and 'tz', e.g.: 'when=YYYY-MM-DD HH:mm XXX'" else year, month, day, hour, minute, tz = string.match( when, '^(%d+)-(%d+)-(%d+) (%d+):(%d+) ([A-Z]+)$' ) tz = timezones[tz]

if tz == nil then utc_time = 0 what = "Unknown timezone in 'when' string! -- add it to the module :)" elseif type( tz ) == 'string' then local when2 = string.format( '%d-%d-%dT%d:%d:00 %s', year or 1980, month or 1, day or 1, hour or 1, minute or 1, tz ) utc_time = mw.getContentLanguage():formatDate( 'U', when2 ) else ts = os.time({ year = year or 1980, month = month or 1, day = day or 1, hour = hour or 1, min = minute or 1 }) utc_time = ts - ( tz * 60 * 60 ) end end

return string.format( "%s,%s,%s,%s,%s", utc_time, length, window, who, what ) end

function p.formatTable( frame ) -- Formats a bunch of rows from p.row() local rows = {} local row local count = 1 local retval = {} local sflocal local sfdatestr local showdate = not ( frame.args['hidedate'] == 'true' ) local tzoffset = 0 local cutcday local sftz = "No data"

-- Load and sort the arguments row = frame.args[count] while not ( row == nil ) do utc, length, window, who, what = string.match( row, '^(.*),(.*),(.*),(.*),(.*)$' ) utc = tonumber( trim ( utc ) ) length = tonumber( trim( length ) ) who = trim( who ) what = trim( what )

table.insert( rows, { utc=utc, length=length, window=window, who=who, what=what } ) count = count + 1 row = frame.args[count] end table.sort( rows, function( a, b ) return a.utc < b.utc end )

-- For each entry create the final row for count = 1, #rows do local datestr

utc = rows[count].utc length = rows[count].length window = rows[count].window who = rows[count].who what = rows[count].what

if ( cutcday ~= os.date( '%x', utc ) ) then -- Create a new date header in the table table.insert( retval, "|-\n| colspan='4' class='deploycal-dayrow' | \n==== " .. os.date( "!%A, %B %d", utc ) .. " ====" ) cutcday = os.date( '%x', utc ) end

sftz = getSanFranciscoTimezone( utc ) tzoffset = timezones[sftz] sflocal = utc + ( tzoffset * 60 * 60 )

-- Determine if the SF day is different from the UTC day if os.date( "!%A", utc ) ~= os.date( "!%A", sflocal ) then sfdatestr = os.date( "!(%a) ", sflocal ) else sfdatestr = "" end

table.insert( retval, string.format( [[ |- id="deploycal-item-%s" class="deploycal-item" | %s–%s UTC #deploycal-item-%s
%s %s–%s %s | %s | %s | %s ]], os.date( "!%Y%m%dT%H%M", utc ), '', os.date( "!%Y%m%dT%H%M", utc ), '", sfdatestr, '", '", sftz, window, who, what )) end

return string.format( [[

Time Component Deployer Changes

]], sftz ) .. table.concat( retval, "\n" ) .. "\n|}" end

function p.dateOf( frame ) local requestedday = frame.args[1] or 'Sun' local tdelta = 0 local weekstart = 0 local adddays = 0

day, hour, minute, sec = string.match( os.date('!%w %H %m %S'), '^(%d+) (%d+) (%d+) (%d+)$' ) tdelta = ( tonumber( day ) * 24 * 60 * 60 ) + ( tonumber( hour) * 60 * 60 ) + ( tonumber( minute ) * 60 ) + tonumber( sec ) weekstart = os.time() - tdelta

if ( requestedday == 'Mon' ) then adddays = 1 elseif ( requestedday == 'Tue' ) then adddays = 2 elseif ( requestedday == 'Wed' ) then adddays = 3 elseif ( requestedday == 'Thu' ) then adddays = 4 elseif ( requestedday == 'Fri' ) then adddays = 5 end

return os.date( '%Y-%m-%d', weekstart + ( adddays * 24*60*60 ) ) end

function p.header( frame ) -- In my young and dumb days, this created a header for formatTable. -- We still need it because the archive page calls it (though it no longer needs to) end

function p.footer( frame ) -- In my young and dumb days, this created a footer for formatTable. -- We still need it because the archive page calls it (though it no longer needs to) end

return p