Example #1
0
def manage_project(id=None):
    project = _(xforms=[], uforms=[]) if not id else db.Project.get_by_id(id)
    if not project:
        raise HTTPError(404, "Project not found: %s" % id)

    xforms = db.XForm.get_unassigned_xforms(False, False)
    uforms = db.XForm.get_unassigned_uforms(False, False)
    session = get_session()
        
    if request.method == 'POST':
        form = forms.ProjectForm(request)
        try:
            if form.is_valid():
                form.save()
                action = ('created' if not id else 'updated')
                session['messages']['pass'].append('Project %s' % action)
            else:
                session['messages']['fail'].extend(form.errors)

            return redirect('/admin/projects/')
        except pymongo.errors.DuplicateKeyError:
            session['messages']['fail'].append("Provided Project Id and/or Name already exists.")
            project = form._instance

    return {
        'title':'Projects',
        'project': project,
        'xforms': [_(f) for f in xforms],
        'uforms': [_(f) for f in uforms],
    }
Example #2
0
File: core.py Project: hkmshb/kedat
def index():
    #+==========================
    #: forms series summary
    records = []
    projects = db.Project.get_all()
    ref_date = _get_ref_date()
    wkdate_bounds = get_weekdate_bounds(ref_date)

    for p in projects:
        record = _(id=p.id, name=p.name)
        captures = db.Capture.get_by_project(p.id, paginate=False)
        if captures.count():
            summary = stats.series_purity_summary(captures, ref_date)
            record.update(summary)
        records.append(record)

    #+==========================
    #: today activity summary
    activity_summary, activity_stats = [], _()
    captures = list(db.Capture.get_by_date(ref_date.isoformat(), paginate=False))
    if captures:
        activity_stats = stats.day_activity_stats(captures, ref_date)
        activity_breakdown = stats.day_activity_breakdown(captures, ref_date)
        for record in activity_breakdown:
            activity_summary.append(_(record))

    return {
        'is_front': True, 
        'title': 'Capture Summary',
        'records': records,
        'activity_records': activity_summary,
        'activity_stats': activity_stats,
        'report_ref_date': ref_date,
        'report_weekdate_bounds': wkdate_bounds,
    }
Example #3
0
def manage_project(id=None):
    project = _(xforms=[], uforms=[]) if not id else db.Project.get_by_id(id)
    if not project:
        raise HTTPError(404, "Project not found: %s" % id)

    xforms = db.XForm.get_unassigned_xforms(False, False)
    uforms = db.XForm.get_unassigned_uforms(False, False)
    session = get_session()

    if request.method == 'POST':
        form = forms.ProjectForm(request)
        try:
            if form.is_valid():
                form.save()
                action = ('created' if not id else 'updated')
                session['messages']['pass'].append('Project %s' % action)
            else:
                session['messages']['fail'].extend(form.errors)

            return redirect('/admin/projects/')
        except pymongo.errors.DuplicateKeyError:
            session['messages']['fail'].append(
                "Provided Project Id and/or Name already exists.")
            project = form._instance

    return {
        'title': 'Projects',
        'project': project,
        'xforms': [_(f) for f in xforms],
        'uforms': [_(f) for f in uforms],
    }
Example #4
0
File: core.py Project: hkmshb/kedat
def project_view(project_id):
    project = db.Project.get_by_id(project_id)
    xrecords, urecords = [], []

    for f in project.xforms:
        xform = db.XForm.get_by_id(f)
        record = _(id=xform.id_string, title=xform.title)
        captures = db.Capture.get_by_form(f, False)
        if captures.count():
            summary = stats.activity_summary(captures)
            record.update(summary)
        xrecords.append(record)

    for f in project.uforms:
        uform = db.XForm.get_by_id(f)
        record = _(id=uform.id_string, title=uform.title)
        updates = db.Update.get_by_form(f, False)
        if updates.count():
            summary = stats.activity_summary(updates)
            record.update(summary)
        urecords.append(record)

    return {
        'title': 'Project: %s' % project.name,
        'project': project,
        'xrecords': xrecords,
        'urecords': urecords
    }
Example #5
0
    def _get_distribution_assets(self, cols):
        # helper
        get = self._get_row_entry

        # find headers
        hdrs = ['fdr.sn', 'fdr.volt', 'fdr.name', 'ss.loc', 'ss.name']
        self._ensure_headers_exist(hdrs)

        # iterate rows and extract asset in nested-form
        source_feeder, location, skip_count = None, None, 0
        for row in self.ws:
            # new source_feeder encountered
            if get(row, cols.fdr_name):
                location = get(row, cols.ss_loc)
                source_feeder = _({
                    'volt':
                    get(row, cols.fdr_volt),
                    'name':
                    get(row, cols.fdr_name).strip().title(),
                })

            # ensure a source_feeder exist at this point otherwise
            # ignore current line and proceed to the next line even
            # if it has substation details.
            if not source_feeder:
                skip_count += 1
                continue

            ## from original list sub-station name needs to be normalized
            ss_name = get(row, cols.ss_name).strip()
            if ss_name.endswith(',') or ss_name.endswith('- '):
                ss_name = (ss_name[:-1]).strip()

            is_public = get(row, cols.ss_type).lower() == 'p'
            station = _({
                'code':
                get(row, cols.ss_code),
                'name':
                ss_name,
                'type':
                'distribution',
                'source_feeder':
                source_feeder.name,
                'state':
                location,
                'is_public':
                is_public,
                'transformers': [
                    _({
                        'name': 'TR1',
                        'capacity': get(row, cols.ss_cap),
                        'upriser_count': 0
                    })
                ]
            })
            yield station
        print('# lines skipped: %s' % skip_count)
Example #6
0
    def _get_injection_assets(self, cols):
        # helper
        get = self._get_row_entry

        # find headers
        hdrs = ['sn', 'source', 'is.code', 'is.name', 'is.loc', 'is.type']
        self._ensure_headers_exist(hdrs)

        # iterate rows, extract data & push into collection
        station, feeders, count = None, None, 0
        for row in self.ws:
            # new station encountered
            if row[cols.source] != None:
                if station:
                    yield station

                is_public = get(row, cols.is_type).lower() == 'p'
                count += 1
                station = _({
                    'code': get(row, cols.is_code),
                    'name': get(row, cols.is_name),
                    'type': 'injection',
                    'source_feeder': get(row, cols.source),
                    'state': get(row, cols.is_loc),
                    'is_public': is_public,
                    'transformers': []
                })

            # new transformer encountered
            if row[cols.xfmr_id] != None:
                feeders = []
                station.transformers.append(
                    _({
                        'name': get(row, cols.xfmr_id),
                        'capacity': get(row, cols.xfmr_cap),
                        'feeders': feeders
                    }))

            if is_public:
                feeders.append(
                    _({
                        'code': get(row, cols.fdr_code),
                        'voltage': get(row, cols.fdr_volt),
                        'name': get(row, cols.fdr_name).strip().title()
                    }))

        # return very last station
        if station:
            yield station
Example #7
0
def manage_station(feeder_code, code=None):
    feeder = db.Feeder.get_by_code(feeder_code)
    if not feeder:
        raise HTTPError(404, "Feeder not found: %s" % feeder_code)

    station = _() if not code else db.Station.get_by_code(code)
    if code and not station:
        raise HTTPError(404, "Station not found: %s" % code)

    session = get_session()['messages']
    if request.method == 'POST':
        form = forms.StationForm(request)
        try:
            if form.is_valid():
                form.save()
                action = ('created' if not code else 'updated')
                session['pass'].append('Station %s' % action)
            else:
                session['fail'].extend(form.errors)
            return redirect('/admin/feeders/%s/' % feeder_code)
        except pymongo.errors.DuplicateKeyError:
            session['fail'].append("Provided Station Code already exists.")
            station = form._instance
    
    return {
        'title': 'Station',
        'station': station,
        'feeder': feeder,
        'vratio_choices': db.Volt.CHOICES,
    }
Example #8
0
def manage_feeder(code=None):
    feeder = _() if not code else db.Feeder.get_by_code(code)
    if code and not feeder:
        raise HTTPError(404, "Feeder not found: %s" % id)

    session = get_session()        
    if request.method == 'POST':
        form = forms.FeederForm(request)
        try:
            if form.is_valid():
                form.save()
                action = ('created' if not code else 'updated')
                session['messages']['pass'].append('Feeder %s' % action)
            else:
                session['messages']['fail'].extend(form.errors)

            return redirect('/admin/feeders/')
        except pymongo.errors.DuplicateKeyError:
            session['messages']['fail'].append("Provided Feeder Code and/or Name already exists.")
            feeder = form._instance

    return {
        'title':'Projects',
        'feeder': feeder,
    }
Example #9
0
def series_purity_summary(records, ref_date=None):
    df = pd.DataFrame(list(records))
    result, key = _(), 'datetime_today'
    if not ref_date:
        ref_date = datetime.now().date()

    # today
    f = df[df[key] == ref_date.isoformat()]
    result.update(_purity_summary(f, 'd'))

    # week
    dates = utils.get_weekdate_bounds(ref_date)
    f = df[df[key] >= dates[0].isoformat()]
    f = f[f[key] <= dates[1].isoformat()]
    result.update(_purity_summary(f, 'w'))

    # month
    dates = utils.get_monthdate_bounds(ref_date)
    f = df[df[key] >= dates[0].isoformat()]
    f = f[f[key] <= dates[1].isoformat()]
    result.update(_purity_summary(f, 'm'))

    # all
    result.update(_purity_summary(df, ''))
    return result
Example #10
0
def manage_station(feeder_code, code=None):
    feeder = db.Feeder.get_by_code(feeder_code)
    if not feeder:
        raise HTTPError(404, "Feeder not found: %s" % feeder_code)

    station = _() if not code else db.Station.get_by_code(code)
    if code and not station:
        raise HTTPError(404, "Station not found: %s" % code)

    session = get_session()['messages']
    if request.method == 'POST':
        form = forms.StationForm(request)
        try:
            if form.is_valid():
                form.save()
                action = ('created' if not code else 'updated')
                session['pass'].append('Station %s' % action)
            else:
                session['fail'].extend(form.errors)
            return redirect('/admin/feeders/%s/' % feeder_code)
        except pymongo.errors.DuplicateKeyError:
            session['fail'].append("Provided Station Code already exists.")
            station = form._instance

    return {
        'title': 'Station',
        'station': station,
        'feeder': feeder,
        'vratio_choices': db.Volt.CHOICES,
    }
Example #11
0
def feeder_stations(feeder, impfile):
    result = _(summary=None, errors=[], passed=0, failed=0)
    result.summary = _make_imxsummary(result)

    try:
        impfile.file.seek(0)
        f = StringIO(impfile.file.read().decode())
        f.seek(0)

        dialect = csv.Sniffer().sniff(f.read(1024))
        f.seek(0)

        reader = csv.DictReader(f, dialect=dialect)
        for row in reader:
            try:
                r = {}
                r['code'] = row['code'].upper()
                r['name'] = row['name'].title()
                r['capacity'] = row['capacity']
                r['public'] = row['type'].upper().strip() == 'P'
                r['type'] = row.get('category', '') or 'D'
                r['source_feeder'] = feeder.code
                r['vratio'] = str(db.Volt.MVOLTL_LVOLT if feeder.voltage ==
                                  '11' else db.Volt.MVOLTH_LVOLT)
                db.Station.insert_one(r)
                result.passed += 1
            except Exception as ex:
                result.failed += 1
    except Exception as ex:
        result.errors.append(str(ex))
    return result
Example #12
0
def series_purity_summary(records, ref_date=None):
    df = pd.DataFrame(list(records))
    result, key = _(), 'datetime_today'
    if not ref_date:
        ref_date = datetime.now().date()

    # today
    f = df[df[key] == ref_date.isoformat()]
    result.update(_purity_summary(f, 'd'))

    # week
    dates = utils.get_weekdate_bounds(ref_date)
    f = df[df[key] >= dates[0].isoformat()]
    f = f[f[key] <= dates[1].isoformat()]
    result.update(_purity_summary(f, 'w'))

    # month
    dates = utils.get_monthdate_bounds(ref_date)
    f = df[df[key] >= dates[0].isoformat()]
    f = f[f[key] <= dates[1].isoformat()]
    result.update(_purity_summary(f, 'm'))

    # all
    result.update(_purity_summary(df, ''))
    return result
Example #13
0
def feeder_stations(feeder, impfile):
    result = _(summary=None, errors=[], passed=0, failed=0)
    result.summary = _make_imxsummary(result)

    try:
        impfile.file.seek(0)
        f = StringIO(impfile.file.read().decode())
        f.seek(0)

        dialect = csv.Sniffer().sniff(f.read(1024))
        f.seek(0)

        reader = csv.DictReader(f, dialect=dialect)
        for row in reader:
            try:
                r = {}
                r["code"] = row["code"].upper()
                r["name"] = row["name"].title()
                r["capacity"] = row["capacity"]
                r["public"] = row["type"].upper().strip() == "P"
                r["type"] = row.get("category", "") or "D"
                r["source_feeder"] = feeder.code
                r["vratio"] = str(db.Volt.MVOLTL_LVOLT if feeder.voltage == "11" else db.Volt.MVOLTH_LVOLT)
                db.Station.insert_one(r)
                result.passed += 1
            except Exception as ex:
                result.failed += 1
    except Exception as ex:
        result.errors.append(str(ex))
    return result
Example #14
0
def manage_feeder(code=None):
    feeder = _() if not code else db.Feeder.get_by_code(code)
    if code and not feeder:
        raise HTTPError(404, "Feeder not found: %s" % id)

    session = get_session()
    if request.method == 'POST':
        form = forms.FeederForm(request)
        try:
            if form.is_valid():
                form.save()
                action = ('created' if not code else 'updated')
                session['messages']['pass'].append('Feeder %s' % action)
            else:
                session['messages']['fail'].extend(form.errors)

            return redirect('/admin/feeders/')
        except pymongo.errors.DuplicateKeyError:
            session['messages']['fail'].append(
                "Provided Feeder Code and/or Name already exists.")
            feeder = form._instance

    return {
        'title': 'Projects',
        'feeder': feeder,
    }
Example #15
0
 def _get_injection_assets(self, cols):
     # helper
     get = self._get_row_entry
     
     # find headers
     hdrs = ['sn', 'source', 'is.code', 'is.name', 'is.loc', 'is.type']
     self._ensure_headers_exist(hdrs)
     
     # iterate rows, extract data & push into collection
     station, feeders, count = None, None, 0
     for row in self.ws:
         # new station encountered
         if row[cols.source] != None:
             if station: 
                 yield station
             
             is_public = get(row, cols.is_type).lower() == 'p'
             count += 1
             station = _({
                 'code': get(row, cols.is_code),
                 'name': get(row, cols.is_name),
                 'type': 'injection',
                 'source_feeder': get(row, cols.source),
                 'state': get(row, cols.is_loc),
                 'is_public': is_public,
                 'transformers': []
             })
         
         # new transformer encountered
         if row[cols.xfmr_id] != None:
             feeders = []
             station.transformers.append(_({
                 'name': get(row, cols.xfmr_id),
                 'capacity': get(row, cols.xfmr_cap),
                 'feeders': feeders
             }))
         
         if is_public:
             feeders.append(_({
                 'code': get(row, cols.fdr_code),
                 'voltage': get(row, cols.fdr_volt),
                 'name': get(row, cols.fdr_name).strip().title()
             }))
     
     # return very last station
     if station:
         yield station
Example #16
0
 def _get_distribution_assets(self, cols):
     # helper
     get = self._get_row_entry
     
     # find headers
     hdrs = ['fdr.sn', 'fdr.volt', 'fdr.name', 'ss.loc', 'ss.name']
     self._ensure_headers_exist(hdrs)
     
     # iterate rows and extract asset in nested-form
     source_feeder, location, skip_count = None, None, 0
     for row in self.ws:
         # new source_feeder encountered
         if get(row, cols.fdr_name):
             location = get(row, cols.ss_loc)
             source_feeder = _({
                 'volt': get(row, cols.fdr_volt),
                 'name': get(row, cols.fdr_name).strip().title(),
             })
         
         # ensure a source_feeder exist at this point otherwise
         # ignore current line and proceed to the next line even 
         # if it has substation details.
         if not source_feeder:
             skip_count += 1
             continue
         
         ## from original list sub-station name needs to be normalized
         ss_name = get(row, cols.ss_name).strip()
         if ss_name.endswith(',') or ss_name.endswith('- '):
             ss_name = (ss_name[:-1]).strip()
         
         is_public = get(row, cols.ss_type).lower() == 'p'
         station = _({
             'code': get(row, cols.ss_code),
             'name': ss_name,
             'type': 'distribution',
             'source_feeder': source_feeder.name,
             'state': location,
             'is_public': is_public,
             'transformers': [_({
                 'name': 'TR1',
                 'capacity': get(row, cols.ss_cap),
                 'upriser_count': 0
             })]
         })
         yield station
     print('# lines skipped: %s' % skip_count)
Example #17
0
def index():
    return {
        'title': 'Admin',
        'stat': _({
            'count_xforms': db.XForm.count(),
            'count_projects': db.Project.count()
        })
    }
Example #18
0
def register():
    session = get_session()['messages']
    if request.method == 'POST':
        form = RegisterForm(request, authnz)
        if form.is_valid():
            form.save()
            session['pass'].append('Account created!')
            return redirect('/')
        else:
            session['fail'].extend(form.errors)
    else:
        form = _(username=None, email=None)

    return {
        'title': 'Register To Use CENTrak',
        'form': _(username=form.username, email=form.email)
    }
Example #19
0
def register():
    session = get_session()['messages']
    if request.method == 'POST':
        form = RegisterForm(request, authnz)
        if form.is_valid():
            form.save()
            session['pass'].append('Account created!')
            return redirect('/')
        else:
            session['fail'].extend(form.errors)
    else:
        form = _(username=None, email=None)
    
    return {
        'title':'Register To Use CENTrak',
        'form': _(username=form.username, email=form.email)
    }
Example #20
0
    def _get_transmission_assets(self, cols):
        # helper
        get = self._get_row_entry

        # find headers
        hdrs = ['sn', 'ts.code', 'ts.name', 'ts.loc', 'xfmr.id']
        self._ensure_headers_exist(hdrs)

        # iterate rows and extract asset in nested-form
        station, feeders = None, None
        for row in self.ws:
            # new station encountered?
            if get(row, cols.ts_name):
                if station:
                    yield station

                station = _({
                    'code': get(row, cols.ts_code),
                    'name': get(row, cols.ts_name),
                    'type': 'transmission',
                    'state': get(row, cols.ts_loc),
                    'transformers': []
                })

            # new transformer encountered?
            if get(row, cols.xfmr_id):
                feeders = []
                station.transformers.append(
                    _({
                        'name': get(row, cols.xfmr_id),
                        'capacity': get(row, cols.xfmr_cap),
                        'feeders': feeders
                    }))

            # collect feeder details
            feeders.append(
                _({
                    'code': get(row, cols.fdr_code),
                    'voltage': get(row, cols.fdr_volt),
                    'name': get(row, cols.fdr_name).strip().title()
                }))

        # return very last station
        if station:
            yield station
Example #21
0
def _acct_status_summary(df):
    result, key = _(), 'acct_status'
    values = ['unknown', 'new', 'active', 'inactive', 'not-conn',
              'disconn-bill', 'disconn-no-bill', 'n_a']
    
    for value in values:
        f = df[df[key] == value]
        result[value.replace('-','_')] = f[key].size
    return result
Example #22
0
File: core.py Project: hkmshb/kedat
def capture_view(item_id):
    r = _query_capture(
            tbl=db.Capture, 
            title='Capture Item', 
            item_id=item_id)
    
    # retrieve duplicates
    qry_dup = {'_id': {'$ne': item_id}, 
              'rseq': r['record']['rseq']}
    
    cur = db.Capture.query(paginate=False, **qry_dup)
    r['duplicates'] = [_(item) for item in cur]
    
    # retrieve updates
    qry_upd = {'rseq': r['record']['rseq']}
    cur = db.Update.query(paginate=False, **qry_upd)
    r['updates'] = [_(item) for item in cur]    
    return r
Example #23
0
File: api.py Project: hkmshb/kedat
def capture_update(record_type, record_id):
    table = db.Capture if record_type == 'captures' else db.Update
    record = table.get(record_id)
    if not record:
        return {'success':False, 'message':'Original record not found' }
    
    if not request.json:
        return {'success':False, 'message':'Posted record not available'}
    
    capture_upd, snapshot = _(request.json['capture']), None
    
    # if rseq changed ensure it doesn't cause duplication
    if record.rseq != capture_upd.rseq:
        assert record._id == capture_upd._id
        params = {'_id': {'$ne': capture_upd._id}, 'rseq': capture_upd.rseq}
        found = list(table.query(paginate=False, **params))
        if found and len(found) > 0:
            return {
                'success':False, 
                'message':(
                    'Update aborted. Capture with route sequence exist.\n'
                    'Existing Record (id=%s, enum_id=%s)' % (
                        found[0]['_id'], found[0]['enum_id']))
            }
    
    # if acct_no changed, ensure it doesn't cause duplication
    if record.acct_no != capture_upd.acct_no:
        assert record._id == capture_upd._id
        params = {'_id': {'$ne': capture_upd._id}, 'acct_no': capture_upd.acct_no}
        found = list(table.query(paginate=False, **params))
        if found and len(found) > 0:
            return {
                'success': False,
                'message': (
                    'Update aborted. Capture with account number exist.\n'
                    'Existing Record (id=%s, enum_id=%s)' % (
                        found[0]['_id'], found[0]['enum_id']))
            }
    
    if not record.snapshots:
        snapshot = record.copy()
        del snapshot['snapshots']
    else:
        snapshot = record['snapshots']['original']['capture']
        
    # save changes
    last_updated = datetime.today().strftime('%Y-%m-%d')
    capture_upd.last_updated = last_updated
    capture_upd.snapshots = {
        'original': {
            'capture': snapshot,
            'last_updated': last_updated,
            'updated_by': None
        }
    }
    table.replace(record_id, capture_upd)
    return {'success':True, 'message':'Record updated!'}
Example #24
0
def index():
    return {
        'title':
        'Admin',
        'stat':
        _({
            'count_xforms': db.XForm.count(),
            'count_projects': db.Project.count()
        })
    }
Example #25
0
 def _get_transmission_assets(self, cols):
     # helper
     get = self._get_row_entry
     
     # find headers
     hdrs = ['sn', 'ts.code', 'ts.name', 'ts.loc', 'xfmr.id']
     self._ensure_headers_exist(hdrs)
     
     # iterate rows and extract asset in nested-form
     station, feeders = None, None
     for row in self.ws:
         # new station encountered?
         if get(row, cols.ts_name):
             if station:
                 yield station
             
             station = _({
                 'code': get(row, cols.ts_code),
                 'name': get(row, cols.ts_name),
                 'type': 'transmission',
                 'state': get(row, cols.ts_loc),
                 'transformers': []
             })
         
         # new transformer encountered?
         if get(row, cols.xfmr_id):
             feeders = []
             station.transformers.append(_({
                 'name': get(row, cols.xfmr_id),
                 'capacity': get(row, cols.xfmr_cap),
                 'feeders': feeders
             }))
         
         # collect feeder details
         feeders.append(_({
             'code': get(row, cols.fdr_code),
             'voltage': get(row, cols.fdr_volt),
             'name': get(row, cols.fdr_name).strip().title()
         }))
     
     # return very last station
     if station:
         yield station
Example #26
0
def _meter_type_summary(df):
    result, key = _(), 'meter_type'
    values = ['none', 'analogue', 'ppm']
    
    for value in values:
        try:
            f = df[df[key] == value]
            result[value] = f[key].size
        except:
            result[value] = '-'
    return result
Example #27
0
def _acct_status_summary(df):
    result, key = _(), 'acct_status'
    values = [
        'unknown', 'new', 'active', 'inactive', 'not-conn', 'disconn-bill',
        'disconn-no-bill', 'n_a'
    ]

    for value in values:
        f = df[df[key] == value]
        result[value.replace('-', '_')] = f[key].size
    return result
Example #28
0
def _meter_type_summary(df):
    result, key = _(), 'meter_type'
    values = ['none', 'analogue', 'ppm']

    for value in values:
        try:
            f = df[df[key] == value]
            result[value] = f[key].size
        except:
            result[value] = '-'
    return result
Example #29
0
def get_session():
    """Retrieves the session object"""
    session = request.environ.get('beaker.session')
    if 'messages' not in session:
        session['messages'] = _({
            'pass': [], 
            'fail': [], 
            'warn': []
        })
        session.save()
    return session
Example #30
0
 def object_list(self):
     if not self.cache:
         try:
             p = self.p
             skip_size = self.start_index() - 1
             cursor = p.cursor\
                       .skip(skip_size)\
                       .limit(p.page_size)
             items = [_(item) for item in cursor]
         except Exception as ex:
             items = []
         self.cache = items
     return self.cache
Example #31
0
    def is_valid(self):
        instance, fields = _(), ['impfile']
        for f in fields:
            instance[f] = self.request.files.get(f, _())

        if not instance.impfile:
            self.errors.append("File upload required.")
        
        filename = instance.impfile.filename.lower()
        name, ext = os.path.splitext(filename)
        if self.file_ext != ext:
            msg_fmt = "File with extension '%s' expected but '%s' provided"
            self.errors.append(msg_fmt % (self.file_ext, ext))

        if self.max_file_size:
            uploaded_size = sys.getsizeof(instance.impfile.file)
            if uploaded_size > self.max_file_size:
                msg_fmt = "Maximum file size exceeded. Expected: %s, Actual: %s"
                self.errors.append(msg_fmt % (self.max_file_size, uploaded_size))

        self._instance = instance
        return (len(self.errors) == 0)
Example #32
0
def _activity_breakdown(df):
    results = []
    
    # group records by group-name
    ggroups = df.groupby('group')
    gs = ggroups['rseq'].count()

    for i in range(len(gs)):
        result = _(group=gs.index[i])

        # summaries
        gdf = ggroups.get_group(gs.index[i])
        result.update(_purity_summary(gdf, ''))
        result.update(_acct_status_summary(gdf))
        result.update(_meter_type_summary(gdf))

        #+===========================================:
        #: group by upriser
        ugroups = gdf.groupby('upriser')
        us = ugroups['rseq'].count()
        iresults = []
        result.uprisers = iresults

        for j in range(len(us)):
            iresult = _(upriser=us.index[j])
            
            # summaries
            udf = ugroups.get_group(us.index[j])
            iresult.update(_purity_summary(udf, ''))
            iresult.update(_acct_status_summary(udf))
            iresult.update(_meter_type_summary(udf))

            # collect results
            iresults.append(_(iresult))

        # collect result
        results.append(_(result))
    return results
Example #33
0
def _activity_breakdown(df):
    results = []

    # group records by group-name
    ggroups = df.groupby('group')
    gs = ggroups['rseq'].count()

    for i in range(len(gs)):
        result = _(group=gs.index[i])

        # summaries
        gdf = ggroups.get_group(gs.index[i])
        result.update(_purity_summary(gdf, ''))
        result.update(_acct_status_summary(gdf))
        result.update(_meter_type_summary(gdf))

        #+===========================================:
        #: group by upriser
        ugroups = gdf.groupby('upriser')
        us = ugroups['rseq'].count()
        iresults = []
        result.uprisers = iresults

        for j in range(len(us)):
            iresult = _(upriser=us.index[j])

            # summaries
            udf = ugroups.get_group(us.index[j])
            iresult.update(_purity_summary(udf, ''))
            iresult.update(_acct_status_summary(udf))
            iresult.update(_meter_type_summary(udf))

            # collect results
            iresults.append(_(iresult))

        # collect result
        results.append(_(result))
    return results
Example #34
0
    def is_valid(self):
        instance, fields = _(), ['impfile']
        for f in fields:
            instance[f] = self.request.files.get(f, _())

        if not instance.impfile:
            self.errors.append("File upload required.")

        filename = instance.impfile.filename.lower()
        name, ext = os.path.splitext(filename)
        if self.file_ext != ext:
            msg_fmt = "File with extension '%s' expected but '%s' provided"
            self.errors.append(msg_fmt % (self.file_ext, ext))

        if self.max_file_size:
            uploaded_size = sys.getsizeof(instance.impfile.file)
            if uploaded_size > self.max_file_size:
                msg_fmt = "Maximum file size exceeded. Expected: %s, Actual: %s"
                self.errors.append(msg_fmt %
                                   (self.max_file_size, uploaded_size))

        self._instance = instance
        return (len(self.errors) == 0)
Example #35
0
    def is_valid(self):
        instance, fields = _(), ['_id','code','name','voltage','source']
        for f in fields:
            instance[f] = self.request.forms.get(f, '').strip()
        
        if not instance.code:
            self.errors.append('Feeder code required')
        if not instance.name:
            self.errors.append('Feeder name required')
        if not instance.voltage:
            self.errors.append('Feeder voltage required')

        self._instance = instance
        return (len(self.errors) == 0)
Example #36
0
    def is_valid(self):
        instance, fields = _(), ['_id', 'code', 'name', 'voltage', 'source']
        for f in fields:
            instance[f] = self.request.forms.get(f, '').strip()

        if not instance.code:
            self.errors.append('Feeder code required')
        if not instance.name:
            self.errors.append('Feeder name required')
        if not instance.voltage:
            self.errors.append('Feeder voltage required')

        self._instance = instance
        return (len(self.errors) == 0)
Example #37
0
def _activity_by_day(df):
    results, key = [], 'datetime_today'
    if not df.index.size:
        return results
    
    # group records by date
    groups = df.groupby(key)
    gs = groups['rseq'].count()

    def _verbose(x):
        d = datetime.strptime(x, '%Y-%m-%d')
        return d.strftime('%Y-%m-%d : %a')

    for i in range(len(gs)):
        result = _(date=_verbose(gs.index[i]))
        
        # purity check
        gdf = groups.get_group(gs.index[i])
        result.update(_activity_summary(gdf))
        
        # collect results
        results.append(_(result))

    return results
Example #38
0
 def _get_cols_index_info(self):
     names, indexes = (), ()
     if self.source_type == TRANSMISSION:
         names = ('ts_code', 'ts_name', 'ts_loc', 'xfmr_id', 'xfmr_cap', 
                  'fdr_volt', 'fdr_name', 'fdr_code')
         indexes = list(range(1, 6)) + list(range(9, 12))
     elif self.source_type == INJECTION:
         names = ('source', 'is_code', 'is_name', 'is_loc', 'is_type', 
                  'xfmr_id', 'xfmr_cap', 'fdr_code', 'fdr_volt', 'fdr_name')
         indexes = list(range(1, 8)) + list(range(10, 13))
     elif self.source_type == DISTRIBUTION:
         names = ('fdr_volt', 'fdr_name', 
                  'ss_loc', 'ss_name', 'ss_cap', 'ss_type', 'ss_code')
         indexes = list(range(1, 8)) 
     return _(zip(names, indexes))
Example #39
0
 def _get_cols_index_info(self):
     names, indexes = (), ()
     if self.source_type == TRANSMISSION:
         names = ('ts_code', 'ts_name', 'ts_loc', 'xfmr_id', 'xfmr_cap',
                  'fdr_volt', 'fdr_name', 'fdr_code')
         indexes = list(range(1, 6)) + list(range(9, 12))
     elif self.source_type == INJECTION:
         names = ('source', 'is_code', 'is_name', 'is_loc', 'is_type',
                  'xfmr_id', 'xfmr_cap', 'fdr_code', 'fdr_volt', 'fdr_name')
         indexes = list(range(1, 8)) + list(range(10, 13))
     elif self.source_type == DISTRIBUTION:
         names = ('fdr_volt', 'fdr_name', 'ss_loc', 'ss_name', 'ss_cap',
                  'ss_type', 'ss_code')
         indexes = list(range(1, 8))
     return _(zip(names, indexes))
Example #40
0
def _activity_by_day(df):
    results, key = [], 'datetime_today'
    if not df.index.size:
        return results

    # group records by date
    groups = df.groupby(key)
    gs = groups['rseq'].count()

    def _verbose(x):
        d = datetime.strptime(x, '%Y-%m-%d')
        return d.strftime('%Y-%m-%d : %a')

    for i in range(len(gs)):
        result = _(date=_verbose(gs.index[i]))

        # purity check
        gdf = groups.get_group(gs.index[i])
        result.update(_activity_summary(gdf))

        # collect results
        results.append(_(result))

    return results
Example #41
0
File: core.py Project: hkmshb/kedat
def projects():
    records = []
    projects = db.Project.get_all()

    for p in projects:
        record = _(id=p.id, name=p.name)
        captures = db.Capture.get_by_project(p.id, paginate=False)
        if captures.count():
            summary = stats.activity_summary(captures)
            record.update(summary)
        records.append(record)

    return {
        'title': 'Projects',
        'records': records
    }
Example #42
0
    def is_valid(self):
        instance, fields = _(), ['_id','code','name','capacity','vratio','public','source_feeder']
        for f in fields:
            instance[f] = self.request.forms.get(f, '').strip()

        if not instance.code:
            self.errors.append('Station code required.')
        if not instance.name:
            self.errors.append('Station name required.')
        if not instance.capacity:
            self.errors.append('Station capacity required.')
        if not instance.vratio:
            self.errors.append('Station voltage ratio required.')
        if not instance.source_feeder:
            self.errors.append('Station source feeder required.')

        self._instance = instance
        return (len(self.errors) == 0)
Example #43
0
def _activity_stats(df):
    result = _()

    # total captures
    result.capture_count = df.index.size

    # number of transformers & uprisers
    gdf = df.groupby('station')
    result.transformer_count = gdf['station'].count().index.size

    gdf = df.groupby('upriser')
    result.upriser_count = gdf['upriser'].count().index.size

    # number of devices used
    gdf = df.groupby('device_imei')
    result.device_count = gdf['device_imei'].count().index.size

    return result
Example #44
0
def _activity_stats(df):
    result = _()

    # total captures
    result.capture_count = df.index.size
    
    # number of transformers & uprisers
    gdf = df.groupby('station')
    result.transformer_count = gdf['station'].count().index.size

    gdf = df.groupby('upriser')
    result.upriser_count = gdf['upriser'].count().index.size

    # number of devices used
    gdf = df.groupby('device_imei')
    result.device_count = gdf['device_imei'].count().index.size

    return result
Example #45
0
    def is_valid(self):
        _id = self.request.forms.get('_id', '').strip()
        id  = self.request.forms.get('id', '').strip()
        name = self.request.forms.get('name', '').strip()
        xforms = self.request.forms.getall('xforms')
        uforms = self.request.forms.getall('uforms')

        if not id:
            self.errors.append('Project Id required')
        if not name:
            self.errors.append('Project name required.')
        if self.errors:
            return False

        self._instance = _(
            _id=_id, id=id, name=name, 
            xforms=xforms, uforms=uforms
        )
        return True
Example #46
0
    def is_valid(self):
        _id = self.request.forms.get('_id', '').strip()
        id = self.request.forms.get('id', '').strip()
        name = self.request.forms.get('name', '').strip()
        xforms = self.request.forms.getall('xforms')
        uforms = self.request.forms.getall('uforms')

        if not id:
            self.errors.append('Project Id required')
        if not name:
            self.errors.append('Project name required.')
        if self.errors:
            return False

        self._instance = _(_id=_id,
                           id=id,
                           name=name,
                           xforms=xforms,
                           uforms=uforms)
        return True
Example #47
0
def _purity_summary(df, prefix):
    result, key = _(), 'datetime_today'
    result['%s_total' % prefix] = df.index.size

    # duplicate rseq check
    key = 'rseq'
    f = df[df.duplicated(key) == True]
    result['%s_rseq_duplicates' % prefix] = f.index.size

    # duplicate acct_no check
    key = 'acct_no'
    f = df[df[key].isnull() == False]
    f = f[f.duplicated(key) == True]
    result['%s_acctno_duplicates' % prefix] = f.index.size

    # fixed/updated records
    key = 'last_updated'
    f = df[df[key].isnull() == False]
    result['%s_updated' % prefix] = f.index.size
    return result
Example #48
0
def _purity_summary(df, prefix):
    result, key = _(), 'datetime_today'
    result['%s_total' % prefix] = df.index.size

    # duplicate rseq check
    key = 'rseq'
    f = df[df.duplicated(key) == True]
    result['%s_rseq_duplicates' % prefix] = f.index.size

    # duplicate acct_no check
    key = 'acct_no'
    f = df[df[key].isnull() == False]
    f = f[f.duplicated(key) == True]
    result['%s_acctno_duplicates' % prefix] = f.index.size

    # fixed/updated records
    key = 'last_updated'
    f = df[df[key].isnull() == False]
    result['%s_updated' % prefix] = f.index.size
    return result
Example #49
0
    def is_valid(self):
        instance, fields = _(), [
            '_id', 'code', 'name', 'capacity', 'vratio', 'public',
            'source_feeder'
        ]
        for f in fields:
            instance[f] = self.request.forms.get(f, '').strip()

        if not instance.code:
            self.errors.append('Station code required.')
        if not instance.name:
            self.errors.append('Station name required.')
        if not instance.capacity:
            self.errors.append('Station capacity required.')
        if not instance.vratio:
            self.errors.append('Station voltage ratio required.')
        if not instance.source_feeder:
            self.errors.append('Station source feeder required.')

        self._instance = instance
        return (len(self.errors) == 0)
Example #50
0
File: db.py Project: hkmshb/kedat
 def get_by_id(id):
     record = db.projects.find_one({'id': id})
     return _(record or {})
Example #51
0
File: db.py Project: hkmshb/kedat
 def get_by_code(code):
     record = db.stations.find_one({'code': code.upper()})
     return _(record or {})
Example #52
0
def _activity_summary(df):
    result = _()
    result.update(_purity_summary(df, ''))
    result.update(_acct_status_summary(df))
    result.update(_meter_type_summary(df))
    return result
Example #53
0
File: db.py Project: hkmshb/kedat
 def get(self, _id):
     record = self.db.find_one({'_id': _id})
     return _(record or {})
Example #54
0
File: db.py Project: hkmshb/kedat
 def get_by_rseq(self, rseq):
     record = self.db.find_one({'rseq': rseq})
     return _(record or {})
Example #55
0
File: db.py Project: hkmshb/kedat
 def get_by_code(code):
     record = db.stations.find_one({'code': code.upper()})
     return _(record or {})
Example #56
0
File: db.py Project: hkmshb/kedat
 def get_by_id(id):
     record = db.projects.find_one({'id': id})
     return _(record or {})