corpus-services  1.0
Report.java
Go to the documentation of this file.
1 
10 package de.uni_hamburg.corpora;
11 
13 import java.text.MessageFormat;
14 import java.text.SimpleDateFormat;
15 import java.util.Collection;
16 import java.util.Map;
17 import java.util.HashMap;
18 import java.util.ArrayList;
19 import java.util.Date;
20 import org.jdom.JDOMException;
21 
36 public class Report {
37 
38  //Anne: Is this the ErrorList in the UML? If so, should we rename here or use StatisticsReport in UML? Or maybe best: ErrorReport?
39  //But what would be the Items in here? ReportItems? Errors? StatisticsStuff?
45  public final String ROOT_BUCKET = "root";
46 
50  private Map<String, Collection<ReportItem>> statistics;
51 
55  private Collection<ReportItem> getOrCreateStatistic(String statId) {
56  if (!statistics.containsKey(statId)) {
57  statistics.put(statId, new ArrayList<ReportItem>());
58  }
59  return statistics.get(statId);
60  }
61 
65  public Report() {
66  statistics = new HashMap<String, Collection<ReportItem>>();
67  }
68 
73  public void merge(Report sr) {
74  for (Map.Entry<String, Collection<ReportItem>> kv
75  : sr.statistics.entrySet()) {
76  if (statistics.containsKey(kv.getKey())) {
77  Collection<ReportItem> c
78  = statistics.get(kv.getKey());
79  c.addAll(kv.getValue());
80  statistics.put(kv.getKey(), c);
81  } else {
82  statistics.put(kv.getKey(), kv.getValue());
83  }
84  }
85  }
86 
94  public void addReportItem(String statId, ReportItem reportItem) {
95  Collection<ReportItem> stat = getOrCreateStatistic(statId);
96  stat.add(reportItem);
97  }
98 
104  public void addCritical(String description) {
105  addCritical(ROOT_BUCKET, description);
106  }
107 
111  public void addCritical(String statId, String description) {
112  Collection<ReportItem> stat = getOrCreateStatistic(statId);
113  stat.add(new ReportItem(ReportItem.Severity.CRITICAL,
114  description));
115  }
116 
122  public void addCritical(String statId, String description, String extraBlah) {
123  addCritical(statId, description + extraBlah);
124  }
125 
129  public void addCritical(String statId, Throwable e, String description, String extrablah) {
130  addCritical(statId, description + "::" + extrablah
131  + "..." + e.getStackTrace()[0]);
132  }
133 
139  public void addCritical(String statId, Throwable e, String description) {
140  addCritical(statId, description + e.getStackTrace()[0]);
141  }
142 
146  public void addCritical(String statId, CorpusData cd, String description) {
147  Collection<ReportItem> stat = getOrCreateStatistic(statId);
148  stat.add(new ReportItem(ReportItem.Severity.CRITICAL,
149  cd.getURL().toString(), description, statId));
150  }
151 
155  public void addFix(String statId, CorpusData cd, String description) {
156  Collection<ReportItem> stat = getOrCreateStatistic(statId);
158  cd.getURL().toString(), description, statId));
159  }
160 
164  public void addWarning(String statId, String description) {
165  Collection<ReportItem> stat = getOrCreateStatistic(statId);
166  stat.add(new ReportItem(ReportItem.Severity.WARNING,
167  description));
168  }
169 
175  public void addWarning(String statId, String description, String extraBlah) {
176  addWarning(statId, description + extraBlah);
177  }
178 
182  public void addWarning(String statId, Throwable e, String description, String extrablah) {
183  addWarning(statId, description + "::" + extrablah
184  + "..." + e.getStackTrace()[0]);
185  }
186 
190  public void addWarning(String statId, CorpusData cd, String description) {
191  Collection<ReportItem> stat = getOrCreateStatistic(statId);
192  stat.add(new ReportItem(ReportItem.Severity.WARNING,
193  cd.getURL().toString(), description, statId));
194  }
195 
199  public void addMissing(String statId, String description) {
200  Collection<ReportItem> stat = getOrCreateStatistic(statId);
201  stat.add(new ReportItem(ReportItem.Severity.MISSING,
202  description));
203  }
204 
208  public void addMissing(String statId, CorpusData cd, String description) {
209  Collection<ReportItem> stat = getOrCreateStatistic(statId);
210  stat.add(new ReportItem(ReportItem.Severity.MISSING,
211  cd.getURL().toString(), description, statId));
212  }
213 
217  public void addCorrect(String statId, String description) {
218  Collection<ReportItem> stat = getOrCreateStatistic(statId);
219  stat.add(new ReportItem(ReportItem.Severity.CORRECT,
220  description));
221  }
222 
227  public void addCorrect(String statId, String filename, String description) {
228  Collection<ReportItem> stat = getOrCreateStatistic(statId);
229  stat.add(new ReportItem(ReportItem.Severity.CORRECT, filename,
230  description, statId));
231  }
232 
236  public void addCorrect(String statId, CorpusData cd, String description) {
237  Collection<ReportItem> stat = getOrCreateStatistic(statId);
238  stat.add(new ReportItem(ReportItem.Severity.CORRECT,
239  cd.getURL().toString(), description, statId));
240  }
241 
245  public void addNote(String statId, String description) {
246  Collection<ReportItem> stat = getOrCreateStatistic(statId);
247  stat.add(new ReportItem(ReportItem.Severity.NOTE,
248  description));
249  }
250 
254  public void addNote(String statId, Throwable e, String description) {
255  addNote(statId, description + "..." + e.getStackTrace()[0]);
256  }
257 
261  public void addNote(String statId, Throwable e, String description, String extrablah) {
262  addNote(statId, description + "::" + extrablah
263  + "..." + e.getStackTrace()[0]);
264  }
265 
269  public void addNote(String statId, String description, String extraBlah) {
270  addNote(statId, description + "::" + extraBlah);
271  }
272 
276  public void addNote(String statId, CorpusData cd, String description) {
277  Collection<ReportItem> stat = getOrCreateStatistic(statId);
278  stat.add(new ReportItem(ReportItem.Severity.NOTE,
279  cd.getURL().toString(), description, statId));
280  }
281 
287  public void addException(Throwable e, String description) {
288  addException(ROOT_BUCKET, e, description);
289  }
290 
297  public void addException(String statId, Throwable e, String description) {
298  Collection<ReportItem> stat = getOrCreateStatistic(statId);
299  stat.add(new ReportItem(ReportItem.Severity.CRITICAL,
300  e, description));
301  }
302 
306  public void addException(String statId, Throwable e, String description,
307  String extrablah) {
308  addException(statId, e, description + "\n\t" + extrablah);
309  }
310 
314  public void addException(Throwable e, String statId, CorpusData cd, String description) {
315  Collection<ReportItem> stat = getOrCreateStatistic(statId);
316  stat.add(new ReportItem(ReportItem.Severity.CRITICAL,
317  e, cd.getURL().toString(), description));
318  }
319 
323  public String getSummaryLine(String statId) {
324  int good = 0;
325  int severe = 0;
326  int badish = 0;
327  int unk = 0;
328  Collection<ReportItem> stats = statistics.get(statId);
329  for (ReportItem s : stats) {
330  if (s.isSevere()) {
331  severe += 1;
332  } else if (s.isBad()) {
333  badish += 1;
334  } else if (s.isGood()) {
335  good += 1;
336  } else {
337  unk += 1;
338  }
339  }
340  int totes = good + severe + badish + unk;
341  return MessageFormat.format(" {0}: {1} %: {2} OK, {3} bad, "
342  + "{4} warnings and {5} unknown. "
343  + "= {6} items.\n", statId, 100 * good / totes,
344  good, severe, badish, unk, totes);
345  }
346 
350  public String getAllAsSummaryLine() {
351  int good = 0;
352  int severe = 0;
353  int badish = 0;
354  int unk = 0;
355  Collection<ReportItem> stats = new ArrayList<ReportItem>();
356  for (String statId : statistics.keySet()) {
357  //System.out.println("key : " + statId);
358  //System.out.println("value : " + statistics.get(statId));
359  stats.addAll(statistics.get(statId));
360  }
361  for (ReportItem s : stats) {
362  if (s.isSevere()) {
363  severe += 1;
364  } else if (s.isBad()) {
365  badish += 1;
366  } else if (s.isGood()) {
367  good += 1;
368  } else {
369  unk += 1;
370  }
371  }
372  int totes = good + severe + badish + unk;
373  if (totes > 0) {
374  return MessageFormat.format(" {0}: {1} %: {2} OK, {3} bad, "
375  + "{4} warnings and {5} unknown. "
376  + "= {6} items.\n", "Total", 100 * good / totes,
377  good, severe, badish, unk, totes);
378  } else {
379  return "no elements present.";
380  }
381  }
382 
386  public String getSummaryLines() {
387  String rv = "";
388  for (Map.Entry<String, Collection<ReportItem>> kv
389  : statistics.entrySet()) {
390  rv += getSummaryLine(kv.getKey());
391  }
392  //add summary of all buckets in one line
393  rv += getAllAsSummaryLine();
394  return rv;
395  }
396 
401  public String getErrorReport(String statId) {
402  Collection<ReportItem> stats = statistics.get(statId);
403  String rv = MessageFormat.format("{0}:\n", statId);
404  int suppressed = 0;
405  for (ReportItem s : stats) {
406  if (s.isSevere()) {
407  rv += s.getSummary() + "\n";
408  } else {
409  suppressed += 1;
410  }
411  }
412  if (suppressed != 0) {
413  rv += MessageFormat.format("{0} warnings and notes hidden\n",
414  suppressed);
415  }
416  return rv;
417  }
418 
423  public String getWarningReport(String statId) {
424  Collection<ReportItem> stats = statistics.get(statId);
425  String rv = MessageFormat.format("{0}:\n", statId);
426  int suppressed = 0;
427  for (ReportItem s : stats) {
428  if (s.isBad()) {
429  rv += s.getSummary() + "\n";
430  } else {
431  suppressed += 1;
432  }
433  }
434  if (suppressed != 0) {
435  rv += MessageFormat.format("{0} notes hidden\n",
436  suppressed);
437  }
438  return rv;
439  }
440 
444  public String getErrorReports() {
445  String rv = "Errors:\n";
446  for (Map.Entry<String, Collection<ReportItem>> kv
447  : statistics.entrySet()) {
448  rv += getErrorReport(kv.getKey());
449  }
450  return rv;
451  }
452 
456  public String getWarningReports() {
457  String rv = "Warnings:\n";
458  for (Map.Entry<String, Collection<ReportItem>> kv
459  : statistics.entrySet()) {
460  rv += getWarningReport(kv.getKey());
461  }
462  return rv;
463  }
464 
468  public String getFullReport(String statId) {
469  Collection<ReportItem> stats = statistics.get(statId);
470  String rv = MessageFormat.format("{0}:\n", statId);
471  for (ReportItem s : stats) {
472  if (s.isGood()) {
473  rv += s.toString() + "\n";
474  }
475  }
476  for (ReportItem s : stats) {
477  if (s.isBad()) {
478  rv += s.toString() + "\n";
479  }
480  }
481  return rv;
482  }
483 
487  public String getFullReports() {
488  String rv = "All reports\n";
489  for (Map.Entry<String, Collection<ReportItem>> kv
490  : statistics.entrySet()) {
491  rv += getFullReport(kv.getKey());
492  }
493  return rv;
494  }
495 
499  public Collection<ReportItem> getRawStatistics() {
500  Collection<ReportItem> allStats = new ArrayList<ReportItem>();
501  for (Map.Entry<String, Collection<ReportItem>> kv
502  : statistics.entrySet()) {
503  allStats.addAll(kv.getValue());
504  }
505  return allStats;
506  }
507 
511  public Collection<ReportItem> getErrorStatistics() {
512  Collection<ReportItem> errorStats = new ArrayList<ReportItem>();
513  Collection<ReportItem> onlyerrorStats = new ArrayList<ReportItem>();
514  for (Map.Entry<String, Collection<ReportItem>> kv
515  : statistics.entrySet()) {
516  errorStats.addAll(kv.getValue());
517  }
518  for (ReportItem ri : errorStats) {
519  if (ri.getSeverity().equals(Severity.CRITICAL) || ri.getSeverity().equals(Severity.WARNING) || ri.getSeverity().equals(Severity.MISSING)) {
520  //now make the Location relative to the base dir
521  ri.getLocation();
522  onlyerrorStats.add(ri);
523  }
524 
525  }
526  return onlyerrorStats;
527  }
528 
532  public String getFixJson(Corpus corpus) throws JDOMException {
533  String rv = "";
534  for (Map.Entry<String, Collection<ReportItem>> kfj
535  : statistics.entrySet()) {
536  rv += getFixLine(kfj.getKey(), corpus);
537  }
538  rv = rv + "\n";
539  return rv;
540  }
541 
545  public String getFixJson() {
546  String rv = "";
547  for (Map.Entry<String, Collection<ReportItem>> kfj
548  : statistics.entrySet()) {
549  rv += getFixLine(kfj.getKey());
550  }
551  rv = rv + "\n";
552  return rv;
553  }
554 
558  public String getFixLine(String statId, Corpus corpus) throws JDOMException {
559  Collection<ReportItem> stats = statistics.get(statId);
560  int fix = 0;
561  int good = 0;
562  int severe = 0;
563  int badish = 0;
564  int unk = 0;
565  String line = "";
566  for (ReportItem s : stats) {
567  if (s.isFix()) {
568  fix += 1;
569  } else if (s.isSevere()) {
570  severe += 1;
571  } else if (s.isBad()) {
572  badish += 1;
573  } else if (s.isGood()) {
574  good += 1;
575  } else {
576  unk += 1;
577  }
578  }
579  //"2020-02-17T11:41:00Z"
580  //now add the T that is needed for Kibana between date and time
581  String patternDate = "yyyy-MM-dd";
582  SimpleDateFormat simpleDateFormat = new SimpleDateFormat(patternDate);
583  String date = simpleDateFormat.format(new Date());
584  String patternTime = "hh:mm:ssZ";
585  SimpleDateFormat simpleTimeFormat = new SimpleDateFormat(patternTime);
586  String time = simpleTimeFormat.format(new Date());
587  String dateTime = date + "T" + time;
588  //System.out.println(dateTime);
589  String corpusname = corpus.getCorpusName();
590  //now we also need
591  /*
592  number of words whole corpus
593  number of sentences whole corpus
594  number of transcriptions
595  number of speakers whole corpus
596  number of communications whole corpus
597  */
598  String corpuswords = corpus.getCorpusWords();
599  String corpussents = corpus.getCorpusSentenceNumber();
600  String corpustrans = corpus.getCorpusTranscriptionNumber();
601  String corpusspeaks = corpus.getCorpusSpeakerNumber();
602  String corpuscomms = corpus.getCorpusCommunicationNumber();
603  //"corpus-words":1234,"corpus-sentences":2345,"corpus-transcriptions":12,"corpus-speakers":34,"corpus-transcriptions":12
604  line = "{ \"index\": { \"_index\": \"inel-curation\", \"_type\": \"corpus-service-report\" }}\n{ \"doc\": { \"corpus\": \""
605  + corpusname + "\", \"name\": \"" + statId + "\", \"method\": \"fix\", \"date\": \""
606  + dateTime + "\", \"ok\": " + good + ", \"bad\": " + severe + ", \"fixed\": "
607  + fix + ", \"corpus-words\": " + corpuswords + ", \"corpus-sentences\": " + corpussents + ", \"corpus-transcriptions\": " + corpustrans
608  + ", \"corpus-speaker\": " + corpusspeaks + ", \"corpus-communications\": " + corpuscomms + " }}\n";
609  return line;
610  }
611 
615  public String getFixLine(String statId) {
616  Collection<ReportItem> stats = statistics.get(statId);
617  int fix = 0;
618  int good = 0;
619  int severe = 0;
620  int badish = 0;
621  int unk = 0;
622  String line = "";
623  for (ReportItem s : stats) {
624  if (s.isFix()) {
625  fix += 1;
626  } else if (s.isSevere()) {
627  severe += 1;
628  } else if (s.isBad()) {
629  badish += 1;
630  } else if (s.isGood()) {
631  good += 1;
632  } else {
633  unk += 1;
634  }
635  }
636  //"2020-02-17T11:41:00Z"
637  //now add the T that is needed for Kibana between date and time
638  String patternDate = "yyyy-MM-dd";
639  SimpleDateFormat simpleDateFormat = new SimpleDateFormat(patternDate);
640  String date = simpleDateFormat.format(new Date());
641  String patternTime = "hh:mm:ssZ";
642  SimpleDateFormat simpleTimeFormat = new SimpleDateFormat(patternTime);
643  String time = simpleTimeFormat.format(new Date());
644  String dateTime = date + "T" + time;
645  //System.out.println(dateTime);
646  line = "{ \"index\": { \"_index\": \"inel-curation\", \"_type\": \"corpus-service-report\" }}\n{\"doc\": { \"name\": \"" + statId + "\", \"method\": \"fix\", \"date\": \"" + dateTime + "\", \"ok\": " + good + ", \"bad\": " + severe + ", \"fixed\": " + fix + " }}\n";
647  return line;
648  }
649 
650 }
void addReportItem(String statId, ReportItem reportItem)
Definition: Report.java:94
void addWarning(String statId, CorpusData cd, String description)
Definition: Report.java:190
void addMissing(String statId, String description)
Definition: Report.java:199
void addNote(String statId, String description)
Definition: Report.java:245
void addNote(String statId, CorpusData cd, String description)
Definition: Report.java:276
String getErrorReport(String statId)
Definition: Report.java:401
void addCritical(String statId, String description)
Definition: Report.java:111
void merge(Report sr)
Definition: Report.java:73
String getWarningReport(String statId)
Definition: Report.java:423
void addWarning(String statId, String description, String extraBlah)
Definition: Report.java:175
void addCorrect(String statId, String filename, String description)
Definition: Report.java:227
void addCritical(String statId, Throwable e, String description, String extrablah)
Definition: Report.java:129
void addCritical(String statId, CorpusData cd, String description)
Definition: Report.java:146
Collection< ReportItem > getRawStatistics()
Definition: Report.java:499
void addWarning(String statId, Throwable e, String description, String extrablah)
Definition: Report.java:182
void addNote(String statId, String description, String extraBlah)
Definition: Report.java:269
void addCorrect(String statId, CorpusData cd, String description)
Definition: Report.java:236
void addCritical(String description)
Definition: Report.java:104
void addException(Throwable e, String statId, CorpusData cd, String description)
Definition: Report.java:314
void addNote(String statId, Throwable e, String description)
Definition: Report.java:254
void addWarning(String statId, String description)
Definition: Report.java:164
void addMissing(String statId, CorpusData cd, String description)
Definition: Report.java:208
void addCorrect(String statId, String description)
Definition: Report.java:217
void addNote(String statId, Throwable e, String description, String extrablah)
Definition: Report.java:261
String getSummaryLine(String statId)
Definition: Report.java:323
String getFullReport(String statId)
Definition: Report.java:468
String getFixJson(Corpus corpus)
Definition: Report.java:532
String getFixLine(String statId)
Definition: Report.java:615
void addException(Throwable e, String description)
Definition: Report.java:287
void addCritical(String statId, Throwable e, String description)
Definition: Report.java:139
void addException(String statId, Throwable e, String description)
Definition: Report.java:297
Collection< ReportItem > getErrorStatistics()
Definition: Report.java:511
String getFixLine(String statId, Corpus corpus)
Definition: Report.java:558
void addException(String statId, Throwable e, String description, String extrablah)
Definition: Report.java:306
void addCritical(String statId, String description, String extraBlah)
Definition: Report.java:122
void addFix(String statId, CorpusData cd, String description)
Definition: Report.java:155