Search in sources :

Example 1 with MethodDescriptor2

use of io.codekvast.dashboard.dashboard.model.methods.MethodDescriptor2 in project codekvast by crispab.

the class DashboardServiceImpl method getMethods2.

@Override
@Transactional(readOnly = true)
public GetMethodsResponse2 getMethods2(@Valid GetMethodsRequest request) {
    long startedAt = clock.millis();
    Long customerId = customerIdProvider.getCustomerId();
    PricePlan pricePlan = customerService.getCustomerDataByCustomerId(customerId).getPricePlan();
    MapSqlParameterSource params = new MapSqlParameterSource();
    params.addValue("latestCollectedSince", clock.instant().minus(request.getMinCollectedDays(), DAYS));
    params.addValue("now", new Timestamp(clock.millis()));
    params.addValue("customerId", customerId);
    String whereClause = "i.customerId = :customerId AND m.modifiers NOT LIKE '%abstract%'";
    String normalizedSignature = request.getNormalizedSignature();
    if (!normalizedSignature.equals("%")) {
        params.addValue("signature", normalizedSignature);
        whereClause += // Make it case-insensitive
        " AND m.signature LIKE :signature COLLATE utf8mb4_general_ci";
    }
    if (request.getApplications() != null && !request.getApplications().isEmpty()) {
        params.addValue("applicationIds", translateNamesToIds("applications", "name", request.getApplications()));
        whereClause += " AND i.applicationId IN (:applicationIds)";
    }
    if (request.getEnvironments() != null && !request.getEnvironments().isEmpty()) {
        params.addValue("environmentIds", translateNamesToIds("environments", "name", request.getEnvironments()));
        whereClause += " AND i.environmentId IN (:environmentIds)";
    }
    if (request.getLocations() != null && !request.getLocations().isEmpty()) {
        params.addValue("locationIds", translateNamesToIds("method_locations", "locationNoVersion", request.getLocations()));
        whereClause += " AND ml.id IN (:locationIds)";
    }
    String sql = "SELECT m.id, m.signature, " + "MAX(i.createdAt) AS latestCollectedSince, " + "MAX(i.status) AS status, " + "MAX(i.invokedAtMillis) AS lastInvokedAtMillis, " + "MAX(i.timestamp) AS lastPublishedAt, " + "m.annotation AS methodAnnotation, " + "ml.annotation AS methodLocationAnnotation, " + "p.annotation AS packageAnnotation, " + "t.annotation AS typeAnnotation " + "FROM invocations i " + "  INNER JOIN methods m ON i.methodId = m.id AND m.customerId = i.customerId " + "  INNER JOIN types t ON m.declaringType = t.name AND t.customerId = m.customerId " + "  INNER JOIN packages p ON m.packageName = p.name AND p.customerId = m.customerId " + "  LEFT JOIN method_locations ml ON ml.methodId = m.id AND ml.customerId = m.customerId " + "WHERE " + whereClause + " GROUP BY i.methodId " + "HAVING latestCollectedSince <= :latestCollectedSince " + "ORDER BY lastInvokedAtMillis ";
    List<MethodDescriptor2> methods = new ArrayList<>(request.getMaxResults());
    namedParameterJdbcTemplate.query(sql, params, rs -> {
        if (methods.size() >= request.getMaxResults()) {
            logger.trace("Ignoring row {}, since max result already achieved", rs.getRow());
            return;
        }
        String signature = rs.getString("signature");
        SignatureStatus2 status = SignatureStatus2.valueOf(rs.getString("status"));
        if (request.isSuppressUntrackedMethods() && !status.isTracked()) {
            logger.trace("Suppressing untracked method {} with status {}", signature, status);
            return;
        }
        Long lastInvokedAtMillis = pricePlan.adjustTimestampMillis(rs.getLong("lastInvokedAtMillis"), clock);
        if (lastInvokedAtMillis > request.getOnlyInvokedBeforeMillis()) {
            logger.trace("Suppressing method invoked after requested range");
            return;
        }
        if (lastInvokedAtMillis < request.getOnlyInvokedAfterMillis()) {
            logger.trace("Suppressing method invoked before requested range");
            return;
        }
        methods.add(MethodDescriptor2.builder().id(rs.getLong("id")).signature(signature).trackedPercent(status.isTracked() ? 100 : 0).collectedDays(pricePlan.adjustCollectedDays(getCollectedDays(rs.getTimestamp("latestCollectedSince")))).lastInvokedAtMillis(lastInvokedAtMillis).collectedToMillis(rs.getTimestamp("lastPublishedAt").getTime()).methodAnnotation(rs.getString("methodAnnotation")).methodLocationAnnotation(rs.getString("methodLocationAnnotation")).typeAnnotation(rs.getString("typeAnnotation")).packageAnnotation(rs.getString("packageAnnotation")).build());
    });
    methods.sort(Comparator.comparing(MethodDescriptor2::getSignature));
    long queryTimeMillis = clock.millis() - startedAt;
    logger.debug("Processed {} in {} ms.", request, queryTimeMillis);
    return GetMethodsResponse2.builder().timestamp(startedAt).request(request).numMethods(methods.size()).methods(methods).queryTimeMillis(queryTimeMillis).build();
}
Also used : SignatureStatus2(io.codekvast.javaagent.model.v2.SignatureStatus2) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) ArrayList(java.util.ArrayList) MethodDescriptor2(io.codekvast.dashboard.dashboard.model.methods.MethodDescriptor2) Timestamp(java.sql.Timestamp) PricePlan(io.codekvast.common.customer.PricePlan) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

PricePlan (io.codekvast.common.customer.PricePlan)1 MethodDescriptor2 (io.codekvast.dashboard.dashboard.model.methods.MethodDescriptor2)1 SignatureStatus2 (io.codekvast.javaagent.model.v2.SignatureStatus2)1 Timestamp (java.sql.Timestamp)1 ArrayList (java.util.ArrayList)1 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)1 Transactional (org.springframework.transaction.annotation.Transactional)1