<commit_before><?php

include_once 'header.php';
include_once 'database.php';

if( isset( $_POST['months'] ) )
    $howManyMonths = intval( $_POST[ 'months' ] );
else
    $howManyMonths = 36;

echo '
    <form method="post" action="">
    Show AWS interaction in last <input type="text" name="months" 
        value="' . $howManyMonths . '" />
    months.
    <button name="response" value="Submit">Submit</button>
    </form>
    ';


$from = date( 'Y-m-d', strtotime( 'today' . " -$howManyMonths months"));

$fromD = date( 'M d, Y', strtotime( $from ) );
echo "<p>
    Following graph shows the interaction among faculty since $fromD.
    <br>

    Number on edges are number of AWSs between two faculty, either of them is involved 
    in an AWS as co-supervisor or as a thesis committee member.

    </p>";

$awses = getAWSFromPast( $from  );
$network = array( 'nodes' => array(), 'edges' => array( ) );

echo printInfo( "Total " . count( $awses) . " AWSs found in database since $fromD" );

echo '
    <strong>
    Hover over a node to see the interaction of particular faculty.
    </strong>
    ';

$community = array();


/** 
 * Here we collect all the unique PIs. This is to make sure that we don't draw 
 * a node for external PI who is/was not on NCBS faculty list. Sometimes, we 
 * can't get all relevant PIs if we only search in AWSs given in specific time. 
 * Therefore we query the faculty table to get the list of all PIs.
 */
$faculty = getFaculty( );
$pis = array( );                                // Just the email
foreach( $faculty as $fac )
{
    $pi = $fac[ 'email' ];
    array_push( $pis, $pi );
    $community[ $pi ] = array( 
        'count' => 0  //  Number of AWS by this supervisor
        , 'edges' => array( )  // Outgoiing edges
        , 'degree' => 0 // Degree of this supervisor.
    );
}

// Each node must have a unique integer id. We use it to generate a distinct 
// color for each node in d3. Also edges are drawn from id to id.
foreach( $awses as $aws )
{
    // How many AWSs this supervisor has.
    $pi = $aws[ 'supervisor_1' ];
    if( ! in_array( $pi, $pis ) )
        continue;

    $community[ $pi ]['count'] += 1;
    $community[ $pi ]['degree'] += 1;

    // Co-supervisor is an edge
    if( strlen( $aws[ "supervisor_2" ] ) > 0 )
    {
        // Only if PI is from NCBS.
        $super2 = $aws[ 'supervisor_2' ];
        if( in_array( $super2, $pis ) )
        {
            array_push( $community[ $pi ]['edges'], $super2 );
            $community[ $super2 ]['degree'] += 1;
        }
    }

    // All TCM members are edges
    $foundAnyTcm = false;
    for( $i = 1; $i < 5; $i++ )
    {
        if( ! array_key_exists( "tcm_member_$i", $aws ) )
            continue;

        $tcmMember = trim( $aws[ "tcm_member_$i" ] );
        if( strpos( $tcmMember, '@') == false ) // Invalid email id.
            continue;

        if( in_array( $tcmMember, $pis ) )
        {
            array_push( $community[ $pi ][ 'edges' ], $tcmMember );
            $community[ $tcmMember ][ "degree" ] += 1;
            $foundAnyTcm = true;
        }
    }

    // If not TCM member is found for this TCM. Add an edge onto PI.
    if(! $foundAnyTcm )
        array_push( $community[ $pi ][ 'edges' ], $pi );
}

// Now for each PI, draw edges to other PIs.
foreach( $community as $pi => $value )
{
    // If there are not edges from or onto this PI, ignore it.
    $login = explode( '@', $pi)[0];

    // This PI is not involved in any AWS for this duration.
    if( $value[ "degree" ] < 1 )
        continue;

    // Width represent AWS per month.
    $count = $value[ 'count' ];
    $width = 0.1 + 0.5 * ( $count / $howManyMonths );
    array_push( 
        $network[ 'nodes' ], array( 'name' => $login, 'count' => $count, 'width' => $width ) 
    );

    foreach( array_count_values( $value['edges'] ) as $val => $edgeNum )
        array_push( $network[ 'edges']
            , array( 'source_email' => $pi, 'tgt_email' => $val, 'width' => $edgeNum 
                    , 'count' => $edgeNum )
        );
}

// Before writing to JSON use index of node as source and target in edges.
$nodeIds = array();
foreach( $network['nodes'] as $node )
    $nodeIds[ $node['name'] ] = count( $nodeIds );

for( $i = 0; $i < count( $network['edges']); $i++)
{
    $src = explode( '@', $network['edges'][$i][ 'source_email' ] )[0];
    $tgt = explode( '@', $network['edges'][$i][ 'tgt_email' ] )[0];
    $network['edges'][$i][ 'source' ] = $nodeIds[ $src ];
    $network['edges'][$i][ 'target' ] = $nodeIds[ $tgt ];
}

// Write the network array to json array.
$networkJSON = json_encode( $network, JSON_PRETTY_PRINT );
$networkJSONFileName = "data/network.json";
$handle = fopen( $networkJSONFileName, "w+" );
fwrite( $handle, $networkJSON );
fclose( $handle );
?>

<!-- Use d3 to draw graph -->
<div>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script>

    var w = 1000;
    var h = 1000;<commit_msg>Not writing jSON file.<commit_after><?php

include_once 'header.php';
include_once 'database.php';

if( isset( $_POST['months'] ) )
    $howManyMonths = intval( $_POST[ 'months' ] );
else
    $howManyMonths = 36;

echo '
    <form method="post" action="">
    Show AWS interaction in last <input type="text" name="months" 
        value="' . $howManyMonths . '" />
    months.
    <button name="response" value="Submit">Submit</button>
    </form>
    ';


$from = date( 'Y-m-d', strtotime( 'today' . " -$howManyMonths months"));

$fromD = date( 'M d, Y', strtotime( $from ) );
echo "<p>
    Following graph shows the interaction among faculty since $fromD.
    <br>

    Number on edges are number of AWSs between two faculty, either of them is involved 
    in an AWS as co-supervisor or as a thesis committee member.

    </p>";

$awses = getAWSFromPast( $from  );
$network = array( 'nodes' => array(), 'edges' => array( ) );

echo printInfo( "Total " . count( $awses) . " AWSs found in database since $fromD" );

echo '
    <strong>
    Hover over a node to see the interaction of particular faculty.
    </strong>
    ';

$community = array();


/** 
 * Here we collect all the unique PIs. This is to make sure that we don't draw 
 * a node for external PI who is/was not on NCBS faculty list. Sometimes, we 
 * can't get all relevant PIs if we only search in AWSs given in specific time. 
 * Therefore we query the faculty table to get the list of all PIs.
 */
$faculty = getFaculty( );
$pis = array( );                                // Just the email
foreach( $faculty as $fac )
{
    $pi = $fac[ 'email' ];
    array_push( $pis, $pi );
    $community[ $pi ] = array( 
        'count' => 0  //  Number of AWS by this supervisor
        , 'edges' => array( )  // Outgoiing edges
        , 'degree' => 0 // Degree of this supervisor.
    );
}

// Each node must have a unique integer id. We use it to generate a distinct 
// color for each node in d3. Also edges are drawn from id to id.
foreach( $awses as $aws )
{
    // How many AWSs this supervisor has.
    $pi = $aws[ 'supervisor_1' ];
    if( ! in_array( $pi, $pis ) )
        continue;

    $community[ $pi ]['count'] += 1;
    $community[ $pi ]['degree'] += 1;

    // Co-supervisor is an edge
    if( strlen( $aws[ "supervisor_2" ] ) > 0 )
    {
        // Only if PI is from NCBS.
        $super2 = $aws[ 'supervisor_2' ];
        if( in_array( $super2, $pis ) )
        {
            array_push( $community[ $pi ]['edges'], $super2 );
            $community[ $super2 ]['degree'] += 1;
        }
    }

    // All TCM members are edges
    $foundAnyTcm = false;
    for( $i = 1; $i < 5; $i++ )
    {
        if( ! array_key_exists( "tcm_member_$i", $aws ) )
            continue;

        $tcmMember = trim( $aws[ "tcm_member_$i" ] );
        if( strpos( $tcmMember, '@') == false ) // Invalid email id.
            continue;

        if( in_array( $tcmMember, $pis ) )
        {
            array_push( $community[ $pi ][ 'edges' ], $tcmMember );
            $community[ $tcmMember ][ "degree" ] += 1;
            $foundAnyTcm = true;
        }
    }

    // If not TCM member is found for this TCM. Add an edge onto PI.
    if(! $foundAnyTcm )
        array_push( $community[ $pi ][ 'edges' ], $pi );
}

// Now for each PI, draw edges to other PIs.
foreach( $community as $pi => $value )
{
    // If there are not edges from or onto this PI, ignore it.
    $login = explode( '@', $pi)[0];

    // This PI is not involved in any AWS for this duration.
    if( $value[ "degree" ] < 1 )
        continue;

    // Width represent AWS per month.
    $count = $value[ 'count' ];
    $width = 0.1 + 0.5 * ( $count / $howManyMonths );
    array_push( 
        $network[ 'nodes' ], array( 'name' => $login, 'count' => $count, 'width' => $width ) 
    );

    foreach( array_count_values( $value['edges'] ) as $val => $edgeNum )
        array_push( $network[ 'edges']
            , array( 'source_email' => $pi, 'tgt_email' => $val, 'width' => $edgeNum 
                    , 'count' => $edgeNum )
        );
}

// Before writing to JSON use index of node as source and target in edges.
$nodeIds = array();
foreach( $network['nodes'] as $node )
    $nodeIds[ $node['name'] ] = count( $nodeIds );

for( $i = 0; $i < count( $network['edges']); $i++)
{
    $src = explode( '@', $network['edges'][$i][ 'source_email' ] )[0];
    $tgt = explode( '@', $network['edges'][$i][ 'tgt_email' ] )[0];
    $network['edges'][$i][ 'source' ] = $nodeIds[ $src ];
    $network['edges'][$i][ 'target' ] = $nodeIds[ $tgt ];
}

// Write the network array to json array.
$networkJSON = json_encode( $network, JSON_PRETTY_PRINT );
$networkJSONFileName = "data/network.json";
//$handle = fopen( $networkJSONFileName, "w+" );
//fwrite( $handle, $networkJSON );
//fclose( $handle );
?>

<!-- Use d3 to draw graph -->
<div>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script>

    var w = 1000;
    var h = 1000;
{'fraction_non_alphanumeric': 0.1666161463069617, 'fraction_numerical': 0.008083257552793776, 'mean_word_length': 2.48153359127682, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 65, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2088849', 'n_tokens_mistral': 3446, 'n_tokens_neox': 3308, 'n_words': 1088}