Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add forked to indicator to query #37

Closed
wants to merge 2 commits into from
Closed
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
10 changes: 9 additions & 1 deletion rd_service/data/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ class Query(models.Model):
ttl = models.IntegerField()
user = models.CharField(max_length=360)
created_at = models.DateTimeField(auto_now_add=True)
parent = models.ForeignKey('self', related_name='forks')

class Meta:
app_label = 'redash'
db_table = 'queries'

def to_dict(self, with_result=True):
def to_dict(self, with_result=True, with_forks=True):
d = {
'id': self.id,
'latest_query_data_id': self.latest_query_data_id,
Expand All @@ -59,11 +60,18 @@ def to_dict(self, with_result=True):
'ttl': self.ttl,
'user': self.user,
'created_at': self.created_at,
'parent_id': self.parent_id
}

if with_result and self.latest_query_data_id:
d['latest_query_data'] = self.latest_query_data.to_dict()

if self.parent_id:
d['parent_name'] = self.parent.name

if with_forks:
d['forks'] = [{'id': q.id, 'name': q.name} for q in self.forks.all()]

return d

def save(self, *args, **kwargs):
Expand Down
5 changes: 3 additions & 2 deletions rd_service/data/tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ CREATE TABLE "queries" (
"query_hash" varchar(32) NOT NULL,
"ttl" integer NOT NULL,
"user" varchar(360) NOT NULL,
"created_at" timestamp with time zone NOT NULL
"created_at" timestamp with time zone NOT NULL,
"parent_id" integer REFERENCES "queries" ("id") DEFERRABLE INITIALLY DEFERRED
)
;
CREATE TABLE "dashboards" (
Expand All @@ -42,4 +43,4 @@ CREATE INDEX "queries_latest_query_data_id" ON "queries" ("latest_query_data_id"
CREATE INDEX "widgets_query_id" ON "widgets" ("query_id");
CREATE INDEX "widgets_dashboard_id" ON "widgets" ("dashboard_id");

COMMIT;
COMMIT;
4 changes: 4 additions & 0 deletions rd_service/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,15 @@ def post(self, id=None):
query_def['created_at'] = dateutil.parser.parse(query_def['created_at'])

query_def.pop('latest_query_data', None)
query_def.pop('forks', None)
query_def.pop('parent_name', None)

if id:
query = data.models.Query(**query_def)
fields = query_def.keys()
# The following are only set on creation, make sure they don't change by mistake.
fields.remove('id')
fields.remove('parent_id')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you remove it, then it will never get saved, no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind. Got it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If something isn't clear on first read, then it should be commented.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's only unclear without context (the rest of the code). When seen in context it's ok.

query.save(update_fields=fields)
else:
query_def['user'] = self.current_user
Expand Down
1 change: 1 addition & 0 deletions rd_ui/app/scripts/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
$scope.duplicateQuery = function () {
var oldId = $scope.query.id;
$scope.query.id = null;
$scope.query.parent_id = oldId;
$scope.query.ttl = -1;

$scope.saveQuery(true, oldId);
Expand Down
13 changes: 12 additions & 1 deletion rd_ui/app/views/queryfiddle.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ <h3 class="panel-title">
<span ng-show="queryResult.getStatus() == 'done'">Rows: {{queryResult.getData().length}} | </span>
Created by: {{query.user}}
<div class="pull-right">Refresh query: <select ng-model="query.ttl" ng-options="c.value as c.name for c in refreshOptions"></select><br></div>
<div ng-show="query.parent_id">
Duplicated from: <a ng-href="/queries/{{query.parent_id}}">{{query.parent_name}}</a>
</div>

<div ng-show="query.forks.length">
Duplicated to:
<span ng-repeat="fork in query.forks">
<a ng-href="/queries/{{fork.id}}">{{fork.name}}</a> |
</span>
</div>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume you already have examples for this, can you attach a screenshot of how it looks?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rd-forked

I'd like to do it in a graph (just like Github), but that's for a later update.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(of course, the duplicated from and duplicated to lines only show when they are needed)


</div>
</div>

Expand Down Expand Up @@ -58,4 +69,4 @@ <h3 class="panel-title">
<cohort-renderer query-result="queryResult"></cohort-renderer>
</div>
</div>
</div>
</div>