Search in sources :

Example 1 with DateTimeRange

use of au.gov.asd.tac.constellation.plugins.parameters.types.DateTimeRange in project constellation by constellation-app.

the class LayerByTimePlugin method updateParameters.

@Override
public void updateParameters(final Graph graph, final PluginParameters parameters) {
    final ReadableGraph rg = graph.getReadableGraph();
    final List<String> dateTimeAttributes = new ArrayList<>();
    try {
        final int attributeCount = rg.getAttributeCount(GraphElementType.TRANSACTION);
        for (int i = 0; i < attributeCount; i++) {
            final int attrId = rg.getAttribute(GraphElementType.TRANSACTION, i);
            final Attribute attr = new GraphAttribute(rg, attrId);
            if (attr.getAttributeType().equals(ZonedDateTimeAttributeDescription.ATTRIBUTE_NAME)) {
                dateTimeAttributes.add(attr.getName());
            }
        }
    } finally {
        rg.release();
    }
    SingleChoiceParameterType.setOptions(dtAttrParam, dateTimeAttributes);
    parameters.addController(DATETIME_ATTRIBUTE_PARAMETER_ID, (masterId, paramMap, change) -> {
        if (change == ParameterChange.VALUE) {
            final String attrName = paramMap.get(DATETIME_ATTRIBUTE_PARAMETER_ID).getStringValue();
            final ReadableGraph rg2 = graph.getReadableGraph();
            try {
                final int attrId = rg2.getAttribute(GraphElementType.TRANSACTION, attrName);
                if (attrId == Graph.NOT_FOUND) {
                    return;
                }
                ZonedDateTime min = ZonedDateTime.ofInstant(Instant.now(), TimeZoneUtilities.UTC);
                ZonedDateTime max = ZonedDateTime.ofInstant(Instant.EPOCH, TimeZoneUtilities.UTC);
                final int txCount = rg.getTransactionCount();
                boolean nonNullDateTimeFound = false;
                for (int position = 0; position < txCount; position++) {
                    final int txId = rg.getTransaction(position);
                    // Ignore zero and "null" dates.
                    final ZonedDateTime dt = rg.getObjectValue(attrId, txId);
                    if (dt != null) {
                        nonNullDateTimeFound = true;
                        if (dt.toInstant().isBefore(min.toInstant())) {
                            min = dt;
                        }
                        if (dt.toInstant().isAfter(max.toInstant())) {
                            max = dt;
                        }
                    }
                }
                if (!nonNullDateTimeFound) {
                    final ZonedDateTime swap = min;
                    min = max;
                    max = swap;
                }
                dateRangeParam.setDateTimeRangeValue(new DateTimeRange(min, max));
            } finally {
                rg2.release();
            }
        }
    });
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) Attribute(au.gov.asd.tac.constellation.graph.Attribute) GraphAttribute(au.gov.asd.tac.constellation.graph.GraphAttribute) ZonedDateTime(java.time.ZonedDateTime) GraphAttribute(au.gov.asd.tac.constellation.graph.GraphAttribute) ArrayList(java.util.ArrayList) DateTimeRange(au.gov.asd.tac.constellation.plugins.parameters.types.DateTimeRange)

Example 2 with DateTimeRange

use of au.gov.asd.tac.constellation.plugins.parameters.types.DateTimeRange in project constellation by constellation-app.

the class TestParametersPlugin method query.

@Override
protected RecordStore query(final RecordStore query, final PluginInteraction interaction, final PluginParameters parameters) throws PluginException {
    final int sleep = parameters.getParameters().get(SLEEP_PARAMETER_ID).getIntegerValue();
    for (int i = 0; i < sleep; i++) {
        LOGGER.log(Level.INFO, "sleep {0}/{1}", new Object[] { i, sleep });
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            Exceptions.printStackTrace(ex);
            Thread.currentThread().interrupt();
        }
    }
    LOGGER.log(Level.INFO, "slept for {0} seconds", sleep);
    LOGGER.log(Level.INFO, "parameters: {0}", parameters);
    LOGGER.log(Level.INFO, "GraphElementType: {0}", parameters.getSingleChoice(ELEMENT_TYPE_PARAMETER_ID));
    final LocalDate localDate = parameters.getLocalDateValue(LOCAL_DATE_PARAMETER_ID);
    LOGGER.log(Level.INFO, "localdate: {0} ", localDate);
    if (localDate != null) {
        final Calendar cal = LocalDateParameterType.toCalendar(localDate);
        LOGGER.log(Level.INFO, String.format("toDate: [%s] [%04d-%02d-%02d]", cal, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)));
        LOGGER.log(Level.INFO, String.format("fields: [%04d-%02d-%02d]", localDate.get(ChronoField.YEAR), localDate.get(ChronoField.MONTH_OF_YEAR), localDate.get(ChronoField.DAY_OF_MONTH)));
    }
    final MultiChoiceParameterValue planets = parameters.getMultiChoiceValue(PLANETS_PARAMETER_ID);
    planets.getChoices().stream().forEach(planet -> LOGGER.log(Level.INFO, "Planet: {0}", planet));
    LOGGER.log(Level.INFO, "==== begin string values");
    parameters.getParameters().values().stream().forEach(param -> LOGGER.log(Level.INFO, "String {0}: \"{1}\"", new Object[] { param.getName(), param.getStringValue() }));
    LOGGER.log(Level.INFO, "==== end string values");
    final File outputDir = DataAccessPreferenceUtilities.getDataAccessResultsDir();
    if (outputDir != null) {
        final String fnam = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss")) + "-testChainer.txt";
        final File fout = new File(outputDir, fnam);
        try (final PrintWriter writer = new PrintWriter(fout, StandardCharsets.UTF_8.name())) {
            parameters.getParameters().values().stream().forEach(param -> writer.printf("%s: '%s'", param.getName(), param.getStringValue()));
        } catch (final FileNotFoundException | UnsupportedEncodingException ex) {
            Exceptions.printStackTrace(ex);
        }
    }
    final List<String> keys = query.keys();
    while (query.next()) {
        keys.stream().forEach(key -> LOGGER.log(Level.INFO, String.format("%-20s: %s", key, query.get(key))));
        LOGGER.log(Level.INFO, "--");
    }
    final boolean crash = parameters.getBooleanValue(CRASH_PARAMETER_ID);
    if (crash) {
        throw new RuntimeException("Simulated plugin failure");
    }
    try {
        interaction.setProgress(1, 0, String.format("Pretended to add %d node(s), modify %d node(s)", r.nextInt(100) + 1, r.nextInt(100) + 1), false);
    } catch (InterruptedException ex) {
        Exceptions.printStackTrace(ex);
        Thread.currentThread().interrupt();
    }
    final String queryName = parameters.getStringValue(CoreGlobalParameters.QUERY_NAME_PARAMETER_ID);
    LOGGER.log(Level.INFO, "query name: {0}", queryName);
    final DateTimeRange dtr = parameters.getDateTimeRangeValue(CoreGlobalParameters.DATETIME_RANGE_PARAMETER_ID);
    final ZonedDateTime[] dtrStartEnd = dtr.getZonedStartEnd();
    LOGGER.log(Level.INFO, "range: (zdt) {0} .. {1}", new Object[] { dtrStartEnd[0], dtrStartEnd[1] });
    LOGGER.log(Level.INFO, "range: (zdt) {0} .. {1}", new Object[] { DateTimeFormatter.ISO_INSTANT.format(dtrStartEnd[0]), DateTimeFormatter.ISO_INSTANT.format(dtrStartEnd[1]) });
    final String interactionLevel = parameters.getParameters().get(INTERACTION_PARAMETER_ID).getStringValue();
    final PluginNotificationLevel pnInteractionLevel;
    if (interactionLevel != null) {
        switch(interactionLevel) {
            case DEBUG:
                pnInteractionLevel = PluginNotificationLevel.DEBUG;
                break;
            case INFO:
                pnInteractionLevel = PluginNotificationLevel.INFO;
                break;
            case WARNING:
                pnInteractionLevel = PluginNotificationLevel.WARNING;
                break;
            case ERROR:
                pnInteractionLevel = PluginNotificationLevel.ERROR;
                break;
            case FATAL:
                pnInteractionLevel = PluginNotificationLevel.FATAL;
                break;
            default:
                pnInteractionLevel = null;
                break;
        }
        if (pnInteractionLevel != null) {
            interaction.notify(pnInteractionLevel, "Interaction from plugin");
        }
    }
    final String exceptionLevel = parameters.getParameters().get(LEVEL_PARAMETER_ID).getStringValue();
    final PluginNotificationLevel pnExceptionLevel;
    if (exceptionLevel != null) {
        switch(exceptionLevel) {
            case DEBUG:
                pnExceptionLevel = PluginNotificationLevel.DEBUG;
                break;
            case INFO:
                pnExceptionLevel = PluginNotificationLevel.INFO;
                break;
            case WARNING:
                pnExceptionLevel = PluginNotificationLevel.WARNING;
                break;
            case ERROR:
                pnExceptionLevel = PluginNotificationLevel.ERROR;
                break;
            case FATAL:
                pnExceptionLevel = PluginNotificationLevel.FATAL;
                break;
            default:
                pnExceptionLevel = null;
                break;
        }
        if (pnExceptionLevel != null) {
            throw new PluginException(pnExceptionLevel, "Exception thrown from plugin");
        }
    }
    // Add nodes containing global query parameters
    final RecordStore results = new GraphRecordStore();
    results.add();
    results.set(GraphRecordStoreUtilities.SOURCE + AnalyticConcept.VertexAttribute.RAW, "name1@domain1.com");
    results.set(GraphRecordStoreUtilities.SOURCE + AnalyticConcept.VertexAttribute.TYPE, "Email");
    results.set(GraphRecordStoreUtilities.SOURCE + AnalyticConcept.VertexAttribute.COMMENT, queryName);
    results.set(GraphRecordStoreUtilities.SOURCE + TemporalConcept.VertexAttribute.LAST_SEEN, DateTimeFormatter.ISO_INSTANT.format(dtrStartEnd[0]).replace("Z", ".000Z"));
    results.set(GraphRecordStoreUtilities.DESTINATION + AnalyticConcept.VertexAttribute.RAW, "name2@domain2.com");
    results.set(GraphRecordStoreUtilities.DESTINATION + AnalyticConcept.VertexAttribute.TYPE, "Email");
    results.set(GraphRecordStoreUtilities.DESTINATION + AnalyticConcept.VertexAttribute.COMMENT, queryName);
    results.set(GraphRecordStoreUtilities.DESTINATION + TemporalConcept.VertexAttribute.LAST_SEEN, DateTimeFormatter.ISO_INSTANT.format(dtrStartEnd[1]).replace("Z", ".000Z"));
    results.set(GraphRecordStoreUtilities.TRANSACTION + AnalyticConcept.TransactionAttribute.COMMENT, parameters.toString());
    return results;
}
Also used : Calendar(java.util.Calendar) MultiChoiceParameterValue(au.gov.asd.tac.constellation.plugins.parameters.types.MultiChoiceParameterType.MultiChoiceParameterValue) PluginNotificationLevel(au.gov.asd.tac.constellation.plugins.PluginNotificationLevel) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) LocalDate(java.time.LocalDate) ZonedDateTime(java.time.ZonedDateTime) RecordStore(au.gov.asd.tac.constellation.graph.processing.RecordStore) GraphRecordStore(au.gov.asd.tac.constellation.graph.processing.GraphRecordStore) GraphRecordStore(au.gov.asd.tac.constellation.graph.processing.GraphRecordStore) DateTimeRange(au.gov.asd.tac.constellation.plugins.parameters.types.DateTimeRange) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 3 with DateTimeRange

use of au.gov.asd.tac.constellation.plugins.parameters.types.DateTimeRange in project constellation by constellation-app.

the class DataAccessTabPaneNGTest method validateTabTimeRange.

@Test
public void validateTabTimeRange() {
    final Tab tab = mock(Tab.class);
    final ScrollPane scrollPane = mock(ScrollPane.class);
    final QueryPhasePane queryPhasePane = mock(QueryPhasePane.class);
    final GlobalParametersPane globalParametersPane = mock(GlobalParametersPane.class);
    final PluginParameters globalPluginParameters = mock(PluginParameters.class);
    when(tab.getContent()).thenReturn(scrollPane);
    when(scrollPane.getContent()).thenReturn(queryPhasePane);
    when(queryPhasePane.getGlobalParametersPane()).thenReturn(globalParametersPane);
    when(globalParametersPane.getParams()).thenReturn(globalPluginParameters);
    final ZonedDateTime before = ZonedDateTime.of(2020, 1, 1, 1, 0, 0, 0, ZoneOffset.UTC);
    final ZonedDateTime after = ZonedDateTime.of(2021, 1, 1, 1, 0, 0, 0, ZoneOffset.UTC);
    // A tab's date range is valid if the to part of the range is after the from part.
    when(globalPluginParameters.getDateTimeRangeValue("CoreGlobalParameters.datetime_range")).thenReturn(new DateTimeRange(before, after));
    assertTrue(DataAccessTabPane.validateTabTimeRange(tab));
    when(globalPluginParameters.getDateTimeRangeValue("CoreGlobalParameters.datetime_range")).thenReturn(new DateTimeRange(after, before));
    assertFalse(DataAccessTabPane.validateTabTimeRange(tab));
}
Also used : Tab(javafx.scene.control.Tab) QueryPhasePane(au.gov.asd.tac.constellation.views.dataaccess.panes.QueryPhasePane) ZonedDateTime(java.time.ZonedDateTime) ScrollPane(javafx.scene.control.ScrollPane) GlobalParametersPane(au.gov.asd.tac.constellation.views.dataaccess.panes.GlobalParametersPane) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) DateTimeRange(au.gov.asd.tac.constellation.plugins.parameters.types.DateTimeRange) Test(org.testng.annotations.Test)

Example 4 with DateTimeRange

use of au.gov.asd.tac.constellation.plugins.parameters.types.DateTimeRange in project constellation by constellation-app.

the class TestParametersPluginNGTest method testQueryResults.

/**
 * Test of query method, of class TestParametersPlugin. Tests querying
 * results from graph
 */
@Test
public void testQueryResults() throws Exception {
    System.out.println("Test Query Results");
    final TestParametersPlugin instance = new TestParametersPlugin();
    final PluginParameters result = instance.createParameters();
    final GraphRecordStore recordStore = new GraphRecordStore();
    final DefaultPluginInteraction interaction = new DefaultPluginInteraction(null, null);
    // Set plugin query name here before execution
    result.getParameters().get(CoreGlobalParameters.QUERY_NAME_PARAMETER_ID).setStringValue("TESTPARAMETERSPLUGIN");
    // Set plugin parameters here before execution
    result.getParameters().get(TestParametersPlugin.LEVEL_PARAMETER_ID).setStringValue("None");
    final RecordStore queryResults = instance.query(recordStore, interaction, result);
    // Test the amount of entries in the RecordStore
    assertEquals(queryResults.size(), 1);
    final DateTimeRange dtr = result.getDateTimeRangeValue(CoreGlobalParameters.DATETIME_RANGE_PARAMETER_ID);
    final ZonedDateTime[] dtrStartEnd = dtr.getZonedStartEnd();
    // Test the values entered
    assertEquals(queryResults.get(GraphRecordStoreUtilities.SOURCE + AnalyticConcept.VertexAttribute.RAW), "name1@domain1.com");
    assertEquals(queryResults.get(GraphRecordStoreUtilities.SOURCE + AnalyticConcept.VertexAttribute.TYPE), "Email");
    assertEquals(queryResults.get(GraphRecordStoreUtilities.SOURCE + AnalyticConcept.VertexAttribute.COMMENT), "TESTPARAMETERSPLUGIN");
    assertEquals(queryResults.get(GraphRecordStoreUtilities.SOURCE + TemporalConcept.VertexAttribute.LAST_SEEN), DateTimeFormatter.ISO_INSTANT.format(dtrStartEnd[0]).replace("Z", ".000Z"));
    assertEquals(queryResults.get(GraphRecordStoreUtilities.DESTINATION + AnalyticConcept.VertexAttribute.RAW), "name2@domain2.com");
    assertEquals(queryResults.get(GraphRecordStoreUtilities.DESTINATION + AnalyticConcept.VertexAttribute.TYPE), "Email");
    assertEquals(queryResults.get(GraphRecordStoreUtilities.DESTINATION + AnalyticConcept.VertexAttribute.COMMENT), "TESTPARAMETERSPLUGIN");
    assertEquals(queryResults.get(GraphRecordStoreUtilities.DESTINATION + TemporalConcept.VertexAttribute.LAST_SEEN), DateTimeFormatter.ISO_INSTANT.format(dtrStartEnd[1]).replace("Z", ".000Z"));
}
Also used : ZonedDateTime(java.time.ZonedDateTime) RecordStore(au.gov.asd.tac.constellation.graph.processing.RecordStore) GraphRecordStore(au.gov.asd.tac.constellation.graph.processing.GraphRecordStore) GraphRecordStore(au.gov.asd.tac.constellation.graph.processing.GraphRecordStore) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) DefaultPluginInteraction(au.gov.asd.tac.constellation.graph.node.plugins.DefaultPluginInteraction) DateTimeRange(au.gov.asd.tac.constellation.plugins.parameters.types.DateTimeRange) Test(org.testng.annotations.Test)

Aggregations

DateTimeRange (au.gov.asd.tac.constellation.plugins.parameters.types.DateTimeRange)4 ZonedDateTime (java.time.ZonedDateTime)4 GraphRecordStore (au.gov.asd.tac.constellation.graph.processing.GraphRecordStore)2 RecordStore (au.gov.asd.tac.constellation.graph.processing.RecordStore)2 PluginParameters (au.gov.asd.tac.constellation.plugins.parameters.PluginParameters)2 Test (org.testng.annotations.Test)2 Attribute (au.gov.asd.tac.constellation.graph.Attribute)1 GraphAttribute (au.gov.asd.tac.constellation.graph.GraphAttribute)1 ReadableGraph (au.gov.asd.tac.constellation.graph.ReadableGraph)1 DefaultPluginInteraction (au.gov.asd.tac.constellation.graph.node.plugins.DefaultPluginInteraction)1 PluginException (au.gov.asd.tac.constellation.plugins.PluginException)1 PluginNotificationLevel (au.gov.asd.tac.constellation.plugins.PluginNotificationLevel)1 MultiChoiceParameterValue (au.gov.asd.tac.constellation.plugins.parameters.types.MultiChoiceParameterType.MultiChoiceParameterValue)1 GlobalParametersPane (au.gov.asd.tac.constellation.views.dataaccess.panes.GlobalParametersPane)1 QueryPhasePane (au.gov.asd.tac.constellation.views.dataaccess.panes.QueryPhasePane)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 PrintWriter (java.io.PrintWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 LocalDate (java.time.LocalDate)1