BrightSide Workbench Full Report + Source Code
ServiceIndicator.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.indicator;
20 
21 import java.time.Duration;
22 import java.time.Instant;
23 import java.util.stream.Stream;
24 import org.turro.string.Strings;
25 import org.turro.annotation.ElephantIndicator;
26 import org.turro.contacts.ContactService;
27 import org.turro.contacts.db.ContactsPU;
28 import org.turro.elephant.db.WhereClause;
29 import org.turro.jpa.Dao;
30 import org.turro.matching.GenericMatching;
31 import org.turro.matching.IMatching;
32 
37 @ElephantIndicator
38 public class ServiceIndicator extends AbstractIndicator {
39 
40  public ServiceIndicator() {
41  root = "service";
42  }
43 
44  @Override
45  protected void initVariables() {
46  addVariable("participation", true, VariableType.MATCHING_VARIABLE);
47  addVariable("daysCreation", VariableType.RANKING_VARIABLE);
51  }
52 
53  @Override
54  protected void initExternalVariables() {
55  addExternalVariable("generic", "stars");
56  addExternalVariable("generic", "comments");
57  addExternalVariable("forum", "topics");
58  addExternalVariable("forum", "posts");
59  addExternalVariable("commons", "followed");
60  addExternalVariable("commons", "seen");
61  addExternalVariable("commons", "like");
62  }
63 
64  @Override
65  protected Dao createDao() {
66  return new ContactsPU();
67  }
68 
69  @Override
70  public double getValue(IndicatorVariable variable, Object entity) {
71  if((entity instanceof ContactService) && itsMine(variable)) {
72  ContactService service = (ContactService) entity;
73  switch(variable.getName()) {
74  case "daysCreation":
75  return service.getCreation() != null ? Duration.between(service.getCreation().toInstant(), Instant.now()).toDays() : 9999.9;
76  case "daysStart":
77  return service.getStartDate()!= null ? Duration.between(service.getStartDate().toInstant(), Instant.now()).toDays() : 9999.9;
78  case "daysEnd":
79  return service.getEndDate()!= null ? Duration.between(service.getEndDate().toInstant(), Instant.now()).toDays() : 9999.9;
80  case "words":
81  return countWords(service);
82  }
83  }
84  return 0.0;
85  }
86 
87  @Override
88  protected void processRankingVariable(Dao dao, IPreprocessor preprocessor, IndicatorVariable variable, String entityRoot) {
89  }
90 
91  @Override
92  protected void processMatchingVariable(Dao dao, IPreprocessor preprocessor, IndicatorVariable variable, String entityRoot, String relatedRoot) {
93  WhereClause wc = null;
94  if("contact".equals(relatedRoot)) {
95  switch(variable.getName()) {
96  case "participation":
97  wc = PreprocessClause.load("ContactService")
98  .setRankingRoot(entityRoot)
99  .setMatchingRoot(relatedRoot)
100  .setRankingField("concat('/service/',id)")
101  .setMatchingField("responsible.id")
102  .setVariable(variable)
103  .setAggregate("count(id)")
104  .getClause();
105  break;
106  }
107  }
108  if(wc != null) {
109  try(Stream<GenericMatching> stream = dao.stream(GenericMatching.class, wc, 1000)) {
110  stream.forEach(r -> {
111  preprocessor.poolInstance(r.copyTo((IMatching) preprocessor.createInstance()));
112  });
113  preprocessor.finishPreprocessor();
114  }
115  }
116  }
117 
118  private double countWords(ContactService service) {
119  double count = 0;
120  if(service.getPlainText() != null) {
121  count += Strings.countWords(service.getPlainText());
122  }
123  return count;
124  }
125 
126 }
void addVariable(String name, VariableType type)
void addExternalVariable(String root, String name)
PreprocessClause setMatchingField(String pathField)
PreprocessClause setVariable(IndicatorVariable variable)
PreprocessClause setMatchingRoot(String entityRoot)
PreprocessClause setAggregate(String aggregate)
PreprocessClause setRankingRoot(String entityRoot)
static PreprocessClause load(String table)
PreprocessClause setRankingField(String pathField)
void processMatchingVariable(Dao dao, IPreprocessor preprocessor, IndicatorVariable variable, String entityRoot, String relatedRoot)
double getValue(IndicatorVariable variable, Object entity)
void processRankingVariable(Dao dao, IPreprocessor preprocessor, IndicatorVariable variable, String entityRoot)
void poolInstance(Object value)