text:
/**
**  @file tcGoalTracker.cpp
*/
/*
**  Copyright (c) 2014, GCBLUE PROJECT
**  All rights reserved.
**
**  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
**
**  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
**
**  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the
**     documentation and/or other materials provided with the distribution.
**
**  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from
**     this software without specific prior written permission.
**
**  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
**  NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
**  COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
**  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
**  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
**  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "stdwx.h" // precompiled header file

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

#include <sstream>
#include <fstream>
#include "tcGoalTracker.h"
#include "tcGoal.h"
#include "tcSimState.h"
#include "tcUserInfo.h"
#include "common/tcStream.h"
#include "common/tcObjStream.h"
#include "tcGameStream.h"
#include "tcAllianceInfo.h"
#include "tcGameObject.h"
#include "wx/xml/xml.h"
#include "tcSensorTrackIterator.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

/**
* Singleton accessor
*/
tcGoalTracker* tcGoalTracker::Get()
{
	static tcGoalTracker instance;

	return &instance;
}

/**
* Loads state from update stream
*/
tcUpdateStream& tcGoalTracker::operator<<(tcUpdateStream& stream)
{
	bool allianceFailedPrev = allianceFailed;
	bool allianceSucceededPrev = allianceSucceeded;

	stream >> allianceFailed;
	stream >> allianceSucceeded;
	stream >> goalStatusDescription;

	statusChanged = (allianceFailed != allianceFailedPrev) ||
		            (allianceSucceeded != allianceSucceededPrev);

    return stream;
}

/**
* Saves state to update stream
* alliance value must be stored with stream.SetDetailLevel
*/
tcUpdateStream& tcGoalTracker::operator>>(tcUpdateStream& stream)
{
	int alliance = stream.GetDetailLevel(); // a hack, really should not do this

	allianceFailed = HasAllianceFailed(alliance);
	allianceSucceeded = HasAllianceSucceeded(alliance);
	goalStatusDescription = GetAllianceGoalStatusDescription(alliance);

	stream << allianceFailed;
	stream << allianceSucceeded;
	stream << goalStatusDescription;

    return stream;
}

/**
* Load
*/
tcGameStream& tcGoalTracker::operator<<(tcGameStream& stream)
{
    Clear();

    stream >> lastUpdateTime;
    stream >> perfectScore;

    unsigned int nGoals;
    stream >> nGoals;

    for (unsigned int k=0; k<nGoals; k++)
    {
        bool isValid;
        stream >> isValid;

        if (isValid)
        {
            int alliance;
            stream >> alliance;

            float scoreOffset;
            stream >> scoreOffset;

            ROEStatus allianceROE;
			int val;
			stream >> val;
			allianceROE.airMode = (ROEMode)val;
			stream >> val;
			allianceROE.surfMode = (ROEMode)val;
			stream >> val;
			allianceROE.subMode = (ROEMode)val;
			stream >> val;
			allianceROE.landMode = (ROEMode)val;
			stream >> allianceROE.roeViolations;

            int goalType;
            stream >> goalType;

            if (goalType == 0)
            { // WORKAROUND, should be able to eliminate this eventually
                //stream >> goalState; // int, skip this since goalType read was this value, coincidentally always 0 for tcGoal type
                float tempFloat;
                unsigned long tempULong;
                unsigned int tempUInt;
                stream >> tempFloat; // failScore
                stream >> tempFloat; // passScore
                stream >> tempULong; // id
                stream >> tempUInt; // alliance
            }
            else
            {
                tcGoal* goal = tcGoal::CreateByType(goalType);
                if (goal != 0)
                {
                    *goal << stream;

                    SetAllianceGoal(alliance, goal);
                    AdjustAllianceScore(alliance, scoreOffset);
                    SetAllianceROE(alliance, allianceROE);
                }
                else
                {
                    wxASSERT(false);
                    throw "Invalid goal in scenario file";
                }
            }
        }
    }

    // load damageLog
    unsigned int nLog;
    stream >> nLog;

    for (unsigned int n=0; n<nLog; n++)
    {
        std::string unitName;
        stream >> unitName;

        DamageReport report;
        report.unitName = unitName;
        stream >> report.alliance;
        stream >> report.databaseClass;
        stream >> report.classification;
        stream >> report.time;
        stream >> report.damageFraction;
        stream >> report.landed;

        stream >> report.damagerClass;
        stream >> report.damagerQuantity;

        damageLog[unitName] = report;
    }

    return stream;
}

/**
* Save
*/
tcGameStream& tcGoalTracker::operator>>(tcGameStream& stream)
{
    stream << lastUpdateTime;
    stream << perfectScore;

    unsigned int nGoals = (unsigned int)allianceGoals.size();
    stream << nGoals;

    std::map<int, tcGoal*>::iterator iter = allianceGoals.begin();

    for (; iter != allianceGoals.end(); ++iter)
    {
        int alliance = iter->first;
        tcGoal* allianceGoal = iter->second;
        bool isValid = (allianceGoal != 0) && (allianceGoal->GetType() != 0); // 7 APR 2012, fixed so we don't save default tcGoals

        stream << isValid;

        if (isValid)
        {
            float scoreOffset = GetAllianceOffset(alliance);
            ROEStatus allianceROE = GetAllianceROE(alliance);

            stream << alliance;
            stream << scoreOffset;
			stream << allianceROE.airMode;
			stream << allianceROE.surfMode;
			stream << allianceROE.subMode;
			stream << allianceROE.landMode;
			stream << allianceROE.roeViolations;

            *allianceGoal >> stream; // all goals save their type as first field
        }
    }

    // save damageLog
    unsigned int nLog = damageLog.size();
    stream << nLog;
    std::map<std::string, DamageReport>::const_iterator logIter =
        damageLog.begin();
    for (; logIter != damageLog.end(); ++logIter)
    {
        stream << logIter->first;

        DamageReport report = logIter->second;
        stream << report.alliance;
        stream << report.databaseClass;
        stream << report.classification;
        stream << report.time;
        stream << report.damageFraction;
        stream << report.landed;

        stream << report.damagerClass;
        stream << report.damagerQuantity;
    }

    return stream;
}

/**
* Add or update damage report
*/
void tcGoalTracker::AddDamageReport(const std::string& unitName, const DamageReport& report)
{
    std::map<std::string, DamageReport>::iterator iter =
        damageLog.find(unitName);

    if (iter == damageLog.end())
    {
        damageLog[unitName] = report;
        return;
    }

    DamageReport& logData = iter->second;

    wxASSERT(report.unitName == unitName);
    wxASSERT(report.alliance == logData.alliance);
    wxASSERT(report.databaseClass == logData.databaseClass);
    wxASSERT(report.classification == logData.classification);
    wxASSERT(report.time >= logData.time);
    wxASSERT(logData.damagerClass.size() == logData.damagerQuantity.size());
    wxASSERT(report.damagerClass.size() <= 1);

    logData.time = report.time;
    logData.damageFraction = report.damageFraction;
    logData.landed = report.landed;

    if (report.damagerClass.size() == 0) return; // crash damage, populate with "crash" type?

    // search for matching damagerClass and increment quantity if found
    for (size_t n=0; n<logData.damagerClass.size(); n++)
    {
        if (report.damagerClass[0] == logData.damagerClass[n])
        {
            logData.damagerQuantity[n]++;
            return;
        }
    }

    const size_t MAX_DAMAGERS = 4;
    if (logData.damagerClass.size() >= MAX_DAMAGERS)
    {
        for (size_t k=1; k<logData.damagerClass.size(); k++)
        {
            logData.damagerClass[k-1] = logData.damagerClass[k];
            logData.damagerQuantity[k-1] = logData.damagerQuantity[k];
        }
        logData.damagerClass.pop_back();
        logData.damagerQuantity.pop_back();
    }

    logData.damagerClass.push_back(report.damagerClass[0]);
    logData.damagerQuantity.push_back(1);
    wxASSERT(report.damagerQuantity[0] == 1);
}

/**
* Clears all alliance goals.
*/
void tcGoalTracker::Clear()
{
    for(int i=0;i<MAX_ALLIANCES;i++)
    {
        if (allianceGoals[i])
        {
            delete allianceGoals[i];
            allianceGoals[i] = new tcGoal(); // set default goal (always unresolved) (leaks memory)
			previousGoalStatus[i] = tcGoal::UNRESOLVED;
        }
    }

	allianceFailed = false;
	allianceSucceeded = false;
	statusChanged = false;
	goalStatusDescription.clear();
    allianceScoreOffset.clear();
    reportedNoGoals = false;
    lastUpdateTime = -updateInterval; // so first update happens immediately after game start

    damageLog.clear();
}

const std::string& tcGoalTracker::GetAllianceGoalStatusDescription(int alliance)
{
	static std::string resultString;

	if (tcSimState::Get()->IsMultiplayerClient())
	{
		if (tcUserInfo::Get()->IsOwnAlliance(alliance))
		{
			return goalStatusDescription;
		}
		else
		{
			resultString = "Error";
			return resultString;
		}
	}

	std::stringstream statusStream;

	WriteStatus(statusStream, alliance);

	resultString = statusStream.str();
	return resultString;
}

void tcGoalTracker::GetAllianceGoalStatusDescription(int alliance, wxArrayString& arrayString)
{
    arrayString.clear();

	if (tcSimState::Get()->IsMultiplayerClient())
	{
        wxASSERT(false);
        return;
	}

	WriteStatus(arrayString, alliance);
}

/**
* @return score offset for alliance, normally enemy damages - alliance damages in thousands of dollars
*/
float tcGoalTracker::GetAllianceOffset(int alliance)
{
    std::map<int, float>::const_iterator iter = allianceScoreOffset.find(alliance);

    float scoreOffset = 0;
    if (iter != allianceScoreOffset.end())
    {
        scoreOffset = iter->second;
    }

    return scoreOffset;
}

/**
* @return current ROE info for alliance
*/
tcGoalTracker::ROEStatus tcGoalTracker::GetAllianceROE(int alliance) const
{
    std::map<int, ROEStatus>::const_iterator iter = allianceROE.find(alliance);

	ROEStatus roeStatus;
	roeStatus.airMode = WEAPONS_FREE; // default to weapons free
	roeStatus.surfMode = WEAPONS_FREE; // default to weapons free
	roeStatus.subMode = WEAPONS_FREE; // default to weapons free
	roeStatus.landMode = WEAPONS_FREE; // default to weapons free
	roeStatus.roeViolations = 0;

    if (iter != allianceROE.end())
    {
        roeStatus = iter->second;
    }

    return roeStatus;
}

float tcGoalTracker::GetAllianceScore(int alliance)
{
    const tcGoal* goal = GetAllianceGoal(alliance);

    float scoreOffset = GetAllianceOffset(alliance);

    if (goal != 0)
    {
        return scoreOffset + goal->GetNetScore();
    }
    else
    {
        fprintf(stderr, "tcGoalTracker::GetAllianceScore - alliance not found\n");
        return -999.0f;
    }
}

bool EquipmentSortPredicate(const tcGoalTracker::EquipmentReport& a, const tcGoalTracker::EquipmentReport& b)
{
    return (a.databaseClass.compare(b.databaseClass) > 0);
}

void tcGoalTracker::GetDamageReportSummary(unsigned int alliance, std::vector<DamageReport>& damageSummary) const
{
    damageSummary.clear();

    std::map<std::string, DamageReport>::const_iterator iter =
        damageLog.begin();

    for (; iter!=damageLog.end(); ++iter)
    {
        //const std::string& unitName = iter->first;
        const DamageReport& logData = iter->second;

        bool includeInReport = false;
        switch (logData.classification)
        {
        case PTYPE_SMALLSURFACE:
        case PTYPE_LARGESURFACE:
        case PTYPE_CARRIER:
        case PTYPE_FIXEDWING:
        case PTYPE_HELO:
        case PTYPE_SUBMARINE:
        case PTYPE_GROUND:
        case PTYPE_AIRFIELD:
            includeInReport = true;
            break;
        default:
            includeInReport = false;
        }

        if (includeInReport && (logData.alliance == alliance))
        {
            damageSummary.push_back(logData);
        }
    }

}

void tcGoalTracker::GetExpendedEquipmentSummary(unsigned int alliance, std::vector<EquipmentReport>& equipmentSummary) const
{
    equipmentSummary.clear();

    // first build unsorted equipment report for <alliance>
    std::vector<EquipmentReport> equipment;

    std::map<std::string, DamageReport>::const_iterator iter =
        damageLog.begin();

    for (; iter!=damageLog.end(); ++iter)
    {
        //const std::string& unitName = iter->first;
        const DamageReport& logData = iter->second;

        bool includeInReport = false;
        switch (logData.classification)
        {
        case PTYPE_AIRCM:
        case PTYPE_MISSILE:
        case PTYPE_TORPEDO:
        case PTYPE_SONOBUOY:
        case PTYPE_WATERCM:
        case PTYPE_WATERMINE:
        case PTYPE_BALLISTIC:
            includeInReport = true;
            break;
        default:
            includeInReport = false;
        }

        if (includeInReport && (logData.alliance == alliance))
        {
            EquipmentReport report;
            report.databaseClass = logData.databaseClass;
            report.quantity = 1;

            equipment.push_back(report);
        }
    }

    if (equipment.size() == 0)
    {
        return;
    }

    // sort by class name
    std::sort(equipment.begin(), equipment.end(), EquipmentSortPredicate);

    // combine matching databaseClass into one report per equipment databaseClass
    EquipmentReport mergedReport = equipment[0];
    for (size_t k=1; k<equipment.size(); k++)
    {
        if (equipment[k].databaseClass == mergedReport.databaseClass)
        {
            mergedReport.quantity++;
        }
        else
        {
            equipmentSummary.push_back(mergedReport);
            mergedReport = equipment[k];
        }
    }
    equipmentSummary.push_back(mergedReport);

}

/**
* Adds offset to alliance score for non-goal score adjustments (e.g. loss of equipment or
* destruction of enemy equipment)
*/
void tcGoalTracker::AdjustAllianceScore(int alliance, float offset)
{
    std::map<int, float>::iterator iter = allianceScoreOffset.find(alliance);

    if (iter != allianceScoreOffset.end())
    {
        iter->second += offset;
    }
    else
    {
        allianceScoreOffset[alliance] = offset;
    }
}

void tcGoalTracker::SetPerfectScore(float score)
{
    perfectScore = score;
}

/**
* @return true if alliance has failed.
*/
bool tcGoalTracker::HasAllianceFailed(int alliance)
{
    if ((alliance < 0)||(alliance >= MAX_ALLIANCES))
    {
        wxMessageBox("Alliance out of range in tcGoalTracker::HasAllianceFailed");
        return false;
    }
    tcGoal *goal = allianceGoals[alliance];
    if (goal == NULL)
    {
        ReportNoGoals(alliance);
        return false;
    }
    else
    {
        return goal->IsFailed();
    }
}

/**
* @return true if alliance has succeeded.
*/
bool tcGoalTracker::HasAllianceSucceeded(int alliance)
{
    if ((alliance < 0)||(alliance >= MAX_ALLIANCES))
    {
        wxMessageBox("Alliance out of range in tcGoalTracker::HasAllianceSucceeded");
        return false;
    }
    tcGoal *goal = allianceGoals[alliance];
    if (goal == NULL)
    {
        ReportNoGoals(alliance);
        return false;
    }
    else
    {
        return goal->IsPassed();
    }
}

/**
* @return true if goalState for alliance changed during last update
*/
bool tcGoalTracker::HasStatusChanged(int alliance)
{
	if (tcSimState::Get()->IsMultiplayerClient())
	{
		if (statusChanged)
		{
			// set false so that HasStatusChanged returns true only once per change
			statusChanged = false;
			return true;
		}
		return false;
	}

	tcGoal* allianceGoal = allianceGoals[alliance];
    if (!allianceGoal) return false;

	bool result = (allianceGoal &&
		(allianceGoal->goalState != previousGoalStatus[alliance]));

    previousGoalStatus[alliance] = allianceGoal->goalState;

    return result;
}

bool tcGoalTracker::IsTargetLegal(tcGameObject* attacker, tcSensorMapTrack* targetTrack) const
{
    wxASSERT(attacker != 0);

	if (targetTrack == 0) return false;

    unsigned int attackerAlliance = attacker->GetAlliance();
    ROEStatus roeStatus = GetAllianceROE(int(attackerAlliance));

	bool isWeapon = (targetTrack->IsMissile() || targetTrack->IsTorpedo()) && (targetTrack->mnAffiliation != tcAllianceInfo::FRIENDLY);
	bool isHostile = targetTrack->IsIdentified() && (targetTrack->mnAffiliation == tcAllianceInfo::HOSTILE);
	bool isNeutralOrFriendly = (targetTrack->mnAffiliation == tcAllianceInfo::NEUTRAL) ||
							   (targetTrack->mnAffiliation == tcAllianceInfo::FRIENDLY);

	ROEMode roeMode = WEAPONS_HOLD;
	switch (targetTrack->mnClassification)
	{
	case PTYPE_AIR:
	case PTYPE_FIXEDWING:
	case PTYPE_HELO:
	case PTYPE_AIRCM:
	case PTYPE_MISSILE:
	case PTYPE_BALLISTIC:
		roeMode = roeStatus.airMode;
		break;

	case PTYPE_SURFACE:
	case PTYPE_SMALLSURFACE:
	case PTYPE_LARGESURFACE:
	case PTYPE_CARRIER:
		roeMode = roeStatus.surfMode;
		break;

	case PTYPE_SUBSURFACE:
	case PTYPE_SUBMARINE:
	case PTYPE_TORPEDO:
	case PTYPE_SONOBUOY:
	case PTYPE_WATERCM:
    case PTYPE_WATERMINE:
		roeMode = roeStatus.subMode;
		break;

	case PTYPE_GROUND:
	case PTYPE_AIRFIELD:
	case PTYPE_GROUNDVEHICLE:
		roeMode = roeStatus.landMode;
		break;

	case PTYPE_UNKNOWN:
		return false;
		break;
	}

	switch (roeMode)
	{
	case WEAPONS_FREE:
		return !isNeutralOrFriendly || isWeapon;
	case WEAPONS_TIGHT:
		return isWeapon || isHostile;
	case WEAPONS_HOLD:
		return isWeapon;
	default:
		return false;
	}

}

bool tcGoalTracker::IsTargetLegal(tcGameObject* attacker, long targetTrackId) const
{
    wxASSERT(attacker != 0);

    unsigned int attackerAlliance = attacker->GetAlliance();

    tcSensorMapTrack targetTrack;
    if (tcSimState::Get()->GetTrack(targetTrackId, attackerAlliance, targetTrack))
    {
		return IsTargetLegal(attacker, &targetTrack);
    }
    else
    {
        return false; // target not found in sensor map
    }

}

bool tcGoalTracker::IsTargetLegal(tcGameObject* attacker, float lon_rad, float lat_rad) const
{
    wxASSERT(attacker != 0);

    unsigned int attackerAlliance = attacker->GetAlliance();

    const float searchRange_km = 10.0f;
    GeoPoint p1(lon_rad, lat_rad, 0);
    GeoPoint p2 = p1;

    p1.Offset(searchRange_km, C_PIOVER180 * 225.0f);
    p2.Offset(searchRange_km, C_PIOVER180 * 45.0f);

    tcGeoRect region;
    region.Set(p1.mfLon_rad, p2.mfLon_rad, p1.mfLat_rad, p2.mfLat_rad);

    tcSensorTrackIterator iter(attackerAlliance, 0xFFFF, region);

    for (iter.First();iter.NotDone();iter.Next())
    {
        tcSensorMapTrack* targetTrack = iter.Get();

		bool isLegal = IsTargetLegal(attacker, targetTrack);

        if (isLegal)
        {
            return true; // legal target found
        }
    }

    return false; // no legal targets found
}

/**
* Version that writes damageLog to csv file
*/
void tcGoalTracker::LogAllDamageReports(const std::string& fileName)
{
    tcFile logFile;

    logFile.Open(fileName.c_str(),  tcFile::modeWrite | tcFile::modeText);

    wxString line;
    line.Printf("Unit Name, Unit Class, Alliance, Classification, Time, Damage, WasLanded, DClass1, DQty1, DClass2, DQty2, DClass3, DQty3\n");
    logFile.WriteString(line.c_str());

    std::map<std::string, DamageReport>::iterator iter =
        damageLog.begin();

    for (; iter!=damageLog.end(); ++iter)
    {
        const std::string& unitName = iter->first;
        DamageReport& logData = iter->second;

        bool includeInReport = false;
        switch (logData.classification)
        {
        case PTYPE_SMALLSURFACE:
        case PTYPE_LARGESURFACE:
        case PTYPE_CARRIER:
        case PTYPE_FIXEDWING:
        case PTYPE_HELO:
        case PTYPE_SUBMARINE:
        case PTYPE_GROUND:
        case PTYPE_AIRFIELD:
            includeInReport = true;
            break;
        default:
            includeInReport = false;
        }

        if (includeInReport)
        {
            wxString line;
            line.Printf("%s, %s, %d, %d, %f, %f, %d",
                unitName.c_str(), logData.databaseClass.c_str(), logData.alliance, logData.classification, logData.time, logData.damageFraction, logData.landed);

            for (size_t k=0; k<logData.damagerClass.size(); k++)
            {
                wxString str = wxString::Format(",%s,%d", logData.damagerClass[k].c_str(), logData.damagerQuantity[k]);
                line += str;
            }
            for (size_t k=logData.damagerClass.size(); k<3; k++)
            {
                wxString str = ",,";
                line += str;
            }

            line += "\n";

            logFile.WriteString(line.c_str());
        }
    }

    logFile.Close();
}

/**
* Writes goal status out to file <fileName>. This should
* be called at least once at the end of the game.
*/
void tcGoalTracker::LogAllianceGoalStatus(std::string fileName, int alliance)
{
	std::stringstream statusStream;

	WriteStatus(statusStream, alliance);

	std::ofstream logFile;

	logFile.open(fileName.c_str());

	logFile << statusStream.str();

	logFile.close();
}

tcGoal* tcGoalTracker::GetAllianceGoal(int alliance)
{
    std::map<int, tcGoal*>::const_iterator iter = allianceGoals.find(alliance);

    if (iter != allianceGoals.end())
    {
        return iter->second;
    }
    else
    {
        fprintf(stderr, "tcGoalTracker::GetAllianceGoal - Alliance not found (%d)\n", alliance);
        return 0;
    }
}

/**
* Updates name for destroy and protect targets
*/
void tcGoalTracker::RenameTarget(const std::string& oldName, const std::string& newName)
{
    for(int i=0;i<MAX_ALLIANCES;i++)
    {
        tcGoal* allianceGoal = allianceGoals[i];
        if (allianceGoal)
		{
			allianceGoal->RenameTarget(oldName, newName);
		}
    }
}

void tcGoalTracker::ReportNoGoals(unsigned alliance)
{
    if (reportedNoGoals) return;

    tcAllianceInfo* allianceInfo = tcAllianceInfo::Get();
    if (allianceInfo->IsAllianceNeutral((unsigned char)alliance))
    {
        return; // okay if neutral alliance has no goals
    }

    reportedNoGoals = true;
    wxString s;
    s.Printf("Alliance (%s) has no goals", allianceInfo->GetAllianceDefaultCountry(alliance).c_str());
#ifdef _DEBUG
    wxMessageBox(s);
#else
    fprintf(stderr, "%s\n", s.c_str());
#endif
}

void tcGoalTracker::SetAllianceGoal(int alliance, tcGoal* goal)
{
    if ((alliance < 0)||(alliance >= MAX_ALLIANCES))
    {
        fprintf(stderr, "Alliance out of range in tcGoalTracker::AddGoal (%d)\n", alliance);
        return;
    }
    if (allianceGoals[alliance] != NULL) delete allianceGoals[alliance];

    if (goal != 0)
    {
        allianceGoals[alliance] = goal;
        goal->SetAlliance(unsigned int(alliance));
        SetAllianceROE(alliance, WEAPONS_FREE, WEAPONS_FREE, WEAPONS_FREE, WEAPONS_FREE); // default ROE
    }
}

void tcGoalTracker::AddChildGoalToId(unsigned long id, tcGoal* goal)
{
    tcGoal* parentGoal = LookupGoalById(id);
    tcCompoundGoal* compound = dynamic_cast<tcCompoundGoal*>(parentGoal);
    if (compound != 0)
    {
        compound->AddGoalByPtr(goal);
    }
}

void tcGoalTracker::DeleteGoalById(unsigned long id)
{
    std::map<int, tcGoal*>::iterator iter = allianceGoals.begin();
    for (;iter != allianceGoals.end(); ++iter)
    {
        // first check if top level goal is to be deleted
        if ((iter->second != 0) && (iter->second->GetId() == id))
        {
            delete iter->second;
            allianceGoals.erase(iter);
            return;
        }

        // next search for child of top level to be deleted if it's a compound goal
        if (tcCompoundGoal* compound = dynamic_cast<tcCompoundGoal*>(iter->second))
        {
            if (tcCompoundGoal* parent = FindParentGoalForId(id, compound))
            {
                std::vector<tcGoal*> temp;
                for (size_t n=0; n<parent->children.size(); n++)
                {
                    if (parent->children[n]->GetId() != id)
                    {
                        temp.push_back(parent->children[n]);
                    }
                    else
                    {
                        delete parent->children[n];
                    }
                }
                parent->children = temp;
                return;
            }

        }

    }
}

tcGoal* tcGoalTracker::LookupGoalById(unsigned long id)
{
    std::map<int, tcGoal*>::iterator iter = allianceGoals.begin();
    for (;iter != allianceGoals.end(); ++iter)
    {
        tcGoal* result = SearchGoalForId(id, iter->second);
        if (result != 0) return result;
    }

    return 0;
}

tcGoal* tcGoalTracker::SearchGoalForId(unsigned long id, tcGoal* goal)
{
    if (goal == 0) return 0;

    if (goal->GetId() == id) return goal;

    if (tcCompoundGoal* compound = dynamic_cast<tcCompoundGoal*>(goal))
    {
        for (size_t n=0; n<compound->children.size(); n++)
        {
            tcGoal* result = SearchGoalForId(id, compound->children[n]);
            if (result != 0) return result;
        }
    }

    return 0;
}

tcCompoundGoal* tcGoalTracker::FindParentGoalForId(unsigned long id, tcCompoundGoal* compound)
{
    if (compound == 0)
    {
        wxASSERT(false);
        return 0;
    }

    if (compound->GetId() == id)
    {
        wxASSERT(false); // this shouldn't happen
        return 0;
    }

    for (size_t n=0; n<compound->children.size(); n++)
    {
        tcGoal* child = compound->children[n];

        if (child->GetId() == id)
        {
            return compound;
        }
        else if (tcCompoundGoal* compoundChild = dynamic_cast<tcCompoundGoal*>(child))
        {
            return FindParentGoalForId(id, compoundChild);
        }
    }

    return 0;
}

void tcGoalTracker::Update(double currentTime)
{
    bool isEditMode = tcGameObject::IsEditMode();
    if ((currentTime - lastUpdateTime < updateInterval) && (!isEditMode)) return;

    if (!isEditMode) lastUpdateTime = currentTime;

	if (tcSimState::Get()->IsMultiplayerClient()) return;

    for(int i=0;i<MAX_ALLIANCES;i++)
    {
        tcGoal* allianceGoal = allianceGoals[i];
        if (allianceGoal)
		{
			//previousGoalStatus[i] = allianceGoal->goalState;
			allianceGoal->Update();
		}
    }
}

bool tcGoalTracker::ReadAAR(AARSummary& summary, const std::string& fileName) const
{
    summary.alliances.clear();
    summary.allianceNames.clear();
    summary.damageSummary.clear();
    summary.equipmentSummary.clear();
    summary.playerGoalStatus.clear();

    wxXmlDocument doc(fileName);
    if (!doc.IsOk()) return false;

    wxXmlNode* root = doc.GetRoot();
    if (root == 0) return false;

    wxXmlNode* child = root->GetChildren();

    while (child != 0)
    {
        if (child->GetName() == "scenario")
        {
            summary.scenarioName = child->GetPropVal("name", "");

            unsigned long val = 0;
            child->GetPropVal("player_alliance", "0").ToULong(&val);
            summary.playerAlliance = (unsigned int)val;

            summary.completedTime = child->GetPropVal("stopped_on", "UNK");
        }
        else if (child->GetName() == "alliance")
        {
            unsigned long allianceId = 0;
            child->GetPropVal("id", "999").ToULong(&allianceId);
            summary.alliances.push_back(unsigned int(allianceId));

            summary.allianceNames.push_back(child->GetPropVal("name", "UNK"));

            std::vector<DamageReport2> damage;
            std::vector<EquipmentReport2> equipment;
            wxArrayString goalStatus;
            ReadAARAllianceNode(unsigned int(allianceId), child, damage, equipment, goalStatus);

            summary.damageSummary.push_back(damage);
            summary.equipmentSummary.push_back(equipment);

            if (summary.playerAlliance == unsigned int(allianceId))
            {
                summary.playerGoalStatus = goalStatus;
            }
        }

        child = child->GetNext();
    }

    return true;
}

bool tcGoalTracker::ReadAARAllianceNode(unsigned int alliance, wxXmlNode* node, std::vector<DamageReport2>& damage, std::vector<EquipmentReport2>& equipment, wxArrayString& goalStatus) const
{
    wxXmlNode* child = node->GetChildren();

    while (child != 0)
    {
        if (child->GetName() == "damage_report")
        {
            wxXmlNode* child2 = child->GetChildren();
            while (child2 != 0)
            {
                if (child2->GetName() == "damage")
                {
                    DamageReport2 dr;

                    dr.unitName = child2->GetPropVal("unit", "");
                    dr.databaseClass = child2->GetPropVal("db", "");
                    dr.damagerString = child2->GetPropVal("damager", "");

                    unsigned long classId = 0;
                    child2->GetPropVal("class", "0").ToULong(&classId);
                    dr.classification = unsigned int(classId);

                    double damageFraction = 0;
                    child2->GetPropVal("damage", "0").ToDouble(&damageFraction);
                    dr.damageFraction = float(damageFraction);

                    child2->GetPropVal("time", "0").ToDouble(&dr.time);

                    unsigned long landed = 0;
                    child2->GetPropVal("landed", "0").ToULong(&landed);
                    dr.landed = (landed != 0);

                    damage.push_back(dr);
                }

                child2 = child2->GetNext();
            }
        }
        else if (child->GetName() == "equipment_report")
        {
            wxXmlNode* child2 = child->GetChildren();
            while (child2 != 0)
            {
                if (child2->GetName() == "equipment")
                {
                    EquipmentReport2 er;

                    er.databaseClass = child2->GetPropVal("db", "");

                    unsigned long qty = 0;
                    child2->GetPropVal("quantity", "0").ToULong(&qty);
                    er.quantity = unsigned int(qty);

                    equipment.push_back(er);
                }

                child2 = child2->GetNext();
            }
        }
        else if (child->GetName() == "goal_summary")
        {
            wxXmlNode* child2 = child->GetChildren();
            while (child2 != 0)
            {
                if (child2->GetName() == "line")
                {
                    wxXmlNode* child3 = child2->GetChildren();
                    wxString lineText = (child3 != 0) ? child3->GetContent() : "";
                    goalStatus.push_back(lineText);
                }

                child2 = child2->GetNext();
            }
        }

        child = child->GetNext();
    }

    return true;
}

bool tcGoalTracker::ReadAARInfo(AARInfo& info, const std::string& fileName) const
{
    wxXmlDocument doc(fileName);
    if (!doc.IsOk()) return false;

    wxXmlNode* root = doc.GetRoot();
    if (root == 0) return false;

    wxXmlNode* child = root->GetChildren();

    while (child != 0)
    {
        if (child->GetName() == "scenario")
        {
            info.scenarioName = child->GetPropVal("name", "");

            unsigned long val = 0;
            child->GetPropVal("player_alliance", "0").ToULong(&val);
            info.playerAlliance = (unsigned int)val;

            info.completedTime = child->GetPropVal("stopped_on", "UNK");
            return true;
        }

        child = child->GetNext();
    }

    return false;
}

void tcGoalTracker::SetAllianceROE(int alliance, ROEMode air, ROEMode surf, ROEMode sub, ROEMode land)
{
    if ((alliance < 0)||(alliance >= MAX_ALLIANCES))
    {
        fprintf(stderr, "Alliance out of range in tcGoalTracker::SetAllianceROE (%d)\n", alliance);
        return;
    }

    wxASSERT((air != ROE_ERROR) && (surf != ROE_ERROR) && (sub != ROE_ERROR) && (land != ROE_ERROR));

    ROEStatus newStatus;
	newStatus.airMode = air;
	newStatus.surfMode = surf;
	newStatus.subMode = sub;
	newStatus.landMode = land;

    newStatus.roeViolations = 0;

    allianceROE[alliance] = newStatus;
}

void tcGoalTracker::SetAllianceROE(int alliance, ROEStatus roeStatus)
{
    if ((alliance < 0)||(alliance >= MAX_ALLIANCES))
    {
        fprintf(stderr, "Alliance out of range in tcGoalTracker::SetAllianceROE (%d)\n", alliance);
        return;
    }

    allianceROE[alliance] = roeStatus;
}

/**
* Writes damageLog and other AAR info to xml file
*/
void tcGoalTracker::WriteAAR(const std::string& fileName)
{
    wxXmlDocument doc;

    wxXmlNode* root = new wxXmlNode(wxXML_ELEMENT_NODE, "AAR");

    doc.SetRoot(root);

    std::vector<unsigned char> allianceList = tcAllianceInfo::Get()->GetAllianceList();

    for (unsigned char n=0; n<allianceList.size(); n++)
    {
        wxXmlNode* alliance_n = new wxXmlNode(root, wxXML_ELEMENT_NODE, "alliance");
        alliance_n->AddProperty("id", wxString::Format("%d", allianceList[n]));
        alliance_n->AddProperty("name", wxString(tcAllianceInfo::Get()->GetAllianceName(allianceList[n]).c_str()));

        // write goal summary
        wxXmlNode* goalSummaryNode = new wxXmlNode(alliance_n, wxXML_ELEMENT_NODE, "goal_summary");
        wxArrayString goalSummary;
        GetAllianceGoalStatusDescription(allianceList[n], goalSummary);
        for (size_t line_idx=0; line_idx<goalSummary.size(); line_idx++)
        {
            wxXmlNode* lineNode = new wxXmlNode(goalSummaryNode, wxXML_ELEMENT_NODE, "line");
            wxXmlNode* textNode = new wxXmlNode(lineNode, wxXML_TEXT_NODE, "");
            textNode->SetContent(goalSummary[goalSummary.size()-line_idx-1]); // to get order correct
        }

        // write damage report
        wxXmlNode* damage_report = new wxXmlNode(alliance_n, wxXML_ELEMENT_NODE, "damage_report");

        std::vector<DamageReport> damageSummary;
        GetDamageReportSummary(allianceList[n], damageSummary);

        for (size_t k=0; k<damageSummary.size(); k++)
        {
            wxXmlNode* nodek = new wxXmlNode(damage_report, wxXML_ELEMENT_NODE, "damage");

            nodek->AddProperty("unit", wxString(damageSummary[k].unitName.c_str()));
            nodek->AddProperty("db", wxString(damageSummary[k].databaseClass.c_str()));
            nodek->AddProperty("class", wxString::Format("%d", damageSummary[k].classification));
            nodek->AddProperty("damage", wxString::Format("%f", damageSummary[k].damageFraction));
            nodek->AddProperty("time", wxString::Format("%f", damageSummary[k].time));
            nodek->AddProperty("landed", wxString::Format("%d", damageSummary[k].landed));
            wxString damagerInfo;
            for (size_t idx=0; idx<damageSummary[k].damagerClass.size(); idx++)
            {
                damagerInfo += wxString::Format("%s (%d) ",
                    damageSummary[k].damagerClass[idx].c_str(), damageSummary[k].damagerQuantity[idx]);
            }
            nodek->AddProperty("damager", damagerInfo);
        }

        // write expended equipment report
        wxXmlNode* equipment_report = new wxXmlNode(alliance_n, wxXML_ELEMENT_NODE, "equipment_report");
        std::vector<EquipmentReport> equipmentSummary;
        GetExpendedEquipmentSummary(allianceList[n], equipmentSummary);

        for (size_t k=0; k<equipmentSummary.size(); k++)
        {
            wxXmlNode* nodek = new wxXmlNode(equipment_report, wxXML_ELEMENT_NODE, "equipment");

            nodek->AddProperty("db", equipmentSummary[k].databaseClass);
            nodek->AddProperty("quantity", wxString::Format("%d", equipmentSummary[k].quantity));
        }

    }

    // add general scenario information
    tcSimState* simState = tcSimState::Get();
    tcUserInfo* userInfo = tcUserInfo::Get();

    wxXmlNode* scenario = new wxXmlNode(root, wxXML_ELEMENT_NODE, "scenario");
    scenario->AddProperty("name", wxString(simState->GetScenarioName()));
    scenario->AddProperty("player_alliance", wxString::Format("%d", userInfo->GetOwnAlliance()));

    wxDateTime now = wxDateTime::Now();
    wxString nowString = now.Format("%Y-%m-%d %H:%M");
    scenario->AddProperty("stopped_on", nowString);

    doc.Save(fileName, 1);
}

void tcGoalTracker::WriteStatus(std::stringstream& stream, int alliance)
{
    tcAllianceInfo* allianceInfo = tcAllianceInfo::Get();
    bool allianceNeutral = allianceInfo->IsAllianceNeutral(alliance);

	if (tcGoal* allianceGoal = allianceGoals[alliance])
	{
        const char* offsetString = tcDatabaseObject::CostToString(GetAllianceOffset(alliance));

        float score = GetAllianceScore(alliance) * (100.0 / perfectScore);
        if (score > 100.0f) score = 100.0f;
        else if (score < -100.0f) score = -100.0f;
        wxString scoreString;
        scoreString.Printf("SCORE: %.0f\nRELATIVE DAMAGE: %s\n\n", score, offsetString);
        stream << scoreString.c_str();

        stream << "GOAL SUMMARY:\n\n";
        allianceGoal->WriteStatusB(stream);

        stream << "\n\n\nGOAL SUMMARY DETAILS:\n";
		allianceGoal->WriteStatus(stream);
        stream << "\n\n";
	}
	else if (!allianceNeutral)
	{
		fprintf(stderr, "tcGoalTracker::WriteStatus - null alliance goal\n");
	}
}

void tcGoalTracker::WriteStatus(wxArrayString& arrayString, int alliance)
{
    tcAllianceInfo* allianceInfo = tcAllianceInfo::Get();
    bool allianceNeutral = allianceInfo->IsAllianceNeutral(alliance);

	if (tcGoal* allianceGoal = allianceGoals[alliance])
	{
        if (HasAllianceSucceeded(alliance))
        {
            arrayString.push_back("VICTORY");
        }
        else if (HasAllianceFailed(alliance))
        {
            arrayString.push_back("DEFEAT");
        }
        else
        {
            arrayString.push_back("SCENARIO UNRESOLVED");
        }
        arrayString.push_back("");

        float score = GetAllianceScore(alliance) * (100.0 / perfectScore);
        if (score > 100.0f) score = 100.0f;
        else if (score < -100.0f) score = -100.0f;
        wxString scoreString;

        scoreString.Printf("SCORE: %.0f", score);

        arrayString.push_back(scoreString);
        arrayString.push_back("");

        arrayString.push_back("GOAL STATUS:");

        allianceGoal->WriteStatusB(arrayString);

        arrayString.push_back("");

        arrayString.push_back("GOAL DETAILS:");

		allianceGoal->WriteStatus(arrayString);
	}
	else if (!allianceNeutral)
	{
		fprintf(stderr, "tcGoalTracker::WriteStatus - null alliance goal\n");
	}
}

tcGoalTracker::tcGoalTracker()
: updateInterval(9.01),
   allianceFailed(false),
   allianceSucceeded(false),
   statusChanged(false),
   perfectScore(1e9),
   reportedNoGoals(false),
   lastUpdateTime(-10.0f)
{

    for(int i=0;i<MAX_ALLIANCES;i++)
    {
        allianceGoals[i] = NULL;
    }
    Clear();

    wxASSERT(MAX_ALLIANCES == tcAllianceInfo::MAX_ALLIANCES); // these should match
}

tcGoalTracker::~tcGoalTracker()
{
	// should use map iterator here
	for(int i=0;i<MAX_ALLIANCES;i++)
	{
		if (allianceGoals[i])
		{
			delete allianceGoals[i];
		}
	}

}


text:
package org.drools.core.phreak;

import org.drools.core.common.Memory;
import org.drools.core.common.PhreakPropagationContext;
import org.drools.core.reteoo.LeftTuple;
import org.drools.core.reteoo.RightTuple;
import org.drools.core.spi.PropagationContext;

public class RightTupleEntry implements TupleEntry {

    private final RightTuple         rt;
    private final PropagationContext pctx;
    private final Memory             nodeMemory;
    private final int                propagationType;

    public RightTupleEntry(RightTuple rt, PropagationContext pctx, Memory nodeMemory, int propagationType) {
        this.rt = rt;
        this.pctx = pctx;
        this.nodeMemory = nodeMemory;
        this.propagationType = propagationType;
    }

    public LeftTuple getLeftTuple() {
        return null;
    }

    public RightTuple getRightTuple() {
        return rt;
    }

    public PropagationContext getPropagationContext() {
        return pctx;
    }

    public Memory getNodeMemory() {
        return nodeMemory;
    }

    public int getPropagationType() {
        return propagationType;
    }

    @Override
    public String toString() {

        return "RightTupleEntry{" +
               "rt=" + rt +
               ", pctx=" + PhreakPropagationContext.intEnumToString(pctx) +
               ", nodeMemory=" + nodeMemory +
               '}';
    }
}

redpajama_set_name: RedPajamaGithub


text:
<?php declare(strict_types=1);
namespace Imbo\Behat;

use Imbo\EventListener\ExifMetadata;

return [
    'eventListeners' => [
        'exifMetadataListener' => [
            'listener' => ExifMetadata::class,
            'params' => [
                'allowedTags' => ['exif:*', 'png:*'],
            ],
        ],
    ],
];


text:
import { HandyGridPaginator } from '../paginator/handy-grid-paginator.class';

export interface HandyGridControlParamsInterface {
  current?: number;
  itemCountPerPage?: number;
  sortType?: number;
  sortField?: string;
}

/**
 * Klasa pozwala na zmianę parametrów odpowiadających za kontrolę listingu
 * (aktualana strona, ilość rekordów na stronie itp.)
 */
export class HandyGridControlParams {
  protected paginator: HandyGridPaginator;
  protected params: HandyGridControlParamsInterface = {
    current: 1,
    itemCountPerPage: 10,

    sortType: 0, // enum -> 0: ASC, 1: DESC
    sortField: ''
  };
  // filter: oddzielny interfejs

  setPaginatorService(paginatorService: HandyGridPaginator): void {
    this.paginator = paginatorService;
  }

  getParams(): HandyGridControlParamsInterface {
    if (this.paginator) {
      this.params.current = this.paginator.getCurrent();
      this.params.itemCountPerPage = this.paginator.getItemCountPerPage();
    }
    return this.params;
  }

  getParamsAsQuery(params?: HandyGridControlParamsInterface): string {
    if (params) {
      this.addParams(params);
    }
    let controlParams: HandyGridControlParamsInterface = this.getParams();

    let paramsUrl: string = '';
    let keys = Object.keys(controlParams);
    for (let key of keys) {
      if (controlParams[key] !== '') {
        paramsUrl += key + '=' + controlParams[key] + '&';
      }
    }

    return '?' + paramsUrl.substring(0, paramsUrl.length - 1);
  }

  addParams(params: HandyGridControlParamsInterface): void {
    let keys = Object.keys(params);

    for (let key of keys) {
      if (this.params.hasOwnProperty(key)) {
        this.params[key] = params[key];
      }
    }
  }
}


text: Email


text:
<?php

namespace DashboardBundle\Form\Type;

use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;

class UserType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('username')
            ->add('plainPassword', RepeatedType::class, array(
                'type' => PasswordType::class
            ))
            ->add('Enregistrer', SubmitType::class)
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'DashboardBundle\Entity\User'
        ));
    }
}


text:
yarn-logs-helpers
=================
Scripts for parsing / making sense of yarn logs.

# Contents
#### `yarn-container-logs`
The main script of note here is [`yarn-container-logs`](https://github.com/hammerlab/yarn-logs-helpers/blob/master/yarn-container-logs):

```
$ yarn-container-logs 0018
```

*  It can take a full application ID (e.g. `application_1416279928169_0018`) or **just the last 4 digits of one** (`0018`).
*  It downloads the YARN logs for that application into a local directory (defaulting to the application ID, but can be overriden with an optional second argument, after the app ID) and splits them into per-container files:

    ```
    # Directory created by yarn-container-logs
    $ cd application_1416279928169_0018

    # Directory with per-container logs
    $ cd containers

    # Per-container log files have prefix /container_/
    $ ls container_*
    container_1416279928169_0018_01_000015
    container_1416279928169_0018_01_000016
    container_1416279928169_0018_01_000017
    ...

    # The files contain exactly what was pulled down from YARN.
    $ head container_1416279928169_0018_01_000015
    Container: container_1416279928169_0018_01_000015 on my-node-11-10.rest.of.domain.name_port
    ===================================================================================================
    LogType: stderr
    LogLength: 700
    Log Contents:
    ...
    ```

* It also creates a directory per node (a.k.a. "host") containing symlinks to the log-files of all containers that ran on that node:

    ```
    $ cd hosts
    $ ls -l my-node-*
    my-node-08-1:
    lrwxrwxrwx 1 <user> <group> 41 Nov 20 04:42 container_1416279928169_0018_01_000065 -> ../container_1416279928169_0018_01_000065
    lrwxrwxrwx 1 <user> <group> 41 Nov 20 04:42 container_1416279928169_0018_01_000094 -> ../container_1416279928169_0018_01_000094
    lrwxrwxrwx 1 <user> <group> 41 Nov 20 04:42 container_1416279928169_0018_01_000123 -> ../container_1416279928169_0018_01_000123
    lrwxrwxrwx 1 <user> <group> 41 Nov 20 04:42 container_1416279928169_0018_01_000258 -> ../container_1416279928169_0018_01_000258
    lrwxrwxrwx 1 <user> <group> 41 Nov 20 04:42 container_1416279928169_0018_01_000338 -> ../container_1416279928169_0018_01_000338

    my-node-08-10:
    lrwxrwxrwx 1 <user> <group> 41 Nov 20 04:42 container_1416279928169_0018_01_000041 -> ../container_1416279928169_0018_01_000041
    lrwxrwxrwx 1 <user> <group> 41 Nov 20 04:42 container_1416279928169_0018_01_000158 -> ../container_1416279928169_0018_01_000158
    lrwxrwxrwx 1 <user> <group> 41 Nov 20 04:42 container_1416279928169_0018_01_000275 -> ../container_1416279928169_0018_01_000275
    lrwxrwxrwx 1 <user> <group> 41 Nov 20 04:42 container_1416279928169_0018_01_000354 -> ../container_1416279928169_0018_01_000354
    lrwxrwxrwx 1 <user> <group> 41 Nov 20 04:42 container_1416279928169_0018_01_000424 -> ../container_1416279928169_0018_01_000424
    ...
    ```

    * This functionality lives in [`rename-and-link-hosts`](https://github.com/hammerlab/yarn-logs-helpers/blob/master/rename-and-link-hosts).
    * In this example, the per-node directories have had a shared suffix of the form `.rest.of.domain.name_<port>` removed for brevity; this is enabled by setting the `$YARN_HELPERS_DROP_HOST_SUFFIX_FROM` environment variable; see the [Installing](#installing) section for more details on setting `$YARN_HELPERS_DROP_HOST_SUFFIX_FROM`.

##### Spark-specific parsing
A common use case is parsing logs from Spark apps running on YARN, for which `yarn-container-logs` has some specific functionality:
* It can identify the logs corresponding to Spark driver containers. It `grep`s all container logs for `spark.SparkContext` to identify drivers (you can override this by setting the `$YARN_HELPERS_DRIVER_GREP_NEEDLE` environment variable), and creates symlinks to them in the `drivers` directory:

        $ ls -l drivers
        lrwxrwxrwx 1 <user> <group> 41 Nov 20 04:42 0 -> ../container_1416279928169_0018_01_000015
        lrwxrwxrwx 1 <user> <group> 41 Nov 20 04:42 container_1416279928169_0018_01_000015 -> ../container_1416279928169_0018_01_000015

    If exactly one was found, an additional top-level `driver` symlink will point to it:

        $ ls -l driver
        lrwxrwxrwx 1 <user> <group> 9 Nov 20 04:42 driver -> drivers/0

    This functionality lives in [`link-driver-logs`](https://github.com/hammerlab/yarn-logs-helpers/blob/master/link-driver-logs).

* It will create a `tids` directory and populate it with symlinks for each Spark task ID that it finds evidence of in the logs to the container-log-file where that TID seemingly ran.
    * This functionality lives in [`link-tids`](https://github.com/hammerlab/yarn-logs-helpers/blob/master/link-tids).
    * Note that if a Spark job had multiple Application Masters ("drivers"), it will likely have had multiple tasks with some task IDs, which will cause errors to be emitted by this stage. See discussion at [#2](https://github.com/hammerlab/yarn-logs-helpers/issues/2#issuecomment-63861447).

#### Stack Trace Parsing / Histogram
`yarn-logs-stack-traces` uses a [stack-trace-parsing library](https://github.com/ryan-williams/stack-traces) on the output of `yarn-logs`. Example usage:
```
$ yls 0018 -d  # -d means "show a histogram in descending order"
635 stacks in total

71 occurrences:
org.apache.spark.shuffle.MetadataFetchFailedException: Missing an output location for shuffle 4
        at org.apache.spark.MapOutputTracker$$anonfun$org$apache$spark$MapOutputTracker$$convertMapStatuses$1.apply(MapOutputTracker.scala:386)
        at org.apache.spark.MapOutputTracker$$anonfun$org$apache$spark$MapOutputTracker$$convertMapStatuses$1.apply(MapOutputTracker.scala:383)
        at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
        ...
        at java.lang.Thread.run(Thread.java:744)

60 occurrences:
java.io.IOException: Failed to connect to demeter-csmaz11-16.demeter.hpc.mssm.edu/172.29.46.86:33263
        at org.apache.spark.network.client.TransportClientFactory.createClient(TransportClientFactory.java:141)
        at org.apache.spark.network.netty.NettyBlockTransferService$$anon$1.createAndStart(NettyBlockTransferService.scala:78)
        at org.apache.spark.network.shuffle.RetryingBlockFetcher.fetchAllOutstanding(RetryingBlockFetcher.java:140)
        ...
        at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116)

...
```

#### Other Miscellaneous Scripts
This repo contains several other scripts that basically wrap YARN commands in calls to [`yarn-appid`](https://github.com/hammerlab/yarn-logs-helpers/blob/master/yarn-appid), allowing last-4-lookup of application IDs:
* [`yarn-kill`](https://github.com/hammerlab/yarn-logs-helpers/blob/master/yarn-kill): wrapper for `yarn application -kill <appid>`.
* [`yarn-logs`](https://github.com/hammerlab/yarn-logs-helpers/blob/master/yarn-logs): wrapper for `yarn logs -applicationId <appid>`.
    * [`yarn-logs-less`](https://github.com/hammerlab/yarn-logs-helpers/blob/master/yarn-logs-less): pipes `yarn-logs` to `less`.

# Installing
Download this repository with:

        git clone --recursive https://github.com/hammerlab/yarn-logs-helpers.git

In your `.bashrc` (or equivalent), source [`.yarn-logs-helpers.sourceme`](https://github.com/hammerlab/yarn-logs-helpers/blob/master/.yarn-logs-helpers.sourceme):

        $ source /path/to/repo/.yarn-logs-helpers.sourceme

This will:
* try to fetch your cluster's ID using the [`yarn-refresh-cluster-id`](https://github.com/hammerlab/yarn-logs-helpers/blob/master/yarn-refresh-cluster-id) script.
    * If found, the result will be cached in `$yarn_cluster_id_file` ([default: `$HOME/.yarn-cluster-id`](https://github.com/hammerlab/yarn-logs-helpers/blob/master/.yarn-logs-helpers.sourceme#L15)).
    * This will allow all scripts in this repo to look up YARN application IDs by their last 4 digits (using [`yarn-appid`](https://github.com/hammerlab/yarn-logs-helpers/blob/master/yarn-appid)).
* [set aliases](https://github.com/hammerlab/yarn-logs-helpers/blob/master/.yarn-logs-helpers.sourceme#L22-L31) for most functionality in this repo.
* add the root directory of this repo to your `$PATH`.

## Env vars
Setting `$YARN_LOGS_USER` may allow `yarn-container-logs` to fetch logs from apps run by users other than you.

You can set it permanently in your `.bashrc` to a user that has permissions to read all YARN users' logs, or just on the cmdline for one call:

```bash
YARN_LOGS_USER=someone yarn-logs 1234
```

You may also want to export `YARN_HELPERS_DROP_HOST_SUFFIX_FROM` (discussed above):

        # Pattern for abbreviating host names when creating per-host log directories.
        export YARN_HELPERS_DROP_HOST_SUFFIX_FROM=".rest.of.domain.name_"

##### `stack-traces` submodule
Finally, [ryan-williams/stack-traces](https://github.com/ryan-williams/stack-traces) is included in this repository as a git submodule, and used by [`yarn-log-stack-traces`](https://github.com/hammerlab/yarn-logs-helpers/blob/master/yarn-log-stack-traces).

You'll need to `git clone --recursive` when you check out the project, or run `git submodule init && git submodule update` from within the `stack-traces` subdirectory, for it to work. [git-scm.com](http://git-scm.com/book/en/v2/Git-Tools-Submodules#Cloning-a-Project-with-Submodules) has a good intro to using git submodules if you are not familiar.

With those done you should be all set!


text:
var cmiUtil = require('../../cmiUtil');
var assert = require('assert');

function CheckBundle (data){
    data = data || {};
    this._checks = data._checks;
    this._cid = data._cid;
    this._created = data._created;
    this._last_modified = data._last_modified;
    this._last_modified_by = data._last_modified_by;
    this.brokers = data.brokers || [];
    this.config = data.config || {};
    this.display_name = data.display_name;
    this.metrics = data.metrics || [];
    this.notes = data.notes;
    this.period = data.period;
    this.status = data.status;
    this.tags = data.tags || [];
    this.target = data.target;
    this.timeout = data.timeout;
    this.type = data.type;
}

CheckBundle.prototype.updateTags = function(tags){
    var newTags = updateArray('tag',this.tags, tags);
    if(newTags) this.tags = newTags;
    return this.tags;
};

CheckBundle.prototype.updateMetrics = function(metrics){
    var newMetrics = updateArray('metric',this.metrics,metrics);
    var uniqueMetrics = [];
    if(newMetrics){
        var metricNames = {};
        for(var i = newMetrics.length - 1; i >= 0; i--){
            var metric = newMetrics[i];
            if(metricNames[metric.name]) continue;
            metricNames[metric.name] = true;
            uniqueMetrics.push(metric);
        }
    }
    return this.metrics = uniqueMetrics.reverse();
};

CheckBundle.prototype.merge = function(checkBundle){
    var origCheck = JSON.stringify(this);

    // Tags and metrics update separately due to the regex removals
    this.updateTags(checkBundle.tags);
    this.updateMetrics(checkBundle.metrics);
    checkBundle.tags = [];
    checkBundle.metrics = [];

    cmiUtil.mergeDeep(this, checkBundle);

    var newCheck = JSON.stringify(this);

    if(origCheck !== newCheck) return true;

    return false;
};

var removalRegExp = /^!\/.+\/(i)?/;

CheckBundle.prototype.export = function(){
    var data = JSON.parse(JSON.stringify(this));
    data.tags = removeRegExpFromArray(data.tags);
    data.metrics = removeRegExpFromArray(data.metrics);
    return data;
}

function removeRegExpFromArray(arr){
    return arr.filter(function(element){
        return !removalRegExp.test(element);
    });
}

function updateArray(type, orig, updates){
    if(!(orig instanceof Array) || !(updates instanceof Array)) return;

    var add = [];
    // Process removals first, then union in the rest
    for(var i in updates){
        var value = updates[i];
        // Removals are regex prefixed with !
        if(removalRegExp.test(value)){
            var tmp = value.split('/');
            tmp.shift(); // Get rid of !
            var flag = tmp.pop(); // Pull off i flag if present
            var regexp = new RegExp(tmp.join('/'),flag);
            orig = orig.filter(function(element){
                var testElement = (type === 'metric') ? element.name : element;
                return !regexp.test(testElement);
            });
        } else {
            add.push(value);
        }
    }

    return cmiUtil.unionArray(orig, add);
}

module.exports = CheckBundle;


text:
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from "@angular/router";
import { SnackbarService } from "../../../../services/snackbar.service";
import { GroupService } from "../../../../services/group.service";
import { OrganizationService } from "../../../../services/organization.service";

@Component({
  selector: 'group-creation',
  templateUrl: './group-creation.component.html',
  styleUrls: ['./group-creation.component.scss']
})
export class GroupCreationComponent implements OnInit {
  private domainId: string;
  private organizationContext: boolean;
  group: any = {};

  constructor(private groupService: GroupService,
              private organizationService: OrganizationService,
              private router: Router,
              private route: ActivatedRoute,
              private snackbarService: SnackbarService) { }

  ngOnInit() {
    this.domainId = this.route.snapshot.data['domain']?.id;
    if (this.router.routerState.snapshot.url.startsWith('/settings')) {
      this.organizationContext = true;
    }
  }

  create() {
    if (this.organizationContext) {
      this.organizationService.createGroup(this.group).subscribe(data => {
        this.snackbarService.open('Group ' + data.name + ' created');
        this.router.navigate(['/settings', 'groups', data.id]);
      });
    } else {
      this.groupService.create(this.domainId, this.group).subscribe(data => {
        this.snackbarService.open('Group ' + data.name + ' created');
        this.router.navigate(['..', data.id], {relativeTo: this.route} );
      });
    }
  }
}


text: This project is represent to develop websites very easy and generate reports.


text:
using Mono.Cecil;

namespace ExtensibleILRewriter.Processors.Parameters
{
    public struct ParameterValueHandlingCodeProviderArgument
    {
        public ParameterValueHandlingCodeProviderArgument(MethodParameterProcessableComponent parameter, FieldDefinition stateField)
        {
            Parameter = parameter;
            StateField = stateField;
        }

        public MethodParameterProcessableComponent Parameter { get; }

        public FieldDefinition StateField { get; }
    }
}


text:
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace DraftIt.Data
{
    public class DraftUser : IEntity
    {
        [Key, Column(Order = 0), ForeignKey("User")]
        public int UserId { get; set; }

        [Key, Column(Order = 1), ForeignKey("Draft")]
        public int DraftId { get; set; }

        public virtual User User { get; set; }
        public virtual Draft Draft { get; set; }
    }
}


text:
Handlebars.registerHelper('capitalize', function (context, options) {
	if ('string' !== typeof context) return context;
	return context.charAt(0).toUpperCase() + context.slice(1);
});


text:
//---------------------------------------------------------------------------
//		E L E N A   P r o j e c t:  ELENA IDE
//                     IDE Project Model implementation File
//                                             (C)2021-2022, by Aleksey Rakov
//---------------------------------------------------------------------------

#include "ideproject.h"

using namespace elena_lang;

// --- ProjectModel ---

ProjectModel :: ProjectModel(IDEStatus* status)
   : lastOpenFiles(nullptr), sources(nullptr)
{
   this->status = status;

   this->singleSourceProject = true;
   this->autoRecompile = /*true*/false;// !! temporal
   this->projectPath.copy("C:\\Alex\\ELENA\\tests60\\sandbox\\"); // !! temporal
   this->outputPath.copy("C:\\Alex\\ELENA\\tests60\\sandbox\\");  // !! temporal

   this->paths.libraryRoot.copy("C:\\Alex\\ELENA\\lib60\\");      // !! temporal
   this->paths.librarySourceRoot.copy("C:\\Alex\\ELENA\\src60\\");// !! temporal
}

path_t ProjectModel::getOutputPath()
{
   return *outputPath;
}

ustr_t ProjectModel :: getTarget()
{
   return "sandbox.exe"; // !! temporal
}

ustr_t ProjectModel::getPackage()
{
   return "sandbox"; // !! temporal

}

ustr_t ProjectModel::getArguments()
{
   return nullptr; // !! temporal
}

bool ProjectModel :: getDebugMode()
{
   return true; // !! temporal
}


text:
package main

import (
	"encoding/xml"
	"fmt"
	"github.com/wmbest2/rats-server/rats"
	"github.com/wmbest2/rats-server/test"
	"os"
)

func main() {
	rats.PollDevices()

	argCount := len(os.Args)
	if argCount != 2 && argCount != 3 {
		fmt.Println("Usage: cli-client <main apk [optional]> <test apk>")
		fmt.Println("   * main apk not required for library tests")
		return
	}

	devices := <-rats.GetAllDevices()

	for _, arg := range os.Args[1:] {
		rats.Install(arg, devices)
	}

	for _, device := range devices {
		device.SetScreenOn(true)
		device.Unlock()
	}

	testFile := os.Args[len(os.Args)-1]
	manifest := rats.GetManifest(testFile)

	s := test.RunTests(manifest, devices)
	str, err := xml.Marshal(s)
	if err == nil {
		fmt.Println(string(str))
	}

	rats.Uninstall(manifest.Package, devices)
	rats.Uninstall(manifest.Instrument.Target, devices)
}


text:
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <parent>
      <groupId>org.apache.activemq</groupId>
      <artifactId>artemis-pom</artifactId>
      <version>1.1.0-wildfly-2-SNAPSHOT</version>
   </parent>

   <artifactId>artemis-dto</artifactId>
   <packaging>jar</packaging>
   <name>ActiveMQ Artemis DTO</name>

   <properties>
     <activemq.basedir>${project.basedir}/..</activemq.basedir>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.apache.activemq</groupId>
         <artifactId>artemis-commons</artifactId>
         <version>${project.version}</version>
      </dependency>
   </dependencies>

   <build>
     <resources>
       <resource>
         <directory>target/schema</directory>
         <includes>
           <include>**/*</include>
         </includes>
       </resource>
       <resource>
         <directory>src/main/resources</directory>
         <includes>
           <include>**/*</include>
         </includes>
         <filtering>true</filtering>
       </resource>
     </resources>

     <plugins>
       <plugin>
         <artifactId>maven-antrun-plugin</artifactId>
         <version>1.7</version>
         <executions>
           <execution>
             <phase>generate-resources</phase>
             <configuration>
               <tasks>
                 <taskdef name="schemagen" classname="com.sun.tools.jxc.SchemaGenTask" />
                 <mkdir dir="${project.build.directory}/schema/org.apache.activemq/dto" />
                 <echo message="Generating XSD to: ${project.build.directory}/schema/org.apache.activemq/dto" />
                 <schemagen srcdir="${basedir}/.." destdir="${project.build.directory}/schema/org.apache.activemq/dto" includeantruntime="false">
                   <schema namespace="http://activemq.org/schema" file="activemq.xsd" />
                   <classpath refid="maven.compile.classpath" />
                   <include name="**/package-info.java" />
                   <include name="**/*DTO.java" />
                   <exclude name="**/.git/**" />
                   <exclude name="**/.svn/**" />
                 </schemagen>
                 <copy todir="${project.build.directory}/classes">
                   <fileset dir="${project.build.directory}/schema" />
                 </copy>
               </tasks>
             </configuration>
             <goals>
               <goal>run</goal>
             </goals>
           </execution>
         </executions>
         <dependencies>
            <dependency>
               <groupId>javax.xml.bind</groupId>
               <artifactId>jaxb-api</artifactId>
               <version>2.2.7</version>
            </dependency>
            <dependency>
               <groupId>com.sun.xml.bind</groupId>
               <artifactId>jaxb-impl</artifactId>
               <version>2.2.7</version>
            </dependency>
            <dependency>
               <groupId>com.sun.xml.bind</groupId>
               <artifactId>jaxb-jxc</artifactId>
               <version>2.2.7</version>
            </dependency>
         </dependencies>
       </plugin>
     </plugins>
     <pluginManagement>
     	<plugins>
     		<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
     		<plugin>
     			<groupId>org.eclipse.m2e</groupId>
     			<artifactId>lifecycle-mapping</artifactId>
     			<version>1.0.0</version>
     			<configuration>
     				<lifecycleMappingMetadata>
     					<pluginExecutions>
     						<pluginExecution>
     							<pluginExecutionFilter>
     								<groupId>org.apache.maven.plugins</groupId>
     								<artifactId>maven-antrun-plugin</artifactId>
     								<versionRange>[1.7,)</versionRange>
     								<goals>
     									<goal>run</goal>
     								</goals>
     							</pluginExecutionFilter>
     							<action>
     								<ignore />
     							</action>
     						</pluginExecution>
     					</pluginExecutions>
     				</lifecycleMappingMetadata>
     			</configuration>
     		</plugin>
     	</plugins>
     </pluginManagement>
   </build>

   <profiles>
     <profile>
       <id>jdk-1.5</id>
       <activation>
         <jdk>1.5</jdk>
       </activation>
       <dependencies>
         <dependency>
           <groupId>javax.xml.bind</groupId>
           <artifactId>jaxb-api</artifactId>
           <version>${jaxb-api-version}</version>
         </dependency>
         <dependency>
           <groupId>com.sun.xml.bind</groupId>
           <artifactId>jaxb-impl</artifactId>
           <version>${jaxb-version}</version>
         </dependency>
       </dependencies>
     </profile>

    <profile>
       <id>ibmjdk</id>
       <activation>
         <file>
           <exists>${java.home}/../lib/tools.jar</exists>
         </file>
       </activation>
       <build>
         <pluginManagement>
           <plugins>
             <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-antrun-plugin</artifactId>
               <dependencies>
                 <dependency>
                   <groupId>com.sun</groupId>
                   <artifactId>tools</artifactId>
                   <!--the real JDK version could be 1.5 or 1.6-->
                   <version>1.5.0</version>
                   <scope>system</scope>
                   <optional>true</optional>
                   <systemPath>${java.home}/../lib/tools.jar</systemPath>
                 </dependency>
               </dependencies>
             </plugin>
           </plugins>
         </pluginManagement>
       </build>
     </profile>
   </profiles>

</project>


text:
napi_value metallic_fchdir(napi_env env, napi_callback_info args)
{
    const size_t arity = 2;
    size_t argc = arity;
    napi_value argv[arity];

    napi_get_cb_info(env, args, &argc, argv, (void*)0, (void*)0);

    napi_value result = 0;

    napi_create_int32(env, fchdir(ToInt32(env, argv[1])) * errno, &result);

    return result;
}
#endif


text:
package xcordion.impl.command;

import xcordion.api.CommandType;
import xcordion.api.EvaluationContext;
import xcordion.api.TestElement;
import xcordion.api.Xcordion;
import xcordion.api.events.ExceptionThrownEvent;
import xcordion.api.events.SuccessfulExecuteEvent;

public class ExecCommand extends ChildrenInSetupRunVerifyOrderCommand {

	@Override
	public CommandType getCommandType() {
		return CommandType.EXECUTE;
	}

	@Override
	protected <T extends TestElement<T>, C extends EvaluationContext<C>> void run(Xcordion<T> xcordion, T target, C context, String expression) {
        try {
            context.eval(expression, target);
            xcordion.getBroadcaster().handleEvent(new SuccessfulExecuteEvent<T>(target, context.getIgnoreState(), expression));
        } catch (Throwable e) {
            xcordion.getBroadcaster().handleEvent(new ExceptionThrownEvent<T>(target, context.getIgnoreState(), expression, e));
            return;
        }
	}

}


text:
import React from 'react';
import styles from '../../css/skeleton.css';

function P(props) {
  return (

{props.children}

  );
}

P.propTypes = {
  children: React.PropTypes.any.isRequired,
  align: React.PropTypes.string,
};

export default P;


text:
import { Component } from 'angular2/core';
import { PirateService } from './pirate.service';
import { Observable } from 'rxjs/Observable';

@Component({
  selector: 'pirate-term',
  providers: [PirateService],
  template: `

Pirate life is the life for me

        <form>
          <input type="text" #box/>
          <input type="submit" value="Translate" (click)="translate(box.value)" />
        </form>

{{pirate}}

    `,
   styles: [`
    .pirate {
      padding: 10px;
    }
   `]
})
export class PirateTalkComponent {
  pirate: string;
  constructor(private pirateService: PirateService) { }

  translate(term: string) {
    if(term) {
      this.pirateService.getPirateText(term)
        .then(res => this.pirate = res.json().translation.pirate);
    }
  }

}


text:
<?php

final class PhabricatorDirectoryMainController
  extends PhabricatorDirectoryController {

  private $filter;
  private $minipanels = array();

  public function willProcessRequest(array $data) {
    $this->filter = idx($data, 'filter');
  }

  public function processRequest() {
    $user = $this->getRequest()->getUser();

    if ($this->filter == 'jump') {
      return $this->buildJumpResponse();
    }

    $nav = $this->buildNav();

    $project_query = new PhabricatorProjectQuery();
    $project_query->setViewer($user);
    $project_query->withMemberPHIDs(array($user->getPHID()));
    $projects = $project_query->execute();

    return $this->buildMainResponse($nav, $projects);
  }

  private function buildMainResponse($nav, array $projects) {
    assert_instances_of($projects, 'PhabricatorProject');

    $maniphest = 'PhabricatorApplicationManiphest';
    if (PhabricatorApplication::isClassInstalled($maniphest)) {
      $unbreak_panel = $this->buildUnbreakNowPanel();
      $triage_panel = $this->buildNeedsTriagePanel($projects);
      $tasks_panel = $this->buildTasksPanel();
    } else {
      $unbreak_panel = null;
      $triage_panel = null;
      $tasks_panel = null;
    }

    if (PhabricatorEnv::getEnvConfig('welcome.html') !== null) {
      $welcome_panel = $this->buildWelcomePanel();
    } else {
      $welcome_panel = null;
    }
    $jump_panel = $this->buildJumpPanel();
    $revision_panel = $this->buildRevisionPanel();
    $audit_panel = $this->buildAuditPanel();
    $commit_panel = $this->buildCommitPanel();

    $content = array(
      $jump_panel,
      $welcome_panel,
      $unbreak_panel,
      $triage_panel,
      $revision_panel,
      $tasks_panel,
      $audit_panel,
      $commit_panel,
      $this->minipanels,
    );

    $nav->appendChild($content);
    $nav->appendChild(new PhabricatorGlobalUploadTargetView());

    return $this->buildApplicationPage(
      $nav,
      array(
        'title' => 'Phabricator',
        'device' => true,
        'dust' => true,
      ));
  }

  private function buildJumpResponse() {
    $request = $this->getRequest();

    $jump = $request->getStr('jump');

    $response = PhabricatorJumpNavHandler::jumpPostResponse($jump);

    if ($response) {

      return $response;
    } else if ($request->isFormPost()) {
      $query = new PhabricatorSearchQuery();
      $query->setQuery($jump);
      $query->save();

      return id(new AphrontRedirectResponse())
        ->setURI('/search/'.$query->getQueryKey().'/');
    } else {
      return id(new AphrontRedirectResponse())->setURI('/');
    }
  }

  private function buildUnbreakNowPanel() {
    $user = $this->getRequest()->getUser();
    $user_phid = $user->getPHID();

    $task_query = new ManiphestTaskQuery();
    $task_query->withStatus(ManiphestTaskQuery::STATUS_OPEN);
    $task_query->withPriority(ManiphestTaskPriority::PRIORITY_UNBREAK_NOW);
    $task_query->setLimit(10);

    $tasks = $task_query->execute();

    if (!$tasks) {
      return $this->renderMiniPanel(
        'No "Unbreak Now!" Tasks',
        'Nothing appears to be critically broken right now.');
    }

    $panel = new AphrontPanelView();
    $panel->setHeader('Unbreak Now!');
    $panel->setCaption('Open tasks with "Unbreak Now!" priority.');
    $panel->addButton(
      phutil_tag(
        'a',
        array(
          'href' => '/maniphest/view/all/',
          'class' => 'grey button',
        ),
        "View All Unbreak Now \xC2\xBB"));

    $panel->appendChild($this->buildTaskListView($tasks));
    $panel->setNoBackground();

    return $panel;
  }

  private function buildNeedsTriagePanel(array $projects) {
    assert_instances_of($projects, 'PhabricatorProject');

    $user = $this->getRequest()->getUser();
    $user_phid = $user->getPHID();

    if ($projects) {
      $task_query = new ManiphestTaskQuery();
      $task_query->withStatus(ManiphestTaskQuery::STATUS_OPEN);
      $task_query->withPriority(ManiphestTaskPriority::PRIORITY_TRIAGE);
      $task_query->withAnyProjects(mpull($projects, 'getPHID'));
      $task_query->setLimit(10);
      $tasks = $task_query->execute();
    } else {
      $tasks = array();
    }

    if (!$tasks) {
      return $this->renderMiniPanel(
        'No "Needs Triage" Tasks',
        hsprintf(
          'No tasks in projects you are a member of '.
          'need triage.'));
    }

    $panel = new AphrontPanelView();
    $panel->setHeader('Needs Triage');
    $panel->setCaption(hsprintf(
      'Open tasks with "Needs Triage" priority in '.
      'projects you are a member of.'));

    $panel->addButton(
      phutil_tag(
        'a',
        array(
          // TODO: This should filter to just your projects' need-triage
          // tasks?
          'href' => '/maniphest/view/projecttriage/',
          'class' => 'grey button',
        ),
        "View All Triage \xC2\xBB"));
    $panel->appendChild($this->buildTaskListView($tasks));
    $panel->setNoBackground();

    return $panel;
  }

  private function buildRevisionPanel() {
    $user = $this->getRequest()->getUser();
    $user_phid = $user->getPHID();

    $revision_query = new DifferentialRevisionQuery();
    $revision_query->withStatus(DifferentialRevisionQuery::STATUS_OPEN);
    $revision_query->withResponsibleUsers(array($user_phid));
    $revision_query->needRelationships(true);

    // NOTE: We need to unlimit this query to hit the responsible user
    // fast-path.
    $revision_query->setLimit(null);
    $revisions = $revision_query->execute();

    list($blocking, $active, ) = DifferentialRevisionQuery::splitResponsible(
        $revisions,
        array($user_phid));

    if (!$blocking && !$active) {
      return $this->renderMiniPanel(
        'No Waiting Revisions',
        'No revisions are waiting on you.');
    }

    $panel = new AphrontPanelView();
    $panel->setHeader('Revisions Waiting on You');
    $panel->setCaption('Revisions waiting for you for review or commit.');

    $panel->addButton(
      phutil_tag(
        'a',
        array(
          'href' => '/differential/',
          'class' => 'button grey',
        ),
        "View Active Revisions \xC2\xBB"));

    $revision_view = id(new DifferentialRevisionListView())
      ->setHighlightAge(true)
      ->setRevisions(array_merge($blocking, $active))
      ->setFields(DifferentialRevisionListView::getDefaultFields($user))
      ->setUser($user)
      ->loadAssets();
    $phids = array_merge(
      array($user_phid),
      $revision_view->getRequiredHandlePHIDs());
    $handles = $this->loadViewerHandles($phids);

    $revision_view->setHandles($handles);

    $panel->appendChild($revision_view);
    $panel->setNoBackground();

    return $panel;
  }

  private function buildWelcomePanel() {
    $panel = new AphrontPanelView();
    $panel->appendChild(
      phutil_safe_html(
        PhabricatorEnv::getEnvConfig('welcome.html')));
    $panel->setNoBackground();

    return $panel;
  }

  private function buildTasksPanel() {
    $user = $this->getRequest()->getUser();
    $user_phid = $user->getPHID();

    $task_query = new ManiphestTaskQuery();
    $task_query->withStatus(ManiphestTaskQuery::STATUS_OPEN);
    $task_query->setGroupBy(ManiphestTaskQuery::GROUP_PRIORITY);
    $task_query->withOwners(array($user_phid));
    $task_query->setLimit(10);

    $tasks = $task_query->execute();

    if (!$tasks) {
      return $this->renderMiniPanel(
        'No Assigned Tasks',
        'You have no assigned tasks.');
    }

    $panel = new AphrontPanelView();
    $panel->setHeader('Assigned Tasks');

    $panel->addButton(
      phutil_tag(
        'a',
        array(
          'href' => '/maniphest/',
          'class' => 'button grey',
        ),
        "View Active Tasks \xC2\xBB"));
    $panel->appendChild($this->buildTaskListView($tasks));
    $panel->setNoBackground();

    return $panel;
  }

  private function buildTaskListView(array $tasks) {
    assert_instances_of($tasks, 'ManiphestTask');
    $user = $this->getRequest()->getUser();

    $phids = array_merge(
      array_filter(mpull($tasks, 'getOwnerPHID')),
      array_mergev(mpull($tasks, 'getProjectPHIDs')));

    $handles = $this->loadViewerHandles($phids);

    $view = new ManiphestTaskListView();
    $view->setTasks($tasks);
    $view->setUser($user);
    $view->setHandles($handles);

    return $view;
  }

  private function buildJumpPanel($query=null) {
    $request = $this->getRequest();
    $user = $request->getUser();

    $uniq_id = celerity_generate_unique_node_id();

    Javelin::initBehavior(
      'phabricator-autofocus',
      array(
        'id' => $uniq_id,
      ));

    require_celerity_resource('phabricator-jump-nav');

    $doc_href = PhabricatorEnv::getDocLink('article/Jump_Nav_User_Guide.html');
    $doc_link = phutil_tag(
      'a',
      array(
        'href' => $doc_href,
      ),
      'Jump Nav User Guide');

    $jump_input = phutil_tag(
      'input',
      array(
        'type'  => 'text',
        'class' => 'phabricator-jump-nav',
        'name'  => 'jump',
        'id'    => $uniq_id,
        'value' => $query,
      ));
    $jump_caption = phutil_tag(
      'p',
      array(
        'class' => 'phabricator-jump-nav-caption',
      ),
      hsprintf(
        'Enter the name of an object like <tt>D123</tt> to quickly jump to '.
          'it. See %s or type <tt>help</tt>.',
        $doc_link));

    $form = phabricator_form(
      $user,
      array(
        'action' => '/jump/',
        'method' => 'POST',
        'class'  => 'phabricator-jump-nav-form',
      ),
      array(
        $jump_input,
        $jump_caption,
      ));

    $panel = new AphrontPanelView();
    $panel->setHeader('Jump Nav');
    $panel->setNoBackground();
    // $panel->appendChild();

    $list_filter = new AphrontListFilterView();
    $list_filter->appendChild(phutil_tag('h1', array(), 'Jump Nav'));
    $list_filter->appendChild($form);

    $container = phutil_tag('div',
      array('class' => 'phabricator-jump-nav-container'),
      $list_filter);

    return $container;
  }

  private function renderMiniPanel($title, $body) {
    $panel = new AphrontMiniPanelView();
    $panel->appendChild(
      phutil_tag(
        'p',
        array(
        ),
        array(
          phutil_tag('strong', array(), $title.': '),
          $body
        )));
    $this->minipanels[] = $panel;
  }

  public function buildAuditPanel() {
    $request = $this->getRequest();
    $user = $request->getUser();

    $phids = PhabricatorAuditCommentEditor::loadAuditPHIDsForUser($user);

    $query = new PhabricatorAuditQuery();
    $query->withAuditorPHIDs($phids);
    $query->withStatus(PhabricatorAuditQuery::STATUS_OPEN);
    $query->withAwaitingUser($user);
    $query->needCommitData(true);
    $query->setLimit(10);

    $audits = $query->execute();
    $commits = $query->getCommits();

    if (!$audits) {
      return $this->renderMinipanel(
        'No Audits',
        'No commits are waiting for you to audit them.');
    }

    $view = new PhabricatorAuditListView();
    $view->setAudits($audits);
    $view->setCommits($commits);
    $view->setUser($user);

    $phids = $view->getRequiredHandlePHIDs();
    $handles = $this->loadViewerHandles($phids);
    $view->setHandles($handles);

    $panel = new AphrontPanelView();
    $panel->setHeader('Audits');
    $panel->setCaption('Commits awaiting your audit.');
    $panel->appendChild($view);
    $panel->addButton(
      phutil_tag(
        'a',
        array(
          'href' => '/audit/',
          'class' => 'button grey',
        ),
        "View Active Audits \xC2\xBB"));
    $panel->setNoBackground();

    return $panel;
  }

  public function buildCommitPanel() {
    $request = $this->getRequest();
    $user = $request->getUser();

    $phids = array($user->getPHID());

    $query = new PhabricatorAuditCommitQuery();
    $query->withAuthorPHIDs($phids);
    $query->withStatus(PhabricatorAuditCommitQuery::STATUS_CONCERN);
    $query->needCommitData(true);
    $query->setLimit(10);

    $commits = $query->execute();

    if (!$commits) {
      return $this->renderMinipanel(
        'No Problem Commits',
        'No one has raised concerns with your commits.');
    }

    $view = new PhabricatorAuditCommitListView();
    $view->setCommits($commits);
    $view->setUser($user);

    $phids = $view->getRequiredHandlePHIDs();
    $handles = $this->loadViewerHandles($phids);
    $view->setHandles($handles);

    $panel = new AphrontPanelView();
    $panel->setHeader('Problem Commits');
    $panel->setCaption('Commits which auditors have raised concerns about.');
    $panel->appendChild($view);
    $panel->addButton(
      phutil_tag(
        'a',
        array(
          'href' => '/audit/',
          'class' => 'button grey',
        ),
        "View Problem Commits \xC2\xBB"));
    $panel->setNoBackground();

    return $panel;
  }

}


text:
from argparse import RawDescriptionHelpFormatter
import textwrap

from conda.cli.conda_argparse import add_parser_json
from .. import exceptions
from ..env import from_file
from ..utils.uploader import Uploader, is_installed

description = """
WARNING: This command is deprecated in conda 4.4 and scheduled for removal in conda 4.5.

Upload an environment to anaconda.org
"""

example = """
examples:
    conda env upload
    conda env upload project
    conda env upload --file=/path/to/environment.yml
    conda env upload --file=/path/to/environment.yml project
"""

def configure_parser(sub_parsers):
    p = sub_parsers.add_parser(
        'upload',
        formatter_class=RawDescriptionHelpFormatter,
        description=description,
        help=description,
        epilog=example,
    )
    p.add_argument(
        '-n', '--name',
        action='store',
        help='environment definition [Deprecated]',
        default=None,
        dest='old_name'
    )
    p.add_argument(
        '-f', '--file',
        action='store',
        help='environment definition file (default: environment.yml)',
        default='environment.yml',
    )
    p.add_argument(
        '--summary',
        help='Short summary of the environment',
        default='Environment file'
    )
    p.add_argument(
        '-q', '--quiet',
        default=False,
        action='store_true'
    )
    p.add_argument(
        'name',
        help='environment definition',
        action='store',
        default=None,
        nargs='?'
    )
    add_parser_json(p)
    p.set_defaults(func='.main_upload.execute')

def execute(args, parser):
    print("WARNING: conda env upload is deprecated and will be removed as part of conda 4.5.")

    if not is_installed():
        raise exceptions.NoBinstar()

    try:
        env = from_file(args.file)
    except exceptions.EnvironmentFileNotFound as e:
        msg = 'Unable to locate environment file: %s\n\n' % e.filename
        msg += "\n".join(textwrap.wrap(textwrap.dedent("""
            Please verify that the above file is present and that you have
            permission read the file's contents.  Note, you can specify the
            file to use by explictly adding --file=/path/to/file when calling
            conda env create.""").lstrip()))
        raise exceptions.CondaEnvException(msg)

    if args.old_name:
        print("`--name` is deprecated. Use:\n"
              "  conda env upload {}".format(args.old_name))

    try:
        summary = args.summary or env.summary
    except AttributeError:
        summary = None

    try:
        name = args.name or args.old_name or env.name
    except AttributeError:
        msg = """An environment name is required.\n
                 You can specify on the command line as in:
                 \tconda env upload name
                 or you can add a name property to your {} file.""".lstrip().format(args.file)
        raise exceptions.CondaEnvException(msg)

    uploader = Uploader(name, args.file, summary=summary, env_data=dict(env.to_dict()))

    if uploader.authorized():
        info = uploader.upload()
        url = info.get('url', 'anaconda.org')
        print("Your environment file has been uploaded to {}".format(url))
    else:
        msg = "\n".join(["You are not authorized to upload a package into Anaconda.org",
                         "Verify that you are logged in anaconda.org with:",
                         "    anaconda login\n"])
        raise exceptions.CondaEnvException(msg)

    print("Done.")


text:
<?php

defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * PDO 4D Forge Class
 *
 * @category    Database
 * @author        EllisLab Dev Team
 * @link        http://codeigniter.com/user_guide/database/
 */
class CI_DB_pdo_4d_forge extends CI_DB_pdo_forge
{

    /**
     * CREATE DATABASE statement
     *
     * @var    string
     */
    protected $_create_database = 'CREATE SCHEMA %s';

    /**
     * DROP DATABASE statement
     *
     * @var    string
     */
    protected $_drop_database = 'DROP SCHEMA %s';

    /**
     * CREATE TABLE IF statement
     *
     * @var    string
     */
    protected $_create_table_if = 'CREATE TABLE IF NOT EXISTS';

    /**
     * RENAME TABLE statement
     *
     * @var    string
     */
    protected $_rename_table = FALSE;

    /**
     * DROP TABLE IF statement
     *
     * @var    string
     */
    protected $_drop_table_if = 'DROP TABLE IF EXISTS';

    /**
     * UNSIGNED support
     *
     * @var    array
     */
    protected $_unsigned = array(
        'INT16' => 'INT',
        'SMALLINT' => 'INT',
        'INT' => 'INT64',
        'INT32' => 'INT64'
    );

    /**
     * DEFAULT value representation in CREATE/ALTER TABLE statements
     *
     * @var    string
     */
    protected $_default = FALSE;

    // --------------------------------------------------------------------

    /**
     * ALTER TABLE
     *
     * @param    string $alter_type ALTER type
     * @param    string $table Table name
     * @param    mixed $field Column definition
     * @return    string|string[]
     */
    protected function _alter_table($alter_type, $table, $field)
    {
        if (in_array($alter_type, array('ADD', 'DROP'), TRUE)) {
            return parent::_alter_table($alter_type, $table, $field);
        }

        // No method of modifying columns is supported
        return FALSE;
    }

    // --------------------------------------------------------------------

    /**
     * Process column
     *
     * @param    array $field
     * @return    string
     */
    protected function _process_column($field)
    {
        return $this->db->escape_identifiers($field['name'])
        . ' ' . $field['type'] . $field['length']
        . $field['null']
        . $field['unique']
        . $field['auto_increment'];
    }

    // --------------------------------------------------------------------

    /**
     * Field attribute TYPE
     *
     * Performs a data type mapping between different databases.
     *
     * @param    array &$attributes
     * @return    void
     */
    protected function _attr_type(&$attributes)
    {
        switch (strtoupper($attributes['TYPE'])) {
            case 'TINYINT':
                $attributes['TYPE'] = 'SMALLINT';
                $attributes['UNSIGNED'] = FALSE;
                return;
            case 'MEDIUMINT':
                $attributes['TYPE'] = 'INTEGER';
                $attributes['UNSIGNED'] = FALSE;
                return;
            case 'INTEGER':
                $attributes['TYPE'] = 'INT';
                return;
            case 'BIGINT':
                $attributes['TYPE'] = 'INT64';
                return;
            default:
                return;
        }
    }

    // --------------------------------------------------------------------

    /**
     * Field attribute UNIQUE
     *
     * @param    array &$attributes
     * @param    array &$field
     * @return    void
     */
    protected function _attr_unique(&$attributes, &$field)
    {
        if (!empty($attributes['UNIQUE']) && $attributes['UNIQUE'] === TRUE) {
            $field['unique'] = ' UNIQUE';

            // UNIQUE must be used with NOT NULL
            $field['null'] = ' NOT NULL';
        }
    }

    // --------------------------------------------------------------------

    /**
     * Field attribute AUTO_INCREMENT
     *
     * @param    array &$attributes
     * @param    array &$field
     * @return    void
     */
    protected function _attr_auto_increment(&$attributes, &$field)
    {
        if (!empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) {
            if (stripos($field['type'], 'int') !== FALSE) {
                $field['auto_increment'] = ' AUTO_INCREMENT';
            } elseif (strcasecmp($field['type'], 'UUID') === 0) {
                $field['auto_increment'] = ' AUTO_GENERATE';
            }
        }
    }

}