Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# IT490-004 | System Integration Project


### Overview
Class Project to demonstrate the creation of a full stack application.

### Build
146 changes: 146 additions & 0 deletions dmz-processor/processor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#!/usr/bin/php
<?php

require_once __DIR__ . '/../get_host_info.inc';
require_once __DIR__ . '/../rabbitMQLib.inc';

$API = "b97a6678535cddda9458dcee24dddac2";
$URL = "http://ws.audioscrobbler.com/2.0/";


function lastfm($url, $api_key, $method, $extras=[]) { //call api
$query = array_merge([
"method" => $method,
"api_key" => $api_key,
"format" => "json", ], $extras);

$endpoint = $url . "?" . http_build_query($query); //creates url

$data = file_get_contents($endpoint);

return json_decode($data, true); //returns data
}

function many_artists($url, $api) { //gets info for 300 artists
echo "Artists \n";

$top_artists = lastfm($url, $api, "chart.getTopArtists", ["limit" => 100000]); //limit # of artists

if (!$top_artists || !isset($top_artists["artists"]["artist"])) { //error checking
echo "No Artists Data \n";
return null;
}

$results = [];
foreach ($top_artists["artists"]["artist"] as $artist) { //loop through each artist
$name = $artist["name"];

$artist_info = lastfm($url, $api, "artist.getInfo", ["artist" => $name]); //gets info

if (!$artist_info || !isset($artist_info["artist"])) { //skips if no data
echo "No data for: $name \n";
continue;
}

$results[] = [
"name" => $artist_info["artist"]["name"],
"listeners" => (int)($artist_info["artist"]["stats"]["listeners"]),
"play_count" => (int)($artist_info["artist"]["stats"]["playcount"]),
"bio" => trim(strip_tags($artist_info["artist"]["bio"]["summary"])),
"url" => $artist_info["artist"]["url"],
"mbid" => $artist_info["artist"]["mbid"]
];
}
echo "DONE \n";
return [
"type" => "many_artists",
"fetched_at" => date("Y-m-d"),
"payload" => $results
];
}


function many_tracks($url, $api) { //many tracks
echo "Tracks \n";

$top_tracks = lastfm($url, $api, "chart.getTopTracks",["limit" => 100000]) ; //chart.getTopTracks

if (!$top_tracks || !isset($top_tracks["tracks"]["track"])) { //checks for error
echo "No Track Data";
return null;
}

$tracks = [];
foreach ($top_tracks["tracks"]["track"] as $track) { //puts them into a list
$tracks[] = [
"track_name" => $track["name"],
"artist" => $track["artist"]["name"],
"play_count" => (int)($track["playcount"] ),
"url" => $track["url"],
"mbid" => $track["mbid"]
];
}

echo "DONE \n";
return [ //returns formatted list
"type" => "many_tracks",
"fetched_at" => date("Y-m-d"),
"payload" => $tracks
];
}

function single_artist($url, $api, $name) { //gets the information on a certain artist
echo "Artist Info \n";

$artist = lastfm($url, $api, "artist.getInfo", ["artist" => $name]); //artst.getInfo

if (!$artist || !isset($artist["artist"])) { //error checking
echo "Artist not found: $name\n";
return null;
}

$artist[] = [
"name" => $artist["artist"]["name"],
"listeners" => (int)($artist["artist"]["stats"]["listeners"]),
"play_count" => (int)($artist["artist"]["stats"]["playcount"]),
"bio" => trim(strip_tags($artist["artist"]["bio"]["summary"])),
"url" => $artist["artist"]["url"],
"mbid" => $artist["artist"]["mbid"]
];

echo "DONE \n";
return [ //returns formatted info
"type" => "single_artist",
"fetched_at" => date("Y-m-d"),
"payload" => $artist
];
}

function requests($request) { //handles the requests
global $API, $URL;

if (!isset($request['type'])) { //error checking
return ["status" => "error", "message" => "unsupported type"];

} elseif ($request["type"] == "many_artists") {
$data = many_artists($URL, $API);
return ["status" => "success", "data" => $data];

} elseif ($request["type"] == "many_tracks") {
$data = many_tracks($URL,$API);
return ["status" => "success", "data" => $data];

} elseif ($request["type"] == "single_artist") {
$data = single_artist($URL,$API);
return ["status" => "success", "data" => $data];

} else {
echo "Unknown type: " . $request['type'] . "\n";
}
}

$server = new rabbitMQServer(__DIR__ . '/../testRabbitMQ.ini', 'testServer3'); //connection to the broker
echo "API ready \n";
$server->process_requests('requests');
exit();
?>
25 changes: 25 additions & 0 deletions mysqlconnect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/php
<?php

$mydb = new mysqli('127.0.0.1','testUser','12345','testdb');

if ($mydb->errno != 0)
{
echo "failed to connect to database: ". $mydb->error . PHP_EOL;
exit(0);
}

echo "successfully connected to database".PHP_EOL;

$query = "select * from students;";

$response = $mydb->query($query);
if ($mydb->errno != 0)
{
echo "failed to execute query:".PHP_EOL;
echo __FILE__.':'.__LINE__.":error: ".$mydb->error.PHP_EOL;
exit(0);
}


?>
6 changes: 5 additions & 1 deletion rabbitMQLib.inc
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ class rabbitMQServer
}
$this->exchange = $this->machine[$server]["EXCHANGE"];
$this->queue = $this->machine[$server]["QUEUE"];
$this->routing_key = $this->machine[$server]["ROUTING_KEY"];
}

function process_message($msg)
{
// send the ack to clear the item from the queue
if ($msg->getRoutingKey() !== "*")
if ($msg->getRoutingKey() !== $this->routing_key)
{
return;
}
Expand Down Expand Up @@ -74,6 +75,7 @@ class rabbitMQServer
$conn_queue = new AMQPQueue($channel);
$conn_queue->setName($msg->getReplyTo());
$replykey = $this->routing_key.".response";
$conn_queue->declare();
$conn_queue->bind($exchange->getName(),$replykey);
$exchange->publish(json_encode($response),$replykey,AMQP_NOPARAM,array('correlation_id'=>$msg->getCorrelationId()));

Expand Down Expand Up @@ -166,6 +168,7 @@ class rabbitMQClient
}
$this->exchange = $this->machine[$server]["EXCHANGE"];
$this->queue = $this->machine[$server]["QUEUE"];
$this->routing_key = $this->machine[$server]["ROUTING_KEY"];
}

function process_response($response)
Expand Down Expand Up @@ -267,6 +270,7 @@ class rabbitMQClient
die("failed to send message to exchange: ". $e->getMessage()."\n");
}
}

}
?>

23 changes: 23 additions & 0 deletions testRabbitMQ.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,26 @@ QUEUE = testQueue
;EXCHANGE_TYPE = ????
AUTO_DELETE = true

[testServer2]
BROKER_HOST = 100.88.147.61
BROKER_PORT = 5672
USER = test
PASSWORD = test
VHOST = testHost
EXCHANGE = testExchange
QUEUE = testQueue
ROUTING_KEY = us.frontend
;EXCHANGE_TYPE = ????
AUTO_DELETE = true

[testServer3]
BROKER_HOST = 100.116.117.114
BROKER_PORT = 5672
USER = test
PASSWORD = test
VHOST = testHost
EXCHANGE = testExchange
QUEUE = dmz_jobs
ROUTING_KEY = us.api
;EXCHANGE_TYPE = ????
AUTO_DELETE = true
2 changes: 1 addition & 1 deletion testRabbitMQClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require_once('get_host_info.inc');
require_once('rabbitMQLib.inc');

$client = new rabbitMQClient("testRabbitMQ.ini","testServer");
$client = new rabbitMQClient("testRabbitMQ.ini","testServer2");
if (isset($argv[1]))
{
$msg = $argv[1];
Expand Down