@Override
  public int getTotalProgressCount(
      HProjectIteration sourceVersion, HProjectIteration targetVersion) {
    int matchCount = getTotalMatchCount(sourceVersion.getId(), targetVersion.getId());

    List<HLocale> locales =
        getSupportedLocales(targetVersion.getProject().getSlug(), targetVersion.getSlug());

    return matchCount * locales.size();
  }
Ejemplo n.º 2
0
  @Test
  public void traverseProjectGraph() throws Exception {
    EntityManager em = getEm();
    HIterationProject project = em.find(HIterationProject.class, 1l);
    assertThat(project, notNullValue());

    List<HProjectIteration> projectTargets = project.getProjectIterations();
    assertThat("Project should have 2 targets", projectTargets.size(), is(2));

    HProjectIteration target = projectTargets.get(0);
    assertThat("Expect target with id 1", target.getId(), is(1l));
  }
 protected int mergeTranslationBatch(
     HProjectIteration sourceVersion,
     HProjectIteration targetVersion,
     List<HLocale> supportedLocales,
     boolean useNewerTranslation,
     int offset,
     int batchSize) {
   try {
     return runInTransaction(
         () ->
             this.mergeTranslations(
                 sourceVersion.getId(),
                 targetVersion.getId(),
                 offset,
                 batchSize,
                 useNewerTranslation,
                 supportedLocales));
   } catch (Exception e) {
     log.warn("exception during copy text flow target", e);
     return 0;
   }
 }
  @Override
  @Async
  public Future<Void> startMergeTranslations(
      String sourceProjectSlug,
      String sourceVersionSlug,
      String targetProjectSlug,
      String targetVersionSlug,
      boolean useNewerTranslation,
      MergeTranslationsTaskHandle handle) {

    HProjectIteration sourceVersion =
        projectIterationDAO.getBySlug(sourceProjectSlug, sourceVersionSlug);

    if (sourceVersion == null) {
      log.error("Cannot find source version of {}:{}", sourceProjectSlug, sourceVersionSlug);
      return AsyncTaskResult.taskResult();
    }

    HProjectIteration targetVersion =
        projectIterationDAO.getBySlug(targetProjectSlug, targetVersionSlug);

    if (targetVersion == null) {
      log.error("Cannot find target version of {}:{}", targetProjectSlug, targetVersionSlug);
      return AsyncTaskResult.taskResult();
    }

    if (isVersionsEmpty(sourceVersion, targetVersion)) {
      return AsyncTaskResult.taskResult();
    }

    if (getSupportedLocales(targetProjectSlug, targetVersionSlug).isEmpty()) {
      log.error(
          "No locales enabled in target version of {} [{}]", targetProjectSlug, targetVersionSlug);
      return AsyncTaskResult.taskResult();
    }

    Optional<MergeTranslationsTaskHandle> taskHandleOpt = Optional.fromNullable(handle);

    if (taskHandleOpt.isPresent()) {
      prepareMergeTranslationsHandle(sourceVersion, targetVersion, taskHandleOpt.get());
    }

    Stopwatch overallStopwatch = Stopwatch.createStarted();
    log.info(
        "merge translations start: from {} to {}",
        sourceProjectSlug + ":" + sourceVersionSlug,
        targetProjectSlug + ":" + targetVersionSlug);

    int startCount = 0;
    int totalCount = getTotalMatchCount(sourceVersion.getId(), targetVersion.getId());

    List<HLocale> supportedLocales =
        getSupportedLocales(targetVersion.getProject().getSlug(), targetVersion.getSlug());

    while (startCount < totalCount) {
      int processedCount =
          mergeTranslationBatch(
              sourceVersion,
              targetVersion,
              supportedLocales,
              useNewerTranslation,
              startCount,
              TEXTFLOWS_PER_BATCH);
      if (taskHandleOpt.isPresent()) {
        taskHandleOpt.get().increaseProgress(processedCount);
      }

      startCount += TEXTFLOWS_PER_BATCH;
      textFlowDAO.clear();
    }
    versionStateCacheImpl.clearVersionStatsCache(targetVersion.getId());
    log.info(
        "merge translation end: from {} to {}, {}",
        sourceProjectSlug + ":" + sourceVersionSlug,
        targetProjectSlug + ":" + targetVersionSlug,
        overallStopwatch);

    return AsyncTaskResult.taskResult();
  }
  // TODO Need to refactor this method to get Message statistic by default.
  // This is to be consistance with UI which uses message stats, and for
  // calculating remaining hours.
  @Override
  public ContainerTranslationStatistics getStatistics(
      String projectSlug,
      String iterationSlug,
      boolean includeDetails,
      boolean includeWordStats,
      String[] locales) {
    LocaleId[] localeIds;

    // if no locales are specified, search in all locales
    if (locales.length == 0) {
      List<HLocale> iterationLocales =
          localeServiceImpl.getSupportedLangugeByProjectIteration(projectSlug, iterationSlug);
      localeIds = new LocaleId[iterationLocales.size()];
      for (int i = 0, iterationLocalesSize = iterationLocales.size();
          i < iterationLocalesSize;
          i++) {
        HLocale loc = iterationLocales.get(i);
        localeIds[i] = loc.getLocaleId();
      }
    } else {
      localeIds = new LocaleId[locales.length];
      for (int i = 0; i < locales.length; i++) {
        localeIds[i] = new LocaleId(locales[i]);
      }
    }

    HProjectIteration iteration = projectIterationDAO.getBySlug(projectSlug, iterationSlug);

    if (iteration == null) {
      throw new NoSuchEntityException(projectSlug + "/" + iterationSlug);
    }

    Map<String, TransUnitCount> transUnitIterationStats =
        projectIterationDAO.getAllStatisticsForContainer(iteration.getId());
    Map<String, TransUnitWords> wordIterationStats =
        projectIterationDAO.getAllWordStatsStatistics(iteration.getId());

    ContainerTranslationStatistics iterationStats = new ContainerTranslationStatistics();
    iterationStats.setId(iterationSlug);
    iterationStats.addRef(
        new Link(
            URI.create(zPathService.generatePathForProjectIteration(iteration)),
            "statSource",
            "PROJ_ITER"));
    long iterationTotalMssgs =
        projectIterationDAO.getTotalMessageCountForIteration(iteration.getId());
    long iterationTotalWords = projectIterationDAO.getTotalWordCountForIteration(iteration.getId());

    for (LocaleId locId : localeIds) {
      // trans unit level stats
      TransUnitCount count = transUnitIterationStats.get(locId.getId());
      // Stats might not return anything if nothing is translated
      if (count == null) {
        count = new TransUnitCount(0, 0, (int) iterationTotalMssgs);
      }

      HTextFlowTarget target =
          localeServiceImpl.getLastTranslated(projectSlug, iterationSlug, locId);

      String lastModifiedBy = "";
      Date lastModifiedDate = null;

      if (target != null) {
        lastModifiedDate = target.getLastChanged();
        if (target.getLastModifiedBy() != null) {
          lastModifiedBy = target.getLastModifiedBy().getAccount().getUsername();
        }
      }

      TransUnitWords wordCount = wordIterationStats.get(locId.getId());
      if (wordCount == null) {
        wordCount = new TransUnitWords(0, 0, (int) iterationTotalWords);
      }

      TranslationStatistics transUnitStats =
          getMessageStats(count, locId, lastModifiedDate, lastModifiedBy);
      transUnitStats.setRemainingHours(StatisticsUtil.getRemainingHours(wordCount));
      iterationStats.addStats(transUnitStats);

      // word level stats
      if (includeWordStats) {
        TranslationStatistics wordsStats =
            getWordsStats(wordCount, locId, lastModifiedDate, lastModifiedBy);
        wordsStats.setRemainingHours(StatisticsUtil.getRemainingHours(wordCount));
        iterationStats.addStats(wordsStats);
      }
    }

    // TODO Do in a single query
    if (includeDetails) {
      for (String docId : iteration.getDocuments().keySet()) {
        iterationStats.addDetailedStats(
            this.getStatistics(projectSlug, iterationSlug, docId, includeWordStats, locales));
      }
    }

    return iterationStats;
  }