Search in sources :

Example 1 with TransferStatus

use of org.libresonic.player.domain.TransferStatus in project libresonic by Libresonic.

the class StatusChartController method handleRequest.

@RequestMapping(method = RequestMethod.GET)
public synchronized ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    String type = request.getParameter("type");
    int index = Integer.parseInt(request.getParameter("index"));
    List<TransferStatus> statuses = Collections.emptyList();
    if ("stream".equals(type)) {
        statuses = statusService.getAllStreamStatuses();
    } else if ("download".equals(type)) {
        statuses = statusService.getAllDownloadStatuses();
    } else if ("upload".equals(type)) {
        statuses = statusService.getAllUploadStatuses();
    }
    if (index < 0 || index >= statuses.size()) {
        return null;
    }
    TransferStatus status = statuses.get(index);
    TimeSeries series = new TimeSeries("Kbps", Millisecond.class);
    TransferStatus.SampleHistory history = status.getHistory();
    long to = System.currentTimeMillis();
    long from = to - status.getHistoryLengthMillis();
    Range range = new DateRange(from, to);
    if (!history.isEmpty()) {
        TransferStatus.Sample previous = history.get(0);
        for (int i = 1; i < history.size(); i++) {
            TransferStatus.Sample sample = history.get(i);
            long elapsedTimeMilis = sample.getTimestamp() - previous.getTimestamp();
            long bytesStreamed = Math.max(0L, sample.getBytesTransfered() - previous.getBytesTransfered());
            double kbps = (8.0 * bytesStreamed / 1024.0) / (elapsedTimeMilis / 1000.0);
            series.addOrUpdate(new Millisecond(new Date(sample.getTimestamp())), kbps);
            previous = sample;
        }
    }
    // Compute moving average.
    series = MovingAverage.createMovingAverage(series, "Kbps", 20000, 5000);
    // Find min and max values.
    double min = 100;
    double max = 250;
    for (Object obj : series.getItems()) {
        TimeSeriesDataItem item = (TimeSeriesDataItem) obj;
        double value = item.getValue().doubleValue();
        if (item.getPeriod().getFirstMillisecond() > from) {
            min = Math.min(min, value);
            max = Math.max(max, value);
        }
    }
    // Add 10% to max value.
    max *= 1.1D;
    // Subtract 10% from min value.
    min *= 0.9D;
    TimeSeriesCollection dataset = new TimeSeriesCollection();
    dataset.addSeries(series);
    JFreeChart chart = ChartFactory.createTimeSeriesChart(null, null, null, dataset, false, false, false);
    XYPlot plot = (XYPlot) chart.getPlot();
    plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
    Paint background = new GradientPaint(0, 0, Color.lightGray, 0, IMAGE_HEIGHT, Color.white);
    plot.setBackgroundPaint(background);
    XYItemRenderer renderer = plot.getRendererForDataset(dataset);
    renderer.setSeriesPaint(0, Color.blue.darker());
    renderer.setSeriesStroke(0, new BasicStroke(2f));
    // Set theme-specific colors.
    Color bgColor = getBackground(request);
    Color fgColor = getForeground(request);
    chart.setBackgroundPaint(bgColor);
    ValueAxis domainAxis = plot.getDomainAxis();
    domainAxis.setRange(range);
    domainAxis.setTickLabelPaint(fgColor);
    domainAxis.setTickMarkPaint(fgColor);
    domainAxis.setAxisLinePaint(fgColor);
    ValueAxis rangeAxis = plot.getRangeAxis();
    rangeAxis.setRange(new Range(min, max));
    rangeAxis.setTickLabelPaint(fgColor);
    rangeAxis.setTickMarkPaint(fgColor);
    rangeAxis.setAxisLinePaint(fgColor);
    ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart, IMAGE_WIDTH, IMAGE_HEIGHT);
    return null;
}
Also used : Range(org.jfree.data.Range) Date(java.util.Date) JFreeChart(org.jfree.chart.JFreeChart) XYPlot(org.jfree.chart.plot.XYPlot) ValueAxis(org.jfree.chart.axis.ValueAxis) TransferStatus(org.libresonic.player.domain.TransferStatus) XYItemRenderer(org.jfree.chart.renderer.xy.XYItemRenderer) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with TransferStatus

use of org.libresonic.player.domain.TransferStatus in project libresonic by Libresonic.

the class TransferService method getUploadInfo.

/**
     * Returns info about any ongoing upload within the current session.
     * @return Info about ongoing upload.
     */
public UploadInfo getUploadInfo() {
    HttpSession session = WebContextFactory.get().getSession();
    TransferStatus status = (TransferStatus) session.getAttribute(UploadController.UPLOAD_STATUS);
    if (status != null) {
        return new UploadInfo(status.getBytesTransfered(), status.getBytesTotal());
    }
    return new UploadInfo(0L, 0L);
}
Also used : HttpSession(javax.servlet.http.HttpSession) TransferStatus(org.libresonic.player.domain.TransferStatus)

Example 3 with TransferStatus

use of org.libresonic.player.domain.TransferStatus in project libresonic by Libresonic.

the class StatusService method getPlayStatuses.

public synchronized List<PlayStatus> getPlayStatuses() {
    Map<String, PlayStatus> result = new LinkedHashMap<String, PlayStatus>();
    for (PlayStatus remotePlay : remotePlays) {
        if (!remotePlay.isExpired()) {
            result.put(remotePlay.getPlayer().getId(), remotePlay);
        }
    }
    List<TransferStatus> statuses = new ArrayList<TransferStatus>();
    statuses.addAll(inactiveStreamStatuses.values());
    statuses.addAll(streamStatuses);
    for (TransferStatus streamStatus : statuses) {
        Player player = streamStatus.getPlayer();
        File file = streamStatus.getFile();
        if (file == null) {
            continue;
        }
        MediaFile mediaFile = mediaFileService.getMediaFile(file);
        if (player == null || mediaFile == null) {
            continue;
        }
        Date time = new Date(System.currentTimeMillis() - streamStatus.getMillisSinceLastUpdate());
        result.put(player.getId(), new PlayStatus(mediaFile, player, time));
    }
    return new ArrayList<PlayStatus>(result.values());
}
Also used : MediaFile(org.libresonic.player.domain.MediaFile) Player(org.libresonic.player.domain.Player) TransferStatus(org.libresonic.player.domain.TransferStatus) MediaFile(org.libresonic.player.domain.MediaFile) File(java.io.File) PlayStatus(org.libresonic.player.domain.PlayStatus)

Example 4 with TransferStatus

use of org.libresonic.player.domain.TransferStatus in project libresonic by Libresonic.

the class StatusService method createStatus.

private synchronized TransferStatus createStatus(Player player, List<TransferStatus> statusList) {
    TransferStatus status = new TransferStatus();
    status.setPlayer(player);
    statusList.add(status);
    return status;
}
Also used : TransferStatus(org.libresonic.player.domain.TransferStatus)

Example 5 with TransferStatus

use of org.libresonic.player.domain.TransferStatus in project libresonic by Libresonic.

the class StatusService method getAllStreamStatuses.

public synchronized List<TransferStatus> getAllStreamStatuses() {
    List<TransferStatus> result = new ArrayList<TransferStatus>(streamStatuses);
    // Add inactive status for those players that have no active status.
    Set<String> activePlayers = new HashSet<String>();
    for (TransferStatus status : streamStatuses) {
        activePlayers.add(status.getPlayer().getId());
    }
    for (Map.Entry<String, TransferStatus> entry : inactiveStreamStatuses.entrySet()) {
        if (!activePlayers.contains(entry.getKey())) {
            result.add(entry.getValue());
        }
    }
    return result;
}
Also used : TransferStatus(org.libresonic.player.domain.TransferStatus)

Aggregations

TransferStatus (org.libresonic.player.domain.TransferStatus)9 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 File (java.io.File)2 MediaFile (org.libresonic.player.domain.MediaFile)2 Player (org.libresonic.player.domain.Player)2 ModelAndView (org.springframework.web.servlet.ModelAndView)2 Date (java.util.Date)1 HttpSession (javax.servlet.http.HttpSession)1 FileItem (org.apache.commons.fileupload.FileItem)1 FileItemFactory (org.apache.commons.fileupload.FileItemFactory)1 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)1 ZipFile (org.apache.tools.zip.ZipFile)1 JFreeChart (org.jfree.chart.JFreeChart)1 ValueAxis (org.jfree.chart.axis.ValueAxis)1 XYPlot (org.jfree.chart.plot.XYPlot)1 XYItemRenderer (org.jfree.chart.renderer.xy.XYItemRenderer)1 Range (org.jfree.data.Range)1 PlayStatus (org.libresonic.player.domain.PlayStatus)1 User (org.libresonic.player.domain.User)1 MonitoredDiskFileItemFactory (org.libresonic.player.upload.MonitoredDiskFileItemFactory)1