Search in sources :

Example 11 with AnalyticsTableColumn

use of org.hisp.dhis.analytics.AnalyticsTableColumn in project dhis2-core by dhis2.

the class JdbcEnrollmentAnalyticsTableManager method getDimensionColumns.

@Override
protected List<AnalyticsTableColumn> getDimensionColumns(AnalyticsTable table) {
    final String dbl = statementBuilder.getDoubleColumnType();
    final String numericClause = " and value " + statementBuilder.getRegexpMatch() + " '" + NUMERIC_LENIENT_REGEXP + "'";
    final String dateClause = " and value " + statementBuilder.getRegexpMatch() + " '" + DATE_REGEXP + "'";
    List<AnalyticsTableColumn> columns = new ArrayList<>();
    List<OrganisationUnitLevel> levels = organisationUnitService.getFilledOrganisationUnitLevels();
    List<OrganisationUnitGroupSet> orgUnitGroupSets = idObjectManager.getDataDimensionsNoAcl(OrganisationUnitGroupSet.class);
    for (OrganisationUnitLevel level : levels) {
        String column = quote(PREFIX_ORGUNITLEVEL + level.getLevel());
        columns.add(new AnalyticsTableColumn(column, "character(11)", "ous." + column, level.getCreated()));
    }
    for (OrganisationUnitGroupSet groupSet : orgUnitGroupSets) {
        columns.add(new AnalyticsTableColumn(quote(groupSet.getUid()), "character(11)", "ougs." + quote(groupSet.getUid()), groupSet.getCreated()));
    }
    for (PeriodType periodType : PeriodType.getAvailablePeriodTypes()) {
        String column = quote(periodType.getName().toLowerCase());
        columns.add(new AnalyticsTableColumn(column, "character varying(15)", "dps." + column));
    }
    for (ProgramStage programStage : table.getProgram().getProgramStages()) {
        for (ProgramStageDataElement programStageDataElement : programStage.getProgramStageDataElements()) {
            DataElement dataElement = programStageDataElement.getDataElement();
            ValueType valueType = dataElement.getValueType();
            String dataType = getColumnType(valueType);
            String dataClause = dataElement.isNumericType() ? numericClause : dataElement.getValueType().isDate() ? dateClause : "";
            String select = getSelectClause(valueType);
            boolean skipIndex = NO_INDEX_VAL_TYPES.contains(dataElement.getValueType()) && !dataElement.hasOptionSet();
            String sql = "(select " + select + " from trackedentitydatavalue tedv " + "inner join programstageinstance psi on psi.programstageinstanceid = tedv.programstageinstanceid " + "where psi.executiondate is not null " + "and psi.deleted is false " + "and psi.programinstanceid=pi.programinstanceid " + dataClause + " " + "and tedv.dataelementid=" + dataElement.getId() + " " + "and psi.programstageid=" + programStage.getId() + " " + "order by psi.executiondate desc " + "limit 1) as " + quote(programStage.getUid() + DB_SEPARATOR_ID + dataElement.getUid());
            columns.add(new AnalyticsTableColumn(quote(programStage.getUid() + DB_SEPARATOR_ID + dataElement.getUid()), dataType, sql, skipIndex));
        }
    }
    for (TrackedEntityAttribute attribute : table.getProgram().getNonConfidentialTrackedEntityAttributes()) {
        String dataType = getColumnType(attribute.getValueType());
        String dataClause = attribute.isNumericType() ? numericClause : attribute.isDateType() ? dateClause : "";
        String select = getSelectClause(attribute.getValueType());
        boolean skipIndex = NO_INDEX_VAL_TYPES.contains(attribute.getValueType()) && !attribute.hasOptionSet();
        String sql = "(select " + select + " from trackedentityattributevalue " + "where trackedentityinstanceid=pi.trackedentityinstanceid " + "and trackedentityattributeid=" + attribute.getId() + dataClause + ") as " + quote(attribute.getUid());
        columns.add(new AnalyticsTableColumn(quote(attribute.getUid()), dataType, sql, skipIndex));
    }
    AnalyticsTableColumn pi = new AnalyticsTableColumn(quote("pi"), "character(11) not null", "pi.uid");
    AnalyticsTableColumn erd = new AnalyticsTableColumn(quote("enrollmentdate"), "timestamp", "pi.enrollmentdate");
    AnalyticsTableColumn id = new AnalyticsTableColumn(quote("incidentdate"), "timestamp", "pi.incidentdate");
    final String executionDateSql = "(select psi.executionDate from programstageinstance psi " + "where psi.programinstanceid=pi.programinstanceid " + "and psi.executiondate is not null " + "and psi.deleted is false " + "order by psi.executiondate desc " + "limit 1) as " + quote("executiondate");
    AnalyticsTableColumn ed = new AnalyticsTableColumn(quote("executiondate"), "timestamp", executionDateSql);
    final String dueDateSql = "(select psi.duedate from programstageinstance psi " + "where psi.programinstanceid = pi.programinstanceid " + "and psi.duedate is not null " + "and psi.deleted is false " + "order by psi.duedate desc " + "limit 1) as " + quote("duedate");
    AnalyticsTableColumn dd = new AnalyticsTableColumn(quote("duedate"), "timestamp", dueDateSql);
    AnalyticsTableColumn cd = new AnalyticsTableColumn(quote("completeddate"), "timestamp", "case status when 'COMPLETED' then enddate end");
    AnalyticsTableColumn es = new AnalyticsTableColumn(quote("enrollmentstatus"), "character(50)", "pi.status");
    AnalyticsTableColumn longitude = new AnalyticsTableColumn(quote("longitude"), dbl, "pi.longitude");
    AnalyticsTableColumn latitude = new AnalyticsTableColumn(quote("latitude"), dbl, "pi.latitude");
    AnalyticsTableColumn ou = new AnalyticsTableColumn(quote("ou"), "character(11) not null", "ou.uid");
    AnalyticsTableColumn oun = new AnalyticsTableColumn(quote("ouname"), "character varying(230) not null", "ou.name");
    AnalyticsTableColumn ouc = new AnalyticsTableColumn(quote("oucode"), "character varying(50)", "ou.code");
    columns.addAll(Lists.newArrayList(pi, erd, id, ed, es, dd, cd, longitude, latitude, ou, oun, ouc));
    if (databaseInfo.isSpatialSupport()) {
        String alias = "(select ST_SetSRID(ST_MakePoint(pi.longitude, pi.latitude), 4326)) as geom";
        columns.add(new AnalyticsTableColumn(quote("geom"), "geometry(Point, 4326)", alias, false, "gist"));
    }
    if (table.hasProgram() && table.getProgram().isRegistration()) {
        columns.add(new AnalyticsTableColumn(quote("tei"), "character(11)", "tei.uid"));
    }
    return filterDimensionColumns(columns);
}
Also used : PeriodType(org.hisp.dhis.period.PeriodType) ValueType(org.hisp.dhis.common.ValueType) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) UniqueArrayList(org.hisp.dhis.commons.collection.UniqueArrayList) AnalyticsTableColumn(org.hisp.dhis.analytics.AnalyticsTableColumn) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) DataElement(org.hisp.dhis.dataelement.DataElement) OrganisationUnitLevel(org.hisp.dhis.organisationunit.OrganisationUnitLevel) ProgramStage(org.hisp.dhis.program.ProgramStage) OrganisationUnitGroupSet(org.hisp.dhis.organisationunit.OrganisationUnitGroupSet)

Example 12 with AnalyticsTableColumn

use of org.hisp.dhis.analytics.AnalyticsTableColumn in project dhis2-core by dhis2.

the class JdbcEnrollmentAnalyticsTableManager method getTables.

private List<AnalyticsTable> getTables() {
    List<AnalyticsTable> tables = new UniqueArrayList<>();
    List<Program> programs = idObjectManager.getAllNoAcl(Program.class);
    String baseName = getTableName();
    for (Program program : programs) {
        AnalyticsTable table = new AnalyticsTable(baseName, null, null, program);
        List<AnalyticsTableColumn> dimensionColumns = getDimensionColumns(table);
        table.setDimensionColumns(dimensionColumns);
        tables.add(table);
    }
    return tables;
}
Also used : Program(org.hisp.dhis.program.Program) AnalyticsTable(org.hisp.dhis.analytics.AnalyticsTable) UniqueArrayList(org.hisp.dhis.commons.collection.UniqueArrayList) AnalyticsTableColumn(org.hisp.dhis.analytics.AnalyticsTableColumn)

Example 13 with AnalyticsTableColumn

use of org.hisp.dhis.analytics.AnalyticsTableColumn in project dhis2-core by dhis2.

the class JdbcEventAnalyticsTableManager method populateTable.

@Override
protected void populateTable(AnalyticsTable table) {
    final String start = DateUtils.getMediumDateString(table.getPeriod().getStartDate());
    final String end = DateUtils.getMediumDateString(table.getPeriod().getEndDate());
    final String tableName = table.getTempTableName();
    final String psiExecutionDate = statementBuilder.getCastToDate("psi.executiondate");
    String sql = "insert into " + table.getTempTableName() + " (";
    List<AnalyticsTableColumn> columns = getDimensionColumns(table);
    validateDimensionColumns(columns);
    for (AnalyticsTableColumn col : columns) {
        sql += col.getName() + ",";
    }
    sql = removeLast(sql, 1) + ") select ";
    for (AnalyticsTableColumn col : columns) {
        sql += col.getAlias() + ",";
    }
    sql = removeLast(sql, 1) + " ";
    sql += "from programstageinstance psi " + "inner join programinstance pi on psi.programinstanceid=pi.programinstanceid " + "inner join programstage ps on psi.programstageid=ps.programstageid " + "inner join program pr on pi.programid=pr.programid and pi.deleted is false " + "inner join categoryoptioncombo ao on psi.attributeoptioncomboid=ao.categoryoptioncomboid " + "left join trackedentityinstance tei on pi.trackedentityinstanceid=tei.trackedentityinstanceid and tei.deleted is false " + "inner join organisationunit ou on psi.organisationunitid=ou.organisationunitid " + "left join _orgunitstructure ous on psi.organisationunitid=ous.organisationunitid " + "left join _organisationunitgroupsetstructure ougs on psi.organisationunitid=ougs.organisationunitid " + "inner join _categorystructure acs on psi.attributeoptioncomboid=acs.categoryoptioncomboid " + "left join _dateperiodstructure dps on " + psiExecutionDate + "=dps.dateperiod " + "where psi.executiondate >= '" + start + "' " + "and psi.executiondate <= '" + end + "' " + "and pr.programid=" + table.getProgram().getId() + " " + "and psi.organisationunitid is not null " + "and psi.executiondate is not null " + "and psi.deleted is false ";
    populateAndLog(sql, tableName);
}
Also used : AnalyticsTableColumn(org.hisp.dhis.analytics.AnalyticsTableColumn)

Example 14 with AnalyticsTableColumn

use of org.hisp.dhis.analytics.AnalyticsTableColumn in project dhis2-core by dhis2.

the class JdbcOrgUnitTargetTableManager method createTable.

@Override
public void createTable(AnalyticsTable table) {
    final String tableName = table.getTempTableName();
    final String sqlDrop = "drop table " + tableName;
    executeSilently(sqlDrop);
    String sqlCreate = "create table " + tableName + " (";
    List<AnalyticsTableColumn> columns = getDimensionColumns(table);
    validateDimensionColumns(columns);
    for (AnalyticsTableColumn col : columns) {
        sqlCreate += col.getName() + " " + col.getDataType() + ",";
    }
    sqlCreate += "value double precision)";
    log.info("Creating table: " + tableName + ", columns: " + columns.size());
    log.debug("Create SQL: " + sqlCreate);
    jdbcTemplate.execute(sqlCreate);
}
Also used : AnalyticsTableColumn(org.hisp.dhis.analytics.AnalyticsTableColumn)

Example 15 with AnalyticsTableColumn

use of org.hisp.dhis.analytics.AnalyticsTableColumn in project dhis2-core by dhis2.

the class JdbcCompletenessTableManager method getDimensionColumns.

@Override
public List<AnalyticsTableColumn> getDimensionColumns(AnalyticsTable table) {
    List<AnalyticsTableColumn> columns = new ArrayList<>();
    List<OrganisationUnitGroupSet> orgUnitGroupSets = idObjectManager.getDataDimensionsNoAcl(OrganisationUnitGroupSet.class);
    List<OrganisationUnitLevel> levels = organisationUnitService.getFilledOrganisationUnitLevels();
    List<CategoryOptionGroupSet> attributeCategoryOptionGroupSets = categoryService.getAttributeCategoryOptionGroupSetsNoAcl();
    List<DataElementCategory> attributeCategories = categoryService.getAttributeDataDimensionCategoriesNoAcl();
    for (OrganisationUnitGroupSet groupSet : orgUnitGroupSets) {
        columns.add(new AnalyticsTableColumn(quote(groupSet.getUid()), "character(11)", "ougs." + quote(groupSet.getUid()), groupSet.getCreated()));
    }
    for (OrganisationUnitLevel level : levels) {
        String column = quote(PREFIX_ORGUNITLEVEL + level.getLevel());
        columns.add(new AnalyticsTableColumn(column, "character(11)", "ous." + column, level.getCreated()));
    }
    for (CategoryOptionGroupSet groupSet : attributeCategoryOptionGroupSets) {
        columns.add(new AnalyticsTableColumn(quote(groupSet.getUid()), "character(11)", "acs." + quote(groupSet.getUid()), groupSet.getCreated()));
    }
    for (DataElementCategory category : attributeCategories) {
        columns.add(new AnalyticsTableColumn(quote(category.getUid()), "character(11)", "acs." + quote(category.getUid()), category.getCreated()));
    }
    for (PeriodType periodType : PeriodType.getAvailablePeriodTypes()) {
        String column = quote(periodType.getName().toLowerCase());
        columns.add(new AnalyticsTableColumn(column, "character varying(15)", "ps." + column));
    }
    String timelyDateDiff = statementBuilder.getDaysBetweenDates("pe.enddate", statementBuilder.getCastToDate("cdr.date"));
    String timelyAlias = "(select (" + timelyDateDiff + ") <= ds.timelydays) as timely";
    AnalyticsTableColumn tm = new AnalyticsTableColumn(quote("timely"), "boolean", timelyAlias);
    AnalyticsTableColumn ds = new AnalyticsTableColumn(quote("dx"), "character(11) not null", "ds.uid");
    columns.addAll(Lists.newArrayList(ds, tm));
    return filterDimensionColumns(columns);
}
Also used : PeriodType(org.hisp.dhis.period.PeriodType) CategoryOptionGroupSet(org.hisp.dhis.dataelement.CategoryOptionGroupSet) DataElementCategory(org.hisp.dhis.dataelement.DataElementCategory) AnalyticsTableColumn(org.hisp.dhis.analytics.AnalyticsTableColumn) OrganisationUnitLevel(org.hisp.dhis.organisationunit.OrganisationUnitLevel) OrganisationUnitGroupSet(org.hisp.dhis.organisationunit.OrganisationUnitGroupSet)

Aggregations

AnalyticsTableColumn (org.hisp.dhis.analytics.AnalyticsTableColumn)16 OrganisationUnitLevel (org.hisp.dhis.organisationunit.OrganisationUnitLevel)5 UniqueArrayList (org.hisp.dhis.commons.collection.UniqueArrayList)4 OrganisationUnitGroupSet (org.hisp.dhis.organisationunit.OrganisationUnitGroupSet)4 CategoryOptionGroupSet (org.hisp.dhis.dataelement.CategoryOptionGroupSet)3 DataElementCategory (org.hisp.dhis.dataelement.DataElementCategory)3 PeriodType (org.hisp.dhis.period.PeriodType)3 ArrayList (java.util.ArrayList)2 AnalyticsTable (org.hisp.dhis.analytics.AnalyticsTable)2 ValueType (org.hisp.dhis.common.ValueType)2 DataElement (org.hisp.dhis.dataelement.DataElement)2 Program (org.hisp.dhis.program.Program)2 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)2 Calendar (org.hisp.dhis.calendar.Calendar)1 LegendSet (org.hisp.dhis.legend.LegendSet)1 Period (org.hisp.dhis.period.Period)1 ProgramStage (org.hisp.dhis.program.ProgramStage)1 ProgramStageDataElement (org.hisp.dhis.program.ProgramStageDataElement)1 Transactional (org.springframework.transaction.annotation.Transactional)1