Changeset 510


Ignore:
Timestamp:
02/06/11 23:43:18 (2 years ago)
Author:
philsmart
Message:
 
Location:
raptor-mua/trunk/src/main/java/uk/ac/cardiff/raptormua
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • raptor-mua/trunk/src/main/java/uk/ac/cardiff/raptormua/engine/statistics/AuthenticationStatistic.java

    r506 r510  
    154154//                                      + timeInterval + " minutes, where "+sqlWhere+")"); 
    155155//              } 
    156  
    157                 observations = buckets; 
     156                ObservationSeries series=  new ObservationSeries(); 
     157                series.setObservations(buckets); 
     158                getObservationSeries().add(series); 
     159                 
    158160 
    159161                // finished successfully, no exception thrown 
     
    257259//              } 
    258260 
    259                 observations = buckets; 
    260  
     261                ObservationSeries series=  new ObservationSeries(); 
     262                series.setObservations(buckets); 
     263                getObservationSeries().add(series); 
     264                 
    261265                return true; 
    262266 
     
    315319//              } 
    316320 
    317                 observations = groups.toArray(new Group[0]); 
    318  
    319                 if (observations.length == 0) 
     321                if (groups.size() == 0) 
    320322                        return false; 
     323                 
    321324                // finished successfully, no exception thrown 
     325                ObservationSeries series=  new ObservationSeries(); 
     326                series.setObservations(groups.toArray(new Group[0])); 
     327                getObservationSeries().add(series); 
     328                 
    322329                return true; 
    323330 
     
    366373//                      statisticParameters.getSeries().setSeriesLabel("Distinct Values " + groupByField); 
    367374 
    368                 observations = groups.toArray(new Group[0]); 
    369  
    370                 if (observations.length == 0) 
     375                if (groups.size() == 0) 
    371376                        return false; 
     377                 
     378                // finished successfully, no exception thrown 
     379                ObservationSeries series=  new ObservationSeries(); 
     380                series.setObservations(groups.toArray(new Group[0])); 
     381                getObservationSeries().add(series);              
     382                 
    372383                // finished successfully, no exception thrown 
    373384                return true; 
  • raptor-mua/trunk/src/main/java/uk/ac/cardiff/raptormua/engine/statistics/Statistic.java

    r506 r510  
    3939import uk.ac.cardiff.raptormua.exceptions.PostprocessorException; 
    4040import uk.ac.cardiff.raptormua.exceptions.PreprocessorException; 
     41import uk.ac.cardiff.raptormua.exceptions.StatisticalUnitException; 
    4142import uk.ac.cardiff.raptormua.model.EntryHandler; 
    4243import uk.ac.cardiff.raptormua.runtimeutils.EntryClone; 
    4344 
    4445/** 
    45  * @author philsmart Holds a statistics unit or one statistics operation on one piece of data 
     46 * @author philsmart Holds a statistics unit or one statistics operation on one 
     47 *         piece of data 
    4648 */ 
    4749public class Statistic { 
    4850 
    49     static Logger log = LoggerFactory.getLogger(Statistic.class); 
    50  
    51     private EntryHandler entryHandler; 
    52  
    53     protected StatisticParameters statisticParameters; 
    54  
    55     /* add a preprocessing module to the statistical method */ 
    56     private StatisticsPreProcessor preprocessor; 
    57  
    58     /* add a postprocessing module to the statistical method */ 
    59     private List<StatisticsPostProcessor> postprocessor; 
    60  
    61     /* 
    62      * each statistical method produces objects (observations) which are stored in this array variable ready for postprocessing or construction of an 
    63      * <code>AggregatorGraphModel</code> 
    64      */ 
    65     protected Observation[] observations; 
    66  
    67     public void setEntryHandler(EntryHandler entryHandler) { 
    68         if (preprocessor != null) 
    69             try { 
    70                 log.info("Invoking statistical preprocessor " + preprocessor.getClass()); 
    71                 preprocessor.preProcess(entryHandler); 
    72             } catch (PreprocessorException e) { 
    73                 log.error("Could not preprocess entries " + preprocessor.getClass()); 
    74             } 
    75         this.entryHandler = entryHandler; 
    76     } 
    77  
    78  
    79     /** 
    80      * <p> 
    81      * construct a graph model from the data observations and groupings stored in the buckets 
    82      * </p> 
    83      * 
    84      * @return 
    85      */ 
    86     public AggregatorGraphModel constructGraphModel() { 
    87         AggregatorGraphModel gmodel = new AggregatorGraphModel(); 
    88  
    89         log.debug("Observations type " + observations); 
    90  
    91         if (observations instanceof Group[]) { 
    92             log.info("Constructing graph model for Group type"); 
    93             Group[] groups = (Group[]) observations; 
    94             gmodel.setPresentation(statisticParameters.getPresentation()); 
    95  
    96             ArrayList<String> seriesLabels = new ArrayList<String>(); 
    97             for (Series series : statisticParameters.getSeries()){ 
    98                 seriesLabels.add(series.getSeriesLabel()); 
    99             } 
    100             gmodel.setSeriesLabels(seriesLabels); 
    101  
    102             for (int i=0; i < statisticParameters.getSeries().size();i++){ 
    103                     for (Group group : groups) { 
    104                         gmodel.addGroupLabel(group.getGroupName()); 
    105                         List<Double> values = new ArrayList(); 
    106                         Double valueDouble = new Double(group.getValue()); 
    107                         values.add(valueDouble); 
    108                         gmodel.addGroupValue(values); 
    109                     } 
    110             } 
    111         } else if (observations instanceof Bucket[]) { 
    112             log.info("Constructing graph model for Bucket type"); 
    113             Bucket[] buckets = (Bucket[]) observations; 
    114             gmodel.setPresentation(statisticParameters.getPresentation()); 
    115  
    116             ArrayList<String> seriesLabels = new ArrayList<String>(); 
    117             for (Series series : statisticParameters.getSeries()){ 
    118                 seriesLabels.add(series.getSeriesLabel()); 
    119             } 
    120             gmodel.setSeriesLabels(seriesLabels); 
    121  
    122             DateTimeFormatter startParser = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm"); 
    123             DateTimeFormatter endParser = DateTimeFormat.forPattern("HH:mm"); 
    124             for (Bucket bucket : buckets) { 
    125                 gmodel.addGroupLabel(startParser.print(bucket.getStart()) + "-" + endParser.print(bucket.getEnd())); 
    126                 List<Double> values = new ArrayList(); 
    127                 Double valueDouble = new Double(bucket.getValue()); 
    128                 values.add(valueDouble); 
    129                 gmodel.addGroupValue(values); 
    130             } 
    131         } 
    132  
    133         return gmodel; 
    134     } 
    135  
    136     /** 
    137      * <p> 
    138      * pre processing effects the entries that go into the statistical unit post processing effects the observations that result form the statistical unit 
    139      * </p> 
    140      */ 
    141     public void postProcess() { 
    142         try { 
    143             if (getPostprocessor() != null) { 
    144                 for (StatisticsPostProcessor post : postprocessor){ 
    145                     observations = post.postProcess(observations); 
    146                 } 
    147             } 
    148         } catch (PostprocessorException e) { 
    149             log.error("Could not post process entries, using " + getPostprocessor().getClass()); 
    150         } 
    151     } 
    152  
    153     public void setField(String field) { 
    154  
    155     } 
    156  
    157     public void setPreprocessor(StatisticsPreProcessor preprocessor) { 
    158         this.preprocessor = preprocessor; 
    159     } 
    160  
    161     public StatisticsPreProcessor getPreprocessor() { 
    162         return preprocessor; 
    163     } 
    164  
    165     public List<StatisticsPostProcessor> getPostprocessor() { 
    166         return postprocessor; 
    167     } 
    168  
    169     public void setPostprocessor(List<StatisticsPostProcessor> postprocessor) { 
    170         this.postprocessor = postprocessor; 
    171     } 
    172  
    173     public void setStatisticParameters(StatisticParameters statisticParameters) { 
    174         this.statisticParameters = statisticParameters; 
    175     } 
    176  
    177     public StatisticParameters getStatisticParameters() { 
    178         return statisticParameters; 
    179     } 
    180  
    181  
    182     public EntryHandler getEntryHandler() { 
    183         return entryHandler; 
    184     } 
     51        static Logger log = LoggerFactory.getLogger(Statistic.class); 
     52 
     53        private EntryHandler entryHandler; 
     54 
     55        protected StatisticParameters statisticParameters; 
     56 
     57        /* add a preprocessing module to the statistical method */ 
     58        private StatisticsPreProcessor preprocessor; 
     59 
     60        /* add a postprocessing module to the statistical method */ 
     61        private List<StatisticsPostProcessor> postprocessor; 
     62 
     63        private List<ObservationSeries> observationSeries; 
     64 
     65        public Statistic() { 
     66                setObservationSeries(new ArrayList<ObservationSeries>()); 
     67        } 
     68 
     69        public void setEntryHandler(EntryHandler entryHandler) { 
     70                if (preprocessor != null) 
     71                        try { 
     72                                log.info("Invoking statistical preprocessor " + preprocessor.getClass()); 
     73                                preprocessor.preProcess(entryHandler); 
     74                        } catch (PreprocessorException e) { 
     75                                log.error("Could not preprocess entries " + preprocessor.getClass()); 
     76                        } 
     77                this.entryHandler = entryHandler; 
     78        } 
     79 
     80        /** 
     81         * <p> 
     82         * construct a graph model from the data observations and groupings stored 
     83         * in the buckets 
     84         * </p> 
     85         *  
     86         * @return 
     87         */ 
     88        public AggregatorGraphModel constructGraphModel() { 
     89                AggregatorGraphModel gmodel = new AggregatorGraphModel(); 
     90                int countGroup = 0; 
     91                int countBucket = 0; 
     92 
     93                for (ObservationSeries obsSeries : observationSeries) { 
     94                        if (obsSeries.getObservations() instanceof Group[]) 
     95                                countGroup++; 
     96                        if (obsSeries.getObservations() instanceof Bucket[]) 
     97                                countBucket++; 
     98                } 
     99                if (countGroup==0 && countBucket==0)return gmodel; 
     100                log.debug("Statistic has {} series and {} observations",this.getStatisticParameters().getSeries().size(),observationSeries.size()); 
     101                if (countGroup == observationSeries.size()) { 
     102                        log.info("Constructing graph model for Group type"); 
     103                        //construct the groups from the first series (each series will have the same grouping) 
     104                        Observation[] observations = observationSeries.get(0).getObservations(); 
     105                        Group[] groups = (Group[]) observations; 
     106                        for (Group group : groups) { 
     107                                gmodel.addGroupLabel(group.getGroupName()); 
     108                        } 
     109                        //now add each series and their values                   
     110                        for (int i=0; i < observationSeries.size(); i++){                                
     111                                groups = (Group[]) observations; 
     112                                log.debug("Trying to get series label {}, which is {}",i,statisticParameters.getSeries().get(i).getSeriesLabel()); 
     113                                gmodel.getSeriesLabels().add(statisticParameters.getSeries().get(i).getSeriesLabel()); 
     114         
     115                                for (Group group : groups) { 
     116                                        List<Double> values = new ArrayList(); 
     117                                        Double valueDouble = new Double(group.getValue()); 
     118                                        values.add(valueDouble); 
     119                                        gmodel.addGroupValue(values); 
     120                                } 
     121                        } 
     122 
     123                } else if (countBucket==observationSeries.size()) { 
     124//                      log.info("Constructing graph model for {} type", observations.getClass()); 
     125//                      Bucket[] buckets = (Bucket[]) observations; 
     126// 
     127//                      ArrayList<String> seriesLabels = new ArrayList<String>(); 
     128//                      for (Series series : statisticParameters.getSeries()) { 
     129//                              seriesLabels.add(series.getSeriesLabel()); 
     130//                      } 
     131//                      gmodel.setSeriesLabels(seriesLabels); 
     132// 
     133//                      DateTimeFormatter startParser = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm"); 
     134//                      DateTimeFormatter endParser = DateTimeFormat.forPattern("HH:mm"); 
     135//                      for (Bucket bucket : buckets) { 
     136//                              gmodel.addGroupLabel(startParser.print(bucket.getStart()) + "-" + endParser.print(bucket.getEnd())); 
     137//                              List<Double> values = new ArrayList(); 
     138//                              Double valueDouble = new Double(bucket.getValue()); 
     139//                              values.add(valueDouble); 
     140//                              gmodel.addGroupValue(values); 
     141//                      } 
     142                } 
     143                else{ 
     144                        log.error("Statistic had series with mixed observation types, which is currently not supported"); 
     145                } 
     146 
     147                return gmodel; 
     148        } 
     149 
     150        /** 
     151         * <p> 
     152         * pre processing effects the entries that go into the statistical unit post 
     153         * processing effects the observations that result form the statistical unit 
     154         * </p> 
     155         */ 
     156        public void postProcess() { 
     157                try { 
     158                        if (getPostprocessor() != null) { 
     159                                for (StatisticsPostProcessor post : postprocessor) { 
     160                                        // perform the same post process on each observationseries 
     161                                        for (ObservationSeries obsSeries : getObservationSeries()) { 
     162                                                obsSeries.setObservations(post.postProcess(obsSeries.getObservations())); 
     163                                        } 
     164                                } 
     165                        } 
     166                } catch (PostprocessorException e) { 
     167                        log.error("Could not post process entries, using " + getPostprocessor().getClass()); 
     168                } 
     169        } 
     170 
     171        public void setField(String field) { 
     172 
     173        } 
     174 
     175        public void setPreprocessor(StatisticsPreProcessor preprocessor) { 
     176                this.preprocessor = preprocessor; 
     177        } 
     178 
     179        public StatisticsPreProcessor getPreprocessor() { 
     180                return preprocessor; 
     181        } 
     182 
     183        public List<StatisticsPostProcessor> getPostprocessor() { 
     184                return postprocessor; 
     185        } 
     186 
     187        public void setPostprocessor(List<StatisticsPostProcessor> postprocessor) { 
     188                this.postprocessor = postprocessor; 
     189        } 
     190 
     191        public void setStatisticParameters(StatisticParameters statisticParameters) { 
     192                this.statisticParameters = statisticParameters; 
     193        } 
     194 
     195        public StatisticParameters getStatisticParameters() { 
     196                return statisticParameters; 
     197        } 
     198 
     199        public EntryHandler getEntryHandler() { 
     200                return entryHandler; 
     201        } 
     202 
     203        public void setObservationSeries(List<ObservationSeries> observationSeries) { 
     204                this.observationSeries = observationSeries; 
     205        } 
     206 
     207        public List<ObservationSeries> getObservationSeries() { 
     208                return observationSeries; 
     209        } 
    185210 
    186211} 
  • raptor-mua/trunk/src/main/java/uk/ac/cardiff/raptormua/engine/statistics/StatisticsHandler.java

    r506 r510  
    2727 
    2828import uk.ac.cardiff.model.Entry; 
     29import uk.ac.cardiff.model.Series; 
    2930import uk.ac.cardiff.model.Graph.AggregatorGraphModel; 
    3031import uk.ac.cardiff.model.sql.SQLFilter; 
     
    6869                statistic.setEntryHandler(entryHandler); 
    6970                Boolean success = invoke(statistic); 
    70                 log.info("Statistic succedded " + success); 
     71                log.info("Statistic {} succedded {}",statistic.getStatisticParameters().getUnitName(),success); 
    7172                if (success) { 
    7273                        // now send through post processor 
     
    7475                        return statistic.constructGraphModel(); 
    7576                } 
     77                //always reset the observationseries for the statistic, so the next execution is not 
     78                //an accumulation of the ones before it 
     79                statistic.getObservationSeries().clear(); 
    7680                return null; 
    7781        } 
     
    9296                try { 
    9397                        List<MethodParameter> params = statistic.getStatisticParameters().getMethodParams(); 
    94                         SQLFilter sqlFilter = statistic.getStatisticParameters().getSqlFilter(); 
    95                         String whereClause=null; 
    96                         if (sqlFilter !=null){ 
    97                                 SQLFilterConstructor sqlConstructor = new SQLFilterConstructor(sqlFilter); 
    98                                 whereClause = sqlConstructor.convertFilterToString(); 
     98                        List<Series> listOfSeries = statistic.getStatisticParameters().getSeries(); 
     99                        boolean success = true; 
     100                        for (Series series : listOfSeries){ 
     101                                SQLFilter sqlFilter = statistic.getStatisticParameters().getSqlFilter(); 
     102                                String whereClause=null; 
     103                                if (sqlFilter !=null){ 
     104                                        SQLFilterConstructor sqlConstructor = new SQLFilterConstructor(sqlFilter); 
     105                                        whereClause = sqlConstructor.convertFilterToString(); 
     106                                } 
     107                                Object[] paramsO = new Object[params.size() + 1]; 
     108                                for (int i = 0; i < paramsO.length-1; i++) { 
     109                                        paramsO[i] = params.get(i).getParameter(); 
     110                                } 
     111                                if (whereClause!=null) 
     112                                        paramsO[paramsO.length - 1] = whereClause; 
     113                                else 
     114                                        paramsO[paramsO.length - 1] = new String(); 
     115                                success= invoke(statistic.getStatisticParameters().getMethodName(), paramsO, statistic); 
    99116                        } 
    100                         Object[] paramsO = new Object[params.size() + 1]; 
    101                         for (int i = 0; i < paramsO.length-1; i++) { 
    102                                 paramsO[i] = params.get(i).getParameter(); 
    103                         } 
    104                         if (whereClause!=null) 
    105                                 paramsO[paramsO.length - 1] = whereClause; 
    106                         else 
    107                                 paramsO[paramsO.length - 1] = new String(); 
    108                         return invoke(statistic.getStatisticParameters().getMethodName(), paramsO, statistic); 
     117                        return success; 
    109118                } catch (Exception e) { 
    110119                        log.error("Failed to invoke statistics {} -> {}",statistic.getStatisticParameters().getUnitName(),e.getMessage()); 
     
    128137                        return success; 
    129138                } catch (Throwable e) { 
    130                         log.error("Field name '" + fieldname + "' does not match internal model attribute"); 
     139                        log.error("Failed to invoke statistics {} -> {}",fieldname,e.getMessage()); 
    131140                        e.printStackTrace(); 
    132141                        // System.exit(1); 
  • raptor-mua/trunk/src/main/java/uk/ac/cardiff/raptormua/model/PersistantEntryHandler.java

    r495 r510  
    8787        @Override 
    8888        public List query(String query) { 
    89                 log.debug("SQL query to entry handler [{}]",query); 
     89            //log.debug("SQL query to entry handler [{}]",query); 
    9090                return dataConnection.runQuery(query, null); 
    9191        } 
     
    9999        @Override 
    100100        public Object queryUnique(String query) { 
    101                 log.debug("SQL query to entry handler [{}]",query); 
     101                //log.debug("SQL query to entry handler [{}]",query); 
    102102                return dataConnection.runQueryUnique(query, null); 
    103103        } 
Note: See TracChangeset for help on using the changeset viewer.