Search in sources :

Example 1 with ApplicationOpenedEvent

use of ai.elimu.model.analytics.ApplicationOpenedEvent in project webapp by elimu-ai.

the class ApplicationOpenedEventListController method handleRequest.

@RequestMapping(method = RequestMethod.GET)
public String handleRequest(Model model, HttpSession session) {
    logger.info("handleRequest");
    Contributor contributor = (Contributor) session.getAttribute("contributor");
    // To ease development/testing, auto-generate events
    if (EnvironmentContextLoaderListener.env != Environment.PROD) {
        generateEvents(contributor.getLocale());
    }
    List<ApplicationOpenedEvent> applicationOpenedEvents = applicationOpenedEventDao.readAll(contributor.getLocale());
    model.addAttribute("applicationOpenedEvents", applicationOpenedEvents);
    return "analytics/application-opened-event/list";
}
Also used : ApplicationOpenedEvent(ai.elimu.model.analytics.ApplicationOpenedEvent) Contributor(ai.elimu.model.Contributor) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with ApplicationOpenedEvent

use of ai.elimu.model.analytics.ApplicationOpenedEvent in project webapp by elimu-ai.

the class ApplicationOpenedEventDaoTest method testReadAllByDevice.

@Test
public void testReadAllByDevice() {
    Device device = new Device();
    device.setDeviceId("bcaef51200ac6d92bdd81");
    deviceDao.create(device);
    List<ApplicationOpenedEvent> applicationOpenedEvents = applicationOpenedEventDao.readAll(device);
    assertThat(applicationOpenedEvents.size(), is(0));
    ApplicationOpenedEvent applicationOpenedEvent1 = new ApplicationOpenedEvent();
    applicationOpenedEvent1.setDevice(device);
    applicationOpenedEventDao.create(applicationOpenedEvent1);
    ApplicationOpenedEvent applicationOpenedEvent2 = new ApplicationOpenedEvent();
    applicationOpenedEventDao.create(applicationOpenedEvent2);
    applicationOpenedEvents = applicationOpenedEventDao.readAll(device);
    assertThat(applicationOpenedEvents.size(), is(1));
}
Also used : Device(ai.elimu.model.Device) ApplicationOpenedEvent(ai.elimu.model.analytics.ApplicationOpenedEvent) Test(org.junit.Test)

Example 3 with ApplicationOpenedEvent

use of ai.elimu.model.analytics.ApplicationOpenedEvent in project webapp by elimu-ai.

the class ApplicationOpenedEventListController method generateEvents.

private List<ApplicationOpenedEvent> generateEvents(Locale locale) {
    logger.info("generateEvents");
    List<ApplicationOpenedEvent> applicationOpenedEvents = new ArrayList<>();
    String[] eventFiles = new String[] { "application_opened_events_2017-05-25.log", "application_opened_events_2017-05-27.log", "application_opened_events_2017-05-28.log", "application_opened_events_2017-05-29.log", "application_opened_events_2017-05-30.log", "application_opened_events_2017-06-02.log", "application_opened_events_2017-06-03.log", "application_opened_events_2017-06-04.log", "application_opened_events_2017-06-05.log", "application_opened_events_2017-06-06.log", "application_opened_events_2017-06-07.log", "application_opened_events_2017-06-08.log", "application_opened_events_2017-06-09.log" };
    for (String eventFile : eventFiles) {
        logger.info("eventFile: " + eventFile);
        URL url = getClass().getResource(eventFile);
        try {
            Reader reader = new FileReader(url.getFile());
            List<String> lines = IOUtils.readLines(reader);
            logger.info("lines.size(): " + lines.size());
            reader.close();
            for (String eventLine : lines) {
                logger.info("eventLine: " + eventLine);
                // Expected format: id:163|deviceId:2312aff4939750ea|time:1496843219926|packageName:ai.elimu.nyaqd|studentId:2312aff4939750ea_4
                String deviceId = EventLineHelper.getDeviceId(eventLine);
                Device device = deviceDao.read(deviceId);
                logger.info("device: " + device);
                if (device == null) {
                    device = new Device();
                    device.setDeviceId(deviceId);
                    device.setDeviceManufacturer("asdf");
                    device.setDeviceModel("asdf");
                    device.setDeviceSerial("asdf");
                    device.setRemoteAddress("0.0.0.0");
                    device.setOsVersion(23);
                    device.setLocale(locale);
                    deviceDao.create(device);
                }
                Calendar timeOfEvent = EventLineHelper.getTime(eventLine);
                String packageName = EventLineHelper.getPackageName(eventLine);
                ApplicationOpenedEvent existingApplicationOpenedEvent = applicationOpenedEventDao.read(device, timeOfEvent, packageName);
                logger.info("existingApplicationOpenedEvent: " + existingApplicationOpenedEvent);
                if (existingApplicationOpenedEvent == null) {
                    ApplicationOpenedEvent applicationOpenedEvent = new ApplicationOpenedEvent();
                    applicationOpenedEvent.setDevice(device);
                    applicationOpenedEvent.setCalendar(timeOfEvent);
                    applicationOpenedEvent.setPackageName(packageName);
                    applicationOpenedEventDao.create(applicationOpenedEvent);
                    applicationOpenedEvents.add(applicationOpenedEvent);
                }
            }
        } catch (FileNotFoundException ex) {
            logger.error(null, ex);
        } catch (IOException ex) {
            logger.error(null, ex);
        }
    }
    return applicationOpenedEvents;
}
Also used : Device(ai.elimu.model.Device) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) Reader(java.io.Reader) FileReader(java.io.FileReader) IOException(java.io.IOException) URL(java.net.URL) ApplicationOpenedEvent(ai.elimu.model.analytics.ApplicationOpenedEvent) FileReader(java.io.FileReader)

Example 4 with ApplicationOpenedEvent

use of ai.elimu.model.analytics.ApplicationOpenedEvent in project webapp by elimu-ai.

the class ApplicationOpenedEventRestController method create.

@RequestMapping("/create")
public String create(HttpServletRequest request, // TODO: checksum,
@RequestParam MultipartFile multipartFile) {
    logger.info("create");
    logger.info("request.getQueryString(): " + request.getQueryString());
    if (!multipartFile.isEmpty()) {
        try {
            byte[] bytes = multipartFile.getBytes();
            Reader reader = new CharSequenceReader((new String(bytes)));
            List<String> lines = IOUtils.readLines(reader);
            logger.info("lines.size(): " + lines.size());
            reader.close();
            for (String eventLine : lines) {
                logger.info("eventLine: " + eventLine);
                // Expected format: id:163|deviceId:2312aff4939750ea|time:1496843219926|packageName:ai.elimu.nyaqd|studentId:2312aff4939750ea_4
                String deviceId = EventLineHelper.getDeviceId(eventLine);
                Device device = deviceDao.read(deviceId);
                logger.info("device: " + device);
                Calendar timeOfEvent = EventLineHelper.getTime(eventLine);
                String packageName = EventLineHelper.getPackageName(eventLine);
                ApplicationOpenedEvent existingApplicationOpenedEvent = applicationOpenedEventDao.read(device, timeOfEvent, packageName);
                logger.info("existingApplicationOpenedEvent: " + existingApplicationOpenedEvent);
                if (existingApplicationOpenedEvent == null) {
                    ApplicationOpenedEvent applicationOpenedEvent = new ApplicationOpenedEvent();
                    applicationOpenedEvent.setDevice(device);
                    applicationOpenedEvent.setCalendar(timeOfEvent);
                    applicationOpenedEvent.setPackageName(packageName);
                    applicationOpenedEventDao.create(applicationOpenedEvent);
                }
            }
        } catch (IOException ex) {
            logger.error(null, ex);
        }
    }
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("result", "success");
    // TODO: handle error
    logger.info("jsonObject: " + jsonObject);
    return jsonObject.toString();
}
Also used : CharSequenceReader(org.apache.commons.io.input.CharSequenceReader) JSONObject(org.json.JSONObject) Device(ai.elimu.model.Device) Calendar(java.util.Calendar) ApplicationOpenedEvent(ai.elimu.model.analytics.ApplicationOpenedEvent) Reader(java.io.Reader) CharSequenceReader(org.apache.commons.io.input.CharSequenceReader) IOException(java.io.IOException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with ApplicationOpenedEvent

use of ai.elimu.model.analytics.ApplicationOpenedEvent in project webapp by elimu-ai.

the class ApplicationOpenedEventDaoTest method testReadAllByLocale.

@Test
public void testReadAllByLocale() {
    Locale locale = Locale.values()[(int) (Math.random() * Locale.values().length)];
    logger.info("locale: " + locale);
    List<ApplicationOpenedEvent> applicationOpenedEvents = applicationOpenedEventDao.readAll(locale);
    assertThat(applicationOpenedEvents.size(), is(0));
    Device device = new Device();
    device.setLocale(locale);
    deviceDao.create(device);
    ApplicationOpenedEvent applicationOpenedEvent1 = new ApplicationOpenedEvent();
    applicationOpenedEvent1.setDevice(device);
    applicationOpenedEventDao.create(applicationOpenedEvent1);
    ApplicationOpenedEvent applicationOpenedEvent2 = new ApplicationOpenedEvent();
    applicationOpenedEvent2.setDevice(device);
    applicationOpenedEventDao.create(applicationOpenedEvent2);
    applicationOpenedEvents = applicationOpenedEventDao.readAll(locale);
    assertThat(applicationOpenedEvents.size(), is(2));
}
Also used : Locale(ai.elimu.model.enums.Locale) Device(ai.elimu.model.Device) ApplicationOpenedEvent(ai.elimu.model.analytics.ApplicationOpenedEvent) Test(org.junit.Test)

Aggregations

ApplicationOpenedEvent (ai.elimu.model.analytics.ApplicationOpenedEvent)7 Device (ai.elimu.model.Device)4 Test (org.junit.Test)4 IOException (java.io.IOException)2 Reader (java.io.Reader)2 Calendar (java.util.Calendar)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 Contributor (ai.elimu.model.Contributor)1 Student (ai.elimu.model.Student)1 Locale (ai.elimu.model.enums.Locale)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 CharSequenceReader (org.apache.commons.io.input.CharSequenceReader)1 JSONObject (org.json.JSONObject)1