MongoDB php Spatial Astronomy Data What's wrong with this? - php

I want to retrieve data within the bounding box ((ra_min,dec_min),(ra_max,dec_max)).
Could anyone tell me what's wrong with below?
php Code and structure of data are below. Thanks......
Structure :
{
"filter": "E",
"loc": {
"dec": 130,
"ra": 15
},
"path": "00015+00130E.jpeg"
},
{
"filter": "Z",
"loc": {
"dec": 130,
"ra": 15
},
"path": "00015+00130Z.jpeg"
},
Code :
$lowerLeft = array("ra"=>$RA_min - $ra_offset, "dec"=>$DEC_min - $dec_offset);
$upperRight= array("ra"=>$RA_max + $ra_offset, "dec"=>$DEC_max + $dec_offset);
$cond = array("loc" => array('$within' => array('$box' => $lowerLeft, $upperRight)));
$cursor = $collection->find($cond);

i Think problem is on $box Usage
$box Usage Example:
$cursor = $collection->find( array( 'loc'=>array('$within'=> array('$box'=>array(array(0,0), array(100,100) ) ) ) ));
This will return all the documents that are within the box having points at: [0,0], [0,100], [100,0], and [100,100].
use print_r($cond); before find(); for see the array value that show you where is problem

Related

Insert multiple rows into database from json containing multidimensional data

This is the following JSON I am working with. I have everything being pulled and added correctly except the content under "tactics" -- which comes out as Array.
My goal is to store the "tactics" values as a comma-delimited string.
'[
{
"queryFrequency": "P1D",
"queryPeriod": "P1D",
"triggerOperator": "GreaterThan",
"triggerThreshold": 0,
"eventGroupingSettings": {
"aggregationKind": "SingleAlert"
},
"severity": "Medium",
"query": "let extess",
"suppressionDuration": "PT1H",
"suppressionEnabled": false,
"tactics": [
"Execution",
"Persistence"
],
"displayName": "MFA disabled for a user",
"enabled": true,
"description": "Multi-Factor Authentication (MFA) helps prevent credential compromise. This alert identifies when an attempt has been made to diable MFA for a user ",
"alertRuleTemplateName": "65c78944-930b-4cae-bd79-c3664ae30ba7",
"lastModifiedUtc": "2021-06-16T16:29:52.6974983Z",
"name": "1ada95bc-b4d5-4776-bc3e-2dbb3684c0b1",
"id": "/sc0b1",
"kind": "Scheduled",
"createIncident": true,
"groupingConfiguration": {
"enabled": false,
"reopenClosedIncident": false,
"lookbackDuration": "PT5H",
"entitiesMatchingMethod": "All",
"groupByEntities": [
"Account",
"Ip",
"Host",
"Url",
"FileHash"
]
},
"playbookName": ""
},
{
"queryFrequency": "P1D",
"queryPeriod": "P1D",
"triggerOperator": "GreaterThan",
"triggerThreshold": 0,
"eventGroupingSettings": {
"aggregationKind": "SingleAlert"
},
"severity": "Medium",
"query": "StppUsed",
"suppressionDuration": "PT1H",
"suppressionEnabled": false,
"tactics": [
"Execution",
"Persistence"
],
"displayName": "Explicit MFA Deny",
"enabled": true,
"description": "User explicitly denies MFA push, indicating that login was not expected and the account\'s password may be compromised.",
"alertRuleTemplateName": "a22740ec-fc1e-4c91-8de6-c29c6450ad00",
"lastModifiedUtc": "2021-06-16T16:29:54.0826821Z",
"name": "bba57ceb-dd33-4297-8080-b19b1bd07a21",
"id": "/suobba5d07a21",
"kind": "Scheduled",
"createIncident": true,
"groupingConfiguration": {
"enabled": false,
"reopenClosedIncident": false,
"lookbackDuration": "PT5H",
"entitiesMatchingMethod": "All",
"groupByEntities": [
"Account",
"Ip",
"Host",
"Url",
"FileHash"
]
},
"playbookName": ""
} ]'
This is my code:
...
$dep_cols=array("queryFrequency","queryPeriod","triggerOperator","triggerThreshold","aggregationKind","severity","query","suppressionDuration","suppressionEnabled","tactics","displayName","enabled","description","kind","createIncident","playbookName"); // declare columns
$dep_keys=array_map(function($v){return ":$v";},$dep_cols); // build :keys
$dep_cols=array_combine($dep_keys,$dep_cols); // assign :keys
var_export($dep_cols);
$dep_query="INSERT INTO `template_rules` (`id`,`".implode('`,`',$dep_cols)."`)"; // list columns as csv
$dep_query.=" VALUES ('',".implode(',',array_keys($dep_cols)).");";
echo "<div>$dep_query</div>";
$stmt_add_dep=$db->prepare($dep_query);
foreach(json_decode($json) as $d){
foreach($dep_cols as $k=>$v){
if($k==':tactics'){$v=json_decode($v);}
$stmt_add_dep->bindValue($k,(property_exists($d,$v)?$d->$v:""));
echo "<div>$k => {$d->$v}</div>";
}
$stmt_add_dep->execute();
echo "<div>Dep Affected Rows: ",$stmt_add_dep->rowCount(),"</div><br>";
}
...
If I remove the if($k==':tactics') statement, I just get Array. I'm not sure how to pull those values out as they look to just be a string in an array.
Current results look like this:
...
:suppressionDuration => PT1H
:suppressionEnabled =>
:tactics =>
:displayName => MFA disabled for a user
:enabled => 1
...
Here's a working refactor of your script.
Create arrays of the whitelisted column names and a number of placeholders (I prefer the vague ?, but you can use named placeholders if you like).
Create separate payloads of values to be fed to the prepared statement when execute() is called.
You don't need to mention id if you are autoincrementing that column.
Code: (PHPize.online Demo)
$whitelist = [
"queryFrequency", "queryPeriod", "triggerOperator", "triggerThreshold",
"aggregationKind", "severity", "query", "suppressionDuration",
"suppressionEnabled", "tactics", "displayName", "enabled",
"description", "kind", "createIncident", "playbookName"
];
$columns = [];
$placeholders = [];
$valueSets = [];
foreach ($whitelist as $column) {
$columns[] = "`$column`";
$placeholders[] = "?";
}
foreach (json_decode($json) as $i => $obj) {
$obj->aggregationKind = $obj->eventGroupingSettings->aggregationKind ?? null;
$obj->tactics = property_exists($obj, 'tactics') ? implode(',', $obj->tactics) : null;
foreach ($whitelist as $column) {
$valueSets[$i][] = $obj->$column ?? null;
}
}
$stmt = $pdo->prepare(
sprintf(
'INSERT INTO `template_rules` (%s) VALUES (%s)',
implode(',', $columns),
implode(',', $placeholders)
)
);
foreach ($valueSets as $values) {
$stmt->execute($values);
printf("<div>New autoincremented Id: %d</div><br>\n\n", $pdo->lastInsertId());
}
echo json_encode($pdo->query('SELECT * FROM template_rules')->fetchAll(PDO::FETCH_ASSOC), JSON_PRETTY_PRINT);

MongoDB / PHP / MapReduce / Reg Exp / Strings->Floats

I'm new with MapReduce, but I have a collection that I'd like to apply myself to as a chance to learn how mapreduce works.
Example Documents:
{ "filename" : "resume.doc",
"folder" : "work",
"completed": "0.5" },
{ "filename" : "spreadsheet.xls",
"folder" : "work",
"completed": "0.6" },
{ "filename" : "thesis.doc",
"folder" : "school",
"completed": "0.75" },
{ "filename" : "coverletter.doc",
"folder" : "work",
"completed": "0.6"}
So the whole idea is: I'd like to query:
{ "folder" : "work",
"completed": { $gt: 0.5 },
"filename" : new MongoRegex( "/\.[a-zA-Z]{2,}$/" ) }
And ultimately get the count of all documents by extension (.doc, .xls, etc.), as so:
{ ".doc" : 1,
".xls" : 1 }
I also realize i've got an issue because my %-completed are strings, not floats, so i think mongodb is going to need more instruction for comparing the strings.
I'm using (if it matters):
PHP extension: mongo/1.5.7
MongoDB: version 3.2.11
Seems I've stumbled upon my own answer.
Let me know if anybody comes up with a more concise/expert solution.
But this seems to work.
try {
$map = new MongoCode(
'function(){
var re = new RegExp(/(.+)\.([a-zA-Z]{2,})$/);
doctype = this.value.match(re);
if(parseFloat(this.completed)>0.5){
emit(doctype[2], parseFloat(this.completed));
}
}'
);
$reduce = new MongoCode(
'function(key, values){
var sum = 0, num = 0;
for(var i in values){
if(parseFloat(values[i])>0.5){
sum += values[i];
num += 1;
}
}
return { number_of_documents : num,
sum_of_document_completions : sum,
average_completion : (sum/num) };
}'
);
$query = array (
"folder" => "work"
);
$doctypes = $db->command(
array(
'mapReduce' => 'mydocuments',
"map" => $map,
"reduce" => $reduce,
"query" => $query,
"out" => array("inline"=>1)
)
);
print_r ( $doctypes);
}
catch(MongoCursorException $e) {
echo "error message: ".$e->getMessage()."\n";
echo "error code: ".$e->getCode()."\n";
}

Adding array elements with a for(reach) loop

(First a bit/lot of context at the bottom is the question)
I am writing an API that returns a planning amongst other things. The response is in JSON and should be as followed:
"Planning":
[
{
"Name": "Overview",
"Dates":
[
{
"Date": "yyyy-mm-dd",
"Division1": "type",
"Division2": "type"
},
{
"Date": "yyyy-mm-dd",
"Division1": "type",
"Division2": "type"
},
...
]
},
{
"Name": "Division1",
"Dates":
[
{
"Date": "yyyy-mm-dd",
"Type": "type",
"Description": "type"
},
...
]
},
...
There is the standard blok "Name": "Overview" this is always returned, and for each division the requester is a part of, a block with "Name":"Divisionname" is added to the response.
The issue I have is that the amount or names of the divisions aren't set in stone. There can be more or less depending on the deployment.
To cover for this I wrote the following code:
<?php
...
$stmt = $conn->prepare("SELECT `idDivision` FROM Division;");
$stmt->execute();
$stmt->bind_result($idDivision);
while($stmt->fetch()){
$Divisions[] = $idDivision;
$$idDivision = array();
}
...
?>
This should create an array for each division with the array name being the id of that division (correct?).
Then I get the planning data from the DB which i store in the multiple arrays that I will later use for the response building:
<?php
$stmt->bind_result($type, $date, $idDivision, $day, $description, $note);
while($stmt->fetch()){
if(checkarray($date, $arr_date) != true){
array_push($arr_date, $date);
array_push($arr_day, $day);
}
array_push($$idDivision, $type); //This should push it into the correct array.
}
?>
At the end I want to combine all this into the respons ofcourse (This is where I am lost):
<?php
for($i = 0; $i <= count($arr_date); $i++){
$planning[0]['Dates'][] = array(
"Date" => $arr_date[$i],
// How to add every division with "arrayname" => "$type" here?
);
}
?>
As in the comment above, i don't know how to add a key:value for each division that i found dynamically so that it becomes:
<?php
for($i = 0; $i <= count($arr_date); $i++){
$planning[0]['Dates'][] = array(
"Date" => $arr_date[$i],
"Division1" => $value,
"Division2" => $value,
"Division3" => $value,
// and so on for every division
);
}
?>
Is there a way to do this or should I even go about doing this a different way? I feel like I should/could use the $Divisions array.
Try out this.
for($i = 0; $i <= count($arr_date); $i++){
$planning[0]['Dates'][$i]["Date"] = $arr_date[$i];
for($j=0;$j<count($division);$j++){
$planning[0]['Dates'][$i]["division$j"] = $division[$j];
}
}
?>

Notice: Undefined offset & Trying to get property of non-object

I am trying to pull data from the json_decode array, I have done it before but this time I am getting extra issues.
This is what the json looks like.
{
"achievementpercentages": {
"achievements": [
{
"name": "TF_SCOUT_LONG_DISTANCE_RUNNER",
"percent": 54.668815612792969
},
{
"name": "TF_HEAVY_DAMAGE_TAKEN",
"percent": 47.104038238525391
},
{
"name": "TF_GET_CONSECUTIVEKILLS_NODEATHS",
"percent": 44.668777465820312
},
{
"name": "TF_PYRO_CAMP_POSITION",
"percent": 36.480117797851563
},
{
"name": "TF_KILL_NEMESIS",
"percent": 34.392494201660156
},
{
"name": "TF_BURN_PLAYERSINMINIMUMTIME",
"percent": 33.580135345458984
},
{
"name": "TF_PYRO_BURN_MEDICPAIR",
"percent": 32.748367309570312
},
And my code is basically this
$achName = $decodedAch->achievementpercentages->achievements[$achCounter]->name;
$achPercent = $decodedAch->achievementpercentages->achievements[$achCounter]->percent;
The $achCounter is present in a while statement, in an effort to get all the achievements seperately. I don't think that part is wrong, just how I am trying to access the data in the array. I don't see what is wrong with it though.
Any help would be appreciated.
FULL WHILE STATEMENT AS REQUESTED
while($achCounter < $responseCount)
{
$appid = $decodedSteam->response->games[$achCounter]->appid;
$achievementURL = 'http://api.steampowered.com/ISteamUserStats/GetGlobalAchievementPercentagesForApp/v0002/?gameid='.$appid.'&format=json';
$jsonAch = file_get_contents($achievementURL);
$decodedAch = json_decode($jsonAch);
//var_dump($decodedAch);
$achName = $decodedAch->achievementpercentages->achievements[$achCounter]->name;
$achPercent = $decodedAch->achievementpercentages->achievements[$achCounter]->percent;
//echo $achievementURL;
$sql = "INSERT INTO steamid".$steamid."(name, percent) VALUES ('".$achName."','".$achPercent."')";
$achCounter++;
}
You need to iterate again after you fetch data from your url. Your $achCounter is usefull only for $decodedSteam, not for $decodedAch:
while($achCounter < $responseCount)
{
$appid = $decodedSteam->response->games[$achCounter]->appid;
$achievementURL = 'http://api.steampowered.com/ISteamUserStats/GetGlobalAchievementPercentagesForApp/v0002/?gameid='.$appid.'&format=json';
$jsonAch = file_get_contents($achievementURL);
$decodedAch = json_decode($jsonAch);
//var_dump($decodedAch);
foreach($decodedAch->achievementpercentages->achievements as $achievement) { //HERE
$achName = $achievement->name;
$achPercent = $achievement->percent;
//echo $achievementURL;
$sql = "INSERT INTO steamid".$steamid."(name, percent) VALUES ('".$achName."','".$achPercent."')";
}
$achCounter++;
}

Speeding up PHP Calls

so I have some PHP code which is displaying an email list (Name, Surname & Email) from a list I have hosted via API.
The problem I have is that in order to display the name of the subscriber, I have to separately pass the email through the API... and the only way to get the email is to use a separate function.
So, for a list of 100 people, I'm basically hitting calling their API 300 times... so it is taking forever to echo the information (and over 30ish people the server just times out).
Is there a way I can edit the blow code so that I'm not pulling their info EVERY time, and instead bring it all down at once?
Here's my code:
<?php
require('Mailin.php');
/////////Total Subscriber Count $subscribers
$mailin = new Mailin('https://api.sendinblue.com/v2.0','UNIQUEKEY');
$datacount = array( "id"=>13 );
$subscribercount = $mailin->get_list($datacount);
$subscribers = $subscribercount['data']['total_subscribers'] - 1;
/////////Get Emails of Subscribers
$dataemail = array( "listids" => array(2),
"page" => 1,
"page_limit" => 500
);
$getemails = $mailin->display_list_users($dataemail);
/////////Get Name of Subscribers
foreach (range(0, $subscribers) as $number) {
$subscriberemail = $getemails['data']['data'][$number]['email'];
echo $subscriberemail;
echo '<br/>';
$dataname = array( "email" => $subscriberemail );
$getname = $mailin->get_user($dataname);
$subscribername = $getname['data']['attributes']['NAME'];
$subscribersurname = $getname['data']['attributes']['SURNAME'];
echo $subscribername;
echo '<br/>';
echo $subscribersurname;
echo '<br/>';
}
?>
Here is the output you get calling the display_list_users:
{
"code":"success",
"message":"Retrieved details of all users for the given lists",
"data":{
"data":[
{
"blacklisted":0,
"email":"email1#domain.com",
"id":1,
"listid":[1],
"blacklisted_sms":1,
"last_modified" : "2015-05-22 15:30:00"
},
{
"blacklisted":1,
"email":"email2#domain.com",
"id":2,
"listid":[1,2],
"blacklisted_sms":0 ,
"last_modified" : "2015-05-25 19:10:30"
}
],
"page":1,
"page_limit":2,
"total_list_records":100
}
}
And the output you get calling the get_user:
{
"code":"success",
"message":"Data retrieved for email",
"data":{
"attributes":{
"EMAIL":"example#example.net",
"NAME" : "Name",
"SURNAME" : "surname"
},
"blacklisted":1,
"email":"example#example.net",
"entered":"2014-01-15",
"listid":[8],
"message_sent":[{
"camp_id" : 2,
"event_time" : "2013-12-18"
},
{ "camp_id" : 8,
"event_time" : "2014-01-03"
},
{ "camp_id" : 11,
"event_time" : "2014-01-07"
}],
"hard_bounces":[{
"camp_id" : 11,
"event_time" : "2014-01-07"
}],
"soft_bounces":[],
"spam":[{
"camp_id" : 2,
"event_time" : "2014-01-09"
}],
"unsubscription":{
"user_unsubscribe":[
{
"event_time":"2014-02-06",
"camp_id":2,
"ip":"1.2.3.4"
},
{
"event_time":"2014-03-06",
"camp_id":8,
"ip":"1.2.3.4"
}
],
"admin_unsubscribe":[
{
"event_time":"2014-04-06",
"ip":"5.6.7.8"
},
{
"event_time":"2014-04-16",
"ip":"5.6.7.8"
}
]
},
"opened":[{
"camp_id" : 8,
"event_time" : "2014-01-03",
"ip" : "1.2.3.4"
}],
"clicks":[],
"transactional_attributes":[
{
"ORDER_DATE":"2015-07-01",
"ORDER_PRICE":100000,
"ORDER_ID":"1"
},
{
"ORDER_DATE":"2015-07-05",
"ORDER_PRICE":500000,
"ORDER_ID":"2"
}
],
"blacklisted_sms":1
}
}

Categories