Why django-viewflow internal table 'viewflow_task_previous' goes backwards? - django-viewflow

Given the following steps defined
And when I dig into the internal tables, especially viewflow_task_previous table, it seems the from and to are reversed?
pmas=> select * from viewflow_task_previous where from_task_id = 10248;
id | from_task_id | to_task_id
------+--------------+------------
9099 | 10248 | 10247
(1 row)
pmas=> select id, status, flow_task, status from viewflow_task where id = 10248;
id | status | flow_task | status
-------+----------+-------------------------------------------------------------------------+----------
10248 | ASSIGNED | connect_it/flows.new_circuit.flow.NewCircuit.external_task_installation | ASSIGNED
(1 row)
pmas=> select id, status, flow_task, status from viewflow_task where id = 10247;
id | status | flow_task | status
-------+--------+-------------------------------------------------------------------------+--------
10247 | DONE | connect_it/flows.new_circuit.flow.NewCircuit.external_task_provisioning | DONE
(1 row)
Could someone explain why and how this works?

viewflow_task_previous table created by models.ManyToManyField previous field of the Task model
https://github.com/viewflow/viewflow/blob/master/viewflow/models.py#L97
Yep, that gives some confusion on the SQL level

Related

Inconsistent results when “using timestamp” on YugabyteDB YCQL

[Question posted by a user on YugabyteDB Community Slack]
I'm trying to upsert, delete and upsert the same record using using timestamp syntax. The first upsert and delete are successful. After I delete the record, if I upsert the same record again, the update status is true, but the select statement is not showing the row.
ycqlsh:test> CREATE TABLE todo ( id int, seq int, task text, status boolean, primary key (id, seq) );
ycqlsh:test> insert into todo(id, seq, task, status) values(1, 1, 'sample', false);
ycqlsh:test> insert into todo(id, seq, task, status) values(1, 2, 'sample2', false);
ycqlsh:test> select * from todo;
id | seq | task | status
----+-----+---------+--------
1 | 1 | sample | False
1 | 2 | sample2 | False
(2 rows)
ycqlsh:test> UPDATE todo using timestamp 1000 SET status = false, task='sample3' WHERE id=1 and seq=3 returns status as row;
[applied] | [message] | id | seq | task | status
-----------+-----------+------+------+------+--------
True | null | null | null | null | null
ycqlsh:test> select * from todo;
id | seq | task | status
----+-----+---------+--------
1 | 1 | sample | False
1 | 2 | sample2 | False
1 | 3 | sample3 | False
(3 rows)
ycqlsh:test> delete from todo WHERE id=1 and seq=3;
ycqlsh:test> select * from todo;
id | seq | task | status
----+-----+---------+--------
1 | 1 | sample | False
1 | 2 | sample2 | False
(2 rows)
ycqlsh:test> UPDATE todo using timestamp 2000 SET status = false, task='sample3' WHERE id=1 and seq=3 returns status as row;
[applied] | [message] | id | seq | task | status
-----------+-----------+------+------+------+--------
True | null | null | null | null | null
ycqlsh:test> select * from todo;
id | seq | task | status
----+-----+---------+--------
1 | 1 | sample | False
1 | 2 | sample2 | False
This is because you use DELETE without USING. To reintroduce the row back into the table, you have to use UPDATE without USING TIMESTAMP.
The correct usage should have been:
delete from todo USING TIMESTAMP 1000 id=1 and seq=3;
If you want DELETE without the use of USING timestamp... then you should use timestamp values that are actually close to physical time in microseconds since epoch rather than numbers like 1000 or 2000.

Cassandra - select query with token() function

According to this documentation, I was trying a select query with token() function in it, but it gives wrong results.
I am using below cassandra version
[cqlsh 5.0.1 | Cassandra 2.2.5 | CQL spec 3.3.1 | Native protocol v4]
I was trying token query for below table -
CREATE TABLE price_key_test (
objectid int,
createdOn bigint,
price int,
foo text,
PRIMARY KEY ((objectid, createdOn), price));
Inserted data --
insert into nasa.price_key_test (objectid,createdOn,price,foo) values (1,1000,100,'x');
insert into nasa.price_key_test (objectid,createdOn,price,foo) values (1,2000,200,'x');
insert into nasa.price_key_test (objectid,createdOn,price,foo) values (1,3000,300,'x');
Data in table --
objectid | createdon | price | foo
----------+-----------+-------+-----
1 | 3000 | 300 | x
1 | 2000 | 200 | x
1 | 1000 | 100 | x
Select query is --
select * from nasa.price_key_test where token(objectid,createdOn) > token(1,1000) and token(objectid,createdOn) < token(1,3000)
This query suppose to return row with createdOn 2000, but it returns zero rows.
objectid | createdon | price | foo
----------+-----------+-------+-----
(0 rows)
According to my understanding, token(objectid,createdOn) > token(1,1000) and token(objectid,createdOn) < token(1,3000) should select row with partition key with value 1 and 2000.
Is my understanding correct?
Try flipping your greater/less-than signs around:
aploetz#cqlsh:stackoverflow> SELECT * FROM price_key_test
WHERE token(objectid,createdOn) < token(1,1000)
AND token(objectid,createdOn) > token(1,3000) ;
objectid | createdon | price | foo
----------+-----------+-------+-----
1 | 2000 | 200 | x
(1 rows)
Adding the token() function to your SELECT should help you to understand why:
aploetz#cqlsh:stackoverflow> SELECT objectid, createdon, token(objectid,createdon),
price, foo FROM price_key_test ;
objectid | createdon | system.token(objectid, createdon) | price | foo
----------+-----------+-----------------------------------+-------+-----
1 | 3000 | -8449493444802114536 | 300 | x
1 | 2000 | -2885017981309686341 | 200 | x
1 | 1000 | -1219246892563628877 | 100 | x
(3 rows)
The hashed token values generated are not necessarily proportional to their original numeric values. In your case, token(1,3000) generated a hash that was the smallest of the three, and not the largest.

retrieving data from cassandra database

I'm working on smart parking data stored in Cassandra database and i'm trying to get the last status of each device.
I'm working on self-made dataset.
here's the description of the table.
table description
select * from parking.meters
need help please !
trying to get the last status of each device
In Cassandra, you need to design your tables according to your query patterns. Building a table, filling it with data, and then trying to fulfill a query requirement is a very backward approach. The point, is that if you really need to satisfy that query, then your table should have been designed to serve that query from the beginning.
That being said, there may still be a way to make this work. You haven't mentioned which version of Cassandra you are using, but if you are on 3.6+, you can use the PER PARTITION LIMIT clause on your SELECT.
If I build your table structure and INSERT some of your rows:
aploetz#cqlsh:stackoverflow> SELECT * FROM meters ;
parking_id | device_id | date | status
------------+-----------+----------------------+--------
1 | 20 | 2017-01-12T12:14:58Z | False
1 | 20 | 2017-01-10T09:11:51Z | True
1 | 20 | 2017-01-01T13:51:50Z | False
1 | 7 | 2017-01-13T01:20:02Z | False
1 | 7 | 2016-12-02T16:50:04Z | True
1 | 7 | 2016-11-24T23:38:31Z | False
1 | 19 | 2016-12-14T11:36:26Z | True
1 | 19 | 2016-11-22T15:15:23Z | False
(8 rows)
And I consider your PRIMARY KEY and CLUSTERING ORDER definitions:
PRIMARY KEY ((parking_id, device_id), date, status)
) WITH CLUSTERING ORDER BY (date DESC, status ASC);
You are at least clustering by date (which should be an actual date type, not a text), so that will order your rows in a way that helps you here:
aploetz#cqlsh:stackoverflow> SELECT * FROM meters PER PARTITION LIMIT 1;
parking_id | device_id | date | status
------------+-----------+----------------------+--------
1 | 20 | 2017-01-12T12:14:58Z | False
1 | 7 | 2017-01-13T01:20:02Z | False
1 | 19 | 2016-12-14T11:36:26Z | True
(3 rows)

how can we page results from a select query in cassandra

I have a cassandra table that has more than 2 million rows. I need to fetch my results and page them.
how can I page my results from the select query.
I am getting rpc time out when i try to retrieve 1M rows.
From the cqlsh command prompt, one way to do this is by restricting your hashed partition key values, via the token function. Let's say that I have a table that keeps track of ship crew members (with crewname as my partition key):
aploetz#cqlsh:presentation> SELECT crewname,token(crewname),firstname,lastname
FROM crew;
crewname | token(crewname) | firstname | lastname
----------+----------------------+-----------+-----------
Simon | -8694467316808994943 | Simon | Tam
Jayne | -3415298744707363779 | Jayne | Cobb
Wash | 596395343680995623 | Hoban | Washburne
Mal | 4016264465811926804 | Malcolm | Reynolds
Zoey | 7853923060445977899 | Zoey | Washburne
Sheppard | 8386579365973272775 | Derial | Book
(6 rows)
If I just want to bring back the all the crew members from Jayne to Zoey (inclusive) I can run a query like this:
aploetz#cqlsh:presentation> SELECT crewname,token(crewname),firstname,lastname
FROM crew WHERE token(crewname) >= token('Jayne') AND token(crewname) <= token('Zoey');
crewname | token(crewname) | firstname | lastname
----------+----------------------+-----------+-----------
Jayne | -3415298744707363779 | Jayne | Cobb
Wash | 596395343680995623 | Hoban | Washburne
Mal | 4016264465811926804 | Malcolm | Reynolds
Zoey | 7853923060445977899 | Zoey | Washburne
(4 rows)
You should be able to do something similar with your partition key values as well.
Otherwise, you could probably accomplish this using one of the drivers. In her article Things You Should Be Doing When Using Cassandra Drivers DataStax's Rebecca Mills describes how to page through large result sets using setFetchSize (her example is below):
Statement stmt = new SimpleStatement("select * FROM raw_weather_data WHERE wsid= '725474:99999' AND year = 2005 AND month = 6");
stmt.setFetchSize(24);
ResultSet rs = session.execute(stmt);
Iterator<Row> iter = rs.iterator();
while (!rs.isFullyFetched()) {
rs.fetchMoreResults();
Row row = iter.next();
System.out.println(row);
}

Pivot Chart Table in Excel To Calculate the Count and Display the Chart

Here is my Sample Data
****Date | Name | Result****
01-09-14 | John | Fail
01-09-14 | John | PASS
01-09-14 | Raja | Pending
01-09-14 | Raja | Pending
01-09-14 | Natraj | No Response
01-09-14 | Natraj | PASS
02-09-14 | John | PASS
02-09-14 | John | No Response
02-09-14 | Raja | Fail
02-09-14 | Raja | Pending
02-09-14 | Natraj | No Response
02-09-14 | Natraj | PASS
02-09-14 | Natraj | Fail
02-09-14 | Natraj | Fail
Where i need to create a pivot chart for table like this where i need to count the Number of Result for a particular date and for particular Name
Example:
the chart should produce result something like this
Date| Name | Pass | Fail | Pending | No Response
01-09-14| John | 1 | 1 | 0 | 0
01-09-14| Raja | 0 | 0 | 2 | 0
-------------------------------------------
Where i tried of adding an another sheet and table for counting seperately for pass, fail, pending... values and from there i created a pivot chart, but the actual table extends further
For example new user can add, new date will be added in future, as like that chart also have to be responsive of adding new user and new dates , so i failed in that point,
so is there any way to make the pivot chart to be responsive though new user or new date is added, the chart has to built automatically and to show the count result of particular date and particular name
It should look something like this:

Resources