Feature: Check Db Integrity

- Right now it only checks for duplicate attendances.
This commit is contained in:
tanshu 2015-01-09 12:31:08 +05:30
parent e0d3887fa9
commit 948b45b2ce
4 changed files with 54 additions and 0 deletions

View File

@ -52,6 +52,7 @@ def main(global_config, **settings):
config.add_route('api_dashboard', '/api/Dashboard') config.add_route('api_dashboard', '/api/Dashboard')
config.add_route('dashboard', '/Dashboard') config.add_route('dashboard', '/Dashboard')
config.add_route('api_db_integrity', '/api/DbIntegrity')
config.add_route('api_login', '/api/login') config.add_route('api_login', '/api/login')
config.add_route('login', '/login') config.add_route('login', '/login')
config.add_route('logout', '/logout') config.add_route('logout', '/logout')

View File

@ -128,4 +128,12 @@
class="glyphicon glyphicon-paperclip"></i></button> class="glyphicon glyphicon-paperclip"></i></button>
</div> </div>
</div> </div>
<div class="form-group">
<label class="col-md-2 control-label">Integrity Check</label>
<div class="col-md-2">
<button class="btn btn-block btn-danger" ng-click="checkDb()">Check Database <i
class="glyphicon glyphicon-certificate"></i></button>
</div>
</div>
</form> </form>

View File

@ -121,6 +121,16 @@ var SettingsController = ['$scope', '$http', 'asDateFilter', '$modal', 'lockInfo
$scope.products = function ($viewValue) { $scope.products = function ($viewValue) {
return Product.autocomplete({term: $viewValue, count: 20}).$promise; return Product.autocomplete({term: $viewValue, count: 20}).$promise;
}; };
$scope.checkDb = function () {
return $http.post('/api/DbIntegrity', {}).
success(function (data) {
$scope.toasts.push({Type: 'Success', Message: ''});
}).
error(function (errorMessage) {
$scope.toasts.push({Type: 'Danger', Message: errorMessage});
});
};
}]; }];
SettingsController.resolve = { SettingsController.resolve = {

View File

@ -0,0 +1,35 @@
from pyramid.view import view_config
from sqlalchemy import func, distinct, over, desc
import transaction
from brewman.models import DBSession
from brewman.models.voucher import Attendance
@view_config(request_method='POST', route_name='api_db_integrity', renderer='json', permission='Authenticated')
def post_check_db(request):
info = {}
duplicate_attendances = get_duplicate_attendances()
if duplicate_attendances > 0:
fix_duplicate_attendances(duplicate_attendances)
info['Duplicate Attendances Fixed'] = duplicate_attendances
transaction.commit()
return info
def get_duplicate_attendances():
sub_query = DBSession.query(
over(distinct(func.first_value(Attendance.id)), partition_by=[Attendance.employee_id, Attendance.date])
).filter(Attendance.is_valid == True).subquery()
query = DBSession.query(func.count(Attendance.id)). \
filter(~Attendance.id.in_(sub_query)). \
filter(Attendance.is_valid == True)
return query.scalar()
def fix_duplicate_attendances():
sub = DBSession.query(over(distinct(func.first_value(Attendance.id)),
partition_by=[Attendance.employee_id, Attendance.date],
order_by=desc(Attendance.creation_date))).filter(Attendance.is_valid == True).subquery()
DBSession.query(Attendance).filter(~Attendance.id.in_(sub)).filter(Attendance.is_valid == True).delete(False)