Last active
March 26, 2025 18:53
-
-
Save ariunbayar/161cce3aa118edf9f4ac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django.conf import settings | |
| from django.db import connection, transaction | |
| cursor = connection.cursor() | |
| def execute_sql(sql, show_query=False, disable_fk_checks=False): | |
| if disable_fk_checks: | |
| cursor.execute('SET foreign_key_checks = 0;') | |
| cursor.execute(sql) | |
| res = cursor.fetchall() | |
| cursor.execute('SET foreign_key_checks = 1;') | |
| transaction.commit_unless_managed() | |
| if show_query: | |
| print sql | |
| for row in res: | |
| print row | |
| # Following parameters, args and filters are going to be used for the query generation | |
| query_args = { | |
| 'date_from': '2013-03-01 00:00:00', | |
| 'table_prefix': settings.TABLE_PREFIX + '_', | |
| 'user_filters':""" AND | |
| ( | |
| `vendor_participation` LIKE BINARY '%vistatec=assigned%' | |
| OR `vendor_participation` LIKE BINARY '%vistatec=assigned%' | |
| OR `vendor_participation` LIKE BINARY '%thebigword=assigned%' | |
| OR `vendor_participation` LIKE BINARY '%google=assigned%' | |
| ) | |
| AND `status` IN (2) | |
| AND `subtype` IN (0, 1) | |
| """, | |
| 'user_date_filters': """ | |
| AND `updated_at` > '2013-03-01 00:00:00' | |
| AND `updated_at` < '2013-03-31 23:59:59' | |
| """ | |
| } | |
| def exec_query_with_args(msg, sql, show_sql=False): | |
| print msg | |
| execute_sql(sql % query_args, show_sql) | |
| exec_query_with_args("### Initial created issues for open-new-closed chart ###", | |
| """ | |
| SELECT COUNT(`id`) | |
| FROM `%(table_prefix)stracker_query` | |
| WHERE | |
| `status` IN (1, 0, 2, 3) | |
| AND `created_at` <= '%(date_from)s' | |
| %(user_filters)s | |
| """) | |
| exec_query_with_args("### Initial closed issues for open-new-closed chart ###", | |
| """ | |
| SELECT COUNT(`id`) | |
| FROM `%(table_prefix)stracker_query` | |
| WHERE | |
| `status` IN (2, 3) | |
| AND `updated_at` <= '%(date_from)s' | |
| %(user_filters)s | |
| """) | |
| exec_query_with_args("### Closed issues for open-new-closed chart ###", | |
| """ | |
| SELECT (date(`updated_at`)) AS `grouped_date`, COUNT(`id`) AS `dcount` | |
| FROM `%(table_prefix)stracker_query` | |
| WHERE | |
| `status` IN (2, 3) | |
| %(user_filters)s | |
| %(user_date_filters)s | |
| GROUP BY (date(`updated_at`)) ORDER BY `grouped_date` ASC; | |
| """) | |
| exec_query_with_args("### New issues for open-new-closed chart ###", | |
| """ | |
| SELECT (date(`created_at`)) AS `grouped_date`, COUNT(`id`) AS `dcount` | |
| FROM `%(table_prefix)stracker_query` | |
| WHERE | |
| `status` IN (1, 0, 2, 3) | |
| %(user_filters)s | |
| %(user_date_filters)s | |
| GROUP BY (date(`created_at`)) ORDER BY `grouped_date` ASC | |
| """) | |
| exec_query_with_args("### 0-1 day count for time-to-close chart ###", | |
| """ | |
| SELECT COUNT(`id`) | |
| FROM `%(table_prefix)stracker_query` | |
| WHERE | |
| `status` IN (2, 3) | |
| AND DATEDIFF(`updated_at`, `created_at`) < 1 | |
| %(user_filters)s | |
| %(user_date_filters)s | |
| """) | |
| exec_query_with_args("### 1-3 days count for time-to-close chart ###", | |
| """ | |
| SELECT COUNT(`id`) | |
| FROM `%(table_prefix)stracker_query` | |
| WHERE | |
| `status` IN (2, 3) | |
| AND DATEDIFF(`updated_at`, `created_at`) >= 1 | |
| AND DATEDIFF(`updated_at`, `created_at`) < 3 | |
| %(user_filters)s | |
| %(user_date_filters)s | |
| """) | |
| exec_query_with_args("### 3-5 days count for time-to-close chart ###", | |
| """ | |
| SELECT COUNT(`id`) | |
| FROM `%(table_prefix)stracker_query` | |
| WHERE | |
| `status` IN (2, 3) | |
| AND DATEDIFF(`updated_at`, `created_at`) >= 3 | |
| AND DATEDIFF(`updated_at`, `created_at`) < 5 | |
| %(user_filters)s | |
| %(user_date_filters)s | |
| """) | |
| exec_query_with_args("### 5-30 days count for time-to-close chart ###", | |
| """ | |
| SELECT COUNT(`id`) | |
| FROM `%(table_prefix)stracker_query` | |
| WHERE | |
| `status` IN (2, 3) | |
| AND DATEDIFF(`updated_at`, `created_at`) >= 5 | |
| AND DATEDIFF(`updated_at`, `created_at`) < 30 | |
| %(user_filters)s | |
| %(user_date_filters)s | |
| """) | |
| exec_query_with_args("### >30 days count for time-to-close chart ###", | |
| """ | |
| SELECT COUNT(`id`) | |
| FROM `%(table_prefix)stracker_query` | |
| WHERE | |
| `status` IN (2, 3) | |
| AND DATEDIFF(`updated_at`, `created_at`) >= 30 | |
| %(user_filters)s | |
| %(user_date_filters)s | |
| """) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment