PHP-RRD Wasn't Work - php

I am trying to create rrd graph by php5-rrd libraires.
I am trying to draw (LASTHOUR,LASTDAY,LASTWEEK,LASTMONTH) graphs. but not work well, (maybe my RRA settings are missing). i try to use "rrd_update" function. but doesn't work. so try to use "rrd_fetch" function. but all data appear on -NaN.
This is my console log.
1426322479:6249:1817
Array
(
[start] => 1426318800
[end] => 1426322700
[step] => 300
[data] => Array
(
[Users] => Array
(
[1426319100] => NAN
[1426319400] => NAN
[1426319700] => NAN
[1426320000] => NAN
[1426320300] => NAN
[1426320600] => NAN
[1426320900] => NAN
[1426321200] => NAN
[1426321500] => NAN
[1426321800] => NAN
[1426322100] => NAN
[1426322400] => NAN
[1426322700] => NAN
)
[Games] => Array
(
[1426319100] => NAN
[1426319400] => NAN
[1426319700] => NAN
[1426320000] => NAN
[1426320300] => NAN
[1426320600] => NAN
[1426320900] => NAN
[1426321200] => NAN
[1426321500] => NAN
[1426321800] => NAN
[1426322100] => NAN
[1426322400] => NAN
[1426322700] => NAN
)
)
)
This is my PHP Code
$rrdFile = dirname(__FILE__) . "/speed.rrd";
//create rrd file
rrd_create($rrdFile,
array(
"DS:Users:GAUGE:600:0:U",
"DS:Games:GAUGE:600:0:U",
"RRA:AVERAGE:0.5:1:1440",
"RRA:AVERAGE:0.5:5:1440",
"RRA:AVERAGE:0.5:30:800",
"RRA:AVERAGE:0.5:120:800",
"RRA:AVERAGE:0.5:1440:80"
)
);
//update rrd file
if(!(rrd_update($rrdFile,
array(
"N:".rand(0,9999).":".rand(0,9999)
)
))) {
$err = rrd_error();
echo "rrd_update() ERROR: $err\n";
}
// TEST CODE
$result = rrd_fetch( $rrdFile, array( "AVERAGE", "--resolution", "100", "--start", "-1h", "--end", "start+1h" ) );
print_r($result);
//graph output
if(!is_array(rrd_graph(dirname(__FILE__) . "/speed.png",
array(
"--start", "-1h",
"--title", "Status",
"--vertical-label", "Users & Games",
"--width", "600",
"--height", "200",
"DEF:Users=$rrdFile:Users:AVERAGE",
"DEF:Games=$rrdFile:Games:AVERAGE",
"CDEF:cUsers=Users",
"CDEF:cGames=Games",
"LINE:cUsers#FF0000",
"LINE:cGames#00FF00"
)
))) {
$err = rrd_error();
echo "rrd_graph() ERROR: $err\n";
}

You do not have enough sample data, hence your graph and RRAs are empty.
Your graph is set to graph the last 1 hour; since you have the default interval (5min) this means only 12 samples from the highest-resolution RRA.
However, you don't have any data being put in! You only call rrd_update once, with a timestamp of 'now', and this is insufficient to fill even one RRA entry (you will need at least two samples, with a 5min time separation, to completely fill an RRA entry).
If you wish to generate some test data, start with a timestamp 3600 seconds in the past, and add 12 samples, increasing the timestamp by 300 each iteration. This will result in 11 entries in your primary (5-minute) RRA, and maybe 2 in your secondary 25-minute RRA.

Related

PHP Parsing ELA Temperature Sensor raw data

I have successfully converted the positive temperature data from below raw data
array (
'timestamp' => '2020-06-11T11:09:21.335Z',
'type' => 'Unknown',
'mac' => 'F64BB46181EF',
'bleName' => 'P RHT 900350',
'rssi' => -63,
'rawData' => '02010605166E2AC90A04166F2A240D09502052485420393030333530',
)
$cutdata = str_split($rawData,2);
$humidity_cut = hexdec($cutdata[13]);
$x_cut = $cutdata[8].$cutdata[7]; //gives 0AC9
$c_cut = hexdec($x_cut);
$temp_cut = $c_cut/100;
echo $temp_cut;exit;
But when i am getting negative temperature values it giving me issues it increase the temp value more then 600
Here is the negative Temp Raw Data
array (
'timestamp' => '2020-07-03T10:05:53.049Z',
'type' => 'Unknown',
'mac' => 'EDF2F589DCAE',
'bleName' => 'P RHT 900351',
'rssi' => -79,
'rawData' => '02010605166E2AB4FA04166F2A310D09502052485420393030333531',
)
I have asked the support team they said
You have to do a 2 complement, which is reversing all the bits, and add 1 in binary.
I'm assuming that the output is OK, but for a 32 bit number, this code checks if the high bit is set (using & 32768) and if it is, it xors the number with 65535 (all 16 bits set) to invert it and then just adds 1 (the result is then made a -ve number)...
if ( $c_cut & 32768 ) {
$c_cut = -(($c_cut ^ 65535)+1);
}
which gives -13.56 as the result.

Extracting a pack size from a column with mixed formats

I have a column pack_size in a table called product_master_test. The problem that I am facing is that the pack_size is in mixed formats, there is no uniformity to it.
For example:
4 x 2kg (pack size should be 4)
48-43GM (pack size should be 48)
12 x 1BTL (pack size should be 12)
1 x 24EA (pack size should be 24)
I've been thinking about different approaches, but I can't think of anything that would work without having a lot of IF statements in the query/PHP code. Is there a solution that I am missing?
I do have the file in Excel, if there is an easier way to process it using PHP.
I am not including any code, as I'm not entirely sure where to start with this problem.
Using a regex to split the pack size could at least give you the various components which you can then (possibly) infer more from...
$packs = ["4 x 2kg","48-43GM","12 x 1BTL","1 x 24EA", "12 X 1 EA"];
foreach ( $packs as $size ) {
if ( preg_match("/(\d*)(?:\s+)?[xX-](?:\s+)?(\d+)(?:\s+)?(\w*)/", $size, $match) == 1 ) {
print_r($match);
}
else {
echo "cannot determine - ".$size.PHP_EOL;
}
}
(regex can probably be optimised, not my area of expertise). It basically splits it to be a number, some space with either a x or a - and then another number followed by the units (some text). The above with the test cases gives...
Array
(
[0] => 4 x 2kg
[1] => 4
[2] => 2
[3] => kg
)
Array
(
[0] => 48-43GM
[1] => 48
[2] => 43
[3] => GM
)
Array
(
[0] => 12 x 1BTL
[1] => 12
[2] => 1
[3] => BTL
)
Array
(
[0] => 1 x 24EA
[1] => 1
[2] => 24
[3] => EA
)
Array
(
[0] => 12 X 1 EA
[1] => 12
[2] => 1
[3] => EA
)
With the else part it should also give you the ones it cannot determine and perhaps allow you to change it accordingly.
You could present an associative array of all the strings from the table as keys corresponding with correct pack_size you desire.
$packsize = ["4 x 2kg" => 4, "48-43GM" => 48, "12 x 1BTL" => 12, "1 x 24EA" => 24]; //add all pack_sizes here
echo $packsize["4 x 2kg"]; // Output: 4
Now you could get the acutal pack size via the key of associative array. It could save some time you would spend making if/else conditions or switching the input. I'm not sure if there is something wrong with this approach, so correct me if so.

How to convert this date format to PHP

From the JSON snippet that i got from a .net API query, I can't seem to convert the date /Date(1393477200000)/ properly in PHP.
I tried to do echo date('m/n/Y','1393477200000'); but it is still outputting the wrong date which is 07/7/46127 instead of the correct date of 2/27/2014.
Array
(
[status] => ok
[results] => Array
(
[0] => Array
(
[PROJECT_ID] => 1
[COMPANY_ID] => 1
[PROJECT_NAME] => The "Getting Started" Project
[PROJECT_NUMBER] => 000001
[CAN_OPEN_PROJECT] => 1
[DATE_START_DATE] => /Date(1393477200000)/
[DATE_END_DATE] => /Date(1440648000000)/
[PROJECT_DESC] =>
[TASK_NUMBER] => 6
[DATE_CREATED] => /Date(1409142925980)/
[TOTAL_TASKS] => 1
[TOTAL_INCOMPLETE_TASKS] => 1
[TOTAL_COMPLETED_TASKS] => 0
Any ideas how to format [DATE_START_DATE] correctly in PHP? Thanks!
Divide your unix-timestamp by 1000, then use date(..).
$date = '1393477200000';
echo date("m/d/y", $date/1000);
Its that simple.
Result:
02/27/14
echo date('m/d/Y',(1393477200000/1000)); U have to truncate last 3 digit.
The time received is in milliseconds... You have to divide by a thousand to retrieve UNIX epoch time:
echo date('m/d/Y',(1393477200000/1000));
And here is the fiddle

Replace array element with last non zero value

I have a result array like the following :
Array
(
[2013-02-27] => Array
(
[CPD] => 0
[Display] => 0
[Incent] => 0
[Organic] => 1464
[uncategorized] => 0
[total] => 0
)
[2013-02-26] => Array
(
[CPD] => 0
[Display] => 0
[Incent] => 0
[Organic] => 1463
[uncategorized] => 0
[total] => 0
)
[2013-02-25] => Array
(
[CPD] => 0
[Display] => 0
[Incent] => 0
[Organic] => 1459
[uncategorized] => 0
[total] => 0
)
[2013-02-24] => Array
(
[CPD] => 0
[Display] => 2
[Incent] => 0
[Organic] => 1449
[uncategorized] => 0
[total] => 0
)
This is basically a running total thing and I need to have the zero values replaced by the higher value if there is any. I mean I have 2 as the count for 2013-02-24 for Display. So I need to have this has to be the count for other dates for display category. For making things a little more clear, this is the number of downloads for a product and I am calculating the count upto each day from various sources. So if it has a count of 2 upto 24th, it has to be the same upto the coming days if there the downloads for the day is zero. If downloads happen again it will added up. So what I am doing is I am looping the array and checking for zero values. If the value is zero for a particular section say Display, it has to be replaced with the higher number from the previous date if there is any. So how can I do this ?
I have used the following code :
foreach ($result as $key=>$value){
foreach($categories as $cat){
if($value[$cat->category]==0){
$prev_day = date("Y-m-d",strtotime ( '-1 day' , strtotime ( $key ) )) ;
while ($prev_day>=$from) {
if($result[$prev_day][$cat->category]!=0){
$prev_high_value[$cat->category]=$result[$prev_day][$cat->category];
break;
}
$prev_day = strtotime ( '-1 day' , strtotime ( $prev_day ) ) ;
}
$result[$key][$cat->category]=$prev_high_value[$cat->category];
}
}
}
where $category is an object with the category values like Display an others

Get timestamp from base64Binary in PHP

A webservice returns a timestamp field in base64Binary format. It looks like this in SOAP response:
<a:TimeStamp>AAAAAAMpI9Q=</a:TimeStamp>
PHP __soapCall, however, b64_decode()s that and I get a binary string looking like ')#▒'. How do I get actual timestamp out of this? I tried to unpack('L') it but it gives me Array([1] => 0) as a result. Is there really zero i.e. 1970-01-01 or have I missed something?
This test program:
$b = "AAAAAAMpI9Q=";
$ts = base64_decode($b);
print_r(array_map("ord", str_split($ts)));
outputs:
Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 3
[5] => 41
[6] => 35
[7] => 212
)
showing that the base64-encoded string gives you an 8-character string when unpacked. So presumably it represents a 64-bit integer, which might be signed or unsigned, and no, it isn't zero.
Given the values above it looks like the value is 53027796 - is that what you're expecting?

Categories