How to fetch past 30 days data from dynamo db - amazon-dynamodb

I want to fetch the past 30 days data from dynamo db. I can only able to fetch only one id at a time. How can i get all the data of 30 days from dynamo db.
$sevenDaysAgo = date('Y-m-d H:i:s', strtotime('30 days'));
echo $response = $dynamodb->query([ 'TableName' => 'notifications',
'KeyConditionExpression' => 'id = :id and date_time >= :datess', 'ExpressionAttributeValues' => [ ':id' => ['S' => '350'],
':datess' => ['S' => $sevenDaysAgo] ],
'ProjectionExpression' => 'id', 'ConsistentRead' => true ]);

You could use the new TTL feature, Streams, and Lambda to maintain a sliding 30 day window of data in a new table called sliding. Steps:
Create a new table called sliding with the same schema as your base table. This table should have TTL enabled on an attribute named myttl.
Enable your original table's DynamoDB Stream with both old and new images.
Attach a lambda function to your original table's Stream. This lambda would write all item creations/updates/deletions from the base table to the sliding table.
It seems like each item might already contain a timestamp. Scan your original table and add a N attribute called myttl equal to timestamp+30 days in epoch time, if the item's timestamp was in the last 30 days.
The result of steps 1-4 will be a table sliding that contains an eventually consistent view of the last 30 days worth of data.

Related

Nette - database INSERT - number of affected rows

I would like to find out the number of affected (inserted) rows after inserting into the table. I didn't figure out how to do it in the documentation. The update returns the number of affected rows. The insert returns Nette\Database\Table\ActiveRow
How do I get it?
$affected = $context->table('author')->insert([
[
'name' => 'Sansa Stark',
'born' => null
], [
'name' => 'Arya Stark',
'born' => null
]
]);
bdump($affected); // Nette\Database\Table\ActiveRow - I need the number of inserted records
Nette Database Explorer doesn't return count after insert(). It is not useful information as long as you can count data before insert by yourself.
$data = [...];
$count = count($data);
$context->table('author')->insert($data);
It works only with update and delete as is mentioned in documentation.
$count = $context->table('author')
->where('id', 10)
->delete();
It might be possible with getRowCount() over query in Nette Database Core
Nette Database Core is built upon PDO. Alas, the authors tend to create their own objects instead of extending PDO, which makes such elementary operations tedious:
// get Nette ResultSet object
$resultSet = $this->database->query("INSERT/UPDATE/DELETE...");
// get original PDOStatement object
$pdoStatement = $resultSet->getPdoStatement();
// get the affected rows from PDO object (instead of $resultSet->rowCount())
$pdoStatement->rowCount();
A word of warning for those considering Nette for production: There is no real documentation, only cook books and autogenerated PHPDoc which just prints names without any explanation.

How to create trigger based on time period it has to do sum function

I want to create a trigger in oracle 11g - every 5 minutes it has to sum the values of price column in order table. How would i ?
The correct way to do this would be to have a materialized view which calculates the total.
create table t23 (id number primary key , qty number);
create materialized view log on t23;
create materialized view t23_total
refresh force
start with sysdate + 5/(60*24)
next sysdate + 5/(60*24)
as
select sum(qty) as total_qty
from t23
/
This is the most efficient way to main a total. This kicks off a refresh every five minutes. If you wanted to maintain the total in real time you could just use
create materialized view t23_total
refresh on commit
as
select sum(qty) as total_qty
from t23
/
You may create a scheduler job to run every 5 minutes.
declare
v_anon_block varchar2(500) :=
q'{begin
insert into mylog(log_date,total_price) select SYSDATE, SUM(price)
from order;
commit;
end;}';
begin
dbms_scheduler.create_job (
job_name => 'SUM_PRICE_JOB',
job_type => 'PLSQL_BLOCK',
job_action => v_anon_block,
start_date => SYSDATE,
repeat_interval => 'FREQ=MINUTELY;INTERVAL=5;',
enabled => TRUE,
comments => '5 minute Refresh');
end;
/
Why do you want to work with "old" data? I mean, what's the purpose in calculating that value every 5 minutes? Have it available in time, live, per request. Create a view or a function that does that, such as
create or replace view v_sum_price as
select sum(price)
from orders_table;
or
create or replace function f_sum_price
return number
as
retval number;
begin
select sum(price)
into retval
from orders_table;
return retval;
end;
Both of these most probably lack some additional info (order ID? Item ID? Something else?). Furthermore, I can't see a purpose in summing price value in orders table. How do you use it? For example, for a restaurant: ORDERS table contains the following prices:
ITEM PRICE QUANTITY
Coke 1.5 2
Burger 4.0 2
Wine 2.3 1
Salad 0.8 1
and now you know that sum of the PRICE column equals 8.6. What useful information does it contain? Absolutely none (or I can't see it).
Maybe it would help if you described what problem you're trying to solve.

what is %d used for in arrays on wordpress?

I presume it means double but what does it mean in general?
$wpdb->delete(
"data",
[ 'id' => $id ],
[ '%d' ]
);
I get that I'm deleting from table data using array id => id but what is the %d for?
Why am I asking? well, I'd like to do some bulk updates as shown through the features built in with wp_list_table as shown through this tutorial. As I've looked around for that first link I found a few more sources on how to fix my table - I'm just trying do multiple operations and I'm stuck with bulkupdater
its to identify the type of data to be deleted think where=%d which means interger
others include:
%d - interger (just to make it clear)
%f - float
%s - string
so your code
$wpdb->delete(
"data",
[ 'id' => $id ],
[ '%d' ]
);
Is only going to delete an id where the id string is an integer e.g. 43 not if a43
You can have obviously a few types in an array, but they need to match the order of the data array (i.e. array('id'=>$id, 'numval'=>$num) ,array(integer, integer) )

Query with date range

I have the following query;
foreach (Item sourceChild in source.Axes.GetDescendants()
.OrderBy(x => x["date-optional-1"])
.ThenBy(x => x["date-optional-2"])
.Reverse())
{..}
date-optional-1 and 2 are, as the name states, optional, and therefore not guaranteed to be filed.
But if they are, no 1 takes precedence over 2.
I need to add, that if no 1 is filed, then I only need the items from today and forward (it is an event date). How would i go about this in a Where()?
foreach (Item sourceChild in source.Axes.GetDescendants()
.Where(x => DateUtil.ParseDateTime(x["date-optional-1"], DateTime.MaxValue) >= DateTime.UtcNow.Date)
.OrderBy(x => x["date-optional-1"])
.ThenBy(x => x["date-optional-2"])
.Reverse())
{..}
DateUtil.ParseDateTime converts the string value of the 'date-optional-1' field into a DateTime.
Sitecore stores dates in UTC so we compare that DateTime value with DateTime.UtcNow.Date. This includes only items with a 'date-optional-1' value >= today in the result set.
DateTime.MaxValue is passed as the second parameter to DateUtil.ParseDateTime so that items with no 'date-optional-1' value are included in the result set.

How to query the number of view counts for a post in Wordpress JetPack?

I use JetPack stats to follow the stats for my blog. I would like to extract the top 10 most-viewed posts for a given period (e.g. last month).
I used the WordPress stats plugin API before which worked nicely, but after upgrade to JetPack this doesn't work anymore.
Is there an API which allows me to query the number of view counts for a post?
Inside the Database
This option is recorded in the database and is used by the Dashboard widget:
get_option( 'stats_cache' );
It returns an array like this:
array(
['7375996b7a989f95a6ed03ca7c899b1f'] => array(
[1353440532] => array(
[0] => array(
['post_id'] => 0
['post_title'] => 'Home page'
['post_permalink'] => 'http://www.example.com/'
['views'] => 1132
)
[1] => array(
['post_id'] => 4784
['post_title'] => 'Hello World!'
['post_permalink'] =>
['views'] => 493
)
/* till item [9] */
Querying WordPress.com
It is possible to call the following Jetpack function:
if( function_exists( 'stats_get_csv' ) ) {
$top_posts = stats_get_csv( 'postviews', 'period=month&limit=30' );
}
Which returns:
array(
[0] => array(
['post_id'] => 0
['post_title'] => 'Home page'
['post_permalink'] => 'http://www.example.com/'
['views'] => 6806
)
[1] => array(
['post_id'] => 8005
['post_title'] => 'Hello World!'
['post_permalink'] =>
['views'] => 1845
)
/* till item [29] */
Function get_stats_csv
/plugins/jetpack/modules/stats.php
The function get_stats_csv calls http://stats.wordpress.com/csv.php. If we visit this address, we get this response:
Error: api_key is a required parameter.
Required parameters: api_key, blog_id or blog_uri.
Optional parameters: table, post_id, end, days, limit, summarize.
Parameters:
api_key String A secret unique to your WordPress.com user account.
blog_id Integer The number that identifies your blog.
Find it in other stats URLs.
blog_uri String The full URL to the root directory of your blog.
Including the full path.
table String One of views, postviews, referrers, referrers_grouped,
searchterms, clicks, videoplays.
post_id Integer For use with postviews table.
end String The last day of the desired time frame.
Format is 'Y-m-d' (e.g. 2007-05-01)
and default is UTC date.
days Integer The length of the desired time frame.
Default is 30. "-1" means unlimited.
period String For use with views table and the 'days' parameter.
The desired time period grouping. 'week' or 'month'
Use 'days' as the number of results to return
(e.g. '&period=week&days=12' to return 12 weeks)
limit Integer The maximum number of records to return.
Default is 100. "-1" means unlimited.
If days is -1, limit is capped at 500.
summarize Flag If present, summarizes all matching records.
format String The format the data is returned in,
'csv', 'xml' or 'json'.
Default is 'csv'.
Non-working query example:
?api_key=123456789abc&blog_id=155&table=referrers&days=30&limit=-1&summarize
Result format is csv with one row per line and column names in first row.
Strings containing double quotes, commas, or "\n" are enclosed in double-quotes.
Double-qoutes in strings are escaped by inserting another double-quote.
Example: "pet food" recipe
Becomes: """pet food"" recipe"
Developers, please cache the results for at least 180 seconds.

Resources