Skip to content

Commit

Permalink
Merge pull request #243 from davidwatkins73/master
Browse files Browse the repository at this point in the history
Entity Statistics
  • Loading branch information
davidwatkins73 authored Jun 23, 2016
2 parents 7be6a2e + c9e4a82 commit a5b1885
Show file tree
Hide file tree
Showing 36 changed files with 1,021 additions and 3 deletions.
2 changes: 1 addition & 1 deletion curret_future.md → current_future.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Waltz Features
# Waltz Features (current & future)

## Overview

Expand Down
117 changes: 117 additions & 0 deletions waltz-data/src/main/ddl/liquibase/db.changelog-1.0.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1854,6 +1854,123 @@
</createTable>
</changeSet>

<!-- 230 entity statistics -->
<changeSet author="kamransaleem"
id="20160621-230-1">
<createTable tableName="entity_statistic">
<column autoIncrement="true"
name="id"
type="BIGSERIAL">
<constraints primaryKey="true"
primaryKeyName="entity_statistic_pkey"/>
</column>
<column name="name"
type="VARCHAR(128)">
<constraints nullable="false"/>
</column>
<column name="description"
type="${longvarchar.type}">
<constraints nullable="true"/>
</column>
<column name="type"
type="VARCHAR(128)">
<constraints nullable="false"/>
</column>
<column name="category"
type="VARCHAR(128)">
<constraints nullable="false"/>
</column>
<column name="active"
type="BOOLEAN">
<constraints nullable="false"/>
</column>
<column name="renderer"
type="VARCHAR(128)">
<constraints nullable="false"/>
</column>
<column name="historic_renderer"
type="VARCHAR(128)">
<constraints nullable="false"/>
</column>
<column name="provenance"
type="varchar(64)"
defaultValue="waltz">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>

<changeSet author="kamransaleem"
id="20160621-230-2">
<createTable tableName="entity_statistic_value">
<column autoIncrement="true"
name="id"
type="BIGSERIAL">
<constraints primaryKey="true"
primaryKeyName="entity_statistic_value_pkey"/>
</column>
<column name="statistic_id"
type="${long.type}">
<constraints nullable="false"/>
</column>
<column name="entity_kind"
type="VARCHAR(128)">
<constraints nullable="false"/>
</column>
<column name="entity_id"
type="${long.type}">
<constraints nullable="false"/>
</column>
<column name="value"
type="VARCHAR(128)">
<constraints nullable="true"/>
</column>
<column name="outcome"
type="VARCHAR(128)">
<constraints nullable="false"/>
</column>
<column name="state"
type="VARCHAR(128)">
<constraints nullable="false"/>
</column>
<column name="reason"
type="${longvarchar.type}">
<constraints nullable="true"/>
</column>
<column name="created_at"
type="TIMESTAMP">
<constraints nullable="false"/>
</column>
<column name="current"
type="BOOLEAN">
<constraints nullable="false"/>
</column>
<column name="provenance"
type="varchar(64)"
defaultValue="waltz">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>

<changeSet author="kamransaleem"
id="20160621-230-3">
<createIndex indexName="idx_esv_statistic_id"
tableName="entity_statistic_value"
unique="false">
<column name="statistic_id" type="${long.type}"/>
</createIndex>
</changeSet>

<changeSet author="kamransaleem"
id="20160621-230-4">
<createIndex indexName="idx_esv_entity_ref"
tableName="entity_statistic_value"
unique="false">
<column name="entity_kind" type="VARCHAR(128)"/>
<column name="entity_id" type="${long.type}"/>
</createIndex>
</changeSet>

<!-- 237 entity alias -->
<changeSet author="dwatkins"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package com.khartec.waltz.data.entity_statistic;

import com.khartec.waltz.model.*;
import com.khartec.waltz.model.entity_statistic.*;
import com.khartec.waltz.schema.tables.records.EntityStatisticRecord;
import com.khartec.waltz.schema.tables.records.EntityStatisticValueRecord;
import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.RecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.khartec.waltz.common.Checks.checkNotNull;
import static com.khartec.waltz.schema.tables.EntityStatistic.ENTITY_STATISTIC;
import static com.khartec.waltz.schema.tables.EntityStatisticValue.ENTITY_STATISTIC_VALUE;

@Repository
public class EntityStatisticDao {

private static final com.khartec.waltz.schema.tables.EntityStatistic es = ENTITY_STATISTIC.as("es");
private static final com.khartec.waltz.schema.tables.EntityStatisticValue esv = ENTITY_STATISTIC_VALUE.as("esv");

private static final RecordMapper<? super Record, EntityStatistic> TO_ENTITY_STATISTIC_MAPPPER = r -> {
EntityStatisticRecord record = r.into(ENTITY_STATISTIC);
return ImmutableEntityStatistic.builder()
.id(record.getId())
.name(record.getName())
.description((record.getDescription()))
.type(StatisticType.valueOf(record.getType()))
.category(StatisticCategory.valueOf(record.getCategory()))
.active(record.getActive())
.renderer(record.getRenderer())
.historicRenderer(record.getHistoricRenderer())
.provenance(record.getProvenance())
.build();
};


private static final RecordMapper<? super Record, EntityStatisticValue> TO_VALUE_MAPPPER = r -> {
EntityStatisticValueRecord record = r.into(ENTITY_STATISTIC_VALUE);
return ImmutableEntityStatisticValue.builder()
.id(record.getId())
.statisticId(record.getStatisticId())
.entity(ImmutableEntityReference.builder()
.kind(EntityKind.valueOf(record.getEntityKind()))
.id(record.getEntityId())
.build())
.value(record.getValue())
.outcome(record.getOutcome())
.state(StatisticValueState.valueOf(record.getState()))
.reason(record.getReason())
.createdAt(record.getCreatedAt().toLocalDateTime())
.current(record.getCurrent())
.provenance(record.getProvenance())
.build();
};



private static final Function<EntityStatistic, EntityStatisticRecord> TO_RECORD_MAPPER = domainObj -> {
EntityStatisticRecord record = new EntityStatisticRecord();

record.setName(domainObj.name());
record.setDescription(domainObj.description());
record.setType(domainObj.type().name());
record.setCategory(domainObj.category().name());
record.setActive(domainObj.active());
record.setRenderer(domainObj.renderer());
record.setHistoricRenderer(domainObj.historicRenderer());
record.setProvenance(domainObj.provenance());

return record;
};


private static final RecordMapper<? super Record, EntityStatisticWithValue> TO_COMPOUND_MAPPER = record -> {
return ImmutableEntityStatisticWithValue.builder()
.statistic(TO_ENTITY_STATISTIC_MAPPPER.map(record))
.value(TO_VALUE_MAPPPER.map(record))
.build();
};


private final DSLContext dsl;


@Autowired
public EntityStatisticDao(DSLContext dsl) {
checkNotNull(dsl, "dsl cannot be null");
this.dsl = dsl;
}


public List<EntityStatisticWithValue> findStatisticsForEntity(EntityReference ref, boolean active) {
checkNotNull(ref, "ref cannot be null");
return dsl.select(es.fields())
.select(esv.fields())
.from(es)
.innerJoin(esv)
.on(esv.STATISTIC_ID.eq(es.ID))
.where(es.ACTIVE.eq(active)
.and(esv.ENTITY_KIND.eq(ref.kind().name()))
.and(esv.ENTITY_ID.eq(ref.id()))
.and(esv.CURRENT.eq(true)))
.fetch(TO_COMPOUND_MAPPER);
}


public List<EntityStatistic> getAllEntityStatistics() {
return dsl.select(es.fields())
.from(es)
.fetch(TO_ENTITY_STATISTIC_MAPPPER);
}


public boolean addEntityStatistic(EntityStatistic entityStatistic) {
checkNotNull(entityStatistic, "entityStatistic cannot be null");
return dsl.executeInsert(TO_RECORD_MAPPER.apply(entityStatistic)) == 1;
}


public int[] bulkSaveValues(List<EntityStatisticValue> values) {
return dsl
.batch(values.stream()
.map(s -> dsl
.insertInto(
ENTITY_STATISTIC_VALUE,
ENTITY_STATISTIC_VALUE.STATISTIC_ID,
ENTITY_STATISTIC_VALUE.ENTITY_KIND,
ENTITY_STATISTIC_VALUE.ENTITY_ID,
ENTITY_STATISTIC_VALUE.VALUE,
ENTITY_STATISTIC_VALUE.OUTCOME,
ENTITY_STATISTIC_VALUE.STATE,
ENTITY_STATISTIC_VALUE.REASON,
ENTITY_STATISTIC_VALUE.CREATED_AT,
ENTITY_STATISTIC_VALUE.CURRENT,
ENTITY_STATISTIC_VALUE.PROVENANCE)
.values(
s.statisticId(),
s.entity().kind().name(),
s.entity().id(),
s.value(),
s.outcome(),
s.state().name(),
s.reason(),
Timestamp.valueOf(s.createdAt()),
s.current(),
s.provenance()))
.collect(Collectors.toList()))
.execute();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* This file is part of Waltz.
*
* Waltz is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Waltz is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Waltz. If not, see <http://www.gnu.org/licenses/>.
*/

package com.khartec.waltz.jobs;

import com.khartec.waltz.data.entity_statistic.EntityStatisticDao;
import com.khartec.waltz.model.EntityKind;
import com.khartec.waltz.model.ImmutableEntityReference;
import com.khartec.waltz.model.entity_statistic.EntityStatisticWithValue;
import com.khartec.waltz.service.DIConfiguration;
import org.jooq.DSLContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.List;


public class EntityStatisticHarness {

public static void main(String[] args) {

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(DIConfiguration.class);
DSLContext dsl = ctx.getBean(DSLContext.class);
EntityStatisticDao dao = ctx.getBean(EntityStatisticDao.class);

ImmutableEntityReference ref = ImmutableEntityReference.builder()
.kind(EntityKind.APPLICATION)
.id(1415)
.build();

List<EntityStatisticWithValue> values = dao.findStatisticsForEntity(ref, true);

System.out.println(values.size());
// ApplicationService applicationService = ctx.getBean(ApplicationService.class);
// DSLContext dsl = ctx.getBean(DSLContext.class);
//
// List<String> tagList = applicationService.findAllTags();
//
// tagList.forEach(System.out::println);
//
// System.out.println("---------------");
//
// applicationService.findByTag("not-good-at-flying").forEach(a -> System.out.println(a.name()));
//
// System.out.println(applicationService.findTagsForApplication(521L));
//

}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.khartec.waltz.jobs.sample;

import com.khartec.waltz.service.DIConfiguration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
* Created by Kamran on 22/06/2016.
*/
public class DataGenerator {

public static void main(String[] args) {

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(DIConfiguration.class);
new EntityStatisticGenerator().apply(ctx);
}
}
Loading

0 comments on commit a5b1885

Please sign in to comment.